1 /*
   2  * Copyright (c) 1997, 2017, 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  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
  26 #define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
  27 
  28 #include "utilities/compilerWarnings.hpp"
  29 #include "utilities/debug.hpp"
  30 #include "utilities/macros.hpp"
  31 
  32 #include COMPILER_HEADER(utilities/globalDefinitions)
  33 
  34 // Defaults for macros that might be defined per compiler.
  35 #ifndef NOINLINE
  36 #define NOINLINE
  37 #endif
  38 #ifndef ALWAYSINLINE
  39 #define ALWAYSINLINE inline
  40 #endif
  41 
  42 // This file holds all globally used constants & types, class (forward)
  43 // declarations and a few frequently used utility functions.
  44 
  45 //----------------------------------------------------------------------------------------------------
  46 // Printf-style formatters for fixed- and variable-width types as pointers and
  47 // integers.  These are derived from the definitions in inttypes.h.  If the platform
  48 // doesn't provide appropriate definitions, they should be provided in
  49 // the compiler-specific definitions file (e.g., globalDefinitions_gcc.hpp)
  50 
  51 #define BOOL_TO_STR(_b_) ((_b_) ? "true" : "false")
  52 
  53 // Format 32-bit quantities.
  54 #define INT32_FORMAT           "%" PRId32
  55 #define UINT32_FORMAT          "%" PRIu32
  56 #define INT32_FORMAT_W(width)  "%" #width PRId32
  57 #define UINT32_FORMAT_W(width) "%" #width PRIu32
  58 
  59 #define PTR32_FORMAT           "0x%08" PRIx32
  60 #define PTR32_FORMAT_W(width)  "0x%" #width PRIx32
  61 
  62 // Format 64-bit quantities.
  63 #define INT64_FORMAT           "%" PRId64
  64 #define UINT64_FORMAT          "%" PRIu64
  65 #define UINT64_FORMAT_X        "%" PRIx64
  66 #define INT64_FORMAT_W(width)  "%" #width PRId64
  67 #define UINT64_FORMAT_W(width) "%" #width PRIu64
  68 
  69 #define PTR64_FORMAT           "0x%016" PRIx64
  70 
  71 // Format jlong, if necessary
  72 #ifndef JLONG_FORMAT
  73 #define JLONG_FORMAT           INT64_FORMAT
  74 #endif
  75 #ifndef JULONG_FORMAT
  76 #define JULONG_FORMAT          UINT64_FORMAT
  77 #endif
  78 #ifndef JULONG_FORMAT_X
  79 #define JULONG_FORMAT_X        UINT64_FORMAT_X
  80 #endif
  81 
  82 // Format pointers which change size between 32- and 64-bit.
  83 #ifdef  _LP64
  84 #define INTPTR_FORMAT "0x%016" PRIxPTR
  85 #define PTR_FORMAT    "0x%016" PRIxPTR
  86 #else   // !_LP64
  87 #define INTPTR_FORMAT "0x%08"  PRIxPTR
  88 #define PTR_FORMAT    "0x%08"  PRIxPTR
  89 #endif  // _LP64
  90 
  91 #define INTPTR_FORMAT_W(width)   "%" #width PRIxPTR
  92 
  93 #define SSIZE_FORMAT             "%"   PRIdPTR
  94 #define SIZE_FORMAT              "%"   PRIuPTR
  95 #define SIZE_FORMAT_HEX          "0x%" PRIxPTR
  96 #define SSIZE_FORMAT_W(width)    "%"   #width PRIdPTR
  97 #define SIZE_FORMAT_W(width)     "%"   #width PRIuPTR
  98 #define SIZE_FORMAT_HEX_W(width) "0x%" #width PRIxPTR
  99 
 100 #define INTX_FORMAT           "%" PRIdPTR
 101 #define UINTX_FORMAT          "%" PRIuPTR
 102 #define INTX_FORMAT_W(width)  "%" #width PRIdPTR
 103 #define UINTX_FORMAT_W(width) "%" #width PRIuPTR
 104 
 105 //----------------------------------------------------------------------------------------------------
 106 // Constants
 107 
 108 const int LogBytesPerShort   = 1;
 109 const int LogBytesPerInt     = 2;
 110 #ifdef _LP64
 111 const int LogBytesPerWord    = 3;
 112 #else
 113 const int LogBytesPerWord    = 2;
 114 #endif
 115 const int LogBytesPerLong    = 3;
 116 
 117 const int BytesPerShort      = 1 << LogBytesPerShort;
 118 const int BytesPerInt        = 1 << LogBytesPerInt;
 119 const int BytesPerWord       = 1 << LogBytesPerWord;
 120 const int BytesPerLong       = 1 << LogBytesPerLong;
 121 
 122 const int LogBitsPerByte     = 3;
 123 const int LogBitsPerShort    = LogBitsPerByte + LogBytesPerShort;
 124 const int LogBitsPerInt      = LogBitsPerByte + LogBytesPerInt;
 125 const int LogBitsPerWord     = LogBitsPerByte + LogBytesPerWord;
 126 const int LogBitsPerLong     = LogBitsPerByte + LogBytesPerLong;
 127 
 128 const int BitsPerByte        = 1 << LogBitsPerByte;
 129 const int BitsPerShort       = 1 << LogBitsPerShort;
 130 const int BitsPerInt         = 1 << LogBitsPerInt;
 131 const int BitsPerWord        = 1 << LogBitsPerWord;
 132 const int BitsPerLong        = 1 << LogBitsPerLong;
 133 
 134 const int WordAlignmentMask  = (1 << LogBytesPerWord) - 1;
 135 const int LongAlignmentMask  = (1 << LogBytesPerLong) - 1;
 136 
 137 const int WordsPerLong       = 2;       // Number of stack entries for longs
 138 
 139 const int oopSize            = sizeof(char*); // Full-width oop
 140 extern int heapOopSize;                       // Oop within a java object
 141 const int wordSize           = sizeof(char*);
 142 const int longSize           = sizeof(jlong);
 143 const int jintSize           = sizeof(jint);
 144 const int size_tSize         = sizeof(size_t);
 145 
 146 const int BytesPerOop        = BytesPerWord;  // Full-width oop
 147 
 148 extern int LogBytesPerHeapOop;                // Oop within a java object
 149 extern int LogBitsPerHeapOop;
 150 extern int BytesPerHeapOop;
 151 extern int BitsPerHeapOop;
 152 
 153 const int BitsPerJavaInteger = 32;
 154 const int BitsPerJavaLong    = 64;
 155 const int BitsPerSize_t      = size_tSize * BitsPerByte;
 156 
 157 // Size of a char[] needed to represent a jint as a string in decimal.
 158 const int jintAsStringSize = 12;
 159 
 160 // In fact this should be
 161 // log2_intptr(sizeof(class JavaThread)) - log2_intptr(64);
 162 // see os::set_memory_serialize_page()
 163 #ifdef _LP64
 164 const int SerializePageShiftCount = 4;
 165 #else
 166 const int SerializePageShiftCount = 3;
 167 #endif
 168 
 169 // An opaque struct of heap-word width, so that HeapWord* can be a generic
 170 // pointer into the heap.  We require that object sizes be measured in
 171 // units of heap words, so that that
 172 //   HeapWord* hw;
 173 //   hw += oop(hw)->foo();
 174 // works, where foo is a method (like size or scavenge) that returns the
 175 // object size.
 176 class HeapWord {
 177   friend class VMStructs;
 178  private:
 179   char* i;
 180 #ifndef PRODUCT
 181  public:
 182   char* value() { return i; }
 183 #endif
 184 };
 185 
 186 // Analogous opaque struct for metadata allocated from
 187 // metaspaces.
 188 class MetaWord {
 189  private:
 190   char* i;
 191 };
 192 
 193 // HeapWordSize must be 2^LogHeapWordSize.
 194 const int HeapWordSize        = sizeof(HeapWord);
 195 #ifdef _LP64
 196 const int LogHeapWordSize     = 3;
 197 #else
 198 const int LogHeapWordSize     = 2;
 199 #endif
 200 const int HeapWordsPerLong    = BytesPerLong / HeapWordSize;
 201 const int LogHeapWordsPerLong = LogBytesPerLong - LogHeapWordSize;
 202 
 203 // The larger HeapWordSize for 64bit requires larger heaps
 204 // for the same application running in 64bit.  See bug 4967770.
 205 // The minimum alignment to a heap word size is done.  Other
 206 // parts of the memory system may require additional alignment
 207 // and are responsible for those alignments.
 208 #ifdef _LP64
 209 #define ScaleForWordSize(x) align_size_down_((x) * 13 / 10, HeapWordSize)
 210 #else
 211 #define ScaleForWordSize(x) (x)
 212 #endif
 213 
 214 // The minimum number of native machine words necessary to contain "byte_size"
 215 // bytes.
 216 inline size_t heap_word_size(size_t byte_size) {
 217   return (byte_size + (HeapWordSize-1)) >> LogHeapWordSize;
 218 }
 219 
 220 //-------------------------------------------
 221 // Constant for jlong (standardized by C++11)
 222 
 223 // Build a 64bit integer constant
 224 #define CONST64(x)  (x ## LL)
 225 #define UCONST64(x) (x ## ULL)
 226 
 227 const jlong min_jlong = CONST64(0x8000000000000000);
 228 const jlong max_jlong = CONST64(0x7fffffffffffffff);
 229 
 230 const size_t K                  = 1024;
 231 const size_t M                  = K*K;
 232 const size_t G                  = M*K;
 233 const size_t HWperKB            = K / sizeof(HeapWord);
 234 
 235 // Constants for converting from a base unit to milli-base units.  For
 236 // example from seconds to milliseconds and microseconds
 237 
 238 const int MILLIUNITS    = 1000;         // milli units per base unit
 239 const int MICROUNITS    = 1000000;      // micro units per base unit
 240 const int NANOUNITS     = 1000000000;   // nano units per base unit
 241 
 242 const jlong NANOSECS_PER_SEC      = CONST64(1000000000);
 243 const jint  NANOSECS_PER_MILLISEC = 1000000;
 244 
 245 inline const char* proper_unit_for_byte_size(size_t s) {
 246 #ifdef _LP64
 247   if (s >= 10*G) {
 248     return "G";
 249   }
 250 #endif
 251   if (s >= 10*M) {
 252     return "M";
 253   } else if (s >= 10*K) {
 254     return "K";
 255   } else {
 256     return "B";
 257   }
 258 }
 259 
 260 template <class T>
 261 inline T byte_size_in_proper_unit(T s) {
 262 #ifdef _LP64
 263   if (s >= 10*G) {
 264     return (T)(s/G);
 265   }
 266 #endif
 267   if (s >= 10*M) {
 268     return (T)(s/M);
 269   } else if (s >= 10*K) {
 270     return (T)(s/K);
 271   } else {
 272     return s;
 273   }
 274 }
 275 
 276 inline const char* exact_unit_for_byte_size(size_t s) {
 277 #ifdef _LP64
 278   if (s >= G && (s % G) == 0) {
 279     return "G";
 280   }
 281 #endif
 282   if (s >= M && (s % M) == 0) {
 283     return "M";
 284   }
 285   if (s >= K && (s % K) == 0) {
 286     return "K";
 287   }
 288   return "B";
 289 }
 290 
 291 inline size_t byte_size_in_exact_unit(size_t s) {
 292 #ifdef _LP64
 293   if (s >= G && (s % G) == 0) {
 294     return s / G;
 295   }
 296 #endif
 297   if (s >= M && (s % M) == 0) {
 298     return s / M;
 299   }
 300   if (s >= K && (s % K) == 0) {
 301     return s / K;
 302   }
 303   return s;
 304 }
 305 
 306 //----------------------------------------------------------------------------------------------------
 307 // VM type definitions
 308 
 309 // intx and uintx are the 'extended' int and 'extended' unsigned int types;
 310 // they are 32bit wide on a 32-bit platform, and 64bit wide on a 64bit platform.
 311 
 312 typedef intptr_t  intx;
 313 typedef uintptr_t uintx;
 314 
 315 const intx  min_intx  = (intx)1 << (sizeof(intx)*BitsPerByte-1);
 316 const intx  max_intx  = (uintx)min_intx - 1;
 317 const uintx max_uintx = (uintx)-1;
 318 
 319 // Table of values:
 320 //      sizeof intx         4               8
 321 // min_intx             0x80000000      0x8000000000000000
 322 // max_intx             0x7FFFFFFF      0x7FFFFFFFFFFFFFFF
 323 // max_uintx            0xFFFFFFFF      0xFFFFFFFFFFFFFFFF
 324 
 325 typedef unsigned int uint;   NEEDS_CLEANUP
 326 
 327 
 328 //----------------------------------------------------------------------------------------------------
 329 // Java type definitions
 330 
 331 // All kinds of 'plain' byte addresses
 332 typedef   signed char s_char;
 333 typedef unsigned char u_char;
 334 typedef u_char*       address;
 335 typedef uintptr_t     address_word; // unsigned integer which will hold a pointer
 336                                     // except for some implementations of a C++
 337                                     // linkage pointer to function. Should never
 338                                     // need one of those to be placed in this
 339                                     // type anyway.
 340 
 341 //  Utility functions to "portably" (?) bit twiddle pointers
 342 //  Where portable means keep ANSI C++ compilers quiet
 343 
 344 inline address       set_address_bits(address x, int m)       { return address(intptr_t(x) | m); }
 345 inline address       clear_address_bits(address x, int m)     { return address(intptr_t(x) & ~m); }
 346 
 347 //  Utility functions to "portably" make cast to/from function pointers.
 348 
 349 inline address_word  mask_address_bits(address x, int m)      { return address_word(x) & m; }
 350 inline address_word  castable_address(address x)              { return address_word(x) ; }
 351 inline address_word  castable_address(void* x)                { return address_word(x) ; }
 352 
 353 // Pointer subtraction.
 354 // The idea here is to avoid ptrdiff_t, which is signed and so doesn't have
 355 // the range we might need to find differences from one end of the heap
 356 // to the other.
 357 // A typical use might be:
 358 //     if (pointer_delta(end(), top()) >= size) {
 359 //       // enough room for an object of size
 360 //       ...
 361 // and then additions like
 362 //       ... top() + size ...
 363 // are safe because we know that top() is at least size below end().
 364 inline size_t pointer_delta(const volatile void* left,
 365                             const volatile void* right,
 366                             size_t element_size) {
 367   return (((uintptr_t) left) - ((uintptr_t) right)) / element_size;
 368 }
 369 
 370 // A version specialized for HeapWord*'s.
 371 inline size_t pointer_delta(const HeapWord* left, const HeapWord* right) {
 372   return pointer_delta(left, right, sizeof(HeapWord));
 373 }
 374 // A version specialized for MetaWord*'s.
 375 inline size_t pointer_delta(const MetaWord* left, const MetaWord* right) {
 376   return pointer_delta(left, right, sizeof(MetaWord));
 377 }
 378 
 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 
 400 typedef jubyte  u1;
 401 typedef jushort u2;
 402 typedef juint   u4;
 403 typedef julong  u8;
 404 
 405 const jubyte  max_jubyte  = (jubyte)-1;  // 0xFF       largest jubyte
 406 const jushort max_jushort = (jushort)-1; // 0xFFFF     largest jushort
 407 const juint   max_juint   = (juint)-1;   // 0xFFFFFFFF largest juint
 408 const julong  max_julong  = (julong)-1;  // 0xFF....FF largest julong
 409 
 410 typedef jbyte  s1;
 411 typedef jshort s2;
 412 typedef jint   s4;
 413 typedef jlong  s8;
 414 
 415 const jbyte min_jbyte = -(1 << 7);       // smallest jbyte
 416 const jbyte max_jbyte = (1 << 7) - 1;    // largest jbyte
 417 const jshort min_jshort = -(1 << 15);    // smallest jshort
 418 const jshort max_jshort = (1 << 15) - 1; // largest jshort
 419 
 420 const jint min_jint = (jint)1 << (sizeof(jint)*BitsPerByte-1); // 0x80000000 == smallest jint
 421 const jint max_jint = (juint)min_jint - 1;                     // 0x7FFFFFFF == largest jint
 422 
 423 //----------------------------------------------------------------------------------------------------
 424 // JVM spec restrictions
 425 
 426 const int max_method_code_size = 64*K - 1;  // JVM spec, 2nd ed. section 4.8.1 (p.134)
 427 
 428 // Default ProtectionDomainCacheSize values
 429 
 430 const int defaultProtectionDomainCacheSize = NOT_LP64(137) LP64_ONLY(2017);
 431 
 432 //----------------------------------------------------------------------------------------------------
 433 // Default and minimum StringTableSize values
 434 
 435 const int defaultStringTableSize = NOT_LP64(1009) LP64_ONLY(60013);
 436 const int minimumStringTableSize = 1009;
 437 
 438 const int defaultSymbolTableSize = 20011;
 439 const 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 
 455 extern int MinObjAlignment;
 456 extern int MinObjAlignmentInBytes;
 457 extern int MinObjAlignmentInBytesMask;
 458 
 459 extern int LogMinObjAlignment;
 460 extern int LogMinObjAlignmentInBytes;
 461 
 462 const int LogKlassAlignmentInBytes = 3;
 463 const int LogKlassAlignment        = LogKlassAlignmentInBytes - LogHeapWordSize;
 464 const int KlassAlignmentInBytes    = 1 << LogKlassAlignmentInBytes;
 465 const int KlassAlignment           = KlassAlignmentInBytes / HeapWordSize;
 466 
 467 // Maximal size of heap where unscaled compression can be used. Also upper bound
 468 // for heap placement: 4GB.
 469 const  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.
 472 extern 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.)
 477 const  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
 494 const bool support_IRIW_for_not_multiple_copy_atomic_cpu = true;
 495 #else
 496 const 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 
 525 template <typename T, typename A>
 526 inline T align_size_up(T size, A alignment) {
 527   return align_size_up_(size, alignment);
 528 }
 529 
 530 template <typename T, typename A>
 531 inline T align_size_down(T size, A alignment) {
 532   return align_size_down_(size, alignment);
 533 }
 534 
 535 template <typename T, typename A>
 536 inline 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'.
 541 template <typename T, typename A>
 542 inline 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 
 549 template <typename T, typename A>
 550 inline T* align_ptr_up(T* ptr, A alignment) {
 551   return (T*)align_size_up((uintptr_t)ptr, alignment);
 552 }
 553 
 554 template <typename T, typename A>
 555 inline T* align_ptr_down(T* ptr, A alignment) {
 556   return (T*)align_size_down((uintptr_t)ptr, alignment);
 557 }
 558 
 559 template <typename T, typename A>
 560 inline 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
 565 template <typename T>
 566 inline 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.
 571 template <typename T>
 572 inline T align_object_size(T word_size) {
 573   return align_size_up(word_size, MinObjAlignment);
 574 }
 575 
 576 inline bool is_object_aligned(size_t word_size) {
 577   return is_size_aligned(word_size, MinObjAlignment);
 578 }
 579 
 580 inline 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.
 585 template <typename T>
 586 inline 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
 594 template <typename T>
 595 inline 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.
 629 inline 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.
 635 template<typename T>
 636 inline 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
 643 typedef union {
 644   jfloat f;
 645   jint i;
 646 } FloatIntConv;
 647 
 648 typedef union {
 649   jdouble d;
 650   jlong l;
 651   julong ul;
 652 } DoubleLongConv;
 653 
 654 inline jint    jint_cast    (jfloat  x)  { return ((FloatIntConv*)&x)->i; }
 655 inline jfloat  jfloat_cast  (jint    x)  { return ((FloatIntConv*)&x)->f; }
 656 
 657 inline jlong   jlong_cast   (jdouble x)  { return ((DoubleLongConv*)&x)->l;  }
 658 inline julong  julong_cast  (jdouble x)  { return ((DoubleLongConv*)&x)->ul; }
 659 inline jdouble jdouble_cast (jlong   x)  { return ((DoubleLongConv*)&x)->d;  }
 660 
 661 inline jint low (jlong value)                    { return jint(value); }
 662 inline 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
 666 inline void set_low (jlong* value, jint low )    { *value &= (jlong)0xffffffff << 32;
 667                                                    *value |= (jlong)(julong)(juint)low; }
 668 
 669 inline void set_high(jlong* value, jint high)    { *value &= (jlong)(julong)(juint)0xffffffff;
 670                                                    *value |= (jlong)high       << 32; }
 671 
 672 inline 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 
 679 union jlong_accessor {
 680   jint  words[2];
 681   jlong long_value;
 682 };
 683 
 684 void basic_types_init(); // cannot define here; uses assert
 685 
 686 
 687 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
 688 enum 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 
 708 inline bool is_java_primitive(BasicType t) {
 709   return T_BOOLEAN <= t && t <= T_LONG;
 710 }
 711 
 712 inline 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 
 717 inline 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
 722 inline 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 
 739 extern char type2char_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 740 inline char type2char(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2char_tab[t] : 0; }
 741 extern int type2size[T_CONFLICT+1];         // Map BasicType to result stack elements
 742 extern const char* type2name_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 743 inline const char* type2name(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2name_tab[t] : NULL; }
 744 extern BasicType name2type(const char* name);
 745 
 746 // Auxiliary math routines
 747 // least common multiple
 748 extern 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
 752 enum 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
 771 extern BasicType type2field[T_CONFLICT+1];
 772 extern BasicType type2wfield[T_CONFLICT+1];
 773 
 774 
 775 // size in bytes
 776 enum 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 
 797 extern int _type2aelembytes[T_CONFLICT+1]; // maps a BasicType to nof bytes used by its array element
 798 #ifdef ASSERT
 799 extern int type2aelembytes(BasicType t, bool allow_address = false); // asserts
 800 #else
 801 inline 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 
 807 class 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 
 876 enum 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 
 892 inline 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 
 909 inline 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.
 928 TosState 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 //
 947 enum 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 
 969 class ClassFileStream;
 970 
 971 class Event;
 972 
 973 class Thread;
 974 class  VMThread;
 975 class  JavaThread;
 976 class Threads;
 977 
 978 class VM_Operation;
 979 class VMOperationQueue;
 980 
 981 class CodeBlob;
 982 class  CompiledMethod;
 983 class   nmethod;
 984 class RuntimeBlob;
 985 class  OSRAdapter;
 986 class  I2CAdapter;
 987 class  C2IAdapter;
 988 class CompiledIC;
 989 class relocInfo;
 990 class ScopeDesc;
 991 class PcDesc;
 992 
 993 class Recompiler;
 994 class Recompilee;
 995 class RecompilationPolicy;
 996 class RFrame;
 997 class  CompiledRFrame;
 998 class  InterpretedRFrame;
 999 
1000 class vframe;
1001 class   javaVFrame;
1002 class     interpretedVFrame;
1003 class     compiledVFrame;
1004 class     deoptimizedVFrame;
1005 class   externalVFrame;
1006 class     entryVFrame;
1007 
1008 class RegisterMap;
1009 
1010 class Mutex;
1011 class Monitor;
1012 class BasicLock;
1013 class BasicObjectLock;
1014 
1015 class PeriodicTask;
1016 
1017 class JavaCallWrapper;
1018 
1019 class   oopDesc;
1020 class   metaDataOopDesc;
1021 
1022 class NativeCall;
1023 
1024 class zone;
1025 
1026 class StubQueue;
1027 
1028 class outputStream;
1029 
1030 class ResourceArea;
1031 
1032 class DebugInformationRecorder;
1033 class ScopeValue;
1034 class CompressedStream;
1035 class   DebugInfoReadStream;
1036 class   DebugInfoWriteStream;
1037 class LocationValue;
1038 class ConstantValue;
1039 class IllegalValue;
1040 
1041 class PrivilegedElement;
1042 class MonitorArray;
1043 
1044 class MonitorInfo;
1045 
1046 class OffsetClosure;
1047 class OopMapCache;
1048 class InterpreterOopMap;
1049 class OopMapCacheEntry;
1050 class OSThread;
1051 
1052 typedef int (*OSThreadStartFunc)(void*);
1053 
1054 class Space;
1055 
1056 class JavaValue;
1057 class methodHandle;
1058 class JavaCallArguments;
1059 
1060 // Basic support for errors.
1061 extern void basic_fatal(const char* msg);
1062 
1063 //----------------------------------------------------------------------------------------------------
1064 // Special constants for debugging
1065 
1066 const jint     badInt           = -3;                       // generic "bad int" value
1067 const long     badAddressVal    = -2;                       // generic "bad address" value
1068 const long     badOopVal        = -1;                       // generic "bad oop" value
1069 const intptr_t badHeapOopVal    = (intptr_t) CONST64(0x2BAD4B0BBAADBABE); // value used to zap heap after GC
1070 const int      badHandleValue   = 0xBC;                     // value used to zap vm handle area
1071 const int      badResourceValue = 0xAB;                     // value used to zap resource area
1072 const int      freeBlockPad     = 0xBA;                     // value used to pad freed blocks.
1073 const int      uninitBlockPad   = 0xF1;                     // value used to zap newly malloc'd blocks.
1074 const juint    uninitMetaWordVal= 0xf7f7f7f7;               // value used to zap newly allocated metachunk
1075 const intptr_t badJNIHandleVal  = (intptr_t) UCONST64(0xFEFEFEFEFEFEFEFE); // value used to zap jni handle area
1076 const juint    badHeapWordVal   = 0xBAADBABE;               // value used to zap heap after GC
1077 const juint    badMetaWordVal   = 0xBAADFADE;               // value used to zap metadata heap after GC
1078 const int      badCodeHeapNewVal= 0xCC;                     // value used to zap Code heap at allocation
1079 const 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 
1095 const intptr_t AllBits    = ~0; // all bits set in a word
1096 const intptr_t NoBits     =  0; // no bits set in a word
1097 const jlong    NoLongBits =  0; // no bits set in a long
1098 const 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
1107 inline void   set_bits    (intptr_t& x, intptr_t m) { x |= m; }
1108 inline void clear_bits    (intptr_t& x, intptr_t m) { x &= ~m; }
1109 inline intptr_t mask_bits      (intptr_t  x, intptr_t m) { return x & m; }
1110 inline jlong    mask_long_bits (jlong     x, jlong    m) { return x & m; }
1111 inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; }
1112 
1113 // bit-operations using the n.th bit
1114 inline void    set_nth_bit(intptr_t& x, int n) { set_bits  (x, nth_bit(n)); }
1115 inline void  clear_nth_bit(intptr_t& x, int n) { clear_bits(x, nth_bit(n)); }
1116 inline 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!)
1119 inline 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.
1148 template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
1149 template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
1150 template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
1151 template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
1152 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
1153 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
1154 
1155 template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
1156 
1157 // true if x is a power of 2, false otherwise
1158 inline 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
1163 inline 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.
1170 inline 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'
1184 inline 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
1197 inline 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
1203 inline 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)
1210 inline 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)
1217 inline 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 
1224 inline bool is_odd (intx x) { return x & 1;      }
1225 inline bool is_even(intx x) { return !is_odd(x); }
1226 
1227 // "to" should be greater than "from."
1228 inline 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:
1244 inline 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:
1256 inline 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:
1264 inline 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:
1269 inline 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:
1275 inline u2 build_u2_from( u1* p ) {
1276   return  build_u2_from( p[0], p[1] );
1277 }
1278 
1279 // Ditto for floats:
1280 inline 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 
1285 inline 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 
1293 inline 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 
1304 inline 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!
1310 inline 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 
1315 inline 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 
1323 inline void explode_short_to( u2 x, u1& c1, u1& c2 ) {
1324   c1 = u1(x >> 8);
1325   c2 = u1(x);
1326 }
1327 
1328 inline void explode_short_to( u2 x, u1* p ) {
1329   explode_short_to( x, p[0], p[1]);
1330 }
1331 
1332 inline 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 
1339 inline 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 
1346 inline int extract_low_short_from_int(jint x) {
1347   return x & 0xffff;
1348 }
1349 
1350 inline int extract_high_short_from_int(jint x) {
1351   return (x >> 16) & 0xffff;
1352 }
1353 
1354 inline 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.
1359 inline intptr_t p2i(const void * p) {
1360   return (intptr_t) p;
1361 }
1362 
1363 // swap a & b
1364 template<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)  \
1383 inline 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 
1389 JAVA_INTEGER_OP(+, java_add, jint, juint)
1390 JAVA_INTEGER_OP(-, java_subtract, jint, juint)
1391 JAVA_INTEGER_OP(*, java_multiply, jint, juint)
1392 JAVA_INTEGER_OP(+, java_add, jlong, julong)
1393 JAVA_INTEGER_OP(-, java_subtract, jlong, julong)
1394 JAVA_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.
1402 static inline void* dereference_vptr(const void* addr) {
1403   return *(void**)addr;
1404 }
1405 
1406 #endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP