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