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