1 /* 2 * Copyright (c) 2000, 2018, 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 #ifndef SHARE_VM_UTILITIES_SIZES_HPP 26 #define SHARE_VM_UTILITIES_SIZES_HPP 27 28 #include "utilities/globalDefinitions.hpp" 29 30 // The following two classes are used to represent 'sizes' and 'offsets' in the VM; 31 // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while 32 // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words 33 // depending on platform). 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 // These classes should help doing a transition from (currently) word-size based offsets 52 // to byte-size based offsets in the VM (this will be important if we desire to pack 53 // objects more densely in the VM for 64bit machines). Such a transition should proceed 54 // in two steps to minimize the risk of introducing hard-to-find bugs: 55 // 56 // a) first transition the whole VM into a form where all sizes are strongly typed 57 // b) change all WordSize's to ByteSize's where desired and fix the compilation errors 58 59 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; } 139 140 typedef int WordSize; 141 inline WordSize in_WordSize(int size) { return size; } 142 inline int in_words (WordSize x) { return x; } 143 144 #endif // ASSERT 145 146 147 // Use the following #define to get C++ field member offsets 148 149 #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field)) 150 151 #endif // SHARE_VM_UTILITIES_SIZES_HPP