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