]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/types/types.h
update
[l4.git] / kernel / fiasco / src / types / types.h
1 #ifndef TYPES_H__
2 #define TYPES_H__
3
4 #include <stddef.h>
5 #include "types-arch.h"
6 #include "std_macros.h"
7
8 #ifdef __cplusplus
9
10 template< typename a, typename b > inline
11 a nonull_static_cast( b p )
12 {
13   Address d = reinterpret_cast<Address>
14                  (static_cast<a>(reinterpret_cast<b>(10))) - 10;
15   return reinterpret_cast<a>( reinterpret_cast<Address>(p) + d);
16 }
17
18 template< typename T >
19 T access_once(T const &a)
20 { return static_cast<T const volatile &>(a); }
21
22 template< typename T >
23 class Static_object
24 {
25 public:
26   T *get()
27   {
28     Address i = (Address)_i;
29     return reinterpret_cast<T*>(i);
30   }
31
32   T *operator -> ()
33   { return get(); }
34
35   void init()
36   { new ((void*)_i) T; }
37
38   template<typename A1>
39   void init(A1 a1)
40   { new ((void*)_i) T(a1); }
41
42   template<typename A1, typename A2>
43   void init(A1 a1, A2 a2)
44   { new ((void*)_i) T(a1, a2); }
45
46   template<typename A1, typename A2, typename A3>
47   void init(A1 a1, A2 a2, A3 a3)
48   { new ((void*)_i) T(a1, a2, a3); }
49
50   template<typename A1, typename A2, typename A3, typename A4>
51   void init(A1 a1, A2 a2, A3 a3, A4 a4)
52   { new ((void*)_i) T(a1, a2, a3, a4); }
53
54   template<typename A1, typename A2, typename A3, typename A4, typename A5>
55   void init(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
56   { new ((void*)_i) T(a1, a2, a3, a4, a5); }
57
58 private:
59   char __attribute__((aligned(sizeof(Mword)))) _i[sizeof(T)];
60 };
61
62
63 template< typename VALUE, typename TARGET >
64 class Number
65 {
66 public:
67   typedef VALUE Value;
68   typedef TARGET Target;
69   struct Type_conversion_error;
70
71 protected:
72   Number() {}
73   explicit Number(Value v) : _v(v) {}
74
75 public:
76
77   static Target create(Value v)
78   { return Target(v); }
79
80   Target operator = (Target o)
81   { _v = o._v; return *static_cast<Target*>(this); }
82
83   Value value() const { return _v; }
84   Value value() const volatile { return _v; }
85   void set_value(Value v) { _v = v; }
86
87   bool operator < (Target const &o) const { return _v < o._v; }
88   bool operator > (Target const &o) const { return _v > o._v; }
89   bool operator <= (Target const &o) const { return _v <= o._v; }
90   bool operator >= (Target const &o) const { return _v >= o._v; }
91   bool operator == (Target const &o) const { return _v == o._v; }
92   bool operator != (Target const &o) const { return _v != o._v; }
93
94   operator Type_conversion_error const * () const
95   { return (Type_conversion_error const *)_v; }
96
97   Target operator | (Target const &o) const
98   { return Target(_v | o._v); }
99
100   Target operator & (Target const &o) const
101   { return Target(_v & o._v); }
102
103   Target operator + (Target const &o) const
104   { return Target(_v + o._v); }
105
106   Target operator - (Target const &o) const
107   { return Target(_v - o._v); }
108
109   Target operator << (unsigned long s) const
110   { return Target(_v << s); }
111
112   Target operator >> (unsigned long s) const
113   { return Target(_v >> s); }
114
115   void operator += (Target const &o) { _v += o._v; }
116   void operator -= (Target const &o) { _v -= o._v; }
117   void operator <<= (unsigned long s) { _v <<= s; }
118   void operator >>= (unsigned long s) { _v >>= s; }
119
120   Target operator ++ () { return Target(++_v); }
121   Target operator ++ (int) { return Target(_v++); }
122
123   Target operator -- () { return Target(--_v); }
124   Target operator -- (int) { return Target(_v--); }
125
126   Target trunc(Target const &size) const
127   { return Target(_v & ~(size._v - 1)); }
128
129   Target offset(Target const &size) const
130   { return Target(_v & (size._v - 1)); }
131
132   static Target from_shift(unsigned char shift)
133   {
134     if (shift >= (int)sizeof(Value) * 8)
135       return Target(Value(1) << Value((sizeof(Value) * 8 - 1)));
136     return Target(Value(1) << Value(shift));
137   }
138
139 protected:
140   Value _v;
141 };
142
143 template< int SHIFT >
144 class Page_addr : public Number<Address, Page_addr<SHIFT> >
145 {
146 private:
147   typedef Number<Address, Page_addr<SHIFT> > B;
148
149   template< int T >
150   class Itt
151   {};
152
153   template< int SH >
154   Address __shift(Address x, Itt<true>) { return x << SH; }
155
156   template< int SH >
157   Address __shift(Address x, Itt<false>) { return x >> (-SH); }
158
159 public:
160   enum { Shift = SHIFT };
161
162   template< int OSHIFT >
163   Page_addr(Page_addr<OSHIFT> o)
164   : B(__shift<Shift-OSHIFT>(o.value(), Itt<(OSHIFT < Shift)>()))
165   {}
166
167   explicit Page_addr(Address a) : B(a) {}
168
169   Page_addr(Page_addr const volatile &o) : B(o.value()) {}
170   Page_addr(Page_addr const &o) : B(o.value()) {}
171
172
173   Page_addr() {}
174
175
176 };
177
178 class Virt_addr : public Page_addr<ARCH_PAGE_SHIFT>
179 {
180 public:
181   template< int OSHIFT >
182   Virt_addr(Page_addr<OSHIFT> o) : Page_addr<ARCH_PAGE_SHIFT>(o) {}
183
184   explicit Virt_addr(unsigned int a) : Page_addr<ARCH_PAGE_SHIFT>(a) {}
185   explicit Virt_addr(int a) : Page_addr<ARCH_PAGE_SHIFT>(a) {}
186   explicit Virt_addr(long int a) : Page_addr<ARCH_PAGE_SHIFT>(a) {}
187   explicit Virt_addr(unsigned long a) : Page_addr<ARCH_PAGE_SHIFT>(a) {}
188   explicit Virt_addr(void *a) : Page_addr<ARCH_PAGE_SHIFT>(Address(a)) {}
189
190   Virt_addr() {}
191 };
192
193 typedef Page_addr<ARCH_PAGE_SHIFT> Virt_size;
194
195 typedef Page_addr<0> Page_number;
196 typedef Page_number Page_count;
197
198 template<typename T>
199 struct Simple_ptr_policy
200 {
201   typedef T &Deref_type;
202   typedef T *Ptr_type;
203   typedef T *Member_type;
204   typedef T *Storage_type;
205
206   static void init(Storage_type const &) {}
207   static void init(Storage_type &d, Storage_type const &s) { d = s; }
208   static void copy(Storage_type &d, Storage_type const &s) { d = s; }
209   static void destroy(Storage_type const &) {}
210   static Deref_type deref(Storage_type p) { return *p; }
211   static Member_type member(Storage_type p) { return p; }
212   static Ptr_type ptr(Storage_type p) { return p; }
213 };
214
215 template<>
216 struct Simple_ptr_policy<void>
217 {
218   typedef void Deref_type;
219   typedef void *Ptr_type;
220   typedef void Member_type;
221   typedef void *Storage_type;
222
223   static void init(Storage_type const &) {}
224   static void init(Storage_type &d, Storage_type const &s) { d = s; }
225   static void copy(Storage_type &d, Storage_type const &s) { d = s; }
226   static void destroy(Storage_type const &) {}
227   static Deref_type deref(Storage_type p);
228   static Member_type member(Storage_type p);
229   static Ptr_type ptr(Storage_type p) { return p; }
230 };
231
232
233 template<typename T, template<typename P> class Policy = Simple_ptr_policy,
234          typename Discriminator = int>
235 class Smart_ptr
236 {
237 private:
238   struct Null_check_type;
239 public:
240   typedef typename Policy<T>::Deref_type Deref_type;
241   typedef typename Policy<T>::Ptr_type Ptr_type;
242   typedef typename Policy<T>::Member_type Member_type;
243   typedef typename Policy<T>::Storage_type Storage_type;
244
245   template<typename A, template<typename X> class B, typename C>
246   friend class Smart_ptr;
247
248 protected:
249   Storage_type _p;
250
251 public:
252   Smart_ptr()
253   { Policy<T>::init(_p); }
254
255   explicit Smart_ptr(T *p)
256   { Policy<T>::init(_p, p); }
257
258   Smart_ptr(Smart_ptr const &o)
259   { Policy<T>::copy(_p, o._p); }
260
261   template< typename RHT >
262   Smart_ptr(Smart_ptr<RHT, Policy, Discriminator> const &o)
263   { Policy<T>::copy(_p, o._p); }
264
265   ~Smart_ptr()
266   { Policy<T>::destroy(_p); }
267
268   Smart_ptr operator = (Smart_ptr const &o)
269   {
270     if (this == &o)
271       return *this;
272
273     Policy<T>::destroy(_p);
274     Policy<T>::copy(_p, o._p);
275     return *this;
276   }
277
278   Deref_type operator * () const
279   { return Policy<T>::deref(_p); }
280
281   Member_type operator -> () const
282   { return Policy<T>::member(_p); }
283
284   Ptr_type get() const
285   { return Policy<T>::ptr(_p); }
286
287   operator Null_check_type const * () const
288   { return reinterpret_cast<Null_check_type const *>(Policy<T>::ptr(_p)); }
289 };
290
291 enum User_ptr_discr {};
292
293 template<typename T>
294 struct User
295 {
296   typedef Smart_ptr<T, Simple_ptr_policy, User_ptr_discr> Ptr;
297 };
298 #endif
299
300 /// standard size type
301 ///typedef mword_t size_t;
302 ///typedef signed int ssize_t;
303
304 /// momentary only used in UX since there the kernel has a different
305 /// address space than user mode applications
306 enum Address_type { ADDR_KERNEL = 0, ADDR_USER = 1, ADDR_UNKNOWN = 2 };
307
308 #endif // TYPES_H__
309