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