src/share/vm/memory/allocation.hpp

changeset 3900
d2a62e0f25eb
parent 3138
f6f3bb0ee072
child 4037
da91efe96a93
equal deleted inserted replaced
3877:74533f63b116 3900:d2a62e0f25eb
1 /* 1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
38 38
39 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1) 39 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
40 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1)) 40 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
41 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK) 41 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
42 42
43
44 // noinline attribute
45 #ifdef _WINDOWS
46 #define _NOINLINE_ __declspec(noinline)
47 #else
48 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
49 #define _NOINLINE_
50 #else
51 #define _NOINLINE_ __attribute__ ((noinline))
52 #endif
53 #endif
54
43 // All classes in the virtual machine must be subclassed 55 // All classes in the virtual machine must be subclassed
44 // by one of the following allocation classes: 56 // by one of the following allocation classes:
45 // 57 //
46 // For objects allocated in the resource area (see resourceArea.hpp). 58 // For objects allocated in the resource area (see resourceArea.hpp).
47 // - ResourceObj 59 // - ResourceObj
96 virtual void print_on(outputStream* st) const; 108 virtual void print_on(outputStream* st) const;
97 virtual void print_value_on(outputStream* st) const; 109 virtual void print_value_on(outputStream* st) const;
98 }; 110 };
99 #endif 111 #endif
100 112
101 class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 113
102 public: 114 /*
103 void* operator new(size_t size); 115 * MemoryType bitmap layout:
104 void* operator new (size_t size, const std::nothrow_t& nothrow_constant); 116 * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 |
117 * | memory type | object | reserved |
118 * | | type | |
119 */
120 enum MemoryType {
121 // Memory type by sub systems. It occupies lower byte.
122 mtNone = 0x0000, // undefined
123 mtClass = 0x0100, // memory class for Java classes
124 mtThread = 0x0200, // memory for thread objects
125 mtThreadStack = 0x0300,
126 mtCode = 0x0400, // memory for generated code
127 mtGC = 0x0500, // memory for GC
128 mtCompiler = 0x0600, // memory for compiler
129 mtInternal = 0x0700, // memory used by VM, but does not belong to
130 // any of above categories, and not used for
131 // native memory tracking
132 mtOther = 0x0800, // memory not used by VM
133 mtSymbol = 0x0900, // symbol
134 mtNMT = 0x0A00, // memory used by native memory tracking
135 mtChunk = 0x0B00, // chunk that holds content of arenas
136 mtJavaHeap = 0x0C00, // Java heap
137 mtDontTrack = 0x0D00, // memory we donot or cannot track
138 mt_number_of_types = 0x000C, // number of memory types
139 mt_masks = 0x7F00,
140
141 // object type mask
142 otArena = 0x0010, // an arena object
143 otNMTRecorder = 0x0020, // memory recorder object
144 ot_masks = 0x00F0
145 };
146
147 #define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type)
148 #define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone)
149 #define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks)
150
151 #define IS_ARENA_OBJ(flags) ((flags & ot_masks) == otArena)
152 #define IS_NMT_RECORDER(flags) ((flags & ot_masks) == otNMTRecorder)
153 #define NMT_CAN_TRACK(flags) (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack)))
154
155 typedef unsigned short MEMFLAGS;
156
157 extern bool NMT_track_callsite;
158
159 // debug build does not inline
160 #if defined(_DEBUG_)
161 #define CURRENT_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0)
162 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
163 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0)
164 #else
165 #define CURRENT_PC (NMT_track_callsite? os::get_caller_pc(0) : 0)
166 #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0)
167 #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
168 #endif
169
170
171
172 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
173 public:
174 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0);
175 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
176 address caller_pc = 0);
177
105 void operator delete(void* p); 178 void operator delete(void* p);
106 void* new_array(size_t size);
107 }; 179 };
108 180
109 // Base class for objects allocated on the stack only. 181 // Base class for objects allocated on the stack only.
110 // Calling new or delete will result in fatal error. 182 // Calling new or delete will result in fatal error.
111 183
148 }; 220 };
149 221
150 222
151 //------------------------------Chunk------------------------------------------ 223 //------------------------------Chunk------------------------------------------
152 // Linked list of raw memory chunks 224 // Linked list of raw memory chunks
153 class Chunk: public CHeapObj { 225 class Chunk: CHeapObj<mtChunk> {
154 friend class VMStructs; 226 friend class VMStructs;
155 227
156 protected: 228 protected:
157 Chunk* _next; // Next Chunk in list 229 Chunk* _next; // Next Chunk in list
158 const size_t _len; // Size of this Chunk 230 const size_t _len; // Size of this Chunk
195 static void clean_chunk_pool(); 267 static void clean_chunk_pool();
196 }; 268 };
197 269
198 //------------------------------Arena------------------------------------------ 270 //------------------------------Arena------------------------------------------
199 // Fast allocation of memory 271 // Fast allocation of memory
200 class Arena: public CHeapObj { 272 class Arena : public CHeapObj<mtNone|otArena> {
201 protected: 273 protected:
202 friend class ResourceMark; 274 friend class ResourceMark;
203 friend class HandleMark; 275 friend class HandleMark;
204 friend class NoHandleMark; 276 friend class NoHandleMark;
205 friend class VMStructs; 277 friend class VMStructs;
206 278
207 Chunk *_first; // First chunk 279 Chunk *_first; // First chunk
208 Chunk *_chunk; // current chunk 280 Chunk *_chunk; // current chunk
209 char *_hwm, *_max; // High water mark and max in current chunk 281 char *_hwm, *_max; // High water mark and max in current chunk
210 void* grow(size_t x); // Get a new Chunk of at least size x 282 void* grow(size_t x); // Get a new Chunk of at least size x
211 NOT_PRODUCT(size_t _size_in_bytes;) // Size of arena (used for memory usage tracing) 283 size_t _size_in_bytes; // Size of arena (used for native memory tracking)
284
212 NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start 285 NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start
213 friend class AllocStats; 286 friend class AllocStats;
214 debug_only(void* malloc(size_t size);) 287 debug_only(void* malloc(size_t size);)
215 debug_only(void* internal_malloc_4(size_t x);) 288 debug_only(void* internal_malloc_4(size_t x);)
216 NOT_PRODUCT(void inc_bytes_allocated(size_t x);) 289 NOT_PRODUCT(void inc_bytes_allocated(size_t x);)
228 Arena(size_t init_size); 301 Arena(size_t init_size);
229 Arena(Arena *old); 302 Arena(Arena *old);
230 ~Arena(); 303 ~Arena();
231 void destruct_contents(); 304 void destruct_contents();
232 char* hwm() const { return _hwm; } 305 char* hwm() const { return _hwm; }
306
307 // new operators
308 void* operator new (size_t size);
309 void* operator new (size_t size, const std::nothrow_t& nothrow_constant);
310
311 // dynamic memory type tagging
312 void* operator new(size_t size, MEMFLAGS flags);
313 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags);
314 void operator delete(void* p);
233 315
234 // Fast allocate in the arena. Common case is: pointer test + increment. 316 // Fast allocate in the arena. Common case is: pointer test + increment.
235 void* Amalloc(size_t x) { 317 void* Amalloc(size_t x) {
236 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2"); 318 assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
237 x = ARENA_ALIGN(x); 319 x = ARENA_ALIGN(x);
304 386
305 // Total of all chunks in use (not thread-safe) 387 // Total of all chunks in use (not thread-safe)
306 size_t used() const; 388 size_t used() const;
307 389
308 // Total # of bytes used 390 // Total # of bytes used
309 size_t size_in_bytes() const NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0; 391 size_t size_in_bytes() const { return _size_in_bytes; };
310 void set_size_in_bytes(size_t size) NOT_PRODUCT({ _size_in_bytes = size; }) PRODUCT_RETURN; 392 void set_size_in_bytes(size_t size);
393
311 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN; 394 static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
312 static void free_all(char** start, char** end) PRODUCT_RETURN; 395 static void free_all(char** start, char** end) PRODUCT_RETURN;
313 396
397 // how many arena instances
398 NOT_PRODUCT(static volatile jint _instance_count;)
314 private: 399 private:
315 // Reset this Arena to empty, access will trigger grow if necessary 400 // Reset this Arena to empty, access will trigger grow if necessary
316 void reset(void) { 401 void reset(void) {
317 _first = _chunk = NULL; 402 _first = _chunk = NULL;
318 _hwm = _max = NULL; 403 _hwm = _max = NULL;
404 set_size_in_bytes(0);
319 } 405 }
320 }; 406 };
321 407
322 // One of the following macros must be used when allocating 408 // One of the following macros must be used when allocating
323 // an array or object from an arena 409 // an array or object from an arena
371 ResourceObj& operator=(const ResourceObj& r); // default copy assignment 457 ResourceObj& operator=(const ResourceObj& r); // default copy assignment
372 ~ResourceObj(); 458 ~ResourceObj();
373 #endif // ASSERT 459 #endif // ASSERT
374 460
375 public: 461 public:
376 void* operator new(size_t size, allocation_type type); 462 void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
377 void* operator new(size_t size, Arena *arena) { 463 void* operator new(size_t size, Arena *arena) {
378 address res = (address)arena->Amalloc(size); 464 address res = (address)arena->Amalloc(size);
379 DEBUG_ONLY(set_allocation_type(res, ARENA);) 465 DEBUG_ONLY(set_allocation_type(res, ARENA);)
380 return res; 466 return res;
381 } 467 }
407 /* nop */ 493 /* nop */
408 494
409 #define NEW_RESOURCE_OBJ(type)\ 495 #define NEW_RESOURCE_OBJ(type)\
410 NEW_RESOURCE_ARRAY(type, 1) 496 NEW_RESOURCE_ARRAY(type, 1)
411 497
412 #define NEW_C_HEAP_ARRAY(type, size)\ 498 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
413 (type*) (AllocateHeap((size) * sizeof(type), XSTR(type) " in " __FILE__)) 499 (type*) (AllocateHeap((size) * sizeof(type), memflags))
414 500
415 #define REALLOC_C_HEAP_ARRAY(type, old, size)\ 501 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
416 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), XSTR(type) " in " __FILE__)) 502 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
417 503
418 #define FREE_C_HEAP_ARRAY(type,old) \ 504 #define FREE_C_HEAP_ARRAY(type,old,memflags) \
419 FreeHeap((char*)(old)) 505 FreeHeap((char*)(old), memflags)
420 506
421 #define NEW_C_HEAP_OBJ(type)\ 507 #define NEW_C_HEAP_OBJ(type, memflags)\
422 NEW_C_HEAP_ARRAY(type, 1) 508 NEW_C_HEAP_ARRAY(type, 1, memflags)
509
510
511 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
512 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
513
514 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
515 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
516
517 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\
518 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc)
519
423 520
424 extern bool warn_new_operator; 521 extern bool warn_new_operator;
425 522
426 // for statistics 523 // for statistics
427 #ifndef PRODUCT 524 #ifndef PRODUCT

mercurial