src/share/vm/memory/allocation.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6507
752ba2e5f6d0
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_ALLOCATION_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_ALLOCATION_HPP
aoqi@0 27
aoqi@0 28 #include "runtime/globals.hpp"
aoqi@0 29 #include "utilities/globalDefinitions.hpp"
aoqi@0 30 #include "utilities/macros.hpp"
aoqi@0 31 #ifdef COMPILER1
aoqi@0 32 #include "c1/c1_globals.hpp"
aoqi@0 33 #endif
aoqi@0 34 #ifdef COMPILER2
aoqi@0 35 #include "opto/c2_globals.hpp"
aoqi@0 36 #endif
aoqi@0 37
aoqi@0 38 #include <new>
aoqi@0 39
aoqi@0 40 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
aoqi@0 41 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
aoqi@0 42 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
aoqi@0 43
aoqi@0 44
aoqi@0 45 // noinline attribute
aoqi@0 46 #ifdef _WINDOWS
aoqi@0 47 #define _NOINLINE_ __declspec(noinline)
aoqi@0 48 #else
aoqi@0 49 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
aoqi@0 50 #define _NOINLINE_
aoqi@0 51 #else
aoqi@0 52 #define _NOINLINE_ __attribute__ ((noinline))
aoqi@0 53 #endif
aoqi@0 54 #endif
aoqi@0 55
aoqi@0 56 class AllocFailStrategy {
aoqi@0 57 public:
aoqi@0 58 enum AllocFailEnum { EXIT_OOM, RETURN_NULL };
aoqi@0 59 };
aoqi@0 60 typedef AllocFailStrategy::AllocFailEnum AllocFailType;
aoqi@0 61
aoqi@0 62 // All classes in the virtual machine must be subclassed
aoqi@0 63 // by one of the following allocation classes:
aoqi@0 64 //
aoqi@0 65 // For objects allocated in the resource area (see resourceArea.hpp).
aoqi@0 66 // - ResourceObj
aoqi@0 67 //
aoqi@0 68 // For objects allocated in the C-heap (managed by: free & malloc).
aoqi@0 69 // - CHeapObj
aoqi@0 70 //
aoqi@0 71 // For objects allocated on the stack.
aoqi@0 72 // - StackObj
aoqi@0 73 //
aoqi@0 74 // For embedded objects.
aoqi@0 75 // - ValueObj
aoqi@0 76 //
aoqi@0 77 // For classes used as name spaces.
aoqi@0 78 // - AllStatic
aoqi@0 79 //
aoqi@0 80 // For classes in Metaspace (class data)
aoqi@0 81 // - MetaspaceObj
aoqi@0 82 //
aoqi@0 83 // The printable subclasses are used for debugging and define virtual
aoqi@0 84 // member functions for printing. Classes that avoid allocating the
aoqi@0 85 // vtbl entries in the objects should therefore not be the printable
aoqi@0 86 // subclasses.
aoqi@0 87 //
aoqi@0 88 // The following macros and function should be used to allocate memory
aoqi@0 89 // directly in the resource area or in the C-heap, The _OBJ variants
aoqi@0 90 // of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple
aoqi@0 91 // objects which are not inherited from CHeapObj, note constructor and
aoqi@0 92 // destructor are not called. The preferable way to allocate objects
aoqi@0 93 // is using the new operator.
aoqi@0 94 //
aoqi@0 95 // WARNING: The array variant must only be used for a homogenous array
aoqi@0 96 // where all objects are of the exact type specified. If subtypes are
aoqi@0 97 // stored in the array then must pay attention to calling destructors
aoqi@0 98 // at needed.
aoqi@0 99 //
aoqi@0 100 // NEW_RESOURCE_ARRAY(type, size)
aoqi@0 101 // NEW_RESOURCE_OBJ(type)
aoqi@0 102 // NEW_C_HEAP_ARRAY(type, size)
aoqi@0 103 // NEW_C_HEAP_OBJ(type, memflags)
aoqi@0 104 // FREE_C_HEAP_ARRAY(type, old, memflags)
aoqi@0 105 // FREE_C_HEAP_OBJ(objname, type, memflags)
aoqi@0 106 // char* AllocateHeap(size_t size, const char* name);
aoqi@0 107 // void FreeHeap(void* p);
aoqi@0 108 //
aoqi@0 109 // C-heap allocation can be traced using +PrintHeapAllocation.
aoqi@0 110 // malloc and free should therefore never called directly.
aoqi@0 111
aoqi@0 112 // Base class for objects allocated in the C-heap.
aoqi@0 113
aoqi@0 114 // In non product mode we introduce a super class for all allocation classes
aoqi@0 115 // that supports printing.
aoqi@0 116 // We avoid the superclass in product mode since some C++ compilers add
aoqi@0 117 // a word overhead for empty super classes.
aoqi@0 118
aoqi@0 119 #ifdef PRODUCT
aoqi@0 120 #define ALLOCATION_SUPER_CLASS_SPEC
aoqi@0 121 #else
aoqi@0 122 #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
aoqi@0 123 class AllocatedObj {
aoqi@0 124 public:
aoqi@0 125 // Printing support
aoqi@0 126 void print() const;
aoqi@0 127 void print_value() const;
aoqi@0 128
aoqi@0 129 virtual void print_on(outputStream* st) const;
aoqi@0 130 virtual void print_value_on(outputStream* st) const;
aoqi@0 131 };
aoqi@0 132 #endif
aoqi@0 133
aoqi@0 134
aoqi@0 135 /*
aoqi@0 136 * MemoryType bitmap layout:
aoqi@0 137 * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 |
aoqi@0 138 * | memory type | object | reserved |
aoqi@0 139 * | | type | |
aoqi@0 140 */
aoqi@0 141 enum MemoryType {
aoqi@0 142 // Memory type by sub systems. It occupies lower byte.
aoqi@0 143 mtNone = 0x0000, // undefined
aoqi@0 144 mtClass = 0x0100, // memory class for Java classes
aoqi@0 145 mtThread = 0x0200, // memory for thread objects
aoqi@0 146 mtThreadStack = 0x0300,
aoqi@0 147 mtCode = 0x0400, // memory for generated code
aoqi@0 148 mtGC = 0x0500, // memory for GC
aoqi@0 149 mtCompiler = 0x0600, // memory for compiler
aoqi@0 150 mtInternal = 0x0700, // memory used by VM, but does not belong to
aoqi@0 151 // any of above categories, and not used for
aoqi@0 152 // native memory tracking
aoqi@0 153 mtOther = 0x0800, // memory not used by VM
aoqi@0 154 mtSymbol = 0x0900, // symbol
aoqi@0 155 mtNMT = 0x0A00, // memory used by native memory tracking
aoqi@0 156 mtChunk = 0x0B00, // chunk that holds content of arenas
aoqi@0 157 mtJavaHeap = 0x0C00, // Java heap
aoqi@0 158 mtClassShared = 0x0D00, // class data sharing
aoqi@0 159 mtTest = 0x0E00, // Test type for verifying NMT
aoqi@0 160 mtTracing = 0x0F00, // memory used for Tracing
aoqi@0 161 mt_number_of_types = 0x000F, // number of memory types (mtDontTrack
aoqi@0 162 // is not included as validate type)
aoqi@0 163 mtDontTrack = 0x0F00, // memory we do not or cannot track
aoqi@0 164 mt_masks = 0x7F00,
aoqi@0 165
aoqi@0 166 // object type mask
aoqi@0 167 otArena = 0x0010, // an arena object
aoqi@0 168 otNMTRecorder = 0x0020, // memory recorder object
aoqi@0 169 ot_masks = 0x00F0
aoqi@0 170 };
aoqi@0 171
aoqi@0 172 #define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type)
aoqi@0 173 #define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone)
aoqi@0 174 #define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks)
aoqi@0 175
aoqi@0 176 #define IS_ARENA_OBJ(flags) ((flags & ot_masks) == otArena)
aoqi@0 177 #define IS_NMT_RECORDER(flags) ((flags & ot_masks) == otNMTRecorder)
aoqi@0 178 #define NMT_CAN_TRACK(flags) (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack)))
aoqi@0 179
aoqi@0 180 typedef unsigned short MEMFLAGS;
aoqi@0 181
aoqi@0 182 #if INCLUDE_NMT
aoqi@0 183
aoqi@0 184 extern bool NMT_track_callsite;
aoqi@0 185
aoqi@0 186 #else
aoqi@0 187
aoqi@0 188 const bool NMT_track_callsite = false;
aoqi@0 189
aoqi@0 190 #endif // INCLUDE_NMT
aoqi@0 191
aoqi@0 192 // debug build does not inline
aoqi@0 193 #if defined(_NMT_NOINLINE_)
aoqi@0 194 #define CURRENT_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0)
aoqi@0 195 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
aoqi@0 196 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0)
aoqi@0 197 #else
aoqi@0 198 #define CURRENT_PC (NMT_track_callsite? os::get_caller_pc(0) : 0)
aoqi@0 199 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0)
aoqi@0 200 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
aoqi@0 201 #endif
aoqi@0 202
aoqi@0 203
aoqi@0 204
aoqi@0 205 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
aoqi@0 206 public:
aoqi@0 207 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0) throw();
aoqi@0 208 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
aoqi@0 209 address caller_pc = 0) throw();
aoqi@0 210 _NOINLINE_ void* operator new [](size_t size, address caller_pc = 0) throw();
aoqi@0 211 _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
aoqi@0 212 address caller_pc = 0) throw();
aoqi@0 213 void operator delete(void* p);
aoqi@0 214 void operator delete [] (void* p);
aoqi@0 215 };
aoqi@0 216
aoqi@0 217 // Base class for objects allocated on the stack only.
aoqi@0 218 // Calling new or delete will result in fatal error.
aoqi@0 219
aoqi@0 220 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
aoqi@0 221 private:
aoqi@0 222 void* operator new(size_t size) throw();
aoqi@0 223 void* operator new [](size_t size) throw();
aoqi@0 224 #ifdef __IBMCPP__
aoqi@0 225 public:
aoqi@0 226 #endif
aoqi@0 227 void operator delete(void* p);
aoqi@0 228 void operator delete [](void* p);
aoqi@0 229 };
aoqi@0 230
aoqi@0 231 // Base class for objects used as value objects.
aoqi@0 232 // Calling new or delete will result in fatal error.
aoqi@0 233 //
aoqi@0 234 // Portability note: Certain compilers (e.g. gcc) will
aoqi@0 235 // always make classes bigger if it has a superclass, even
aoqi@0 236 // if the superclass does not have any virtual methods or
aoqi@0 237 // instance fields. The HotSpot implementation relies on this
aoqi@0 238 // not to happen. So never make a ValueObj class a direct subclass
aoqi@0 239 // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
aoqi@0 240 // like this:
aoqi@0 241 //
aoqi@0 242 // class A VALUE_OBJ_CLASS_SPEC {
aoqi@0 243 // ...
aoqi@0 244 // }
aoqi@0 245 //
aoqi@0 246 // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
aoqi@0 247 // be defined as a an empty string "".
aoqi@0 248 //
aoqi@0 249 class _ValueObj {
aoqi@0 250 private:
aoqi@0 251 void* operator new(size_t size) throw();
aoqi@0 252 void operator delete(void* p);
aoqi@0 253 void* operator new [](size_t size) throw();
aoqi@0 254 void operator delete [](void* p);
aoqi@0 255 };
aoqi@0 256
aoqi@0 257
aoqi@0 258 // Base class for objects stored in Metaspace.
aoqi@0 259 // Calling delete will result in fatal error.
aoqi@0 260 //
aoqi@0 261 // Do not inherit from something with a vptr because this class does
aoqi@0 262 // not introduce one. This class is used to allocate both shared read-only
aoqi@0 263 // and shared read-write classes.
aoqi@0 264 //
aoqi@0 265
aoqi@0 266 class ClassLoaderData;
aoqi@0 267
aoqi@0 268 class MetaspaceObj {
aoqi@0 269 public:
aoqi@0 270 bool is_metaspace_object() const;
aoqi@0 271 bool is_shared() const;
aoqi@0 272 void print_address_on(outputStream* st) const; // nonvirtual address printing
aoqi@0 273
aoqi@0 274 #define METASPACE_OBJ_TYPES_DO(f) \
aoqi@0 275 f(Unknown) \
aoqi@0 276 f(Class) \
aoqi@0 277 f(Symbol) \
aoqi@0 278 f(TypeArrayU1) \
aoqi@0 279 f(TypeArrayU2) \
aoqi@0 280 f(TypeArrayU4) \
aoqi@0 281 f(TypeArrayU8) \
aoqi@0 282 f(TypeArrayOther) \
aoqi@0 283 f(Method) \
aoqi@0 284 f(ConstMethod) \
aoqi@0 285 f(MethodData) \
aoqi@0 286 f(ConstantPool) \
aoqi@0 287 f(ConstantPoolCache) \
aoqi@0 288 f(Annotation) \
aoqi@0 289 f(MethodCounters)
aoqi@0 290
aoqi@0 291 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
aoqi@0 292 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
aoqi@0 293
aoqi@0 294 enum Type {
aoqi@0 295 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
aoqi@0 296 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
aoqi@0 297 _number_of_types
aoqi@0 298 };
aoqi@0 299
aoqi@0 300 static const char * type_name(Type type) {
aoqi@0 301 switch(type) {
aoqi@0 302 METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
aoqi@0 303 default:
aoqi@0 304 ShouldNotReachHere();
aoqi@0 305 return NULL;
aoqi@0 306 }
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 static MetaspaceObj::Type array_type(size_t elem_size) {
aoqi@0 310 switch (elem_size) {
aoqi@0 311 case 1: return TypeArrayU1Type;
aoqi@0 312 case 2: return TypeArrayU2Type;
aoqi@0 313 case 4: return TypeArrayU4Type;
aoqi@0 314 case 8: return TypeArrayU8Type;
aoqi@0 315 default:
aoqi@0 316 return TypeArrayOtherType;
aoqi@0 317 }
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 void* operator new(size_t size, ClassLoaderData* loader_data,
aoqi@0 321 size_t word_size, bool read_only,
aoqi@0 322 Type type, Thread* thread) throw();
aoqi@0 323 // can't use TRAPS from this header file.
aoqi@0 324 void operator delete(void* p) { ShouldNotCallThis(); }
aoqi@0 325 };
aoqi@0 326
aoqi@0 327 // Base class for classes that constitute name spaces.
aoqi@0 328
aoqi@0 329 class AllStatic {
aoqi@0 330 public:
aoqi@0 331 AllStatic() { ShouldNotCallThis(); }
aoqi@0 332 ~AllStatic() { ShouldNotCallThis(); }
aoqi@0 333 };
aoqi@0 334
aoqi@0 335
aoqi@0 336 //------------------------------Chunk------------------------------------------
aoqi@0 337 // Linked list of raw memory chunks
aoqi@0 338 class Chunk: CHeapObj<mtChunk> {
aoqi@0 339 friend class VMStructs;
aoqi@0 340
aoqi@0 341 protected:
aoqi@0 342 Chunk* _next; // Next Chunk in list
aoqi@0 343 const size_t _len; // Size of this Chunk
aoqi@0 344 public:
aoqi@0 345 void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw();
aoqi@0 346 void operator delete(void* p);
aoqi@0 347 Chunk(size_t length);
aoqi@0 348
aoqi@0 349 enum {
aoqi@0 350 // default sizes; make them slightly smaller than 2**k to guard against
aoqi@0 351 // buddy-system style malloc implementations
aoqi@0 352 #ifdef _LP64
aoqi@0 353 slack = 40, // [RGV] Not sure if this is right, but make it
aoqi@0 354 // a multiple of 8.
aoqi@0 355 #else
aoqi@0 356 slack = 20, // suspected sizeof(Chunk) + internal malloc headers
aoqi@0 357 #endif
aoqi@0 358
aoqi@0 359 tiny_size = 256 - slack, // Size of first chunk (tiny)
aoqi@0 360 init_size = 1*K - slack, // Size of first chunk (normal aka small)
aoqi@0 361 medium_size= 10*K - slack, // Size of medium-sized chunk
aoqi@0 362 size = 32*K - slack, // Default size of an Arena chunk (following the first)
aoqi@0 363 non_pool_size = init_size + 32 // An initial size which is not one of above
aoqi@0 364 };
aoqi@0 365
aoqi@0 366 void chop(); // Chop this chunk
aoqi@0 367 void next_chop(); // Chop next chunk
aoqi@0 368 static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
aoqi@0 369 static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); }
aoqi@0 370
aoqi@0 371 size_t length() const { return _len; }
aoqi@0 372 Chunk* next() const { return _next; }
aoqi@0 373 void set_next(Chunk* n) { _next = n; }
aoqi@0 374 // Boundaries of data area (possibly unused)
aoqi@0 375 char* bottom() const { return ((char*) this) + aligned_overhead_size(); }
aoqi@0 376 char* top() const { return bottom() + _len; }
aoqi@0 377 bool contains(char* p) const { return bottom() <= p && p <= top(); }
aoqi@0 378
aoqi@0 379 // Start the chunk_pool cleaner task
aoqi@0 380 static void start_chunk_pool_cleaner_task();
aoqi@0 381
aoqi@0 382 static void clean_chunk_pool();
aoqi@0 383 };
aoqi@0 384
aoqi@0 385 //------------------------------Arena------------------------------------------
aoqi@0 386 // Fast allocation of memory
aoqi@0 387 class Arena : public CHeapObj<mtNone|otArena> {
aoqi@0 388 protected:
aoqi@0 389 friend class ResourceMark;
aoqi@0 390 friend class HandleMark;
aoqi@0 391 friend class NoHandleMark;
aoqi@0 392 friend class VMStructs;
aoqi@0 393
aoqi@0 394 Chunk *_first; // First chunk
aoqi@0 395 Chunk *_chunk; // current chunk
aoqi@0 396 char *_hwm, *_max; // High water mark and max in current chunk
aoqi@0 397 // Get a new Chunk of at least size x
aoqi@0 398 void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
aoqi@0 399 size_t _size_in_bytes; // Size of arena (used for native memory tracking)
aoqi@0 400
aoqi@0 401 NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start
aoqi@0 402 friend class AllocStats;
aoqi@0 403 debug_only(void* malloc(size_t size);)
aoqi@0 404 debug_only(void* internal_malloc_4(size_t x);)
aoqi@0 405 NOT_PRODUCT(void inc_bytes_allocated(size_t x);)
aoqi@0 406
aoqi@0 407 void signal_out_of_memory(size_t request, const char* whence) const;
aoqi@0 408
aoqi@0 409 bool check_for_overflow(size_t request, const char* whence,
aoqi@0 410 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const {
aoqi@0 411 if (UINTPTR_MAX - request < (uintptr_t)_hwm) {
aoqi@0 412 if (alloc_failmode == AllocFailStrategy::RETURN_NULL) {
aoqi@0 413 return false;
aoqi@0 414 }
aoqi@0 415 signal_out_of_memory(request, whence);
aoqi@0 416 }
aoqi@0 417 return true;
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 public:
aoqi@0 421 Arena();
aoqi@0 422 Arena(size_t init_size);
aoqi@0 423 ~Arena();
aoqi@0 424 void destruct_contents();
aoqi@0 425 char* hwm() const { return _hwm; }
aoqi@0 426
aoqi@0 427 // new operators
aoqi@0 428 void* operator new (size_t size) throw();
aoqi@0 429 void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw();
aoqi@0 430
aoqi@0 431 // dynamic memory type tagging
aoqi@0 432 void* operator new(size_t size, MEMFLAGS flags) throw();
aoqi@0 433 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw();
aoqi@0 434 void operator delete(void* p);
aoqi@0 435
aoqi@0 436 // Fast allocate in the arena. Common case is: pointer test + increment.
aoqi@0 437 void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
aoqi@0 438 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
aoqi@0 439 x = ARENA_ALIGN(x);
aoqi@0 440 debug_only(if (UseMallocOnly) return malloc(x);)
aoqi@0 441 if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode))
aoqi@0 442 return NULL;
aoqi@0 443 NOT_PRODUCT(inc_bytes_allocated(x);)
aoqi@0 444 if (_hwm + x > _max) {
aoqi@0 445 return grow(x, alloc_failmode);
aoqi@0 446 } else {
aoqi@0 447 char *old = _hwm;
aoqi@0 448 _hwm += x;
aoqi@0 449 return old;
aoqi@0 450 }
aoqi@0 451 }
aoqi@0 452 // Further assume size is padded out to words
aoqi@0 453 void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
aoqi@0 454 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
aoqi@0 455 debug_only(if (UseMallocOnly) return malloc(x);)
aoqi@0 456 if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode))
aoqi@0 457 return NULL;
aoqi@0 458 NOT_PRODUCT(inc_bytes_allocated(x);)
aoqi@0 459 if (_hwm + x > _max) {
aoqi@0 460 return grow(x, alloc_failmode);
aoqi@0 461 } else {
aoqi@0 462 char *old = _hwm;
aoqi@0 463 _hwm += x;
aoqi@0 464 return old;
aoqi@0 465 }
aoqi@0 466 }
aoqi@0 467
aoqi@0 468 // Allocate with 'double' alignment. It is 8 bytes on sparc.
aoqi@0 469 // In other cases Amalloc_D() should be the same as Amalloc_4().
aoqi@0 470 void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
aoqi@0 471 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
aoqi@0 472 debug_only(if (UseMallocOnly) return malloc(x);)
aoqi@0 473 #if defined(SPARC) && !defined(_LP64)
aoqi@0 474 #define DALIGN_M1 7
aoqi@0 475 size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
aoqi@0 476 x += delta;
aoqi@0 477 #endif
aoqi@0 478 if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode))
aoqi@0 479 return NULL;
aoqi@0 480 NOT_PRODUCT(inc_bytes_allocated(x);)
aoqi@0 481 if (_hwm + x > _max) {
aoqi@0 482 return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes.
aoqi@0 483 } else {
aoqi@0 484 char *old = _hwm;
aoqi@0 485 _hwm += x;
aoqi@0 486 #if defined(SPARC) && !defined(_LP64)
aoqi@0 487 old += delta; // align to 8-bytes
aoqi@0 488 #endif
aoqi@0 489 return old;
aoqi@0 490 }
aoqi@0 491 }
aoqi@0 492
aoqi@0 493 // Fast delete in area. Common case is: NOP (except for storage reclaimed)
aoqi@0 494 void Afree(void *ptr, size_t size) {
aoqi@0 495 #ifdef ASSERT
aoqi@0 496 if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
aoqi@0 497 if (UseMallocOnly) return;
aoqi@0 498 #endif
aoqi@0 499 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size,
aoqi@0 503 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
aoqi@0 504
aoqi@0 505 // Move contents of this arena into an empty arena
aoqi@0 506 Arena *move_contents(Arena *empty_arena);
aoqi@0 507
aoqi@0 508 // Determine if pointer belongs to this Arena or not.
aoqi@0 509 bool contains( const void *ptr ) const;
aoqi@0 510
aoqi@0 511 // Total of all chunks in use (not thread-safe)
aoqi@0 512 size_t used() const;
aoqi@0 513
aoqi@0 514 // Total # of bytes used
aoqi@0 515 size_t size_in_bytes() const { return _size_in_bytes; };
aoqi@0 516 void set_size_in_bytes(size_t size);
aoqi@0 517
aoqi@0 518 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
aoqi@0 519 static void free_all(char** start, char** end) PRODUCT_RETURN;
aoqi@0 520
aoqi@0 521 // how many arena instances
aoqi@0 522 NOT_PRODUCT(static volatile jint _instance_count;)
aoqi@0 523 private:
aoqi@0 524 // Reset this Arena to empty, access will trigger grow if necessary
aoqi@0 525 void reset(void) {
aoqi@0 526 _first = _chunk = NULL;
aoqi@0 527 _hwm = _max = NULL;
aoqi@0 528 set_size_in_bytes(0);
aoqi@0 529 }
aoqi@0 530 };
aoqi@0 531
aoqi@0 532 // One of the following macros must be used when allocating
aoqi@0 533 // an array or object from an arena
aoqi@0 534 #define NEW_ARENA_ARRAY(arena, type, size) \
aoqi@0 535 (type*) (arena)->Amalloc((size) * sizeof(type))
aoqi@0 536
aoqi@0 537 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \
aoqi@0 538 (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \
aoqi@0 539 (new_size) * sizeof(type) )
aoqi@0 540
aoqi@0 541 #define FREE_ARENA_ARRAY(arena, type, old, size) \
aoqi@0 542 (arena)->Afree((char*)(old), (size) * sizeof(type))
aoqi@0 543
aoqi@0 544 #define NEW_ARENA_OBJ(arena, type) \
aoqi@0 545 NEW_ARENA_ARRAY(arena, type, 1)
aoqi@0 546
aoqi@0 547
aoqi@0 548 //%note allocation_1
aoqi@0 549 extern char* resource_allocate_bytes(size_t size,
aoqi@0 550 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
aoqi@0 551 extern char* resource_allocate_bytes(Thread* thread, size_t size,
aoqi@0 552 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
aoqi@0 553 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
aoqi@0 554 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
aoqi@0 555 extern void resource_free_bytes( char *old, size_t size );
aoqi@0 556
aoqi@0 557 //----------------------------------------------------------------------
aoqi@0 558 // Base class for objects allocated in the resource area per default.
aoqi@0 559 // Optionally, objects may be allocated on the C heap with
aoqi@0 560 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
aoqi@0 561 // ResourceObj's can be allocated within other objects, but don't use
aoqi@0 562 // new or delete (allocation_type is unknown). If new is used to allocate,
aoqi@0 563 // use delete to deallocate.
aoqi@0 564 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
aoqi@0 565 public:
aoqi@0 566 enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
aoqi@0 567 static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
aoqi@0 568 #ifdef ASSERT
aoqi@0 569 private:
aoqi@0 570 // When this object is allocated on stack the new() operator is not
aoqi@0 571 // called but garbage on stack may look like a valid allocation_type.
aoqi@0 572 // Store negated 'this' pointer when new() is called to distinguish cases.
aoqi@0 573 // Use second array's element for verification value to distinguish garbage.
aoqi@0 574 uintptr_t _allocation_t[2];
aoqi@0 575 bool is_type_set() const;
aoqi@0 576 public:
aoqi@0 577 allocation_type get_allocation_type() const;
aoqi@0 578 bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; }
aoqi@0 579 bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
aoqi@0 580 bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; }
aoqi@0 581 bool allocated_on_arena() const { return get_allocation_type() == ARENA; }
aoqi@0 582 ResourceObj(); // default construtor
aoqi@0 583 ResourceObj(const ResourceObj& r); // default copy construtor
aoqi@0 584 ResourceObj& operator=(const ResourceObj& r); // default copy assignment
aoqi@0 585 ~ResourceObj();
aoqi@0 586 #endif // ASSERT
aoqi@0 587
aoqi@0 588 public:
aoqi@0 589 void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw();
aoqi@0 590 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw();
aoqi@0 591 void* operator new(size_t size, const std::nothrow_t& nothrow_constant,
aoqi@0 592 allocation_type type, MEMFLAGS flags) throw();
aoqi@0 593 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
aoqi@0 594 allocation_type type, MEMFLAGS flags) throw();
aoqi@0 595
aoqi@0 596 void* operator new(size_t size, Arena *arena) throw() {
aoqi@0 597 address res = (address)arena->Amalloc(size);
aoqi@0 598 DEBUG_ONLY(set_allocation_type(res, ARENA);)
aoqi@0 599 return res;
aoqi@0 600 }
aoqi@0 601
aoqi@0 602 void* operator new [](size_t size, Arena *arena) throw() {
aoqi@0 603 address res = (address)arena->Amalloc(size);
aoqi@0 604 DEBUG_ONLY(set_allocation_type(res, ARENA);)
aoqi@0 605 return res;
aoqi@0 606 }
aoqi@0 607
aoqi@0 608 void* operator new(size_t size) throw() {
aoqi@0 609 address res = (address)resource_allocate_bytes(size);
aoqi@0 610 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
aoqi@0 611 return res;
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
aoqi@0 615 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
aoqi@0 616 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
aoqi@0 617 return res;
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 void* operator new [](size_t size) throw() {
aoqi@0 621 address res = (address)resource_allocate_bytes(size);
aoqi@0 622 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
aoqi@0 623 return res;
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() {
aoqi@0 627 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
aoqi@0 628 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
aoqi@0 629 return res;
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 void operator delete(void* p);
aoqi@0 633 void operator delete [](void* p);
aoqi@0 634 };
aoqi@0 635
aoqi@0 636 // One of the following macros must be used when allocating an array
aoqi@0 637 // or object to determine whether it should reside in the C heap on in
aoqi@0 638 // the resource area.
aoqi@0 639
aoqi@0 640 #define NEW_RESOURCE_ARRAY(type, size)\
aoqi@0 641 (type*) resource_allocate_bytes((size) * sizeof(type))
aoqi@0 642
aoqi@0 643 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
aoqi@0 644 (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
aoqi@0 645
aoqi@0 646 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
aoqi@0 647 (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
aoqi@0 648
aoqi@0 649 #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\
aoqi@0 650 (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
aoqi@0 651
aoqi@0 652 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
aoqi@0 653 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))
aoqi@0 654
aoqi@0 655 #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\
aoqi@0 656 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\
aoqi@0 657 (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
aoqi@0 658
aoqi@0 659 #define FREE_RESOURCE_ARRAY(type, old, size)\
aoqi@0 660 resource_free_bytes((char*)(old), (size) * sizeof(type))
aoqi@0 661
aoqi@0 662 #define FREE_FAST(old)\
aoqi@0 663 /* nop */
aoqi@0 664
aoqi@0 665 #define NEW_RESOURCE_OBJ(type)\
aoqi@0 666 NEW_RESOURCE_ARRAY(type, 1)
aoqi@0 667
aoqi@0 668 #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\
aoqi@0 669 NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)
aoqi@0 670
aoqi@0 671 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\
aoqi@0 672 (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)
aoqi@0 673
aoqi@0 674 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
aoqi@0 675 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
aoqi@0 676
aoqi@0 677 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
aoqi@0 678 (type*) (AllocateHeap((size) * sizeof(type), memflags))
aoqi@0 679
aoqi@0 680 #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\
aoqi@0 681 NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)
aoqi@0 682
aoqi@0 683 #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\
aoqi@0 684 NEW_C_HEAP_ARRAY3(type, (size), memflags, (address)0, AllocFailStrategy::RETURN_NULL)
aoqi@0 685
aoqi@0 686 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
aoqi@0 687 (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))
aoqi@0 688
aoqi@0 689 #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\
aoqi@0 690 (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))
aoqi@0 691
aoqi@0 692 #define FREE_C_HEAP_ARRAY(type, old, memflags) \
aoqi@0 693 FreeHeap((char*)(old), memflags)
aoqi@0 694
aoqi@0 695 // allocate type in heap without calling ctor
aoqi@0 696 #define NEW_C_HEAP_OBJ(type, memflags)\
aoqi@0 697 NEW_C_HEAP_ARRAY(type, 1, memflags)
aoqi@0 698
aoqi@0 699 #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\
aoqi@0 700 NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)
aoqi@0 701
aoqi@0 702 // deallocate obj of type in heap without calling dtor
aoqi@0 703 #define FREE_C_HEAP_OBJ(objname, memflags)\
aoqi@0 704 FreeHeap((char*)objname, memflags);
aoqi@0 705
aoqi@0 706 // for statistics
aoqi@0 707 #ifndef PRODUCT
aoqi@0 708 class AllocStats : StackObj {
aoqi@0 709 julong start_mallocs, start_frees;
aoqi@0 710 julong start_malloc_bytes, start_mfree_bytes, start_res_bytes;
aoqi@0 711 public:
aoqi@0 712 AllocStats();
aoqi@0 713
aoqi@0 714 julong num_mallocs(); // since creation of receiver
aoqi@0 715 julong alloc_bytes();
aoqi@0 716 julong num_frees();
aoqi@0 717 julong free_bytes();
aoqi@0 718 julong resource_bytes();
aoqi@0 719 void print();
aoqi@0 720 };
aoqi@0 721 #endif
aoqi@0 722
aoqi@0 723
aoqi@0 724 //------------------------------ReallocMark---------------------------------
aoqi@0 725 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
aoqi@0 726 // ReallocMark, which is declared in the same scope as the reallocated
aoqi@0 727 // pointer. Any operation that could __potentially__ cause a reallocation
aoqi@0 728 // should check the ReallocMark.
aoqi@0 729 class ReallocMark: public StackObj {
aoqi@0 730 protected:
aoqi@0 731 NOT_PRODUCT(int _nesting;)
aoqi@0 732
aoqi@0 733 public:
aoqi@0 734 ReallocMark() PRODUCT_RETURN;
aoqi@0 735 void check() PRODUCT_RETURN;
aoqi@0 736 };
aoqi@0 737
aoqi@0 738 // Helper class to allocate arrays that may become large.
aoqi@0 739 // Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit
aoqi@0 740 // and uses mapped memory for larger allocations.
aoqi@0 741 // Most OS mallocs do something similar but Solaris malloc does not revert
aoqi@0 742 // to mapped memory for large allocations. By default ArrayAllocatorMallocLimit
aoqi@0 743 // is set so that we always use malloc except for Solaris where we set the
aoqi@0 744 // limit to get mapped memory.
aoqi@0 745 template <class E, MEMFLAGS F>
aoqi@0 746 class ArrayAllocator VALUE_OBJ_CLASS_SPEC {
aoqi@0 747 char* _addr;
aoqi@0 748 bool _use_malloc;
aoqi@0 749 size_t _size;
aoqi@0 750 bool _free_in_destructor;
aoqi@0 751 public:
aoqi@0 752 ArrayAllocator(bool free_in_destructor = true) :
aoqi@0 753 _addr(NULL), _use_malloc(false), _size(0), _free_in_destructor(free_in_destructor) { }
aoqi@0 754
aoqi@0 755 ~ArrayAllocator() {
aoqi@0 756 if (_free_in_destructor) {
aoqi@0 757 free();
aoqi@0 758 }
aoqi@0 759 }
aoqi@0 760
aoqi@0 761 E* allocate(size_t length);
aoqi@0 762 void free();
aoqi@0 763 };
aoqi@0 764
aoqi@0 765 #endif // SHARE_VM_MEMORY_ALLOCATION_HPP

mercurial