duke@435: /* stefank@2314: * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_UTILITIES_SIZES_HPP stefank@2314: #define SHARE_VM_UTILITIES_SIZES_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "utilities/globalDefinitions.hpp" stefank@2314: duke@435: // The following two classes are used to represent 'sizes' and 'offsets' in the VM; duke@435: // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while duke@435: // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words duke@435: // depending on platform). duke@435: // duke@435: // The classes are defined with friend functions operating on them instead of member duke@435: // functions so that they (the classes) can be re-#define'd to int types in optimized duke@435: // mode. This allows full type checking and maximum safety in debug mode, and full duke@435: // optimizations (constant folding) and zero overhead (time and space wise) in the duke@435: // optimized build (some compilers do not optimize one-element value classes but duke@435: // instead create an object in memory - thus the overhead may be significant). duke@435: // duke@435: // Note: 1) DO NOT add new overloaded friend functions that do not have a unique function duke@435: // function name but require signature types for resolution. This will not work duke@435: // in optimized mode as both, ByteSize and WordSize are mapped to the same type duke@435: // and thus the distinction would not be possible anymore (=> compiler errors). duke@435: // duke@435: // 2) DO NOT add non-static member functions as they cannot be mapped so something duke@435: // compilable in the optimized build. Static member functions could be added duke@435: // but require a corresponding class definition in the optimized build. duke@435: // duke@435: // These classes should help doing a transition from (currently) word-size based offsets duke@435: // to byte-size based offsets in the VM (this will be important if we desire to pack duke@435: // objects more densely in the VM for 64bit machines). Such a transition should proceed duke@435: // in two steps to minimize the risk of introducing hard-to-find bugs: duke@435: // duke@435: // a) first transition the whole VM into a form where all sizes are strongly typed duke@435: // b) change all WordSize's to ByteSize's where desired and fix the compilation errors duke@435: duke@435: duke@435: #ifdef ASSERT duke@435: duke@435: class ByteSize VALUE_OBJ_CLASS_SPEC { duke@435: private: duke@435: int _size; duke@435: duke@435: // Note: This constructor must be private to avoid implicit conversions! duke@435: ByteSize(int size) { _size = size; } duke@435: duke@435: public: duke@435: // constructors duke@435: inline friend ByteSize in_ByteSize(int size); duke@435: duke@435: // accessors duke@435: inline friend int in_bytes(ByteSize x); duke@435: duke@435: // operators duke@435: friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); } duke@435: friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); } duke@435: friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); } duke@435: duke@435: // comparison duke@435: friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); } duke@435: friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); } duke@435: friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); } duke@435: friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); } duke@435: friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); } duke@435: friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); } duke@435: }; duke@435: duke@435: inline ByteSize in_ByteSize(int size) { return ByteSize(size); } duke@435: inline int in_bytes(ByteSize x) { return x._size; } duke@435: duke@435: duke@435: class WordSize VALUE_OBJ_CLASS_SPEC { duke@435: private: duke@435: int _size; duke@435: duke@435: // Note: This constructor must be private to avoid implicit conversions! duke@435: WordSize(int size) { _size = size; } duke@435: duke@435: public: duke@435: // constructors duke@435: inline friend WordSize in_WordSize(int size); duke@435: duke@435: // accessors duke@435: inline friend int in_words(WordSize x); duke@435: duke@435: // operators duke@435: friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); } duke@435: friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); } duke@435: friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); } duke@435: duke@435: // comparison duke@435: friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); } duke@435: friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); } duke@435: friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); } duke@435: friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); } duke@435: friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); } duke@435: friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); } duke@435: }; duke@435: duke@435: inline WordSize in_WordSize(int size) { return WordSize(size); } duke@435: inline int in_words(WordSize x) { return x._size; } duke@435: duke@435: duke@435: #else // ASSERT duke@435: duke@435: // The following definitions must match the corresponding friend declarations duke@435: // in the Byte/WordSize classes if they are typedef'ed to be int. This will duke@435: // be the case in optimized mode to ensure zero overhead for these types. duke@435: // duke@435: // Note: If a compiler does not inline these function calls away, one may duke@435: // want to use #define's to make sure full optimization (constant duke@435: // folding in particular) is possible. duke@435: duke@435: typedef int ByteSize; duke@435: inline ByteSize in_ByteSize(int size) { return size; } duke@435: inline int in_bytes (ByteSize x) { return x; } duke@435: duke@435: typedef int WordSize; duke@435: inline WordSize in_WordSize(int size) { return size; } duke@435: inline int in_words (WordSize x) { return x; } duke@435: duke@435: #endif // ASSERT duke@435: duke@435: duke@435: // Use the following #define to get C++ field member offsets duke@435: duke@435: #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field)) stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_SIZES_HPP