annotate src/share/vm/memory/allocation.hpp @ 1601:126ea7725993

6953477: Increase portability and flexibility of building Hotspot Summary: A collection of portability improvements including shared code support for PPC, ARM platforms, software floating point, cross compilation support and improvements in error crash detail. Reviewed-by: phh, never, coleenp, dholmes
author bobv
date Tue, 03 Aug 2010 08:13:38 -0400
parents c18cbe5936b8
children f4f596978298
rev   line source
duke@0 1 /*
trims@1472 2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
duke@0 26 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
duke@0 27 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
duke@0 28
duke@0 29 // All classes in the virtual machine must be subclassed
duke@0 30 // by one of the following allocation classes:
duke@0 31 //
duke@0 32 // For objects allocated in the resource area (see resourceArea.hpp).
duke@0 33 // - ResourceObj
duke@0 34 //
duke@0 35 // For objects allocated in the C-heap (managed by: free & malloc).
duke@0 36 // - CHeapObj
duke@0 37 //
duke@0 38 // For objects allocated on the stack.
duke@0 39 // - StackObj
duke@0 40 //
duke@0 41 // For embedded objects.
duke@0 42 // - ValueObj
duke@0 43 //
duke@0 44 // For classes used as name spaces.
duke@0 45 // - AllStatic
duke@0 46 //
duke@0 47 // The printable subclasses are used for debugging and define virtual
duke@0 48 // member functions for printing. Classes that avoid allocating the
duke@0 49 // vtbl entries in the objects should therefore not be the printable
duke@0 50 // subclasses.
duke@0 51 //
duke@0 52 // The following macros and function should be used to allocate memory
duke@0 53 // directly in the resource area or in the C-heap:
duke@0 54 //
duke@0 55 // NEW_RESOURCE_ARRAY(type,size)
duke@0 56 // NEW_RESOURCE_OBJ(type)
duke@0 57 // NEW_C_HEAP_ARRAY(type,size)
duke@0 58 // NEW_C_HEAP_OBJ(type)
duke@0 59 // char* AllocateHeap(size_t size, const char* name);
duke@0 60 // void FreeHeap(void* p);
duke@0 61 //
duke@0 62 // C-heap allocation can be traced using +PrintHeapAllocation.
duke@0 63 // malloc and free should therefore never called directly.
duke@0 64
duke@0 65 // Base class for objects allocated in the C-heap.
duke@0 66
duke@0 67 // In non product mode we introduce a super class for all allocation classes
duke@0 68 // that supports printing.
duke@0 69 // We avoid the superclass in product mode since some C++ compilers add
duke@0 70 // a word overhead for empty super classes.
duke@0 71
duke@0 72 #ifdef PRODUCT
duke@0 73 #define ALLOCATION_SUPER_CLASS_SPEC
duke@0 74 #else
duke@0 75 #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
duke@0 76 class AllocatedObj {
duke@0 77 public:
duke@0 78 // Printing support
duke@0 79 void print() const;
duke@0 80 void print_value() const;
duke@0 81
duke@0 82 virtual void print_on(outputStream* st) const;
duke@0 83 virtual void print_value_on(outputStream* st) const;
duke@0 84 };
duke@0 85 #endif
duke@0 86
duke@0 87 class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
duke@0 88 public:
duke@0 89 void* operator new(size_t size);
duke@0 90 void operator delete(void* p);
duke@0 91 void* new_array(size_t size);
duke@0 92 };
duke@0 93
duke@0 94 // Base class for objects allocated on the stack only.
duke@0 95 // Calling new or delete will result in fatal error.
duke@0 96
duke@0 97 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
duke@0 98 public:
duke@0 99 void* operator new(size_t size);
duke@0 100 void operator delete(void* p);
duke@0 101 };
duke@0 102
duke@0 103 // Base class for objects used as value objects.
duke@0 104 // Calling new or delete will result in fatal error.
duke@0 105 //
duke@0 106 // Portability note: Certain compilers (e.g. gcc) will
duke@0 107 // always make classes bigger if it has a superclass, even
duke@0 108 // if the superclass does not have any virtual methods or
duke@0 109 // instance fields. The HotSpot implementation relies on this
duke@0 110 // not to happen. So never make a ValueObj class a direct subclass
duke@0 111 // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
duke@0 112 // like this:
duke@0 113 //
duke@0 114 // class A VALUE_OBJ_CLASS_SPEC {
duke@0 115 // ...
duke@0 116 // }
duke@0 117 //
duke@0 118 // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
duke@0 119 // be defined as a an empty string "".
duke@0 120 //
duke@0 121 class _ValueObj {
duke@0 122 public:
duke@0 123 void* operator new(size_t size);
duke@0 124 void operator delete(void* p);
duke@0 125 };
duke@0 126
duke@0 127 // Base class for classes that constitute name spaces.
duke@0 128
duke@0 129 class AllStatic {
duke@0 130 public:
duke@0 131 AllStatic() { ShouldNotCallThis(); }
duke@0 132 ~AllStatic() { ShouldNotCallThis(); }
duke@0 133 };
duke@0 134
duke@0 135
duke@0 136 //------------------------------Chunk------------------------------------------
duke@0 137 // Linked list of raw memory chunks
duke@0 138 class Chunk: public CHeapObj {
duke@0 139 protected:
duke@0 140 Chunk* _next; // Next Chunk in list
duke@0 141 const size_t _len; // Size of this Chunk
duke@0 142 public:
duke@0 143 void* operator new(size_t size, size_t length);
duke@0 144 void operator delete(void* p);
duke@0 145 Chunk(size_t length);
duke@0 146
duke@0 147 enum {
duke@0 148 // default sizes; make them slightly smaller than 2**k to guard against
duke@0 149 // buddy-system style malloc implementations
duke@0 150 #ifdef _LP64
duke@0 151 slack = 40, // [RGV] Not sure if this is right, but make it
duke@0 152 // a multiple of 8.
duke@0 153 #else
duke@0 154 slack = 20, // suspected sizeof(Chunk) + internal malloc headers
duke@0 155 #endif
duke@0 156
duke@0 157 init_size = 1*K - slack, // Size of first chunk
duke@0 158 medium_size= 10*K - slack, // Size of medium-sized chunk
duke@0 159 size = 32*K - slack, // Default size of an Arena chunk (following the first)
duke@0 160 non_pool_size = init_size + 32 // An initial size which is not one of above
duke@0 161 };
duke@0 162
duke@0 163 void chop(); // Chop this chunk
duke@0 164 void next_chop(); // Chop next chunk
duke@0 165 static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
duke@0 166
duke@0 167 size_t length() const { return _len; }
duke@0 168 Chunk* next() const { return _next; }
duke@0 169 void set_next(Chunk* n) { _next = n; }
duke@0 170 // Boundaries of data area (possibly unused)
duke@0 171 char* bottom() const { return ((char*) this) + aligned_overhead_size(); }
duke@0 172 char* top() const { return bottom() + _len; }
duke@0 173 bool contains(char* p) const { return bottom() <= p && p <= top(); }
duke@0 174
duke@0 175 // Start the chunk_pool cleaner task
duke@0 176 static void start_chunk_pool_cleaner_task();
bobv@1601 177
bobv@1601 178 static void clean_chunk_pool();
duke@0 179 };
duke@0 180
duke@0 181 //------------------------------Arena------------------------------------------
duke@0 182 // Fast allocation of memory
duke@0 183 class Arena: public CHeapObj {
duke@0 184 protected:
duke@0 185 friend class ResourceMark;
duke@0 186 friend class HandleMark;
duke@0 187 friend class NoHandleMark;
duke@0 188 Chunk *_first; // First chunk
duke@0 189 Chunk *_chunk; // current chunk
duke@0 190 char *_hwm, *_max; // High water mark and max in current chunk
duke@0 191 void* grow(size_t x); // Get a new Chunk of at least size x
duke@0 192 NOT_PRODUCT(size_t _size_in_bytes;) // Size of arena (used for memory usage tracing)
duke@0 193 NOT_PRODUCT(static size_t _bytes_allocated;) // total #bytes allocated since start
duke@0 194 friend class AllocStats;
duke@0 195 debug_only(void* malloc(size_t size);)
duke@0 196 debug_only(void* internal_malloc_4(size_t x);)
duke@0 197 public:
duke@0 198 Arena();
duke@0 199 Arena(size_t init_size);
duke@0 200 Arena(Arena *old);
duke@0 201 ~Arena();
duke@0 202 void destruct_contents();
duke@0 203 char* hwm() const { return _hwm; }
duke@0 204
duke@0 205 // Fast allocate in the arena. Common case is: pointer test + increment.
duke@0 206 void* Amalloc(size_t x) {
duke@0 207 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
duke@0 208 x = ARENA_ALIGN(x);
duke@0 209 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 210 NOT_PRODUCT(_bytes_allocated += x);
duke@0 211 if (_hwm + x > _max) {
duke@0 212 return grow(x);
duke@0 213 } else {
duke@0 214 char *old = _hwm;
duke@0 215 _hwm += x;
duke@0 216 return old;
duke@0 217 }
duke@0 218 }
duke@0 219 // Further assume size is padded out to words
duke@0 220 void *Amalloc_4(size_t x) {
duke@0 221 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
duke@0 222 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 223 NOT_PRODUCT(_bytes_allocated += x);
duke@0 224 if (_hwm + x > _max) {
duke@0 225 return grow(x);
duke@0 226 } else {
duke@0 227 char *old = _hwm;
duke@0 228 _hwm += x;
duke@0 229 return old;
duke@0 230 }
duke@0 231 }
duke@0 232
duke@0 233 // Allocate with 'double' alignment. It is 8 bytes on sparc.
duke@0 234 // In other cases Amalloc_D() should be the same as Amalloc_4().
duke@0 235 void* Amalloc_D(size_t x) {
duke@0 236 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
duke@0 237 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 238 #if defined(SPARC) && !defined(_LP64)
duke@0 239 #define DALIGN_M1 7
duke@0 240 size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
duke@0 241 x += delta;
duke@0 242 #endif
duke@0 243 NOT_PRODUCT(_bytes_allocated += x);
duke@0 244 if (_hwm + x > _max) {
duke@0 245 return grow(x); // grow() returns a result aligned >= 8 bytes.
duke@0 246 } else {
duke@0 247 char *old = _hwm;
duke@0 248 _hwm += x;
duke@0 249 #if defined(SPARC) && !defined(_LP64)
duke@0 250 old += delta; // align to 8-bytes
duke@0 251 #endif
duke@0 252 return old;
duke@0 253 }
duke@0 254 }
duke@0 255
duke@0 256 // Fast delete in area. Common case is: NOP (except for storage reclaimed)
duke@0 257 void Afree(void *ptr, size_t size) {
duke@0 258 #ifdef ASSERT
duke@0 259 if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
duke@0 260 if (UseMallocOnly) return;
duke@0 261 #endif
duke@0 262 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
duke@0 263 }
duke@0 264
duke@0 265 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
duke@0 266
duke@0 267 // Move contents of this arena into an empty arena
duke@0 268 Arena *move_contents(Arena *empty_arena);
duke@0 269
duke@0 270 // Determine if pointer belongs to this Arena or not.
duke@0 271 bool contains( const void *ptr ) const;
duke@0 272
duke@0 273 // Total of all chunks in use (not thread-safe)
duke@0 274 size_t used() const;
duke@0 275
duke@0 276 // Total # of bytes used
duke@0 277 size_t size_in_bytes() const NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0;
duke@0 278 void set_size_in_bytes(size_t size) NOT_PRODUCT({ _size_in_bytes = size; }) PRODUCT_RETURN;
duke@0 279 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
duke@0 280 static void free_all(char** start, char** end) PRODUCT_RETURN;
duke@0 281
duke@0 282 private:
duke@0 283 // Reset this Arena to empty, access will trigger grow if necessary
duke@0 284 void reset(void) {
duke@0 285 _first = _chunk = NULL;
duke@0 286 _hwm = _max = NULL;
duke@0 287 }
duke@0 288 };
duke@0 289
duke@0 290 // One of the following macros must be used when allocating
duke@0 291 // an array or object from an arena
duke@0 292 #define NEW_ARENA_ARRAY(arena, type, size)\
duke@0 293 (type*) arena->Amalloc((size) * sizeof(type))
duke@0 294
duke@0 295 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)\
duke@0 296 (type*) arena->Arealloc((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
duke@0 297
duke@0 298 #define FREE_ARENA_ARRAY(arena, type, old, size)\
duke@0 299 arena->Afree((char*)(old), (size) * sizeof(type))
duke@0 300
duke@0 301 #define NEW_ARENA_OBJ(arena, type)\
duke@0 302 NEW_ARENA_ARRAY(arena, type, 1)
duke@0 303
duke@0 304
duke@0 305 //%note allocation_1
duke@0 306 extern char* resource_allocate_bytes(size_t size);
duke@0 307 extern char* resource_allocate_bytes(Thread* thread, size_t size);
duke@0 308 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size);
duke@0 309 extern void resource_free_bytes( char *old, size_t size );
duke@0 310
duke@0 311 //----------------------------------------------------------------------
duke@0 312 // Base class for objects allocated in the resource area per default.
duke@0 313 // Optionally, objects may be allocated on the C heap with
duke@0 314 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
duke@0 315 // ResourceObj's can be allocated within other objects, but don't use
duke@0 316 // new or delete (allocation_type is unknown). If new is used to allocate,
duke@0 317 // use delete to deallocate.
duke@0 318 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
duke@0 319 public:
duke@0 320 enum allocation_type { UNKNOWN = 0, C_HEAP, RESOURCE_AREA, ARENA };
duke@0 321 #ifdef ASSERT
duke@0 322 private:
duke@0 323 allocation_type _allocation;
duke@0 324 public:
duke@0 325 bool allocated_on_C_heap() { return _allocation == C_HEAP; }
duke@0 326 #endif // ASSERT
duke@0 327
duke@0 328 public:
duke@0 329 void* operator new(size_t size, allocation_type type);
duke@0 330 void* operator new(size_t size, Arena *arena) {
duke@0 331 address res = (address)arena->Amalloc(size);
duke@0 332 // Set allocation type in the resource object
duke@0 333 DEBUG_ONLY(((ResourceObj *)res)->_allocation = ARENA;)
duke@0 334 return res;
duke@0 335 }
duke@0 336 void* operator new(size_t size) {
duke@0 337 address res = (address)resource_allocate_bytes(size);
duke@0 338 // Set allocation type in the resource object
duke@0 339 DEBUG_ONLY(((ResourceObj *)res)->_allocation = RESOURCE_AREA;)
duke@0 340 return res;
duke@0 341 }
ysr@342 342 void* operator new(size_t size, void* where, allocation_type type) {
ysr@342 343 void* res = where;
ysr@342 344 // Set allocation type in the resource object
ysr@342 345 DEBUG_ONLY(((ResourceObj *)res)->_allocation = type;)
ysr@342 346 return res;
ysr@342 347 }
duke@0 348 void operator delete(void* p);
duke@0 349 };
duke@0 350
duke@0 351 // One of the following macros must be used when allocating an array
duke@0 352 // or object to determine whether it should reside in the C heap on in
duke@0 353 // the resource area.
duke@0 354
duke@0 355 #define NEW_RESOURCE_ARRAY(type, size)\
duke@0 356 (type*) resource_allocate_bytes((size) * sizeof(type))
duke@0 357
duke@0 358 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
duke@0 359 (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
duke@0 360
duke@0 361 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
duke@0 362 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
duke@0 363
duke@0 364 #define FREE_RESOURCE_ARRAY(type, old, size)\
duke@0 365 resource_free_bytes((char*)(old), (size) * sizeof(type))
duke@0 366
duke@0 367 #define FREE_FAST(old)\
duke@0 368 /* nop */
duke@0 369
duke@0 370 #define NEW_RESOURCE_OBJ(type)\
duke@0 371 NEW_RESOURCE_ARRAY(type, 1)
duke@0 372
duke@0 373 #define NEW_C_HEAP_ARRAY(type, size)\
duke@0 374 (type*) (AllocateHeap((size) * sizeof(type), XSTR(type) " in " __FILE__))
duke@0 375
duke@0 376 #define REALLOC_C_HEAP_ARRAY(type, old, size)\
duke@0 377 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), XSTR(type) " in " __FILE__))
duke@0 378
duke@0 379 #define FREE_C_HEAP_ARRAY(type,old) \
duke@0 380 FreeHeap((char*)(old))
duke@0 381
duke@0 382 #define NEW_C_HEAP_OBJ(type)\
duke@0 383 NEW_C_HEAP_ARRAY(type, 1)
duke@0 384
duke@0 385 extern bool warn_new_operator;
duke@0 386
duke@0 387 // for statistics
duke@0 388 #ifndef PRODUCT
duke@0 389 class AllocStats : StackObj {
duke@0 390 int start_mallocs, start_frees;
duke@0 391 size_t start_malloc_bytes, start_res_bytes;
duke@0 392 public:
duke@0 393 AllocStats();
duke@0 394
duke@0 395 int num_mallocs(); // since creation of receiver
duke@0 396 size_t alloc_bytes();
duke@0 397 size_t resource_bytes();
duke@0 398 int num_frees();
duke@0 399 void print();
duke@0 400 };
duke@0 401 #endif
duke@0 402
duke@0 403
duke@0 404 //------------------------------ReallocMark---------------------------------
duke@0 405 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
duke@0 406 // ReallocMark, which is declared in the same scope as the reallocated
duke@0 407 // pointer. Any operation that could __potentially__ cause a reallocation
duke@0 408 // should check the ReallocMark.
duke@0 409 class ReallocMark: public StackObj {
duke@0 410 protected:
duke@0 411 NOT_PRODUCT(int _nesting;)
duke@0 412
duke@0 413 public:
duke@0 414 ReallocMark() PRODUCT_RETURN;
duke@0 415 void check() PRODUCT_RETURN;
duke@0 416 };