annotate src/share/vm/memory/allocation.hpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 37f87013dfd8
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1997-2005 Sun Microsystems, Inc. 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 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any 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();
duke@0 177 };
duke@0 178
duke@0 179
duke@0 180 //------------------------------Arena------------------------------------------
duke@0 181 // Fast allocation of memory
duke@0 182 class Arena: public CHeapObj {
duke@0 183 protected:
duke@0 184 friend class ResourceMark;
duke@0 185 friend class HandleMark;
duke@0 186 friend class NoHandleMark;
duke@0 187 Chunk *_first; // First chunk
duke@0 188 Chunk *_chunk; // current chunk
duke@0 189 char *_hwm, *_max; // High water mark and max in current chunk
duke@0 190 void* grow(size_t x); // Get a new Chunk of at least size x
duke@0 191 NOT_PRODUCT(size_t _size_in_bytes;) // Size of arena (used for memory usage tracing)
duke@0 192 NOT_PRODUCT(static size_t _bytes_allocated;) // total #bytes allocated since start
duke@0 193 friend class AllocStats;
duke@0 194 debug_only(void* malloc(size_t size);)
duke@0 195 debug_only(void* internal_malloc_4(size_t x);)
duke@0 196 public:
duke@0 197 Arena();
duke@0 198 Arena(size_t init_size);
duke@0 199 Arena(Arena *old);
duke@0 200 ~Arena();
duke@0 201 void destruct_contents();
duke@0 202 char* hwm() const { return _hwm; }
duke@0 203
duke@0 204 // Fast allocate in the arena. Common case is: pointer test + increment.
duke@0 205 void* Amalloc(size_t x) {
duke@0 206 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
duke@0 207 x = ARENA_ALIGN(x);
duke@0 208 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 209 NOT_PRODUCT(_bytes_allocated += x);
duke@0 210 if (_hwm + x > _max) {
duke@0 211 return grow(x);
duke@0 212 } else {
duke@0 213 char *old = _hwm;
duke@0 214 _hwm += x;
duke@0 215 return old;
duke@0 216 }
duke@0 217 }
duke@0 218 // Further assume size is padded out to words
duke@0 219 void *Amalloc_4(size_t x) {
duke@0 220 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
duke@0 221 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 222 NOT_PRODUCT(_bytes_allocated += x);
duke@0 223 if (_hwm + x > _max) {
duke@0 224 return grow(x);
duke@0 225 } else {
duke@0 226 char *old = _hwm;
duke@0 227 _hwm += x;
duke@0 228 return old;
duke@0 229 }
duke@0 230 }
duke@0 231
duke@0 232 // Allocate with 'double' alignment. It is 8 bytes on sparc.
duke@0 233 // In other cases Amalloc_D() should be the same as Amalloc_4().
duke@0 234 void* Amalloc_D(size_t x) {
duke@0 235 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
duke@0 236 debug_only(if (UseMallocOnly) return malloc(x);)
duke@0 237 #if defined(SPARC) && !defined(_LP64)
duke@0 238 #define DALIGN_M1 7
duke@0 239 size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
duke@0 240 x += delta;
duke@0 241 #endif
duke@0 242 NOT_PRODUCT(_bytes_allocated += x);
duke@0 243 if (_hwm + x > _max) {
duke@0 244 return grow(x); // grow() returns a result aligned >= 8 bytes.
duke@0 245 } else {
duke@0 246 char *old = _hwm;
duke@0 247 _hwm += x;
duke@0 248 #if defined(SPARC) && !defined(_LP64)
duke@0 249 old += delta; // align to 8-bytes
duke@0 250 #endif
duke@0 251 return old;
duke@0 252 }
duke@0 253 }
duke@0 254
duke@0 255 // Fast delete in area. Common case is: NOP (except for storage reclaimed)
duke@0 256 void Afree(void *ptr, size_t size) {
duke@0 257 #ifdef ASSERT
duke@0 258 if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
duke@0 259 if (UseMallocOnly) return;
duke@0 260 #endif
duke@0 261 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
duke@0 262 }
duke@0 263
duke@0 264 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
duke@0 265
duke@0 266 // Move contents of this arena into an empty arena
duke@0 267 Arena *move_contents(Arena *empty_arena);
duke@0 268
duke@0 269 // Determine if pointer belongs to this Arena or not.
duke@0 270 bool contains( const void *ptr ) const;
duke@0 271
duke@0 272 // Total of all chunks in use (not thread-safe)
duke@0 273 size_t used() const;
duke@0 274
duke@0 275 // Total # of bytes used
duke@0 276 size_t size_in_bytes() const NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0;
duke@0 277 void set_size_in_bytes(size_t size) NOT_PRODUCT({ _size_in_bytes = size; }) PRODUCT_RETURN;
duke@0 278 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
duke@0 279 static void free_all(char** start, char** end) PRODUCT_RETURN;
duke@0 280
duke@0 281 private:
duke@0 282 // Reset this Arena to empty, access will trigger grow if necessary
duke@0 283 void reset(void) {
duke@0 284 _first = _chunk = NULL;
duke@0 285 _hwm = _max = NULL;
duke@0 286 }
duke@0 287 };
duke@0 288
duke@0 289 // One of the following macros must be used when allocating
duke@0 290 // an array or object from an arena
duke@0 291 #define NEW_ARENA_ARRAY(arena, type, size)\
duke@0 292 (type*) arena->Amalloc((size) * sizeof(type))
duke@0 293
duke@0 294 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)\
duke@0 295 (type*) arena->Arealloc((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
duke@0 296
duke@0 297 #define FREE_ARENA_ARRAY(arena, type, old, size)\
duke@0 298 arena->Afree((char*)(old), (size) * sizeof(type))
duke@0 299
duke@0 300 #define NEW_ARENA_OBJ(arena, type)\
duke@0 301 NEW_ARENA_ARRAY(arena, type, 1)
duke@0 302
duke@0 303
duke@0 304 //%note allocation_1
duke@0 305 extern char* resource_allocate_bytes(size_t size);
duke@0 306 extern char* resource_allocate_bytes(Thread* thread, size_t size);
duke@0 307 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size);
duke@0 308 extern void resource_free_bytes( char *old, size_t size );
duke@0 309
duke@0 310 //----------------------------------------------------------------------
duke@0 311 // Base class for objects allocated in the resource area per default.
duke@0 312 // Optionally, objects may be allocated on the C heap with
duke@0 313 // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
duke@0 314 // ResourceObj's can be allocated within other objects, but don't use
duke@0 315 // new or delete (allocation_type is unknown). If new is used to allocate,
duke@0 316 // use delete to deallocate.
duke@0 317 class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
duke@0 318 public:
duke@0 319 enum allocation_type { UNKNOWN = 0, C_HEAP, RESOURCE_AREA, ARENA };
duke@0 320 #ifdef ASSERT
duke@0 321 private:
duke@0 322 allocation_type _allocation;
duke@0 323 public:
duke@0 324 bool allocated_on_C_heap() { return _allocation == C_HEAP; }
duke@0 325 #endif // ASSERT
duke@0 326
duke@0 327 public:
duke@0 328 void* operator new(size_t size, allocation_type type);
duke@0 329 void* operator new(size_t size, Arena *arena) {
duke@0 330 address res = (address)arena->Amalloc(size);
duke@0 331 // Set allocation type in the resource object
duke@0 332 DEBUG_ONLY(((ResourceObj *)res)->_allocation = ARENA;)
duke@0 333 return res;
duke@0 334 }
duke@0 335 void* operator new(size_t size) {
duke@0 336 address res = (address)resource_allocate_bytes(size);
duke@0 337 // Set allocation type in the resource object
duke@0 338 DEBUG_ONLY(((ResourceObj *)res)->_allocation = RESOURCE_AREA;)
duke@0 339 return res;
duke@0 340 }
duke@0 341 void operator delete(void* p);
duke@0 342 };
duke@0 343
duke@0 344 // One of the following macros must be used when allocating an array
duke@0 345 // or object to determine whether it should reside in the C heap on in
duke@0 346 // the resource area.
duke@0 347
duke@0 348 #define NEW_RESOURCE_ARRAY(type, size)\
duke@0 349 (type*) resource_allocate_bytes((size) * sizeof(type))
duke@0 350
duke@0 351 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
duke@0 352 (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
duke@0 353
duke@0 354 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
duke@0 355 (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
duke@0 356
duke@0 357 #define FREE_RESOURCE_ARRAY(type, old, size)\
duke@0 358 resource_free_bytes((char*)(old), (size) * sizeof(type))
duke@0 359
duke@0 360 #define FREE_FAST(old)\
duke@0 361 /* nop */
duke@0 362
duke@0 363 #define NEW_RESOURCE_OBJ(type)\
duke@0 364 NEW_RESOURCE_ARRAY(type, 1)
duke@0 365
duke@0 366 #define NEW_C_HEAP_ARRAY(type, size)\
duke@0 367 (type*) (AllocateHeap((size) * sizeof(type), XSTR(type) " in " __FILE__))
duke@0 368
duke@0 369 #define REALLOC_C_HEAP_ARRAY(type, old, size)\
duke@0 370 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), XSTR(type) " in " __FILE__))
duke@0 371
duke@0 372 #define FREE_C_HEAP_ARRAY(type,old) \
duke@0 373 FreeHeap((char*)(old))
duke@0 374
duke@0 375 #define NEW_C_HEAP_OBJ(type)\
duke@0 376 NEW_C_HEAP_ARRAY(type, 1)
duke@0 377
duke@0 378 extern bool warn_new_operator;
duke@0 379
duke@0 380 // for statistics
duke@0 381 #ifndef PRODUCT
duke@0 382 class AllocStats : StackObj {
duke@0 383 int start_mallocs, start_frees;
duke@0 384 size_t start_malloc_bytes, start_res_bytes;
duke@0 385 public:
duke@0 386 AllocStats();
duke@0 387
duke@0 388 int num_mallocs(); // since creation of receiver
duke@0 389 size_t alloc_bytes();
duke@0 390 size_t resource_bytes();
duke@0 391 int num_frees();
duke@0 392 void print();
duke@0 393 };
duke@0 394 #endif
duke@0 395
duke@0 396
duke@0 397 //------------------------------ReallocMark---------------------------------
duke@0 398 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
duke@0 399 // ReallocMark, which is declared in the same scope as the reallocated
duke@0 400 // pointer. Any operation that could __potentially__ cause a reallocation
duke@0 401 // should check the ReallocMark.
duke@0 402 class ReallocMark: public StackObj {
duke@0 403 protected:
duke@0 404 NOT_PRODUCT(int _nesting;)
duke@0 405
duke@0 406 public:
duke@0 407 ReallocMark() PRODUCT_RETURN;
duke@0 408 void check() PRODUCT_RETURN;
duke@0 409 };