Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 2000, 2010, 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 */
25 #ifndef SHARE_VM_UTILITIES_SIZES_HPP
26 #define SHARE_VM_UTILITIES_SIZES_HPP
28 #include "memory/allocation.hpp"
29 #include "utilities/globalDefinitions.hpp"
31 // The following two classes are used to represent 'sizes' and 'offsets' in the VM;
32 // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while
33 // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
34 // depending on platform).
35 //
36 // The classes are defined with friend functions operating on them instead of member
37 // functions so that they (the classes) can be re-#define'd to int types in optimized
38 // mode. This allows full type checking and maximum safety in debug mode, and full
39 // optimizations (constant folding) and zero overhead (time and space wise) in the
40 // optimized build (some compilers do not optimize one-element value classes but
41 // instead create an object in memory - thus the overhead may be significant).
42 //
43 // Note: 1) DO NOT add new overloaded friend functions that do not have a unique function
44 // function name but require signature types for resolution. This will not work
45 // in optimized mode as both, ByteSize and WordSize are mapped to the same type
46 // and thus the distinction would not be possible anymore (=> compiler errors).
47 //
48 // 2) DO NOT add non-static member functions as they cannot be mapped so something
49 // compilable in the optimized build. Static member functions could be added
50 // but require a corresponding class definition in the optimized build.
51 //
52 // These classes should help doing a transition from (currently) word-size based offsets
53 // to byte-size based offsets in the VM (this will be important if we desire to pack
54 // objects more densely in the VM for 64bit machines). Such a transition should proceed
55 // in two steps to minimize the risk of introducing hard-to-find bugs:
56 //
57 // a) first transition the whole VM into a form where all sizes are strongly typed
58 // b) change all WordSize's to ByteSize's where desired and fix the compilation errors
61 #ifdef ASSERT
63 class ByteSize VALUE_OBJ_CLASS_SPEC {
64 private:
65 int _size;
67 // Note: This constructor must be private to avoid implicit conversions!
68 ByteSize(int size) { _size = size; }
70 public:
71 // constructors
72 inline friend ByteSize in_ByteSize(int size);
74 // accessors
75 inline friend int in_bytes(ByteSize x);
77 // operators
78 friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); }
79 friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); }
80 friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); }
82 // comparison
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 friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); }
89 };
91 inline ByteSize in_ByteSize(int size) { return ByteSize(size); }
92 inline int in_bytes(ByteSize x) { return x._size; }
95 class WordSize VALUE_OBJ_CLASS_SPEC {
96 private:
97 int _size;
99 // Note: This constructor must be private to avoid implicit conversions!
100 WordSize(int size) { _size = size; }
102 public:
103 // constructors
104 inline friend WordSize in_WordSize(int size);
106 // accessors
107 inline friend int in_words(WordSize x);
109 // operators
110 friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); }
111 friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); }
112 friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); }
114 // comparison
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 friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); }
121 };
123 inline WordSize in_WordSize(int size) { return WordSize(size); }
124 inline int in_words(WordSize x) { return x._size; }
127 #else // ASSERT
129 // The following definitions must match the corresponding friend declarations
130 // in the Byte/WordSize classes if they are typedef'ed to be int. This will
131 // be the case in optimized mode to ensure zero overhead for these types.
132 //
133 // Note: If a compiler does not inline these function calls away, one may
134 // want to use #define's to make sure full optimization (constant
135 // folding in particular) is possible.
137 typedef int ByteSize;
138 inline ByteSize in_ByteSize(int size) { return size; }
139 inline int in_bytes (ByteSize x) { return x; }
141 typedef int WordSize;
142 inline WordSize in_WordSize(int size) { return size; }
143 inline int in_words (WordSize x) { return x; }
145 #endif // ASSERT
148 // Use the following #define to get C++ field member offsets
150 #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field))
152 #endif // SHARE_VM_UTILITIES_SIZES_HPP