1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/sizes.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,152 @@ 1.4 +/* 1.5 + * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_UTILITIES_SIZES_HPP 1.29 +#define SHARE_VM_UTILITIES_SIZES_HPP 1.30 + 1.31 +#include "memory/allocation.hpp" 1.32 +#include "utilities/globalDefinitions.hpp" 1.33 + 1.34 +// The following two classes are used to represent 'sizes' and 'offsets' in the VM; 1.35 +// they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while 1.36 +// WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words 1.37 +// depending on platform). 1.38 +// 1.39 +// The classes are defined with friend functions operating on them instead of member 1.40 +// functions so that they (the classes) can be re-#define'd to int types in optimized 1.41 +// mode. This allows full type checking and maximum safety in debug mode, and full 1.42 +// optimizations (constant folding) and zero overhead (time and space wise) in the 1.43 +// optimized build (some compilers do not optimize one-element value classes but 1.44 +// instead create an object in memory - thus the overhead may be significant). 1.45 +// 1.46 +// Note: 1) DO NOT add new overloaded friend functions that do not have a unique function 1.47 +// function name but require signature types for resolution. This will not work 1.48 +// in optimized mode as both, ByteSize and WordSize are mapped to the same type 1.49 +// and thus the distinction would not be possible anymore (=> compiler errors). 1.50 +// 1.51 +// 2) DO NOT add non-static member functions as they cannot be mapped so something 1.52 +// compilable in the optimized build. Static member functions could be added 1.53 +// but require a corresponding class definition in the optimized build. 1.54 +// 1.55 +// These classes should help doing a transition from (currently) word-size based offsets 1.56 +// to byte-size based offsets in the VM (this will be important if we desire to pack 1.57 +// objects more densely in the VM for 64bit machines). Such a transition should proceed 1.58 +// in two steps to minimize the risk of introducing hard-to-find bugs: 1.59 +// 1.60 +// a) first transition the whole VM into a form where all sizes are strongly typed 1.61 +// b) change all WordSize's to ByteSize's where desired and fix the compilation errors 1.62 + 1.63 + 1.64 +#ifdef ASSERT 1.65 + 1.66 +class ByteSize VALUE_OBJ_CLASS_SPEC { 1.67 + private: 1.68 + int _size; 1.69 + 1.70 + // Note: This constructor must be private to avoid implicit conversions! 1.71 + ByteSize(int size) { _size = size; } 1.72 + 1.73 + public: 1.74 + // constructors 1.75 + inline friend ByteSize in_ByteSize(int size); 1.76 + 1.77 + // accessors 1.78 + inline friend int in_bytes(ByteSize x); 1.79 + 1.80 + // operators 1.81 + friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); } 1.82 + friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); } 1.83 + friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); } 1.84 + 1.85 + // comparison 1.86 + friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); } 1.87 + friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); } 1.88 + friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); } 1.89 + friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); } 1.90 + friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); } 1.91 + friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); } 1.92 +}; 1.93 + 1.94 +inline ByteSize in_ByteSize(int size) { return ByteSize(size); } 1.95 +inline int in_bytes(ByteSize x) { return x._size; } 1.96 + 1.97 + 1.98 +class WordSize VALUE_OBJ_CLASS_SPEC { 1.99 + private: 1.100 + int _size; 1.101 + 1.102 + // Note: This constructor must be private to avoid implicit conversions! 1.103 + WordSize(int size) { _size = size; } 1.104 + 1.105 + public: 1.106 + // constructors 1.107 + inline friend WordSize in_WordSize(int size); 1.108 + 1.109 + // accessors 1.110 + inline friend int in_words(WordSize x); 1.111 + 1.112 + // operators 1.113 + friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); } 1.114 + friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); } 1.115 + friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); } 1.116 + 1.117 + // comparison 1.118 + friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); } 1.119 + friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); } 1.120 + friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); } 1.121 + friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); } 1.122 + friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); } 1.123 + friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); } 1.124 +}; 1.125 + 1.126 +inline WordSize in_WordSize(int size) { return WordSize(size); } 1.127 +inline int in_words(WordSize x) { return x._size; } 1.128 + 1.129 + 1.130 +#else // ASSERT 1.131 + 1.132 +// The following definitions must match the corresponding friend declarations 1.133 +// in the Byte/WordSize classes if they are typedef'ed to be int. This will 1.134 +// be the case in optimized mode to ensure zero overhead for these types. 1.135 +// 1.136 +// Note: If a compiler does not inline these function calls away, one may 1.137 +// want to use #define's to make sure full optimization (constant 1.138 +// folding in particular) is possible. 1.139 + 1.140 +typedef int ByteSize; 1.141 +inline ByteSize in_ByteSize(int size) { return size; } 1.142 +inline int in_bytes (ByteSize x) { return x; } 1.143 + 1.144 +typedef int WordSize; 1.145 +inline WordSize in_WordSize(int size) { return size; } 1.146 +inline int in_words (WordSize x) { return x; } 1.147 + 1.148 +#endif // ASSERT 1.149 + 1.150 + 1.151 +// Use the following #define to get C++ field member offsets 1.152 + 1.153 +#define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field)) 1.154 + 1.155 +#endif // SHARE_VM_UTILITIES_SIZES_HPP