|
32 | 32 | // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
|
33 | 33 | // depending on platform).
|
34 | 34 | //
|
35 |
| -// The classes are defined with friend functions operating on them instead of member |
36 |
| -// functions so that they (the classes) can be re-#define'd to int types in optimized |
37 |
| -// mode. This allows full type checking and maximum safety in debug mode, and full |
38 |
| -// optimizations (constant folding) and zero overhead (time and space wise) in the |
39 |
| -// optimized build (some compilers do not optimize one-element value classes but |
40 |
| -// instead create an object in memory - thus the overhead may be significant). |
41 |
| -// |
42 |
| -// Note: 1) DO NOT add new overloaded friend functions that do not have a unique function |
43 |
| -// function name but require signature types for resolution. This will not work |
44 |
| -// in optimized mode as both, ByteSize and WordSize are mapped to the same type |
45 |
| -// and thus the distinction would not be possible anymore (=> compiler errors). |
46 |
| -// |
47 |
| -// 2) DO NOT add non-static member functions as they cannot be mapped so something |
48 |
| -// compilable in the optimized build. Static member functions could be added |
49 |
| -// but require a corresponding class definition in the optimized build. |
50 |
| -// |
51 | 35 | // These classes should help doing a transition from (currently) word-size based offsets
|
52 | 36 | // to byte-size based offsets in the VM (this will be important if we desire to pack
|
53 | 37 | // objects more densely in the VM for 64bit machines). Such a transition should proceed
|
|
56 | 40 | // a) first transition the whole VM into a form where all sizes are strongly typed
|
57 | 41 | // b) change all WordSize's to ByteSize's where desired and fix the compilation errors
|
58 | 42 |
|
| 43 | +enum class WordSize : int {}; |
59 | 44 |
|
60 |
| -#ifdef ASSERT |
61 |
| - |
62 |
| -class ByteSize { |
63 |
| - private: |
64 |
| - int _size; |
65 |
| - |
66 |
| - // Note: This constructor must be private to avoid implicit conversions! |
67 |
| - ByteSize(int size) { _size = size; } |
68 |
| - |
69 |
| - public: |
70 |
| - // constructors |
71 |
| - inline friend ByteSize in_ByteSize(int size); |
72 |
| - |
73 |
| - // accessors |
74 |
| - inline friend int in_bytes(ByteSize x); |
75 |
| - |
76 |
| - // operators |
77 |
| - friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); } |
78 |
| - friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); } |
79 |
| - friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); } |
80 |
| - |
81 |
| - // comparison |
82 |
| - friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); } |
83 |
| - friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); } |
84 |
| - friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); } |
85 |
| - friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); } |
86 |
| - friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); } |
87 |
| - friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); } |
88 |
| -}; |
89 |
| - |
90 |
| -inline ByteSize in_ByteSize(int size) { return ByteSize(size); } |
91 |
| -inline int in_bytes(ByteSize x) { return x._size; } |
92 |
| - |
93 |
| - |
94 |
| -class WordSize { |
95 |
| - private: |
96 |
| - int _size; |
97 |
| - |
98 |
| - // Note: This constructor must be private to avoid implicit conversions! |
99 |
| - WordSize(int size) { _size = size; } |
100 |
| - |
101 |
| - public: |
102 |
| - // constructors |
103 |
| - inline friend WordSize in_WordSize(int size); |
104 |
| - |
105 |
| - // accessors |
106 |
| - inline friend int in_words(WordSize x); |
107 |
| - |
108 |
| - // operators |
109 |
| - friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); } |
110 |
| - friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); } |
111 |
| - friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); } |
112 |
| - |
113 |
| - // comparison |
114 |
| - friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); } |
115 |
| - friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); } |
116 |
| - friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); } |
117 |
| - friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); } |
118 |
| - friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); } |
119 |
| - friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); } |
120 |
| -}; |
121 |
| - |
122 |
| -inline WordSize in_WordSize(int size) { return WordSize(size); } |
123 |
| -inline int in_words(WordSize x) { return x._size; } |
124 |
| - |
125 |
| - |
126 |
| -#else // ASSERT |
127 |
| - |
128 |
| -// The following definitions must match the corresponding friend declarations |
129 |
| -// in the Byte/WordSize classes if they are typedef'ed to be int. This will |
130 |
| -// be the case in optimized mode to ensure zero overhead for these types. |
131 |
| -// |
132 |
| -// Note: If a compiler does not inline these function calls away, one may |
133 |
| -// want to use #define's to make sure full optimization (constant |
134 |
| -// folding in particular) is possible. |
135 |
| - |
136 |
| -typedef int ByteSize; |
137 |
| -inline ByteSize in_ByteSize(int size) { return size; } |
138 |
| -inline int in_bytes (ByteSize x) { return x; } |
| 45 | +constexpr WordSize in_WordSize(int size) { return static_cast<WordSize>(size); } |
| 46 | +constexpr int in_words(WordSize x) { return static_cast<int>(x); } |
139 | 47 |
|
140 |
| -typedef int WordSize; |
141 |
| -inline WordSize in_WordSize(int size) { return size; } |
142 |
| -inline int in_words (WordSize x) { return x; } |
| 48 | +enum class ByteSize : int {}; |
143 | 49 |
|
144 |
| -#endif // ASSERT |
| 50 | +constexpr ByteSize in_ByteSize(int size) { return static_cast<ByteSize>(size); } |
| 51 | +constexpr int in_bytes(ByteSize x) { return static_cast<int>(x); } |
145 | 52 |
|
| 53 | +constexpr ByteSize operator + (ByteSize x, ByteSize y) { return in_ByteSize(in_bytes(x) + in_bytes(y)); } |
| 54 | +constexpr ByteSize operator - (ByteSize x, ByteSize y) { return in_ByteSize(in_bytes(x) - in_bytes(y)); } |
| 55 | +constexpr ByteSize operator * (ByteSize x, int y) { return in_ByteSize(in_bytes(x) * y ); } |
146 | 56 |
|
147 | 57 | // Use the following #define to get C++ field member offsets
|
148 | 58 |
|
|
0 commit comments