annotate src/share/vm/utilities/sizes.hpp @ 1490:f03d0a26bf83

6888954: argument formatting for assert() and friends Reviewed-by: kvn, twisti, apetrusenko, never, dcubed
author jcoomes
date Thu, 22 Apr 2010 13:23:15 -0700
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // The following two classes are used to represent 'sizes' and 'offsets' in the VM;
a61af66fc99e Initial load
duke
parents:
diff changeset
26 // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // depending on platform).
a61af66fc99e Initial load
duke
parents:
diff changeset
29 //
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // The classes are defined with friend functions operating on them instead of member
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // functions so that they (the classes) can be re-#define'd to int types in optimized
a61af66fc99e Initial load
duke
parents:
diff changeset
32 // mode. This allows full type checking and maximum safety in debug mode, and full
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // optimizations (constant folding) and zero overhead (time and space wise) in the
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // optimized build (some compilers do not optimize one-element value classes but
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // instead create an object in memory - thus the overhead may be significant).
a61af66fc99e Initial load
duke
parents:
diff changeset
36 //
a61af66fc99e Initial load
duke
parents:
diff changeset
37 // Note: 1) DO NOT add new overloaded friend functions that do not have a unique function
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // function name but require signature types for resolution. This will not work
a61af66fc99e Initial load
duke
parents:
diff changeset
39 // in optimized mode as both, ByteSize and WordSize are mapped to the same type
a61af66fc99e Initial load
duke
parents:
diff changeset
40 // and thus the distinction would not be possible anymore (=> compiler errors).
a61af66fc99e Initial load
duke
parents:
diff changeset
41 //
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // 2) DO NOT add non-static member functions as they cannot be mapped so something
a61af66fc99e Initial load
duke
parents:
diff changeset
43 // compilable in the optimized build. Static member functions could be added
a61af66fc99e Initial load
duke
parents:
diff changeset
44 // but require a corresponding class definition in the optimized build.
a61af66fc99e Initial load
duke
parents:
diff changeset
45 //
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // These classes should help doing a transition from (currently) word-size based offsets
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // to byte-size based offsets in the VM (this will be important if we desire to pack
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // objects more densely in the VM for 64bit machines). Such a transition should proceed
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // in two steps to minimize the risk of introducing hard-to-find bugs:
a61af66fc99e Initial load
duke
parents:
diff changeset
50 //
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // a) first transition the whole VM into a form where all sizes are strongly typed
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // b) change all WordSize's to ByteSize's where desired and fix the compilation errors
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 class ByteSize VALUE_OBJ_CLASS_SPEC {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
59 int _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // Note: This constructor must be private to avoid implicit conversions!
a61af66fc99e Initial load
duke
parents:
diff changeset
62 ByteSize(int size) { _size = size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // constructors
a61af66fc99e Initial load
duke
parents:
diff changeset
66 inline friend ByteSize in_ByteSize(int size);
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // accessors
a61af66fc99e Initial load
duke
parents:
diff changeset
69 inline friend int in_bytes(ByteSize x);
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 // operators
a61af66fc99e Initial load
duke
parents:
diff changeset
72 friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); }
a61af66fc99e Initial load
duke
parents:
diff changeset
73 friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); }
a61af66fc99e Initial load
duke
parents:
diff changeset
74 friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); }
a61af66fc99e Initial load
duke
parents:
diff changeset
75
a61af66fc99e Initial load
duke
parents:
diff changeset
76 // comparison
a61af66fc99e Initial load
duke
parents:
diff changeset
77 friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
78 friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
79 friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
81 friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
82 friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
83 };
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 inline ByteSize in_ByteSize(int size) { return ByteSize(size); }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 inline int in_bytes(ByteSize x) { return x._size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 class WordSize VALUE_OBJ_CLASS_SPEC {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
91 int _size;
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // Note: This constructor must be private to avoid implicit conversions!
a61af66fc99e Initial load
duke
parents:
diff changeset
94 WordSize(int size) { _size = size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // constructors
a61af66fc99e Initial load
duke
parents:
diff changeset
98 inline friend WordSize in_WordSize(int size);
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 // accessors
a61af66fc99e Initial load
duke
parents:
diff changeset
101 inline friend int in_words(WordSize x);
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // operators
a61af66fc99e Initial load
duke
parents:
diff changeset
104 friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); }
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // comparison
a61af66fc99e Initial load
duke
parents:
diff changeset
109 friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
112 friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
113 friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 };
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117 inline WordSize in_WordSize(int size) { return WordSize(size); }
a61af66fc99e Initial load
duke
parents:
diff changeset
118 inline int in_words(WordSize x) { return x._size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120
a61af66fc99e Initial load
duke
parents:
diff changeset
121 #else // ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // The following definitions must match the corresponding friend declarations
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // in the Byte/WordSize classes if they are typedef'ed to be int. This will
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // be the case in optimized mode to ensure zero overhead for these types.
a61af66fc99e Initial load
duke
parents:
diff changeset
126 //
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // Note: If a compiler does not inline these function calls away, one may
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // want to use #define's to make sure full optimization (constant
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // folding in particular) is possible.
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 typedef int ByteSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 inline ByteSize in_ByteSize(int size) { return size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
133 inline int in_bytes (ByteSize x) { return x; }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 typedef int WordSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 inline WordSize in_WordSize(int size) { return size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 inline int in_words (WordSize x) { return x; }
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 #endif // ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // Use the following #define to get C++ field member offsets
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field))