globalDefinitions.hpp revision 13242:fcb4803050e8
1228753Smm/*
2228753Smm * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3228753Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4228753Smm *
5228753Smm * This code is free software; you can redistribute it and/or modify it
6228753Smm * under the terms of the GNU General Public License version 2 only, as
7228753Smm * published by the Free Software Foundation.
8228753Smm *
9228753Smm * This code is distributed in the hope that it will be useful, but WITHOUT
10228753Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11228753Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12228753Smm * version 2 for more details (a copy is included in the LICENSE file that
13228753Smm * accompanied this code).
14228753Smm *
15228753Smm * You should have received a copy of the GNU General Public License version
16228753Smm * 2 along with this work; if not, write to the Free Software Foundation,
17228753Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18228753Smm *
19228753Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20228753Smm * or visit www.oracle.com if you need additional information or have any
21228753Smm * questions.
22228753Smm *
23228753Smm */
24228753Smm
25228753Smm#ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
26228753Smm#define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
27228753Smm
28228753Smm#include "utilities/compilerWarnings.hpp"
29228763Smm#include "utilities/debug.hpp"
30228753Smm#include "utilities/macros.hpp"
31228753Smm
32228753Smm#include COMPILER_HEADER(utilities/globalDefinitions)
33228753Smm
34228753Smm// Defaults for macros that might be defined per compiler.
35228753Smm#ifndef NOINLINE
36228753Smm#define NOINLINE
37228753Smm#endif
38228753Smm#ifndef ALWAYSINLINE
39228753Smm#define ALWAYSINLINE inline
40228753Smm#endif
41228753Smm
42228753Smm// This file holds all globally used constants & types, class (forward)
43228753Smm// declarations and a few frequently used utility functions.
44228753Smm
45228753Smm//----------------------------------------------------------------------------------------------------
46228753Smm// Printf-style formatters for fixed- and variable-width types as pointers and
47228753Smm// integers.  These are derived from the definitions in inttypes.h.  If the platform
48228753Smm// doesn't provide appropriate definitions, they should be provided in
49228753Smm// the compiler-specific definitions file (e.g., globalDefinitions_gcc.hpp)
50228753Smm
51228753Smm#define BOOL_TO_STR(_b_) ((_b_) ? "true" : "false")
52228753Smm
53228753Smm// Format 32-bit quantities.
54232153Smm#define INT32_FORMAT           "%" PRId32
55228753Smm#define UINT32_FORMAT          "%" PRIu32
56228753Smm#define INT32_FORMAT_W(width)  "%" #width PRId32
57228753Smm#define UINT32_FORMAT_W(width) "%" #width PRIu32
58228753Smm
59228753Smm#define PTR32_FORMAT           "0x%08" PRIx32
60228753Smm#define PTR32_FORMAT_W(width)  "0x%" #width PRIx32
61228753Smm
62228753Smm// Format 64-bit quantities.
63228753Smm#define INT64_FORMAT           "%" PRId64
64248616Smm#define UINT64_FORMAT          "%" PRIu64
65228753Smm#define UINT64_FORMAT_X        "%" PRIx64
66232153Smm#define INT64_FORMAT_W(width)  "%" #width PRId64
67228753Smm#define UINT64_FORMAT_W(width) "%" #width PRIu64
68228753Smm
69228753Smm#define PTR64_FORMAT           "0x%016" PRIx64
70248616Smm
71228753Smm// Format jlong, if necessary
72228753Smm#ifndef JLONG_FORMAT
73228753Smm#define JLONG_FORMAT           INT64_FORMAT
74228753Smm#endif
75248616Smm#ifndef JULONG_FORMAT
76228753Smm#define JULONG_FORMAT          UINT64_FORMAT
77248616Smm#endif
78228753Smm#ifndef JULONG_FORMAT_X
79228753Smm#define JULONG_FORMAT_X        UINT64_FORMAT_X
80228753Smm#endif
81228753Smm
82228753Smm// Format pointers which change size between 32- and 64-bit.
83228753Smm#ifdef  _LP64
84228753Smm#define INTPTR_FORMAT "0x%016" PRIxPTR
85228753Smm#define PTR_FORMAT    "0x%016" PRIxPTR
86228753Smm#else   // !_LP64
87228753Smm#define INTPTR_FORMAT "0x%08"  PRIxPTR
88248616Smm#define PTR_FORMAT    "0x%08"  PRIxPTR
89228753Smm#endif  // _LP64
90228753Smm
91228753Smm#define INTPTR_FORMAT_W(width)   "%" #width PRIxPTR
92228753Smm
93228753Smm#define SSIZE_FORMAT             "%"   PRIdPTR
94228753Smm#define SIZE_FORMAT              "%"   PRIuPTR
95228753Smm#define SIZE_FORMAT_HEX          "0x%" PRIxPTR
96228753Smm#define SSIZE_FORMAT_W(width)    "%"   #width PRIdPTR
97228753Smm#define SIZE_FORMAT_W(width)     "%"   #width PRIuPTR
98228753Smm#define SIZE_FORMAT_HEX_W(width) "0x%" #width PRIxPTR
99228753Smm
100228753Smm#define INTX_FORMAT           "%" PRIdPTR
101228753Smm#define UINTX_FORMAT          "%" PRIuPTR
102228753Smm#define INTX_FORMAT_W(width)  "%" #width PRIdPTR
103228753Smm#define UINTX_FORMAT_W(width) "%" #width PRIuPTR
104228753Smm
105228753Smm//----------------------------------------------------------------------------------------------------
106228753Smm// Constants
107228753Smm
108228753Smmconst int LogBytesPerShort   = 1;
109228753Smmconst int LogBytesPerInt     = 2;
110228753Smm#ifdef _LP64
111228753Smmconst int LogBytesPerWord    = 3;
112228753Smm#else
113228753Smmconst int LogBytesPerWord    = 2;
114228753Smm#endif
115228753Smmconst int LogBytesPerLong    = 3;
116228753Smm
117228753Smmconst int BytesPerShort      = 1 << LogBytesPerShort;
118232153Smmconst int BytesPerInt        = 1 << LogBytesPerInt;
119228753Smmconst int BytesPerWord       = 1 << LogBytesPerWord;
120228753Smmconst int BytesPerLong       = 1 << LogBytesPerLong;
121228753Smm
122228753Smmconst int LogBitsPerByte     = 3;
123228753Smmconst int LogBitsPerShort    = LogBitsPerByte + LogBytesPerShort;
124228753Smmconst int LogBitsPerInt      = LogBitsPerByte + LogBytesPerInt;
125228753Smmconst int LogBitsPerWord     = LogBitsPerByte + LogBytesPerWord;
126228753Smmconst int LogBitsPerLong     = LogBitsPerByte + LogBytesPerLong;
127228753Smm
128228753Smmconst int BitsPerByte        = 1 << LogBitsPerByte;
129228753Smmconst int BitsPerShort       = 1 << LogBitsPerShort;
130228753Smmconst int BitsPerInt         = 1 << LogBitsPerInt;
131228753Smmconst int BitsPerWord        = 1 << LogBitsPerWord;
132228753Smmconst int BitsPerLong        = 1 << LogBitsPerLong;
133228753Smm
134228753Smmconst int WordAlignmentMask  = (1 << LogBytesPerWord) - 1;
135228753Smmconst int LongAlignmentMask  = (1 << LogBytesPerLong) - 1;
136228753Smm
137228753Smmconst int WordsPerLong       = 2;       // Number of stack entries for longs
138228753Smm
139228753Smmconst int oopSize            = sizeof(char*); // Full-width oop
140228753Smmextern int heapOopSize;                       // Oop within a java object
141228753Smmconst int wordSize           = sizeof(char*);
142228753Smmconst int longSize           = sizeof(jlong);
143228753Smmconst int jintSize           = sizeof(jint);
144228753Smmconst int size_tSize         = sizeof(size_t);
145228753Smm
146228753Smmconst int BytesPerOop        = BytesPerWord;  // Full-width oop
147228753Smm
148228753Smmextern int LogBytesPerHeapOop;                // Oop within a java object
149228753Smmextern int LogBitsPerHeapOop;
150228753Smmextern int BytesPerHeapOop;
151228753Smmextern int BitsPerHeapOop;
152228753Smm
153228753Smmconst int BitsPerJavaInteger = 32;
154228753Smmconst int BitsPerJavaLong    = 64;
155228753Smmconst int BitsPerSize_t      = size_tSize * BitsPerByte;
156228753Smm
157228753Smm// Size of a char[] needed to represent a jint as a string in decimal.
158228753Smmconst int jintAsStringSize = 12;
159228753Smm
160228753Smm// In fact this should be
161228753Smm// log2_intptr(sizeof(class JavaThread)) - log2_intptr(64);
162228753Smm// see os::set_memory_serialize_page()
163228753Smm#ifdef _LP64
164228753Smmconst int SerializePageShiftCount = 4;
165228753Smm#else
166228753Smmconst int SerializePageShiftCount = 3;
167228753Smm#endif
168228753Smm
169228753Smm// An opaque struct of heap-word width, so that HeapWord* can be a generic
170228753Smm// pointer into the heap.  We require that object sizes be measured in
171228753Smm// units of heap words, so that that
172228753Smm//   HeapWord* hw;
173228753Smm//   hw += oop(hw)->foo();
174228753Smm// works, where foo is a method (like size or scavenge) that returns the
175228753Smm// object size.
176228753Smmclass HeapWord {
177228753Smm  friend class VMStructs;
178228753Smm private:
179228753Smm  char* i;
180228753Smm#ifndef PRODUCT
181228753Smm public:
182228753Smm  char* value() { return i; }
183228753Smm#endif
184228753Smm};
185228753Smm
186228753Smm// Analogous opaque struct for metadata allocated from
187228753Smm// metaspaces.
188228753Smmclass MetaWord {
189228753Smm private:
190228753Smm  char* i;
191228753Smm};
192228753Smm
193228753Smm// HeapWordSize must be 2^LogHeapWordSize.
194228753Smmconst int HeapWordSize        = sizeof(HeapWord);
195228753Smm#ifdef _LP64
196228753Smmconst int LogHeapWordSize     = 3;
197232153Smm#else
198228753Smmconst int LogHeapWordSize     = 2;
199228753Smm#endif
200228753Smmconst int HeapWordsPerLong    = BytesPerLong / HeapWordSize;
201228753Smmconst int LogHeapWordsPerLong = LogBytesPerLong - LogHeapWordSize;
202228753Smm
203228753Smm// The larger HeapWordSize for 64bit requires larger heaps
204228753Smm// for the same application running in 64bit.  See bug 4967770.
205228753Smm// The minimum alignment to a heap word size is done.  Other
206228753Smm// parts of the memory system may require additional alignment
207228753Smm// and are responsible for those alignments.
208228753Smm#ifdef _LP64
209228753Smm#define ScaleForWordSize(x) align_size_down_((x) * 13 / 10, HeapWordSize)
210228753Smm#else
211232153Smm#define ScaleForWordSize(x) (x)
212228753Smm#endif
213228753Smm
214228753Smm// The minimum number of native machine words necessary to contain "byte_size"
215228753Smm// bytes.
216228753Smminline size_t heap_word_size(size_t byte_size) {
217228753Smm  return (byte_size + (HeapWordSize-1)) >> LogHeapWordSize;
218228753Smm}
219228753Smm
220228753Smm//-------------------------------------------
221228753Smm// Constant for jlong (standardized by C++11)
222228753Smm
223228753Smm// Build a 64bit integer constant
224228753Smm#define CONST64(x)  (x ## LL)
225228753Smm#define UCONST64(x) (x ## ULL)
226228753Smm
227228753Smmconst jlong min_jlong = CONST64(0x8000000000000000);
228228753Smmconst jlong max_jlong = CONST64(0x7fffffffffffffff);
229228753Smm
230228753Smmconst size_t K                  = 1024;
231228753Smmconst size_t M                  = K*K;
232228753Smmconst size_t G                  = M*K;
233228753Smmconst size_t HWperKB            = K / sizeof(HeapWord);
234228753Smm
235228753Smm// Constants for converting from a base unit to milli-base units.  For
236228753Smm// example from seconds to milliseconds and microseconds
237228753Smm
238228753Smmconst int MILLIUNITS    = 1000;         // milli units per base unit
239228753Smmconst int MICROUNITS    = 1000000;      // micro units per base unit
240228753Smmconst int NANOUNITS     = 1000000000;   // nano units per base unit
241228753Smm
242228753Smmconst jlong NANOSECS_PER_SEC      = CONST64(1000000000);
243228753Smmconst jint  NANOSECS_PER_MILLISEC = 1000000;
244228753Smm
245228753Smminline const char* proper_unit_for_byte_size(size_t s) {
246228753Smm#ifdef _LP64
247228753Smm  if (s >= 10*G) {
248228753Smm    return "G";
249228753Smm  }
250232153Smm#endif
251232153Smm  if (s >= 10*M) {
252232153Smm    return "M";
253228753Smm  } else if (s >= 10*K) {
254228753Smm    return "K";
255228753Smm  } else {
256228753Smm    return "B";
257228753Smm  }
258228753Smm}
259228753Smm
260228753Smmtemplate <class T>
261228753Smminline T byte_size_in_proper_unit(T s) {
262228753Smm#ifdef _LP64
263232153Smm  if (s >= 10*G) {
264228753Smm    return (T)(s/G);
265228753Smm  }
266228753Smm#endif
267228753Smm  if (s >= 10*M) {
268228753Smm    return (T)(s/M);
269228753Smm  } else if (s >= 10*K) {
270228753Smm    return (T)(s/K);
271228753Smm  } else {
272228753Smm    return s;
273228753Smm  }
274228753Smm}
275228753Smm
276228753Smminline const char* exact_unit_for_byte_size(size_t s) {
277228753Smm#ifdef _LP64
278228753Smm  if (s >= G && (s % G) == 0) {
279228753Smm    return "G";
280228753Smm  }
281228753Smm#endif
282228753Smm  if (s >= M && (s % M) == 0) {
283228753Smm    return "M";
284228753Smm  }
285228753Smm  if (s >= K && (s % K) == 0) {
286228753Smm    return "K";
287228753Smm  }
288228753Smm  return "B";
289228753Smm}
290228753Smm
291228753Smminline size_t byte_size_in_exact_unit(size_t s) {
292228753Smm#ifdef _LP64
293228753Smm  if (s >= G && (s % G) == 0) {
294228777Smm    return s / G;
295228777Smm  }
296228753Smm#endif
297228753Smm  if (s >= M && (s % M) == 0) {
298228753Smm    return s / M;
299228753Smm  }
300228753Smm  if (s >= K && (s % K) == 0) {
301228753Smm    return s / K;
302228753Smm  }
303228753Smm  return s;
304228753Smm}
305228753Smm
306228753Smm//----------------------------------------------------------------------------------------------------
307228753Smm// VM type definitions
308228753Smm
309228753Smm// intx and uintx are the 'extended' int and 'extended' unsigned int types;
310228753Smm// they are 32bit wide on a 32-bit platform, and 64bit wide on a 64bit platform.
311228753Smm
312228753Smmtypedef intptr_t  intx;
313228753Smmtypedef uintptr_t uintx;
314228753Smm
315228753Smmconst intx  min_intx  = (intx)1 << (sizeof(intx)*BitsPerByte-1);
316228753Smmconst intx  max_intx  = (uintx)min_intx - 1;
317228753Smmconst uintx max_uintx = (uintx)-1;
318228753Smm
319228753Smm// Table of values:
320228753Smm//      sizeof intx         4               8
321228753Smm// min_intx             0x80000000      0x8000000000000000
322228753Smm// max_intx             0x7FFFFFFF      0x7FFFFFFFFFFFFFFF
323228753Smm// max_uintx            0xFFFFFFFF      0xFFFFFFFFFFFFFFFF
324228753Smm
325228753Smmtypedef unsigned int uint;   NEEDS_CLEANUP
326228753Smm
327228753Smm
328228753Smm//----------------------------------------------------------------------------------------------------
329228753Smm// Java type definitions
330228753Smm
331228753Smm// All kinds of 'plain' byte addresses
332228753Smmtypedef   signed char s_char;
333228753Smmtypedef unsigned char u_char;
334228753Smmtypedef u_char*       address;
335228753Smmtypedef uintptr_t     address_word; // unsigned integer which will hold a pointer
336228753Smm                                    // except for some implementations of a C++
337228753Smm                                    // linkage pointer to function. Should never
338228753Smm                                    // need one of those to be placed in this
339228753Smm                                    // type anyway.
340228753Smm
341228753Smm//  Utility functions to "portably" (?) bit twiddle pointers
342228753Smm//  Where portable means keep ANSI C++ compilers quiet
343228753Smm
344228753Smminline address       set_address_bits(address x, int m)       { return address(intptr_t(x) | m); }
345228753Smminline address       clear_address_bits(address x, int m)     { return address(intptr_t(x) & ~m); }
346228753Smm
347228753Smm//  Utility functions to "portably" make cast to/from function pointers.
348228753Smm
349232153Smminline address_word  mask_address_bits(address x, int m)      { return address_word(x) & m; }
350228753Smminline address_word  castable_address(address x)              { return address_word(x) ; }
351228753Smminline address_word  castable_address(void* x)                { return address_word(x) ; }
352228753Smm
353228753Smm// Pointer subtraction.
354238856Smm// The idea here is to avoid ptrdiff_t, which is signed and so doesn't have
355228753Smm// the range we might need to find differences from one end of the heap
356228753Smm// to the other.
357228753Smm// A typical use might be:
358228753Smm//     if (pointer_delta(end(), top()) >= size) {
359228753Smm//       // enough room for an object of size
360228753Smm//       ...
361228753Smm// and then additions like
362228753Smm//       ... top() + size ...
363228753Smm// are safe because we know that top() is at least size below end().
364228753Smminline size_t pointer_delta(const volatile void* left,
365228753Smm                            const volatile void* right,
366228753Smm                            size_t element_size) {
367228753Smm  return (((uintptr_t) left) - ((uintptr_t) right)) / element_size;
368232153Smm}
369228753Smm
370228753Smm// A version specialized for HeapWord*'s.
371228753Smminline size_t pointer_delta(const HeapWord* left, const HeapWord* right) {
372228753Smm  return pointer_delta(left, right, sizeof(HeapWord));
373228753Smm}
374228753Smm// A version specialized for MetaWord*'s.
375228753Smminline size_t pointer_delta(const MetaWord* left, const MetaWord* right) {
376228753Smm  return pointer_delta(left, right, sizeof(MetaWord));
377228753Smm}
378228753Smm
379//
380// ANSI C++ does not allow casting from one pointer type to a function pointer
381// directly without at best a warning. This macro accomplishes it silently
382// In every case that is present at this point the value be cast is a pointer
383// to a C linkage function. In some case the type used for the cast reflects
384// that linkage and a picky compiler would not complain. In other cases because
385// there is no convenient place to place a typedef with extern C linkage (i.e
386// a platform dependent header file) it doesn't. At this point no compiler seems
387// picky enough to catch these instances (which are few). It is possible that
388// using templates could fix these for all cases. This use of templates is likely
389// so far from the middle of the road that it is likely to be problematic in
390// many C++ compilers.
391//
392#define CAST_TO_FN_PTR(func_type, value) (reinterpret_cast<func_type>(value))
393#define CAST_FROM_FN_PTR(new_type, func_ptr) ((new_type)((address_word)(func_ptr)))
394
395// Unsigned byte types for os and stream.hpp
396
397// Unsigned one, two, four and eigth byte quantities used for describing
398// the .class file format. See JVM book chapter 4.
399
400typedef jubyte  u1;
401typedef jushort u2;
402typedef juint   u4;
403typedef julong  u8;
404
405const jubyte  max_jubyte  = (jubyte)-1;  // 0xFF       largest jubyte
406const jushort max_jushort = (jushort)-1; // 0xFFFF     largest jushort
407const juint   max_juint   = (juint)-1;   // 0xFFFFFFFF largest juint
408const julong  max_julong  = (julong)-1;  // 0xFF....FF largest julong
409
410typedef jbyte  s1;
411typedef jshort s2;
412typedef jint   s4;
413typedef jlong  s8;
414
415const jbyte min_jbyte = -(1 << 7);       // smallest jbyte
416const jbyte max_jbyte = (1 << 7) - 1;    // largest jbyte
417const jshort min_jshort = -(1 << 15);    // smallest jshort
418const jshort max_jshort = (1 << 15) - 1; // largest jshort
419
420const jint min_jint = (jint)1 << (sizeof(jint)*BitsPerByte-1); // 0x80000000 == smallest jint
421const jint max_jint = (juint)min_jint - 1;                     // 0x7FFFFFFF == largest jint
422
423//----------------------------------------------------------------------------------------------------
424// JVM spec restrictions
425
426const int max_method_code_size = 64*K - 1;  // JVM spec, 2nd ed. section 4.8.1 (p.134)
427
428// Default ProtectionDomainCacheSize values
429
430const int defaultProtectionDomainCacheSize = NOT_LP64(137) LP64_ONLY(2017);
431
432//----------------------------------------------------------------------------------------------------
433// Default and minimum StringTableSize values
434
435const int defaultStringTableSize = NOT_LP64(1009) LP64_ONLY(60013);
436const int minimumStringTableSize = 1009;
437
438const int defaultSymbolTableSize = 20011;
439const int minimumSymbolTableSize = 1009;
440
441
442//----------------------------------------------------------------------------------------------------
443// HotSwap - for JVMTI   aka Class File Replacement and PopFrame
444//
445// Determines whether on-the-fly class replacement and frame popping are enabled.
446
447#define HOTSWAP
448
449//----------------------------------------------------------------------------------------------------
450// Object alignment, in units of HeapWords.
451//
452// Minimum is max(BytesPerLong, BytesPerDouble, BytesPerOop) / HeapWordSize, so jlong, jdouble and
453// reference fields can be naturally aligned.
454
455extern int MinObjAlignment;
456extern int MinObjAlignmentInBytes;
457extern int MinObjAlignmentInBytesMask;
458
459extern int LogMinObjAlignment;
460extern int LogMinObjAlignmentInBytes;
461
462const int LogKlassAlignmentInBytes = 3;
463const int LogKlassAlignment        = LogKlassAlignmentInBytes - LogHeapWordSize;
464const int KlassAlignmentInBytes    = 1 << LogKlassAlignmentInBytes;
465const int KlassAlignment           = KlassAlignmentInBytes / HeapWordSize;
466
467// Maximal size of heap where unscaled compression can be used. Also upper bound
468// for heap placement: 4GB.
469const  uint64_t UnscaledOopHeapMax = (uint64_t(max_juint) + 1);
470// Maximal size of heap where compressed oops can be used. Also upper bound for heap
471// placement for zero based compression algorithm: UnscaledOopHeapMax << LogMinObjAlignmentInBytes.
472extern uint64_t OopEncodingHeapMax;
473
474// Maximal size of compressed class space. Above this limit compression is not possible.
475// Also upper bound for placement of zero based class space. (Class space is further limited
476// to be < 3G, see arguments.cpp.)
477const  uint64_t KlassEncodingMetaspaceMax = (uint64_t(max_juint) + 1) << LogKlassAlignmentInBytes;
478
479// Machine dependent stuff
480
481// The maximum size of the code cache.  Can be overridden by targets.
482#define CODE_CACHE_SIZE_LIMIT (2*G)
483// Allow targets to reduce the default size of the code cache.
484#define CODE_CACHE_DEFAULT_LIMIT CODE_CACHE_SIZE_LIMIT
485
486#include CPU_HEADER(globalDefinitions)
487
488// To assure the IRIW property on processors that are not multiple copy
489// atomic, sync instructions must be issued between volatile reads to
490// assure their ordering, instead of after volatile stores.
491// (See "A Tutorial Introduction to the ARM and POWER Relaxed Memory Models"
492// by Luc Maranget, Susmit Sarkar and Peter Sewell, INRIA/Cambridge)
493#ifdef CPU_NOT_MULTIPLE_COPY_ATOMIC
494const bool support_IRIW_for_not_multiple_copy_atomic_cpu = true;
495#else
496const bool support_IRIW_for_not_multiple_copy_atomic_cpu = false;
497#endif
498
499// The byte alignment to be used by Arena::Amalloc.  See bugid 4169348.
500// Note: this value must be a power of 2
501
502#define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
503
504// Signed variants of alignment helpers.  There are two versions of each, a macro
505// for use in places like enum definitions that require compile-time constant
506// expressions and a function for all other places so as to get type checking.
507
508// Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
509// 'alignment' is an unsigned int and 'what' is a wider type. The & operation
510// will widen the inverted mask, and not sign extend it, leading to a mask with
511// zeros in the most significant bits. The use of align_mask_widened() solves
512// this problem.
513#define align_mask(alignment) ((alignment) - 1)
514#define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
515#define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
516
517#define align_size_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
518
519#define align_size_up_(size, alignment) (align_size_down_((size) + align_mask(alignment), (alignment)))
520
521#define is_size_aligned_(size, alignment) ((size) == (align_size_up_(size, alignment)))
522
523// Helpers to align sizes and check for alignment
524
525template <typename T, typename A>
526inline T align_size_up(T size, A alignment) {
527  return align_size_up_(size, alignment);
528}
529
530template <typename T, typename A>
531inline T align_size_down(T size, A alignment) {
532  return align_size_down_(size, alignment);
533}
534
535template <typename T, typename A>
536inline bool is_size_aligned(T size, A alignment) {
537  return is_size_aligned_(size, alignment);
538}
539
540// Align down with a lower bound. If the aligning results in 0, return 'alignment'.
541template <typename T, typename A>
542inline T align_size_down_bounded(T size, A alignment) {
543  A aligned_size = align_size_down(size, alignment);
544  return aligned_size > 0 ? aligned_size : alignment;
545}
546
547// Helpers to align pointers and check for alignment.
548
549template <typename T, typename A>
550inline T* align_ptr_up(T* ptr, A alignment) {
551  return (T*)align_size_up((uintptr_t)ptr, alignment);
552}
553
554template <typename T, typename A>
555inline T* align_ptr_down(T* ptr, A alignment) {
556  return (T*)align_size_down((uintptr_t)ptr, alignment);
557}
558
559template <typename T, typename A>
560inline bool is_ptr_aligned(T* ptr, A alignment) {
561  return is_size_aligned((uintptr_t)ptr, alignment);
562}
563
564// Align metaspace objects by rounding up to natural word boundary
565template <typename T>
566inline T align_metadata_size(T size) {
567  return align_size_up(size, 1);
568}
569
570// Align objects in the Java Heap by rounding up their size, in HeapWord units.
571template <typename T>
572inline T align_object_size(T word_size) {
573  return align_size_up(word_size, MinObjAlignment);
574}
575
576inline bool is_object_aligned(size_t word_size) {
577  return is_size_aligned(word_size, MinObjAlignment);
578}
579
580inline bool is_ptr_object_aligned(const void* addr) {
581  return is_ptr_aligned(addr, MinObjAlignmentInBytes);
582}
583
584// Pad out certain offsets to jlong alignment, in HeapWord units.
585template <typename T>
586inline T align_object_offset(T offset) {
587  return align_size_up(offset, HeapWordsPerLong);
588}
589
590// Clamp an address to be within a specific page
591// 1. If addr is on the page it is returned as is
592// 2. If addr is above the page_address the start of the *next* page will be returned
593// 3. Otherwise, if addr is below the page_address the start of the page will be returned
594template <typename T>
595inline T* clamp_address_in_page(T* addr, T* page_address, size_t page_size) {
596  if (align_ptr_down(addr, page_size) == align_ptr_down(page_address, page_size)) {
597    // address is in the specified page, just return it as is
598    return addr;
599  } else if (addr > page_address) {
600    // address is above specified page, return start of next page
601    return align_ptr_down(page_address, page_size) + page_size;
602  } else {
603    // address is below specified page, return start of page
604    return align_ptr_down(page_address, page_size);
605  }
606}
607
608
609// The expected size in bytes of a cache line, used to pad data structures.
610#ifndef DEFAULT_CACHE_LINE_SIZE
611  #define DEFAULT_CACHE_LINE_SIZE 64
612#endif
613
614
615//----------------------------------------------------------------------------------------------------
616// Utility macros for compilers
617// used to silence compiler warnings
618
619#define Unused_Variable(var) var
620
621
622//----------------------------------------------------------------------------------------------------
623// Miscellaneous
624
625// 6302670 Eliminate Hotspot __fabsf dependency
626// All fabs() callers should call this function instead, which will implicitly
627// convert the operand to double, avoiding a dependency on __fabsf which
628// doesn't exist in early versions of Solaris 8.
629inline double fabsd(double value) {
630  return fabs(value);
631}
632
633// Returns numerator/denominator as percentage value from 0 to 100. If denominator
634// is zero, return 0.0.
635template<typename T>
636inline double percent_of(T numerator, T denominator) {
637  return denominator != 0 ? (double)numerator / denominator * 100.0 : 0.0;
638}
639
640//----------------------------------------------------------------------------------------------------
641// Special casts
642// Cast floats into same-size integers and vice-versa w/o changing bit-pattern
643typedef union {
644  jfloat f;
645  jint i;
646} FloatIntConv;
647
648typedef union {
649  jdouble d;
650  jlong l;
651  julong ul;
652} DoubleLongConv;
653
654inline jint    jint_cast    (jfloat  x)  { return ((FloatIntConv*)&x)->i; }
655inline jfloat  jfloat_cast  (jint    x)  { return ((FloatIntConv*)&x)->f; }
656
657inline jlong   jlong_cast   (jdouble x)  { return ((DoubleLongConv*)&x)->l;  }
658inline julong  julong_cast  (jdouble x)  { return ((DoubleLongConv*)&x)->ul; }
659inline jdouble jdouble_cast (jlong   x)  { return ((DoubleLongConv*)&x)->d;  }
660
661inline jint low (jlong value)                    { return jint(value); }
662inline jint high(jlong value)                    { return jint(value >> 32); }
663
664// the fancy casts are a hopefully portable way
665// to do unsigned 32 to 64 bit type conversion
666inline void set_low (jlong* value, jint low )    { *value &= (jlong)0xffffffff << 32;
667                                                   *value |= (jlong)(julong)(juint)low; }
668
669inline void set_high(jlong* value, jint high)    { *value &= (jlong)(julong)(juint)0xffffffff;
670                                                   *value |= (jlong)high       << 32; }
671
672inline jlong jlong_from(jint h, jint l) {
673  jlong result = 0; // initialization to avoid warning
674  set_high(&result, h);
675  set_low(&result,  l);
676  return result;
677}
678
679union jlong_accessor {
680  jint  words[2];
681  jlong long_value;
682};
683
684void basic_types_init(); // cannot define here; uses assert
685
686
687// NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
688enum BasicType {
689  T_BOOLEAN     =  4,
690  T_CHAR        =  5,
691  T_FLOAT       =  6,
692  T_DOUBLE      =  7,
693  T_BYTE        =  8,
694  T_SHORT       =  9,
695  T_INT         = 10,
696  T_LONG        = 11,
697  T_OBJECT      = 12,
698  T_ARRAY       = 13,
699  T_VOID        = 14,
700  T_ADDRESS     = 15,
701  T_NARROWOOP   = 16,
702  T_METADATA    = 17,
703  T_NARROWKLASS = 18,
704  T_CONFLICT    = 19, // for stack value type with conflicting contents
705  T_ILLEGAL     = 99
706};
707
708inline bool is_java_primitive(BasicType t) {
709  return T_BOOLEAN <= t && t <= T_LONG;
710}
711
712inline bool is_subword_type(BasicType t) {
713  // these guys are processed exactly like T_INT in calling sequences:
714  return (t == T_BOOLEAN || t == T_CHAR || t == T_BYTE || t == T_SHORT);
715}
716
717inline bool is_signed_subword_type(BasicType t) {
718  return (t == T_BYTE || t == T_SHORT);
719}
720
721// Convert a char from a classfile signature to a BasicType
722inline BasicType char2type(char c) {
723  switch( c ) {
724  case 'B': return T_BYTE;
725  case 'C': return T_CHAR;
726  case 'D': return T_DOUBLE;
727  case 'F': return T_FLOAT;
728  case 'I': return T_INT;
729  case 'J': return T_LONG;
730  case 'S': return T_SHORT;
731  case 'Z': return T_BOOLEAN;
732  case 'V': return T_VOID;
733  case 'L': return T_OBJECT;
734  case '[': return T_ARRAY;
735  }
736  return T_ILLEGAL;
737}
738
739extern char type2char_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
740inline char type2char(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2char_tab[t] : 0; }
741extern int type2size[T_CONFLICT+1];         // Map BasicType to result stack elements
742extern const char* type2name_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
743inline const char* type2name(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2name_tab[t] : NULL; }
744extern BasicType name2type(const char* name);
745
746// Auxiliary math routines
747// least common multiple
748extern size_t lcm(size_t a, size_t b);
749
750
751// NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
752enum BasicTypeSize {
753  T_BOOLEAN_size     = 1,
754  T_CHAR_size        = 1,
755  T_FLOAT_size       = 1,
756  T_DOUBLE_size      = 2,
757  T_BYTE_size        = 1,
758  T_SHORT_size       = 1,
759  T_INT_size         = 1,
760  T_LONG_size        = 2,
761  T_OBJECT_size      = 1,
762  T_ARRAY_size       = 1,
763  T_NARROWOOP_size   = 1,
764  T_NARROWKLASS_size = 1,
765  T_VOID_size        = 0
766};
767
768
769// maps a BasicType to its instance field storage type:
770// all sub-word integral types are widened to T_INT
771extern BasicType type2field[T_CONFLICT+1];
772extern BasicType type2wfield[T_CONFLICT+1];
773
774
775// size in bytes
776enum ArrayElementSize {
777  T_BOOLEAN_aelem_bytes     = 1,
778  T_CHAR_aelem_bytes        = 2,
779  T_FLOAT_aelem_bytes       = 4,
780  T_DOUBLE_aelem_bytes      = 8,
781  T_BYTE_aelem_bytes        = 1,
782  T_SHORT_aelem_bytes       = 2,
783  T_INT_aelem_bytes         = 4,
784  T_LONG_aelem_bytes        = 8,
785#ifdef _LP64
786  T_OBJECT_aelem_bytes      = 8,
787  T_ARRAY_aelem_bytes       = 8,
788#else
789  T_OBJECT_aelem_bytes      = 4,
790  T_ARRAY_aelem_bytes       = 4,
791#endif
792  T_NARROWOOP_aelem_bytes   = 4,
793  T_NARROWKLASS_aelem_bytes = 4,
794  T_VOID_aelem_bytes        = 0
795};
796
797extern int _type2aelembytes[T_CONFLICT+1]; // maps a BasicType to nof bytes used by its array element
798#ifdef ASSERT
799extern int type2aelembytes(BasicType t, bool allow_address = false); // asserts
800#else
801inline int type2aelembytes(BasicType t, bool allow_address = false) { return _type2aelembytes[t]; }
802#endif
803
804
805// JavaValue serves as a container for arbitrary Java values.
806
807class JavaValue {
808
809 public:
810  typedef union JavaCallValue {
811    jfloat   f;
812    jdouble  d;
813    jint     i;
814    jlong    l;
815    jobject  h;
816  } JavaCallValue;
817
818 private:
819  BasicType _type;
820  JavaCallValue _value;
821
822 public:
823  JavaValue(BasicType t = T_ILLEGAL) { _type = t; }
824
825  JavaValue(jfloat value) {
826    _type    = T_FLOAT;
827    _value.f = value;
828  }
829
830  JavaValue(jdouble value) {
831    _type    = T_DOUBLE;
832    _value.d = value;
833  }
834
835 jfloat get_jfloat() const { return _value.f; }
836 jdouble get_jdouble() const { return _value.d; }
837 jint get_jint() const { return _value.i; }
838 jlong get_jlong() const { return _value.l; }
839 jobject get_jobject() const { return _value.h; }
840 JavaCallValue* get_value_addr() { return &_value; }
841 BasicType get_type() const { return _type; }
842
843 void set_jfloat(jfloat f) { _value.f = f;}
844 void set_jdouble(jdouble d) { _value.d = d;}
845 void set_jint(jint i) { _value.i = i;}
846 void set_jlong(jlong l) { _value.l = l;}
847 void set_jobject(jobject h) { _value.h = h;}
848 void set_type(BasicType t) { _type = t; }
849
850 jboolean get_jboolean() const { return (jboolean) (_value.i);}
851 jbyte get_jbyte() const { return (jbyte) (_value.i);}
852 jchar get_jchar() const { return (jchar) (_value.i);}
853 jshort get_jshort() const { return (jshort) (_value.i);}
854
855};
856
857
858#define STACK_BIAS      0
859// V9 Sparc CPU's running in 64 Bit mode use a stack bias of 7ff
860// in order to extend the reach of the stack pointer.
861#if defined(SPARC) && defined(_LP64)
862#undef STACK_BIAS
863#define STACK_BIAS      0x7ff
864#endif
865
866
867// TosState describes the top-of-stack state before and after the execution of
868// a bytecode or method. The top-of-stack value may be cached in one or more CPU
869// registers. The TosState corresponds to the 'machine representation' of this cached
870// value. There's 4 states corresponding to the JAVA types int, long, float & double
871// as well as a 5th state in case the top-of-stack value is actually on the top
872// of stack (in memory) and thus not cached. The atos state corresponds to the itos
873// state when it comes to machine representation but is used separately for (oop)
874// type specific operations (e.g. verification code).
875
876enum TosState {         // describes the tos cache contents
877  btos = 0,             // byte, bool tos cached
878  ztos = 1,             // byte, bool tos cached
879  ctos = 2,             // char tos cached
880  stos = 3,             // short tos cached
881  itos = 4,             // int tos cached
882  ltos = 5,             // long tos cached
883  ftos = 6,             // float tos cached
884  dtos = 7,             // double tos cached
885  atos = 8,             // object cached
886  vtos = 9,             // tos not cached
887  number_of_states,
888  ilgl                  // illegal state: should not occur
889};
890
891
892inline TosState as_TosState(BasicType type) {
893  switch (type) {
894    case T_BYTE   : return btos;
895    case T_BOOLEAN: return ztos;
896    case T_CHAR   : return ctos;
897    case T_SHORT  : return stos;
898    case T_INT    : return itos;
899    case T_LONG   : return ltos;
900    case T_FLOAT  : return ftos;
901    case T_DOUBLE : return dtos;
902    case T_VOID   : return vtos;
903    case T_ARRAY  : // fall through
904    case T_OBJECT : return atos;
905  }
906  return ilgl;
907}
908
909inline BasicType as_BasicType(TosState state) {
910  switch (state) {
911    case btos : return T_BYTE;
912    case ztos : return T_BOOLEAN;
913    case ctos : return T_CHAR;
914    case stos : return T_SHORT;
915    case itos : return T_INT;
916    case ltos : return T_LONG;
917    case ftos : return T_FLOAT;
918    case dtos : return T_DOUBLE;
919    case atos : return T_OBJECT;
920    case vtos : return T_VOID;
921  }
922  return T_ILLEGAL;
923}
924
925
926// Helper function to convert BasicType info into TosState
927// Note: Cannot define here as it uses global constant at the time being.
928TosState as_TosState(BasicType type);
929
930
931// JavaThreadState keeps track of which part of the code a thread is executing in. This
932// information is needed by the safepoint code.
933//
934// There are 4 essential states:
935//
936//  _thread_new         : Just started, but not executed init. code yet (most likely still in OS init code)
937//  _thread_in_native   : In native code. This is a safepoint region, since all oops will be in jobject handles
938//  _thread_in_vm       : Executing in the vm
939//  _thread_in_Java     : Executing either interpreted or compiled Java code (or could be in a stub)
940//
941// Each state has an associated xxxx_trans state, which is an intermediate state used when a thread is in
942// a transition from one state to another. These extra states makes it possible for the safepoint code to
943// handle certain thread_states without having to suspend the thread - making the safepoint code faster.
944//
945// Given a state, the xxxx_trans state can always be found by adding 1.
946//
947enum JavaThreadState {
948  _thread_uninitialized     =  0, // should never happen (missing initialization)
949  _thread_new               =  2, // just starting up, i.e., in process of being initialized
950  _thread_new_trans         =  3, // corresponding transition state (not used, included for completness)
951  _thread_in_native         =  4, // running in native code
952  _thread_in_native_trans   =  5, // corresponding transition state
953  _thread_in_vm             =  6, // running in VM
954  _thread_in_vm_trans       =  7, // corresponding transition state
955  _thread_in_Java           =  8, // running in Java or in stub code
956  _thread_in_Java_trans     =  9, // corresponding transition state (not used, included for completness)
957  _thread_blocked           = 10, // blocked in vm
958  _thread_blocked_trans     = 11, // corresponding transition state
959  _thread_max_state         = 12  // maximum thread state+1 - used for statistics allocation
960};
961
962
963
964//----------------------------------------------------------------------------------------------------
965// 'Forward' declarations of frequently used classes
966// (in order to reduce interface dependencies & reduce
967// number of unnecessary compilations after changes)
968
969class ClassFileStream;
970
971class Event;
972
973class Thread;
974class  VMThread;
975class  JavaThread;
976class Threads;
977
978class VM_Operation;
979class VMOperationQueue;
980
981class CodeBlob;
982class  CompiledMethod;
983class   nmethod;
984class RuntimeBlob;
985class  OSRAdapter;
986class  I2CAdapter;
987class  C2IAdapter;
988class CompiledIC;
989class relocInfo;
990class ScopeDesc;
991class PcDesc;
992
993class Recompiler;
994class Recompilee;
995class RecompilationPolicy;
996class RFrame;
997class  CompiledRFrame;
998class  InterpretedRFrame;
999
1000class vframe;
1001class   javaVFrame;
1002class     interpretedVFrame;
1003class     compiledVFrame;
1004class     deoptimizedVFrame;
1005class   externalVFrame;
1006class     entryVFrame;
1007
1008class RegisterMap;
1009
1010class Mutex;
1011class Monitor;
1012class BasicLock;
1013class BasicObjectLock;
1014
1015class PeriodicTask;
1016
1017class JavaCallWrapper;
1018
1019class   oopDesc;
1020class   metaDataOopDesc;
1021
1022class NativeCall;
1023
1024class zone;
1025
1026class StubQueue;
1027
1028class outputStream;
1029
1030class ResourceArea;
1031
1032class DebugInformationRecorder;
1033class ScopeValue;
1034class CompressedStream;
1035class   DebugInfoReadStream;
1036class   DebugInfoWriteStream;
1037class LocationValue;
1038class ConstantValue;
1039class IllegalValue;
1040
1041class PrivilegedElement;
1042class MonitorArray;
1043
1044class MonitorInfo;
1045
1046class OffsetClosure;
1047class OopMapCache;
1048class InterpreterOopMap;
1049class OopMapCacheEntry;
1050class OSThread;
1051
1052typedef int (*OSThreadStartFunc)(void*);
1053
1054class Space;
1055
1056class JavaValue;
1057class methodHandle;
1058class JavaCallArguments;
1059
1060// Basic support for errors.
1061extern void basic_fatal(const char* msg);
1062
1063//----------------------------------------------------------------------------------------------------
1064// Special constants for debugging
1065
1066const jint     badInt           = -3;                       // generic "bad int" value
1067const long     badAddressVal    = -2;                       // generic "bad address" value
1068const long     badOopVal        = -1;                       // generic "bad oop" value
1069const intptr_t badHeapOopVal    = (intptr_t) CONST64(0x2BAD4B0BBAADBABE); // value used to zap heap after GC
1070const int      badHandleValue   = 0xBC;                     // value used to zap vm handle area
1071const int      badResourceValue = 0xAB;                     // value used to zap resource area
1072const int      freeBlockPad     = 0xBA;                     // value used to pad freed blocks.
1073const int      uninitBlockPad   = 0xF1;                     // value used to zap newly malloc'd blocks.
1074const juint    uninitMetaWordVal= 0xf7f7f7f7;               // value used to zap newly allocated metachunk
1075const intptr_t badJNIHandleVal  = (intptr_t) UCONST64(0xFEFEFEFEFEFEFEFE); // value used to zap jni handle area
1076const juint    badHeapWordVal   = 0xBAADBABE;               // value used to zap heap after GC
1077const juint    badMetaWordVal   = 0xBAADFADE;               // value used to zap metadata heap after GC
1078const int      badCodeHeapNewVal= 0xCC;                     // value used to zap Code heap at allocation
1079const int      badCodeHeapFreeVal = 0xDD;                   // value used to zap Code heap at deallocation
1080
1081
1082// (These must be implemented as #defines because C++ compilers are
1083// not obligated to inline non-integral constants!)
1084#define       badAddress        ((address)::badAddressVal)
1085#define       badOop            (cast_to_oop(::badOopVal))
1086#define       badHeapWord       (::badHeapWordVal)
1087#define       badJNIHandle      (cast_to_oop(::badJNIHandleVal))
1088
1089// Default TaskQueue size is 16K (32-bit) or 128K (64-bit)
1090#define TASKQUEUE_SIZE (NOT_LP64(1<<14) LP64_ONLY(1<<17))
1091
1092//----------------------------------------------------------------------------------------------------
1093// Utility functions for bitfield manipulations
1094
1095const intptr_t AllBits    = ~0; // all bits set in a word
1096const intptr_t NoBits     =  0; // no bits set in a word
1097const jlong    NoLongBits =  0; // no bits set in a long
1098const intptr_t OneBit     =  1; // only right_most bit set in a word
1099
1100// get a word with the n.th or the right-most or left-most n bits set
1101// (note: #define used only so that they can be used in enum constant definitions)
1102#define nth_bit(n)        (((n) >= BitsPerWord) ? 0 : (OneBit << (n)))
1103#define right_n_bits(n)   (nth_bit(n) - 1)
1104#define left_n_bits(n)    (right_n_bits(n) << (((n) >= BitsPerWord) ? 0 : (BitsPerWord - (n))))
1105
1106// bit-operations using a mask m
1107inline void   set_bits    (intptr_t& x, intptr_t m) { x |= m; }
1108inline void clear_bits    (intptr_t& x, intptr_t m) { x &= ~m; }
1109inline intptr_t mask_bits      (intptr_t  x, intptr_t m) { return x & m; }
1110inline jlong    mask_long_bits (jlong     x, jlong    m) { return x & m; }
1111inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; }
1112
1113// bit-operations using the n.th bit
1114inline void    set_nth_bit(intptr_t& x, int n) { set_bits  (x, nth_bit(n)); }
1115inline void  clear_nth_bit(intptr_t& x, int n) { clear_bits(x, nth_bit(n)); }
1116inline bool is_set_nth_bit(intptr_t  x, int n) { return mask_bits (x, nth_bit(n)) != NoBits; }
1117
1118// returns the bitfield of x starting at start_bit_no with length field_length (no sign-extension!)
1119inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) {
1120  return mask_bits(x >> start_bit_no, right_n_bits(field_length));
1121}
1122
1123
1124//----------------------------------------------------------------------------------------------------
1125// Utility functions for integers
1126
1127// Avoid use of global min/max macros which may cause unwanted double
1128// evaluation of arguments.
1129#ifdef max
1130#undef max
1131#endif
1132
1133#ifdef min
1134#undef min
1135#endif
1136
1137// The following defines serve the purpose of preventing use of accidentally
1138// included min max macros from compiling, while continuing to allow innocent
1139// min and max identifiers in the code to compile as intended.
1140#define max max
1141#define min min
1142
1143// It is necessary to use templates here. Having normal overloaded
1144// functions does not work because it is necessary to provide both 32-
1145// and 64-bit overloaded functions, which does not work, and having
1146// explicitly-typed versions of these routines (i.e., MAX2I, MAX2L)
1147// will be even more error-prone than macros.
1148template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
1149template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
1150template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
1151template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
1152template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
1153template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
1154
1155template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
1156
1157// true if x is a power of 2, false otherwise
1158inline bool is_power_of_2(intptr_t x) {
1159  return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
1160}
1161
1162// long version of is_power_of_2
1163inline bool is_power_of_2_long(jlong x) {
1164  return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
1165}
1166
1167// Returns largest i such that 2^i <= x.
1168// If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine.
1169// If x == 0, the function returns -1.
1170inline int log2_intptr(intptr_t x) {
1171  int i = -1;
1172  uintptr_t p = 1;
1173  while (p != 0 && p <= (uintptr_t)x) {
1174    // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
1175    i++; p *= 2;
1176  }
1177  // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
1178  // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size).
1179  return i;
1180}
1181
1182//* largest i such that 2^i <= x
1183//  A negative value of 'x' will return '63'
1184inline int log2_long(jlong x) {
1185  int i = -1;
1186  julong p =  1;
1187  while (p != 0 && p <= (julong)x) {
1188    // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
1189    i++; p *= 2;
1190  }
1191  // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
1192  // (if p = 0 then overflow occurred and i = 63)
1193  return i;
1194}
1195
1196//* the argument must be exactly a power of 2
1197inline int exact_log2(intptr_t x) {
1198  assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x);
1199  return log2_intptr(x);
1200}
1201
1202//* the argument must be exactly a power of 2
1203inline int exact_log2_long(jlong x) {
1204  assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x);
1205  return log2_long(x);
1206}
1207
1208
1209// returns integer round-up to the nearest multiple of s (s must be a power of two)
1210inline intptr_t round_to(intptr_t x, uintx s) {
1211  assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s);
1212  const uintx m = s - 1;
1213  return mask_bits(x + m, ~m);
1214}
1215
1216// returns integer round-down to the nearest multiple of s (s must be a power of two)
1217inline intptr_t round_down(intptr_t x, uintx s) {
1218  assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s);
1219  const uintx m = s - 1;
1220  return mask_bits(x, ~m);
1221}
1222
1223
1224inline bool is_odd (intx x) { return x & 1;      }
1225inline bool is_even(intx x) { return !is_odd(x); }
1226
1227// "to" should be greater than "from."
1228inline intx byte_size(void* from, void* to) {
1229  return (address)to - (address)from;
1230}
1231
1232//----------------------------------------------------------------------------------------------------
1233// Avoid non-portable casts with these routines (DEPRECATED)
1234
1235// NOTE: USE Bytes class INSTEAD WHERE POSSIBLE
1236//       Bytes is optimized machine-specifically and may be much faster then the portable routines below.
1237
1238// Given sequence of four bytes, build into a 32-bit word
1239// following the conventions used in class files.
1240// On the 386, this could be realized with a simple address cast.
1241//
1242
1243// This routine takes eight bytes:
1244inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1245  return  (( u8(c1) << 56 )  &  ( u8(0xff) << 56 ))
1246       |  (( u8(c2) << 48 )  &  ( u8(0xff) << 48 ))
1247       |  (( u8(c3) << 40 )  &  ( u8(0xff) << 40 ))
1248       |  (( u8(c4) << 32 )  &  ( u8(0xff) << 32 ))
1249       |  (( u8(c5) << 24 )  &  ( u8(0xff) << 24 ))
1250       |  (( u8(c6) << 16 )  &  ( u8(0xff) << 16 ))
1251       |  (( u8(c7) <<  8 )  &  ( u8(0xff) <<  8 ))
1252       |  (( u8(c8) <<  0 )  &  ( u8(0xff) <<  0 ));
1253}
1254
1255// This routine takes four bytes:
1256inline u4 build_u4_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
1257  return  (( u4(c1) << 24 )  &  0xff000000)
1258       |  (( u4(c2) << 16 )  &  0x00ff0000)
1259       |  (( u4(c3) <<  8 )  &  0x0000ff00)
1260       |  (( u4(c4) <<  0 )  &  0x000000ff);
1261}
1262
1263// And this one works if the four bytes are contiguous in memory:
1264inline u4 build_u4_from( u1* p ) {
1265  return  build_u4_from( p[0], p[1], p[2], p[3] );
1266}
1267
1268// Ditto for two-byte ints:
1269inline u2 build_u2_from( u1 c1, u1 c2 ) {
1270  return  u2((( u2(c1) <<  8 )  &  0xff00)
1271          |  (( u2(c2) <<  0 )  &  0x00ff));
1272}
1273
1274// And this one works if the two bytes are contiguous in memory:
1275inline u2 build_u2_from( u1* p ) {
1276  return  build_u2_from( p[0], p[1] );
1277}
1278
1279// Ditto for floats:
1280inline jfloat build_float_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
1281  u4 u = build_u4_from( c1, c2, c3, c4 );
1282  return  *(jfloat*)&u;
1283}
1284
1285inline jfloat build_float_from( u1* p ) {
1286  u4 u = build_u4_from( p );
1287  return  *(jfloat*)&u;
1288}
1289
1290
1291// now (64-bit) longs
1292
1293inline jlong build_long_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1294  return  (( jlong(c1) << 56 )  &  ( jlong(0xff) << 56 ))
1295       |  (( jlong(c2) << 48 )  &  ( jlong(0xff) << 48 ))
1296       |  (( jlong(c3) << 40 )  &  ( jlong(0xff) << 40 ))
1297       |  (( jlong(c4) << 32 )  &  ( jlong(0xff) << 32 ))
1298       |  (( jlong(c5) << 24 )  &  ( jlong(0xff) << 24 ))
1299       |  (( jlong(c6) << 16 )  &  ( jlong(0xff) << 16 ))
1300       |  (( jlong(c7) <<  8 )  &  ( jlong(0xff) <<  8 ))
1301       |  (( jlong(c8) <<  0 )  &  ( jlong(0xff) <<  0 ));
1302}
1303
1304inline jlong build_long_from( u1* p ) {
1305  return  build_long_from( p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7] );
1306}
1307
1308
1309// Doubles, too!
1310inline jdouble build_double_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1311  jlong u = build_long_from( c1, c2, c3, c4, c5, c6, c7, c8 );
1312  return  *(jdouble*)&u;
1313}
1314
1315inline jdouble build_double_from( u1* p ) {
1316  jlong u = build_long_from( p );
1317  return  *(jdouble*)&u;
1318}
1319
1320
1321// Portable routines to go the other way:
1322
1323inline void explode_short_to( u2 x, u1& c1, u1& c2 ) {
1324  c1 = u1(x >> 8);
1325  c2 = u1(x);
1326}
1327
1328inline void explode_short_to( u2 x, u1* p ) {
1329  explode_short_to( x, p[0], p[1]);
1330}
1331
1332inline void explode_int_to( u4 x, u1& c1, u1& c2, u1& c3, u1& c4 ) {
1333  c1 = u1(x >> 24);
1334  c2 = u1(x >> 16);
1335  c3 = u1(x >>  8);
1336  c4 = u1(x);
1337}
1338
1339inline void explode_int_to( u4 x, u1* p ) {
1340  explode_int_to( x, p[0], p[1], p[2], p[3]);
1341}
1342
1343
1344// Pack and extract shorts to/from ints:
1345
1346inline int extract_low_short_from_int(jint x) {
1347  return x & 0xffff;
1348}
1349
1350inline int extract_high_short_from_int(jint x) {
1351  return (x >> 16) & 0xffff;
1352}
1353
1354inline int build_int_from_shorts( jushort low, jushort high ) {
1355  return ((int)((unsigned int)high << 16) | (unsigned int)low);
1356}
1357
1358// Convert pointer to intptr_t, for use in printing pointers.
1359inline intptr_t p2i(const void * p) {
1360  return (intptr_t) p;
1361}
1362
1363// swap a & b
1364template<class T> static void swap(T& a, T& b) {
1365  T tmp = a;
1366  a = b;
1367  b = tmp;
1368}
1369
1370#define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
1371
1372//----------------------------------------------------------------------------------------------------
1373// Sum and product which can never overflow: they wrap, just like the
1374// Java operations.  Note that we don't intend these to be used for
1375// general-purpose arithmetic: their purpose is to emulate Java
1376// operations.
1377
1378// The goal of this code to avoid undefined or implementation-defined
1379// behavior.  The use of an lvalue to reference cast is explicitly
1380// permitted by Lvalues and rvalues [basic.lval].  [Section 3.10 Para
1381// 15 in C++03]
1382#define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE)  \
1383inline TYPE NAME (TYPE in1, TYPE in2) {                 \
1384  UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \
1385  ures OP ## = static_cast<UNSIGNED_TYPE>(in2);         \
1386  return reinterpret_cast<TYPE&>(ures);                 \
1387}
1388
1389JAVA_INTEGER_OP(+, java_add, jint, juint)
1390JAVA_INTEGER_OP(-, java_subtract, jint, juint)
1391JAVA_INTEGER_OP(*, java_multiply, jint, juint)
1392JAVA_INTEGER_OP(+, java_add, jlong, julong)
1393JAVA_INTEGER_OP(-, java_subtract, jlong, julong)
1394JAVA_INTEGER_OP(*, java_multiply, jlong, julong)
1395
1396#undef JAVA_INTEGER_OP
1397
1398// Dereference vptr
1399// All C++ compilers that we know of have the vtbl pointer in the first
1400// word.  If there are exceptions, this function needs to be made compiler
1401// specific.
1402static inline void* dereference_vptr(const void* addr) {
1403  return *(void**)addr;
1404}
1405
1406#endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
1407