]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/types/types.h
59e3bb2925cdd58dd8c3f31f2175b7ee95d0c0df
[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 #endif
149
150 /// standard size type
151 ///typedef mword_t size_t;
152 ///typedef signed int ssize_t;
153
154 /// momentary only used in UX since there the kernel has a different
155 /// address space than user mode applications
156 enum Address_type { ADDR_KERNEL = 0, ADDR_USER = 1, ADDR_UNKNOWN = 2 };
157
158 #endif // TYPES_H__
159