src/share/vm/memory/allocation.hpp

Wed, 19 Jun 2013 11:02:10 +0100

author
chegar
date
Wed, 19 Jun 2013 11:02:10 +0100
changeset 5252
3a0774193f71
parent 5251
eaf3742822ec
parent 5237
f2110083203d
child 5295
9f3e3245b50f
permissions
-rw-r--r--

Merge

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

mercurial