Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f
aoqi@0 | 1 | /* |
zgu@7074 | 2 | * Copyright (c) 1997, 2014, 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 | /* |
zgu@7074 | 136 | * Memory types |
aoqi@0 | 137 | */ |
aoqi@0 | 138 | enum MemoryType { |
aoqi@0 | 139 | // Memory type by sub systems. It occupies lower byte. |
zgu@7074 | 140 | mtJavaHeap = 0x00, // Java heap |
zgu@7074 | 141 | mtClass = 0x01, // memory class for Java classes |
zgu@7074 | 142 | mtThread = 0x02, // memory for thread objects |
zgu@7074 | 143 | mtThreadStack = 0x03, |
zgu@7074 | 144 | mtCode = 0x04, // memory for generated code |
zgu@7074 | 145 | mtGC = 0x05, // memory for GC |
zgu@7074 | 146 | mtCompiler = 0x06, // memory for compiler |
zgu@7074 | 147 | mtInternal = 0x07, // memory used by VM, but does not belong to |
aoqi@0 | 148 | // any of above categories, and not used for |
aoqi@0 | 149 | // native memory tracking |
zgu@7074 | 150 | mtOther = 0x08, // memory not used by VM |
zgu@7074 | 151 | mtSymbol = 0x09, // symbol |
zgu@7074 | 152 | mtNMT = 0x0A, // memory used by native memory tracking |
zgu@7074 | 153 | mtClassShared = 0x0B, // class data sharing |
zgu@7074 | 154 | mtChunk = 0x0C, // chunk that holds content of arenas |
zgu@7074 | 155 | mtTest = 0x0D, // Test type for verifying NMT |
zgu@7074 | 156 | mtTracing = 0x0E, // memory used for Tracing |
zgu@7074 | 157 | mtNone = 0x0F, // undefined |
zgu@7074 | 158 | mt_number_of_types = 0x10 // number of memory types (mtDontTrack |
aoqi@0 | 159 | // is not included as validate type) |
aoqi@0 | 160 | }; |
aoqi@0 | 161 | |
zgu@7074 | 162 | typedef MemoryType MEMFLAGS; |
aoqi@0 | 163 | |
aoqi@0 | 164 | |
aoqi@0 | 165 | #if INCLUDE_NMT |
aoqi@0 | 166 | |
aoqi@0 | 167 | extern bool NMT_track_callsite; |
aoqi@0 | 168 | |
aoqi@0 | 169 | #else |
aoqi@0 | 170 | |
aoqi@0 | 171 | const bool NMT_track_callsite = false; |
aoqi@0 | 172 | |
aoqi@0 | 173 | #endif // INCLUDE_NMT |
aoqi@0 | 174 | |
zgu@7074 | 175 | class NativeCallStack; |
aoqi@0 | 176 | |
aoqi@0 | 177 | |
aoqi@0 | 178 | template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { |
aoqi@0 | 179 | public: |
zgu@7074 | 180 | _NOINLINE_ void* operator new(size_t size, const NativeCallStack& stack) throw(); |
zgu@7074 | 181 | _NOINLINE_ void* operator new(size_t size) throw(); |
aoqi@0 | 182 | _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, |
zgu@7074 | 183 | const NativeCallStack& stack) throw(); |
zgu@7074 | 184 | _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant) |
zgu@7074 | 185 | throw(); |
zgu@7074 | 186 | _NOINLINE_ void* operator new [](size_t size, const NativeCallStack& stack) throw(); |
zgu@7074 | 187 | _NOINLINE_ void* operator new [](size_t size) throw(); |
aoqi@0 | 188 | _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant, |
zgu@7074 | 189 | const NativeCallStack& stack) throw(); |
zgu@7074 | 190 | _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) |
zgu@7074 | 191 | throw(); |
aoqi@0 | 192 | void operator delete(void* p); |
aoqi@0 | 193 | void operator delete [] (void* p); |
aoqi@0 | 194 | }; |
aoqi@0 | 195 | |
aoqi@0 | 196 | // Base class for objects allocated on the stack only. |
aoqi@0 | 197 | // Calling new or delete will result in fatal error. |
aoqi@0 | 198 | |
aoqi@0 | 199 | class StackObj ALLOCATION_SUPER_CLASS_SPEC { |
aoqi@0 | 200 | private: |
aoqi@0 | 201 | void* operator new(size_t size) throw(); |
aoqi@0 | 202 | void* operator new [](size_t size) throw(); |
aoqi@0 | 203 | #ifdef __IBMCPP__ |
aoqi@0 | 204 | public: |
aoqi@0 | 205 | #endif |
aoqi@0 | 206 | void operator delete(void* p); |
aoqi@0 | 207 | void operator delete [](void* p); |
aoqi@0 | 208 | }; |
aoqi@0 | 209 | |
aoqi@0 | 210 | // Base class for objects used as value objects. |
aoqi@0 | 211 | // Calling new or delete will result in fatal error. |
aoqi@0 | 212 | // |
aoqi@0 | 213 | // Portability note: Certain compilers (e.g. gcc) will |
aoqi@0 | 214 | // always make classes bigger if it has a superclass, even |
aoqi@0 | 215 | // if the superclass does not have any virtual methods or |
aoqi@0 | 216 | // instance fields. The HotSpot implementation relies on this |
aoqi@0 | 217 | // not to happen. So never make a ValueObj class a direct subclass |
aoqi@0 | 218 | // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g., |
aoqi@0 | 219 | // like this: |
aoqi@0 | 220 | // |
aoqi@0 | 221 | // class A VALUE_OBJ_CLASS_SPEC { |
aoqi@0 | 222 | // ... |
aoqi@0 | 223 | // } |
aoqi@0 | 224 | // |
aoqi@0 | 225 | // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can |
aoqi@0 | 226 | // be defined as a an empty string "". |
aoqi@0 | 227 | // |
aoqi@0 | 228 | class _ValueObj { |
aoqi@0 | 229 | private: |
aoqi@0 | 230 | void* operator new(size_t size) throw(); |
aoqi@0 | 231 | void operator delete(void* p); |
aoqi@0 | 232 | void* operator new [](size_t size) throw(); |
aoqi@0 | 233 | void operator delete [](void* p); |
aoqi@0 | 234 | }; |
aoqi@0 | 235 | |
aoqi@0 | 236 | |
aoqi@0 | 237 | // Base class for objects stored in Metaspace. |
aoqi@0 | 238 | // Calling delete will result in fatal error. |
aoqi@0 | 239 | // |
aoqi@0 | 240 | // Do not inherit from something with a vptr because this class does |
aoqi@0 | 241 | // not introduce one. This class is used to allocate both shared read-only |
aoqi@0 | 242 | // and shared read-write classes. |
aoqi@0 | 243 | // |
aoqi@0 | 244 | |
aoqi@0 | 245 | class ClassLoaderData; |
aoqi@0 | 246 | |
aoqi@0 | 247 | class MetaspaceObj { |
aoqi@0 | 248 | public: |
aoqi@0 | 249 | bool is_metaspace_object() const; |
aoqi@0 | 250 | bool is_shared() const; |
aoqi@0 | 251 | void print_address_on(outputStream* st) const; // nonvirtual address printing |
aoqi@0 | 252 | |
aoqi@0 | 253 | #define METASPACE_OBJ_TYPES_DO(f) \ |
aoqi@0 | 254 | f(Unknown) \ |
aoqi@0 | 255 | f(Class) \ |
aoqi@0 | 256 | f(Symbol) \ |
aoqi@0 | 257 | f(TypeArrayU1) \ |
aoqi@0 | 258 | f(TypeArrayU2) \ |
aoqi@0 | 259 | f(TypeArrayU4) \ |
aoqi@0 | 260 | f(TypeArrayU8) \ |
aoqi@0 | 261 | f(TypeArrayOther) \ |
aoqi@0 | 262 | f(Method) \ |
aoqi@0 | 263 | f(ConstMethod) \ |
aoqi@0 | 264 | f(MethodData) \ |
aoqi@0 | 265 | f(ConstantPool) \ |
aoqi@0 | 266 | f(ConstantPoolCache) \ |
aoqi@0 | 267 | f(Annotation) \ |
iklam@7089 | 268 | f(MethodCounters) \ |
iklam@7089 | 269 | f(Deallocated) |
aoqi@0 | 270 | |
aoqi@0 | 271 | #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type, |
aoqi@0 | 272 | #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; |
aoqi@0 | 273 | |
aoqi@0 | 274 | enum Type { |
aoqi@0 | 275 | // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc |
aoqi@0 | 276 | METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE) |
aoqi@0 | 277 | _number_of_types |
aoqi@0 | 278 | }; |
aoqi@0 | 279 | |
aoqi@0 | 280 | static const char * type_name(Type type) { |
aoqi@0 | 281 | switch(type) { |
aoqi@0 | 282 | METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE) |
aoqi@0 | 283 | default: |
aoqi@0 | 284 | ShouldNotReachHere(); |
aoqi@0 | 285 | return NULL; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | static MetaspaceObj::Type array_type(size_t elem_size) { |
aoqi@0 | 290 | switch (elem_size) { |
aoqi@0 | 291 | case 1: return TypeArrayU1Type; |
aoqi@0 | 292 | case 2: return TypeArrayU2Type; |
aoqi@0 | 293 | case 4: return TypeArrayU4Type; |
aoqi@0 | 294 | case 8: return TypeArrayU8Type; |
aoqi@0 | 295 | default: |
aoqi@0 | 296 | return TypeArrayOtherType; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | void* operator new(size_t size, ClassLoaderData* loader_data, |
aoqi@0 | 301 | size_t word_size, bool read_only, |
aoqi@0 | 302 | Type type, Thread* thread) throw(); |
aoqi@0 | 303 | // can't use TRAPS from this header file. |
aoqi@0 | 304 | void operator delete(void* p) { ShouldNotCallThis(); } |
aoqi@0 | 305 | }; |
aoqi@0 | 306 | |
aoqi@0 | 307 | // Base class for classes that constitute name spaces. |
aoqi@0 | 308 | |
aoqi@0 | 309 | class AllStatic { |
aoqi@0 | 310 | public: |
aoqi@0 | 311 | AllStatic() { ShouldNotCallThis(); } |
aoqi@0 | 312 | ~AllStatic() { ShouldNotCallThis(); } |
aoqi@0 | 313 | }; |
aoqi@0 | 314 | |
aoqi@0 | 315 | |
aoqi@0 | 316 | //------------------------------Chunk------------------------------------------ |
aoqi@0 | 317 | // Linked list of raw memory chunks |
aoqi@0 | 318 | class Chunk: CHeapObj<mtChunk> { |
aoqi@0 | 319 | friend class VMStructs; |
aoqi@0 | 320 | |
aoqi@0 | 321 | protected: |
aoqi@0 | 322 | Chunk* _next; // Next Chunk in list |
aoqi@0 | 323 | const size_t _len; // Size of this Chunk |
aoqi@0 | 324 | public: |
aoqi@0 | 325 | void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw(); |
aoqi@0 | 326 | void operator delete(void* p); |
aoqi@0 | 327 | Chunk(size_t length); |
aoqi@0 | 328 | |
aoqi@0 | 329 | enum { |
aoqi@0 | 330 | // default sizes; make them slightly smaller than 2**k to guard against |
aoqi@0 | 331 | // buddy-system style malloc implementations |
aoqi@0 | 332 | #ifdef _LP64 |
aoqi@0 | 333 | slack = 40, // [RGV] Not sure if this is right, but make it |
aoqi@0 | 334 | // a multiple of 8. |
aoqi@0 | 335 | #else |
aoqi@0 | 336 | slack = 20, // suspected sizeof(Chunk) + internal malloc headers |
aoqi@0 | 337 | #endif |
aoqi@0 | 338 | |
aoqi@0 | 339 | tiny_size = 256 - slack, // Size of first chunk (tiny) |
aoqi@0 | 340 | init_size = 1*K - slack, // Size of first chunk (normal aka small) |
aoqi@0 | 341 | medium_size= 10*K - slack, // Size of medium-sized chunk |
aoqi@0 | 342 | size = 32*K - slack, // Default size of an Arena chunk (following the first) |
aoqi@0 | 343 | non_pool_size = init_size + 32 // An initial size which is not one of above |
aoqi@0 | 344 | }; |
aoqi@0 | 345 | |
aoqi@0 | 346 | void chop(); // Chop this chunk |
aoqi@0 | 347 | void next_chop(); // Chop next chunk |
aoqi@0 | 348 | static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); } |
aoqi@0 | 349 | static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); } |
aoqi@0 | 350 | |
aoqi@0 | 351 | size_t length() const { return _len; } |
aoqi@0 | 352 | Chunk* next() const { return _next; } |
aoqi@0 | 353 | void set_next(Chunk* n) { _next = n; } |
aoqi@0 | 354 | // Boundaries of data area (possibly unused) |
aoqi@0 | 355 | char* bottom() const { return ((char*) this) + aligned_overhead_size(); } |
aoqi@0 | 356 | char* top() const { return bottom() + _len; } |
aoqi@0 | 357 | bool contains(char* p) const { return bottom() <= p && p <= top(); } |
aoqi@0 | 358 | |
aoqi@0 | 359 | // Start the chunk_pool cleaner task |
aoqi@0 | 360 | static void start_chunk_pool_cleaner_task(); |
aoqi@0 | 361 | |
aoqi@0 | 362 | static void clean_chunk_pool(); |
aoqi@0 | 363 | }; |
aoqi@0 | 364 | |
aoqi@0 | 365 | //------------------------------Arena------------------------------------------ |
aoqi@0 | 366 | // Fast allocation of memory |
zgu@7074 | 367 | class Arena : public CHeapObj<mtNone> { |
aoqi@0 | 368 | protected: |
aoqi@0 | 369 | friend class ResourceMark; |
aoqi@0 | 370 | friend class HandleMark; |
aoqi@0 | 371 | friend class NoHandleMark; |
aoqi@0 | 372 | friend class VMStructs; |
aoqi@0 | 373 | |
zgu@7074 | 374 | MEMFLAGS _flags; // Memory tracking flags |
zgu@7074 | 375 | |
aoqi@0 | 376 | Chunk *_first; // First chunk |
aoqi@0 | 377 | Chunk *_chunk; // current chunk |
aoqi@0 | 378 | char *_hwm, *_max; // High water mark and max in current chunk |
aoqi@0 | 379 | // Get a new Chunk of at least size x |
aoqi@0 | 380 | void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); |
aoqi@0 | 381 | size_t _size_in_bytes; // Size of arena (used for native memory tracking) |
aoqi@0 | 382 | |
aoqi@0 | 383 | NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start |
aoqi@0 | 384 | friend class AllocStats; |
aoqi@0 | 385 | debug_only(void* malloc(size_t size);) |
aoqi@0 | 386 | debug_only(void* internal_malloc_4(size_t x);) |
aoqi@0 | 387 | NOT_PRODUCT(void inc_bytes_allocated(size_t x);) |
aoqi@0 | 388 | |
aoqi@0 | 389 | void signal_out_of_memory(size_t request, const char* whence) const; |
aoqi@0 | 390 | |
aoqi@0 | 391 | bool check_for_overflow(size_t request, const char* whence, |
aoqi@0 | 392 | AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const { |
aoqi@0 | 393 | if (UINTPTR_MAX - request < (uintptr_t)_hwm) { |
aoqi@0 | 394 | if (alloc_failmode == AllocFailStrategy::RETURN_NULL) { |
aoqi@0 | 395 | return false; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | signal_out_of_memory(request, whence); |
aoqi@0 | 398 | } |
aoqi@0 | 399 | return true; |
aoqi@0 | 400 | } |
aoqi@0 | 401 | |
aoqi@0 | 402 | public: |
zgu@7074 | 403 | Arena(MEMFLAGS memflag); |
zgu@7074 | 404 | Arena(MEMFLAGS memflag, size_t init_size); |
aoqi@0 | 405 | ~Arena(); |
aoqi@0 | 406 | void destruct_contents(); |
aoqi@0 | 407 | char* hwm() const { return _hwm; } |
aoqi@0 | 408 | |
aoqi@0 | 409 | // new operators |
aoqi@0 | 410 | void* operator new (size_t size) throw(); |
aoqi@0 | 411 | void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw(); |
aoqi@0 | 412 | |
aoqi@0 | 413 | // dynamic memory type tagging |
aoqi@0 | 414 | void* operator new(size_t size, MEMFLAGS flags) throw(); |
aoqi@0 | 415 | void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw(); |
aoqi@0 | 416 | void operator delete(void* p); |
aoqi@0 | 417 | |
aoqi@0 | 418 | // Fast allocate in the arena. Common case is: pointer test + increment. |
aoqi@0 | 419 | void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
aoqi@0 | 420 | assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2"); |
aoqi@0 | 421 | x = ARENA_ALIGN(x); |
aoqi@0 | 422 | debug_only(if (UseMallocOnly) return malloc(x);) |
aoqi@0 | 423 | if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode)) |
aoqi@0 | 424 | return NULL; |
aoqi@0 | 425 | NOT_PRODUCT(inc_bytes_allocated(x);) |
aoqi@0 | 426 | if (_hwm + x > _max) { |
aoqi@0 | 427 | return grow(x, alloc_failmode); |
aoqi@0 | 428 | } else { |
aoqi@0 | 429 | char *old = _hwm; |
aoqi@0 | 430 | _hwm += x; |
aoqi@0 | 431 | return old; |
aoqi@0 | 432 | } |
aoqi@0 | 433 | } |
aoqi@0 | 434 | // Further assume size is padded out to words |
aoqi@0 | 435 | void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
aoqi@0 | 436 | assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); |
aoqi@0 | 437 | debug_only(if (UseMallocOnly) return malloc(x);) |
aoqi@0 | 438 | if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode)) |
aoqi@0 | 439 | return NULL; |
aoqi@0 | 440 | NOT_PRODUCT(inc_bytes_allocated(x);) |
aoqi@0 | 441 | if (_hwm + x > _max) { |
aoqi@0 | 442 | return grow(x, alloc_failmode); |
aoqi@0 | 443 | } else { |
aoqi@0 | 444 | char *old = _hwm; |
aoqi@0 | 445 | _hwm += x; |
aoqi@0 | 446 | return old; |
aoqi@0 | 447 | } |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | // Allocate with 'double' alignment. It is 8 bytes on sparc. |
aoqi@0 | 451 | // In other cases Amalloc_D() should be the same as Amalloc_4(). |
aoqi@0 | 452 | void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
aoqi@0 | 453 | assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); |
aoqi@0 | 454 | debug_only(if (UseMallocOnly) return malloc(x);) |
aoqi@0 | 455 | #if defined(SPARC) && !defined(_LP64) |
aoqi@0 | 456 | #define DALIGN_M1 7 |
aoqi@0 | 457 | size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm; |
aoqi@0 | 458 | x += delta; |
aoqi@0 | 459 | #endif |
aoqi@0 | 460 | if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode)) |
aoqi@0 | 461 | return NULL; |
aoqi@0 | 462 | NOT_PRODUCT(inc_bytes_allocated(x);) |
aoqi@0 | 463 | if (_hwm + x > _max) { |
aoqi@0 | 464 | return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes. |
aoqi@0 | 465 | } else { |
aoqi@0 | 466 | char *old = _hwm; |
aoqi@0 | 467 | _hwm += x; |
aoqi@0 | 468 | #if defined(SPARC) && !defined(_LP64) |
aoqi@0 | 469 | old += delta; // align to 8-bytes |
aoqi@0 | 470 | #endif |
aoqi@0 | 471 | return old; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | } |
aoqi@0 | 474 | |
aoqi@0 | 475 | // Fast delete in area. Common case is: NOP (except for storage reclaimed) |
aoqi@0 | 476 | void Afree(void *ptr, size_t size) { |
aoqi@0 | 477 | #ifdef ASSERT |
aoqi@0 | 478 | if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory |
aoqi@0 | 479 | if (UseMallocOnly) return; |
aoqi@0 | 480 | #endif |
aoqi@0 | 481 | if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr; |
aoqi@0 | 482 | } |
aoqi@0 | 483 | |
aoqi@0 | 484 | void *Arealloc( void *old_ptr, size_t old_size, size_t new_size, |
aoqi@0 | 485 | AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); |
aoqi@0 | 486 | |
aoqi@0 | 487 | // Move contents of this arena into an empty arena |
aoqi@0 | 488 | Arena *move_contents(Arena *empty_arena); |
aoqi@0 | 489 | |
aoqi@0 | 490 | // Determine if pointer belongs to this Arena or not. |
aoqi@0 | 491 | bool contains( const void *ptr ) const; |
aoqi@0 | 492 | |
aoqi@0 | 493 | // Total of all chunks in use (not thread-safe) |
aoqi@0 | 494 | size_t used() const; |
aoqi@0 | 495 | |
aoqi@0 | 496 | // Total # of bytes used |
aoqi@0 | 497 | size_t size_in_bytes() const { return _size_in_bytes; }; |
aoqi@0 | 498 | void set_size_in_bytes(size_t size); |
aoqi@0 | 499 | |
aoqi@0 | 500 | static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN; |
aoqi@0 | 501 | static void free_all(char** start, char** end) PRODUCT_RETURN; |
aoqi@0 | 502 | |
aoqi@0 | 503 | private: |
aoqi@0 | 504 | // Reset this Arena to empty, access will trigger grow if necessary |
aoqi@0 | 505 | void reset(void) { |
aoqi@0 | 506 | _first = _chunk = NULL; |
aoqi@0 | 507 | _hwm = _max = NULL; |
aoqi@0 | 508 | set_size_in_bytes(0); |
aoqi@0 | 509 | } |
aoqi@0 | 510 | }; |
aoqi@0 | 511 | |
aoqi@0 | 512 | // One of the following macros must be used when allocating |
aoqi@0 | 513 | // an array or object from an arena |
aoqi@0 | 514 | #define NEW_ARENA_ARRAY(arena, type, size) \ |
aoqi@0 | 515 | (type*) (arena)->Amalloc((size) * sizeof(type)) |
aoqi@0 | 516 | |
aoqi@0 | 517 | #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \ |
aoqi@0 | 518 | (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \ |
aoqi@0 | 519 | (new_size) * sizeof(type) ) |
aoqi@0 | 520 | |
aoqi@0 | 521 | #define FREE_ARENA_ARRAY(arena, type, old, size) \ |
aoqi@0 | 522 | (arena)->Afree((char*)(old), (size) * sizeof(type)) |
aoqi@0 | 523 | |
aoqi@0 | 524 | #define NEW_ARENA_OBJ(arena, type) \ |
aoqi@0 | 525 | NEW_ARENA_ARRAY(arena, type, 1) |
aoqi@0 | 526 | |
aoqi@0 | 527 | |
aoqi@0 | 528 | //%note allocation_1 |
aoqi@0 | 529 | extern char* resource_allocate_bytes(size_t size, |
aoqi@0 | 530 | AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); |
aoqi@0 | 531 | extern char* resource_allocate_bytes(Thread* thread, size_t size, |
aoqi@0 | 532 | AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); |
aoqi@0 | 533 | extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size, |
aoqi@0 | 534 | AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); |
aoqi@0 | 535 | extern void resource_free_bytes( char *old, size_t size ); |
aoqi@0 | 536 | |
aoqi@0 | 537 | //---------------------------------------------------------------------- |
aoqi@0 | 538 | // Base class for objects allocated in the resource area per default. |
aoqi@0 | 539 | // Optionally, objects may be allocated on the C heap with |
aoqi@0 | 540 | // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena) |
aoqi@0 | 541 | // ResourceObj's can be allocated within other objects, but don't use |
aoqi@0 | 542 | // new or delete (allocation_type is unknown). If new is used to allocate, |
aoqi@0 | 543 | // use delete to deallocate. |
aoqi@0 | 544 | class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { |
aoqi@0 | 545 | public: |
aoqi@0 | 546 | enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 }; |
aoqi@0 | 547 | static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN; |
aoqi@0 | 548 | #ifdef ASSERT |
aoqi@0 | 549 | private: |
aoqi@0 | 550 | // When this object is allocated on stack the new() operator is not |
aoqi@0 | 551 | // called but garbage on stack may look like a valid allocation_type. |
aoqi@0 | 552 | // Store negated 'this' pointer when new() is called to distinguish cases. |
aoqi@0 | 553 | // Use second array's element for verification value to distinguish garbage. |
aoqi@0 | 554 | uintptr_t _allocation_t[2]; |
aoqi@0 | 555 | bool is_type_set() const; |
aoqi@0 | 556 | public: |
aoqi@0 | 557 | allocation_type get_allocation_type() const; |
aoqi@0 | 558 | bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; } |
aoqi@0 | 559 | bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; } |
aoqi@0 | 560 | bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; } |
aoqi@0 | 561 | bool allocated_on_arena() const { return get_allocation_type() == ARENA; } |
aoqi@0 | 562 | ResourceObj(); // default construtor |
aoqi@0 | 563 | ResourceObj(const ResourceObj& r); // default copy construtor |
aoqi@0 | 564 | ResourceObj& operator=(const ResourceObj& r); // default copy assignment |
aoqi@0 | 565 | ~ResourceObj(); |
aoqi@0 | 566 | #endif // ASSERT |
aoqi@0 | 567 | |
aoqi@0 | 568 | public: |
aoqi@0 | 569 | void* operator new(size_t size, allocation_type type, MEMFLAGS flags) throw(); |
aoqi@0 | 570 | void* operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw(); |
aoqi@0 | 571 | void* operator new(size_t size, const std::nothrow_t& nothrow_constant, |
aoqi@0 | 572 | allocation_type type, MEMFLAGS flags) throw(); |
aoqi@0 | 573 | void* operator new [](size_t size, const std::nothrow_t& nothrow_constant, |
aoqi@0 | 574 | allocation_type type, MEMFLAGS flags) throw(); |
aoqi@0 | 575 | |
aoqi@0 | 576 | void* operator new(size_t size, Arena *arena) throw() { |
aoqi@0 | 577 | address res = (address)arena->Amalloc(size); |
aoqi@0 | 578 | DEBUG_ONLY(set_allocation_type(res, ARENA);) |
aoqi@0 | 579 | return res; |
aoqi@0 | 580 | } |
aoqi@0 | 581 | |
aoqi@0 | 582 | void* operator new [](size_t size, Arena *arena) throw() { |
aoqi@0 | 583 | address res = (address)arena->Amalloc(size); |
aoqi@0 | 584 | DEBUG_ONLY(set_allocation_type(res, ARENA);) |
aoqi@0 | 585 | return res; |
aoqi@0 | 586 | } |
aoqi@0 | 587 | |
aoqi@0 | 588 | void* operator new(size_t size) throw() { |
aoqi@0 | 589 | address res = (address)resource_allocate_bytes(size); |
aoqi@0 | 590 | DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
aoqi@0 | 591 | return res; |
aoqi@0 | 592 | } |
aoqi@0 | 593 | |
aoqi@0 | 594 | void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() { |
aoqi@0 | 595 | address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); |
aoqi@0 | 596 | DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) |
aoqi@0 | 597 | return res; |
aoqi@0 | 598 | } |
aoqi@0 | 599 | |
aoqi@0 | 600 | void* operator new [](size_t size) throw() { |
aoqi@0 | 601 | address res = (address)resource_allocate_bytes(size); |
aoqi@0 | 602 | DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
aoqi@0 | 603 | return res; |
aoqi@0 | 604 | } |
aoqi@0 | 605 | |
aoqi@0 | 606 | void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() { |
aoqi@0 | 607 | address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); |
aoqi@0 | 608 | DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) |
aoqi@0 | 609 | return res; |
aoqi@0 | 610 | } |
aoqi@0 | 611 | |
aoqi@0 | 612 | void operator delete(void* p); |
aoqi@0 | 613 | void operator delete [](void* p); |
aoqi@0 | 614 | }; |
aoqi@0 | 615 | |
aoqi@0 | 616 | // One of the following macros must be used when allocating an array |
aoqi@0 | 617 | // or object to determine whether it should reside in the C heap on in |
aoqi@0 | 618 | // the resource area. |
aoqi@0 | 619 | |
aoqi@0 | 620 | #define NEW_RESOURCE_ARRAY(type, size)\ |
aoqi@0 | 621 | (type*) resource_allocate_bytes((size) * sizeof(type)) |
aoqi@0 | 622 | |
aoqi@0 | 623 | #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\ |
aoqi@0 | 624 | (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL) |
aoqi@0 | 625 | |
aoqi@0 | 626 | #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\ |
aoqi@0 | 627 | (type*) resource_allocate_bytes(thread, (size) * sizeof(type)) |
aoqi@0 | 628 | |
aoqi@0 | 629 | #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\ |
aoqi@0 | 630 | (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL) |
aoqi@0 | 631 | |
aoqi@0 | 632 | #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\ |
aoqi@0 | 633 | (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type)) |
aoqi@0 | 634 | |
aoqi@0 | 635 | #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\ |
aoqi@0 | 636 | (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\ |
aoqi@0 | 637 | (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL) |
aoqi@0 | 638 | |
aoqi@0 | 639 | #define FREE_RESOURCE_ARRAY(type, old, size)\ |
aoqi@0 | 640 | resource_free_bytes((char*)(old), (size) * sizeof(type)) |
aoqi@0 | 641 | |
aoqi@0 | 642 | #define FREE_FAST(old)\ |
aoqi@0 | 643 | /* nop */ |
aoqi@0 | 644 | |
aoqi@0 | 645 | #define NEW_RESOURCE_OBJ(type)\ |
aoqi@0 | 646 | NEW_RESOURCE_ARRAY(type, 1) |
aoqi@0 | 647 | |
aoqi@0 | 648 | #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\ |
aoqi@0 | 649 | NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1) |
aoqi@0 | 650 | |
aoqi@0 | 651 | #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\ |
aoqi@0 | 652 | (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail) |
aoqi@0 | 653 | |
aoqi@0 | 654 | #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ |
aoqi@0 | 655 | (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) |
aoqi@0 | 656 | |
aoqi@0 | 657 | #define NEW_C_HEAP_ARRAY(type, size, memflags)\ |
aoqi@0 | 658 | (type*) (AllocateHeap((size) * sizeof(type), memflags)) |
aoqi@0 | 659 | |
aoqi@0 | 660 | #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\ |
aoqi@0 | 661 | NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL) |
aoqi@0 | 662 | |
aoqi@0 | 663 | #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\ |
zgu@7074 | 664 | NEW_C_HEAP_ARRAY3(type, (size), memflags, CURRENT_PC, AllocFailStrategy::RETURN_NULL) |
aoqi@0 | 665 | |
aoqi@0 | 666 | #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ |
aoqi@0 | 667 | (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags)) |
aoqi@0 | 668 | |
aoqi@0 | 669 | #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\ |
aoqi@0 | 670 | (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL)) |
aoqi@0 | 671 | |
aoqi@0 | 672 | #define FREE_C_HEAP_ARRAY(type, old, memflags) \ |
aoqi@0 | 673 | FreeHeap((char*)(old), memflags) |
aoqi@0 | 674 | |
aoqi@0 | 675 | // allocate type in heap without calling ctor |
aoqi@0 | 676 | #define NEW_C_HEAP_OBJ(type, memflags)\ |
aoqi@0 | 677 | NEW_C_HEAP_ARRAY(type, 1, memflags) |
aoqi@0 | 678 | |
aoqi@0 | 679 | #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\ |
aoqi@0 | 680 | NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags) |
aoqi@0 | 681 | |
aoqi@0 | 682 | // deallocate obj of type in heap without calling dtor |
aoqi@0 | 683 | #define FREE_C_HEAP_OBJ(objname, memflags)\ |
aoqi@0 | 684 | FreeHeap((char*)objname, memflags); |
aoqi@0 | 685 | |
aoqi@0 | 686 | // for statistics |
aoqi@0 | 687 | #ifndef PRODUCT |
aoqi@0 | 688 | class AllocStats : StackObj { |
aoqi@0 | 689 | julong start_mallocs, start_frees; |
aoqi@0 | 690 | julong start_malloc_bytes, start_mfree_bytes, start_res_bytes; |
aoqi@0 | 691 | public: |
aoqi@0 | 692 | AllocStats(); |
aoqi@0 | 693 | |
aoqi@0 | 694 | julong num_mallocs(); // since creation of receiver |
aoqi@0 | 695 | julong alloc_bytes(); |
aoqi@0 | 696 | julong num_frees(); |
aoqi@0 | 697 | julong free_bytes(); |
aoqi@0 | 698 | julong resource_bytes(); |
aoqi@0 | 699 | void print(); |
aoqi@0 | 700 | }; |
aoqi@0 | 701 | #endif |
aoqi@0 | 702 | |
aoqi@0 | 703 | |
aoqi@0 | 704 | //------------------------------ReallocMark--------------------------------- |
aoqi@0 | 705 | // Code which uses REALLOC_RESOURCE_ARRAY should check an associated |
aoqi@0 | 706 | // ReallocMark, which is declared in the same scope as the reallocated |
aoqi@0 | 707 | // pointer. Any operation that could __potentially__ cause a reallocation |
aoqi@0 | 708 | // should check the ReallocMark. |
aoqi@0 | 709 | class ReallocMark: public StackObj { |
aoqi@0 | 710 | protected: |
aoqi@0 | 711 | NOT_PRODUCT(int _nesting;) |
aoqi@0 | 712 | |
aoqi@0 | 713 | public: |
aoqi@0 | 714 | ReallocMark() PRODUCT_RETURN; |
aoqi@0 | 715 | void check() PRODUCT_RETURN; |
aoqi@0 | 716 | }; |
aoqi@0 | 717 | |
aoqi@0 | 718 | // Helper class to allocate arrays that may become large. |
aoqi@0 | 719 | // Uses the OS malloc for allocations smaller than ArrayAllocatorMallocLimit |
aoqi@0 | 720 | // and uses mapped memory for larger allocations. |
aoqi@0 | 721 | // Most OS mallocs do something similar but Solaris malloc does not revert |
aoqi@0 | 722 | // to mapped memory for large allocations. By default ArrayAllocatorMallocLimit |
aoqi@0 | 723 | // is set so that we always use malloc except for Solaris where we set the |
aoqi@0 | 724 | // limit to get mapped memory. |
aoqi@0 | 725 | template <class E, MEMFLAGS F> |
aoqi@0 | 726 | class ArrayAllocator VALUE_OBJ_CLASS_SPEC { |
aoqi@0 | 727 | char* _addr; |
aoqi@0 | 728 | bool _use_malloc; |
aoqi@0 | 729 | size_t _size; |
aoqi@0 | 730 | bool _free_in_destructor; |
aoqi@0 | 731 | public: |
aoqi@0 | 732 | ArrayAllocator(bool free_in_destructor = true) : |
aoqi@0 | 733 | _addr(NULL), _use_malloc(false), _size(0), _free_in_destructor(free_in_destructor) { } |
aoqi@0 | 734 | |
aoqi@0 | 735 | ~ArrayAllocator() { |
aoqi@0 | 736 | if (_free_in_destructor) { |
aoqi@0 | 737 | free(); |
aoqi@0 | 738 | } |
aoqi@0 | 739 | } |
aoqi@0 | 740 | |
aoqi@0 | 741 | E* allocate(size_t length); |
aoqi@0 | 742 | void free(); |
aoqi@0 | 743 | }; |
aoqi@0 | 744 | |
aoqi@0 | 745 | #endif // SHARE_VM_MEMORY_ALLOCATION_HPP |