1 /* 2 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 // The following two classes are used to represent 'sizes' and 'offsets' in the VM; 26 // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while 27 // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words 28 // depending on platform). 29 // 30 // The classes are defined with friend functions operating on them instead of member 31 // functions so that they (the classes) can be re-#define'd to int types in optimized 32 // mode. This allows full type checking and maximum safety in debug mode, and full 33 // optimizations (constant folding) and zero overhead (time and space wise) in the 34 // optimized build (some compilers do not optimize one-element value classes but 35 // instead create an object in memory - thus the overhead may be significant). 36 // 37 // Note: 1) DO NOT add new overloaded friend functions that do not have a unique function 38 // function name but require signature types for resolution. This will not work 39 // in optimized mode as both, ByteSize and WordSize are mapped to the same type 40 // and thus the distinction would not be possible anymore (=> compiler errors). 41 // 42 // 2) DO NOT add non-static member functions as they cannot be mapped so something 43 // compilable in the optimized build. Static member functions could be added 44 // but require a corresponding class definition in the optimized build. 45 // 46 // These classes should help doing a transition from (currently) word-size based offsets 47 // to byte-size based offsets in the VM (this will be important if we desire to pack 48 // objects more densely in the VM for 64bit machines). Such a transition should proceed 49 // in two steps to minimize the risk of introducing hard-to-find bugs: 50 // 51 // a) first transition the whole VM into a form where all sizes are strongly typed 52 // b) change all WordSize's to ByteSize's where desired and fix the compilation errors 53 54 55 #ifdef ASSERT 56 57 class ByteSize VALUE_OBJ_CLASS_SPEC { 58 private: 59 int _size; 60 61 // Note: This constructor must be private to avoid implicit conversions! 62 ByteSize(int size) { _size = size; } 63 64 public: 65 // constructors 66 inline friend ByteSize in_ByteSize(int size); 67 68 // accessors 69 inline friend int in_bytes(ByteSize x); 70 71 // operators 72 friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); } 73 friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); } 74 friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); } 75 76 // comparison 77 friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); } 78 friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); } 79 friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); } 80 friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); } 81 friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); } 82 friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); } 83 }; 84 85 inline ByteSize in_ByteSize(int size) { return ByteSize(size); } 86 inline int in_bytes(ByteSize x) { return x._size; } 87 88 89 class WordSize VALUE_OBJ_CLASS_SPEC { 90 private: 91 int _size; 92 93 // Note: This constructor must be private to avoid implicit conversions! 94 WordSize(int size) { _size = size; } 95 96 public: 97 // constructors 98 inline friend WordSize in_WordSize(int size); 99 100 // accessors 101 inline friend int in_words(WordSize x); 102 103 // operators 104 friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); } 105 friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); } 106 friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); } 107 108 // comparison 109 friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); } 110 friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); } 111 friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); } 112 friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); } 113 friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); } 114 friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); } 115 }; 116 117 inline WordSize in_WordSize(int size) { return WordSize(size); } 118 inline int in_words(WordSize x) { return x._size; } 119 120 121 #else // ASSERT 122 123 // The following definitions must match the corresponding friend declarations 124 // in the Byte/WordSize classes if they are typedef'ed to be int. This will 125 // be the case in optimized mode to ensure zero overhead for these types. 126 // 127 // Note: If a compiler does not inline these function calls away, one may 128 // want to use #define's to make sure full optimization (constant 129 // folding in particular) is possible. 130 131 typedef int ByteSize; 132 inline ByteSize in_ByteSize(int size) { return size; } 133 inline int in_bytes (ByteSize x) { return x; } 134 135 typedef int WordSize; 136 inline WordSize in_WordSize(int size) { return size; } 137 inline int in_words (WordSize x) { return x; } 138 139 #endif // ASSERT 140 141 142 // Use the following #define to get C++ field member offsets 143 144 #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field))