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