Thu, 28 Jun 2012 17:03:16 -0400
6995781: Native Memory Tracking (Phase 1)
7151532: DCmd for hotspot native memory tracking
Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd
Reviewed-by: acorn, coleenp, fparain
duke@435 | 1 | /* |
zgu@3900 | 2 | * Copyright (c) 1997, 2012, 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" |
stefank@2314 | 30 | #ifdef COMPILER1 |
stefank@2314 | 31 | #include "c1/c1_globals.hpp" |
stefank@2314 | 32 | #endif |
stefank@2314 | 33 | #ifdef COMPILER2 |
stefank@2314 | 34 | #include "opto/c2_globals.hpp" |
stefank@2314 | 35 | #endif |
stefank@2314 | 36 | |
zgu@2834 | 37 | #include <new> |
zgu@2834 | 38 | |
duke@435 | 39 | #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1) |
duke@435 | 40 | #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1)) |
duke@435 | 41 | #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK) |
duke@435 | 42 | |
zgu@3900 | 43 | |
zgu@3900 | 44 | // noinline attribute |
zgu@3900 | 45 | #ifdef _WINDOWS |
zgu@3900 | 46 | #define _NOINLINE_ __declspec(noinline) |
zgu@3900 | 47 | #else |
zgu@3900 | 48 | #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute |
zgu@3900 | 49 | #define _NOINLINE_ |
zgu@3900 | 50 | #else |
zgu@3900 | 51 | #define _NOINLINE_ __attribute__ ((noinline)) |
zgu@3900 | 52 | #endif |
zgu@3900 | 53 | #endif |
zgu@3900 | 54 | |
duke@435 | 55 | // All classes in the virtual machine must be subclassed |
duke@435 | 56 | // by one of the following allocation classes: |
duke@435 | 57 | // |
duke@435 | 58 | // For objects allocated in the resource area (see resourceArea.hpp). |
duke@435 | 59 | // - ResourceObj |
duke@435 | 60 | // |
duke@435 | 61 | // For objects allocated in the C-heap (managed by: free & malloc). |
duke@435 | 62 | // - CHeapObj |
duke@435 | 63 | // |
duke@435 | 64 | // For objects allocated on the stack. |
duke@435 | 65 | // - StackObj |
duke@435 | 66 | // |
duke@435 | 67 | // For embedded objects. |
duke@435 | 68 | // - ValueObj |
duke@435 | 69 | // |
duke@435 | 70 | // For classes used as name spaces. |
duke@435 | 71 | // - AllStatic |
duke@435 | 72 | // |
duke@435 | 73 | // The printable subclasses are used for debugging and define virtual |
duke@435 | 74 | // member functions for printing. Classes that avoid allocating the |
duke@435 | 75 | // vtbl entries in the objects should therefore not be the printable |
duke@435 | 76 | // subclasses. |
duke@435 | 77 | // |
duke@435 | 78 | // The following macros and function should be used to allocate memory |
duke@435 | 79 | // directly in the resource area or in the C-heap: |
duke@435 | 80 | // |
duke@435 | 81 | // NEW_RESOURCE_ARRAY(type,size) |
duke@435 | 82 | // NEW_RESOURCE_OBJ(type) |
duke@435 | 83 | // NEW_C_HEAP_ARRAY(type,size) |
duke@435 | 84 | // NEW_C_HEAP_OBJ(type) |
duke@435 | 85 | // char* AllocateHeap(size_t size, const char* name); |
duke@435 | 86 | // void FreeHeap(void* p); |
duke@435 | 87 | // |
duke@435 | 88 | // C-heap allocation can be traced using +PrintHeapAllocation. |
duke@435 | 89 | // malloc and free should therefore never called directly. |
duke@435 | 90 | |
duke@435 | 91 | // Base class for objects allocated in the C-heap. |
duke@435 | 92 | |
duke@435 | 93 | // In non product mode we introduce a super class for all allocation classes |
duke@435 | 94 | // that supports printing. |
duke@435 | 95 | // We avoid the superclass in product mode since some C++ compilers add |
duke@435 | 96 | // a word overhead for empty super classes. |
duke@435 | 97 | |
duke@435 | 98 | #ifdef PRODUCT |
duke@435 | 99 | #define ALLOCATION_SUPER_CLASS_SPEC |
duke@435 | 100 | #else |
duke@435 | 101 | #define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj |
duke@435 | 102 | class AllocatedObj { |
duke@435 | 103 | public: |
duke@435 | 104 | // Printing support |
duke@435 | 105 | void print() const; |
duke@435 | 106 | void print_value() const; |
duke@435 | 107 | |
duke@435 | 108 | virtual void print_on(outputStream* st) const; |
duke@435 | 109 | virtual void print_value_on(outputStream* st) const; |
duke@435 | 110 | }; |
duke@435 | 111 | #endif |
duke@435 | 112 | |
zgu@3900 | 113 | |
zgu@3900 | 114 | /* |
zgu@3900 | 115 | * MemoryType bitmap layout: |
zgu@3900 | 116 | * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 | |
zgu@3900 | 117 | * | memory type | object | reserved | |
zgu@3900 | 118 | * | | type | | |
zgu@3900 | 119 | */ |
zgu@3900 | 120 | enum MemoryType { |
zgu@3900 | 121 | // Memory type by sub systems. It occupies lower byte. |
zgu@3900 | 122 | mtNone = 0x0000, // undefined |
zgu@3900 | 123 | mtClass = 0x0100, // memory class for Java classes |
zgu@3900 | 124 | mtThread = 0x0200, // memory for thread objects |
zgu@3900 | 125 | mtThreadStack = 0x0300, |
zgu@3900 | 126 | mtCode = 0x0400, // memory for generated code |
zgu@3900 | 127 | mtGC = 0x0500, // memory for GC |
zgu@3900 | 128 | mtCompiler = 0x0600, // memory for compiler |
zgu@3900 | 129 | mtInternal = 0x0700, // memory used by VM, but does not belong to |
zgu@3900 | 130 | // any of above categories, and not used for |
zgu@3900 | 131 | // native memory tracking |
zgu@3900 | 132 | mtOther = 0x0800, // memory not used by VM |
zgu@3900 | 133 | mtSymbol = 0x0900, // symbol |
zgu@3900 | 134 | mtNMT = 0x0A00, // memory used by native memory tracking |
zgu@3900 | 135 | mtChunk = 0x0B00, // chunk that holds content of arenas |
zgu@3900 | 136 | mtJavaHeap = 0x0C00, // Java heap |
zgu@3900 | 137 | mtDontTrack = 0x0D00, // memory we donot or cannot track |
zgu@3900 | 138 | mt_number_of_types = 0x000C, // number of memory types |
zgu@3900 | 139 | mt_masks = 0x7F00, |
zgu@3900 | 140 | |
zgu@3900 | 141 | // object type mask |
zgu@3900 | 142 | otArena = 0x0010, // an arena object |
zgu@3900 | 143 | otNMTRecorder = 0x0020, // memory recorder object |
zgu@3900 | 144 | ot_masks = 0x00F0 |
zgu@3900 | 145 | }; |
zgu@3900 | 146 | |
zgu@3900 | 147 | #define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type) |
zgu@3900 | 148 | #define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone) |
zgu@3900 | 149 | #define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks) |
zgu@3900 | 150 | |
zgu@3900 | 151 | #define IS_ARENA_OBJ(flags) ((flags & ot_masks) == otArena) |
zgu@3900 | 152 | #define IS_NMT_RECORDER(flags) ((flags & ot_masks) == otNMTRecorder) |
zgu@3900 | 153 | #define NMT_CAN_TRACK(flags) (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack))) |
zgu@3900 | 154 | |
zgu@3900 | 155 | typedef unsigned short MEMFLAGS; |
zgu@3900 | 156 | |
zgu@3900 | 157 | extern bool NMT_track_callsite; |
zgu@3900 | 158 | |
zgu@3900 | 159 | // debug build does not inline |
zgu@3900 | 160 | #if defined(_DEBUG_) |
zgu@3900 | 161 | #define CURRENT_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0) |
zgu@3900 | 162 | #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) |
zgu@3900 | 163 | #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0) |
zgu@3900 | 164 | #else |
zgu@3900 | 165 | #define CURRENT_PC (NMT_track_callsite? os::get_caller_pc(0) : 0) |
zgu@3900 | 166 | #define CALLER_PC (NMT_track_callsite ? os::get_caller_pc(1) : 0) |
zgu@3900 | 167 | #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) |
zgu@3900 | 168 | #endif |
zgu@3900 | 169 | |
zgu@3900 | 170 | |
zgu@3900 | 171 | |
zgu@3900 | 172 | template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { |
duke@435 | 173 | public: |
zgu@3900 | 174 | _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); |
zgu@3900 | 175 | _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, |
zgu@3900 | 176 | address caller_pc = 0); |
zgu@3900 | 177 | |
duke@435 | 178 | void operator delete(void* p); |
duke@435 | 179 | }; |
duke@435 | 180 | |
duke@435 | 181 | // Base class for objects allocated on the stack only. |
duke@435 | 182 | // Calling new or delete will result in fatal error. |
duke@435 | 183 | |
duke@435 | 184 | class StackObj ALLOCATION_SUPER_CLASS_SPEC { |
duke@435 | 185 | public: |
duke@435 | 186 | void* operator new(size_t size); |
duke@435 | 187 | void operator delete(void* p); |
duke@435 | 188 | }; |
duke@435 | 189 | |
duke@435 | 190 | // Base class for objects used as value objects. |
duke@435 | 191 | // Calling new or delete will result in fatal error. |
duke@435 | 192 | // |
duke@435 | 193 | // Portability note: Certain compilers (e.g. gcc) will |
duke@435 | 194 | // always make classes bigger if it has a superclass, even |
duke@435 | 195 | // if the superclass does not have any virtual methods or |
duke@435 | 196 | // instance fields. The HotSpot implementation relies on this |
duke@435 | 197 | // not to happen. So never make a ValueObj class a direct subclass |
duke@435 | 198 | // of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g., |
duke@435 | 199 | // like this: |
duke@435 | 200 | // |
duke@435 | 201 | // class A VALUE_OBJ_CLASS_SPEC { |
duke@435 | 202 | // ... |
duke@435 | 203 | // } |
duke@435 | 204 | // |
duke@435 | 205 | // With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can |
duke@435 | 206 | // be defined as a an empty string "". |
duke@435 | 207 | // |
duke@435 | 208 | class _ValueObj { |
duke@435 | 209 | public: |
duke@435 | 210 | void* operator new(size_t size); |
duke@435 | 211 | void operator delete(void* p); |
duke@435 | 212 | }; |
duke@435 | 213 | |
duke@435 | 214 | // Base class for classes that constitute name spaces. |
duke@435 | 215 | |
duke@435 | 216 | class AllStatic { |
duke@435 | 217 | public: |
duke@435 | 218 | AllStatic() { ShouldNotCallThis(); } |
duke@435 | 219 | ~AllStatic() { ShouldNotCallThis(); } |
duke@435 | 220 | }; |
duke@435 | 221 | |
duke@435 | 222 | |
duke@435 | 223 | //------------------------------Chunk------------------------------------------ |
duke@435 | 224 | // Linked list of raw memory chunks |
zgu@3900 | 225 | class Chunk: CHeapObj<mtChunk> { |
never@3138 | 226 | friend class VMStructs; |
never@3138 | 227 | |
duke@435 | 228 | protected: |
duke@435 | 229 | Chunk* _next; // Next Chunk in list |
duke@435 | 230 | const size_t _len; // Size of this Chunk |
duke@435 | 231 | public: |
duke@435 | 232 | void* operator new(size_t size, size_t length); |
duke@435 | 233 | void operator delete(void* p); |
duke@435 | 234 | Chunk(size_t length); |
duke@435 | 235 | |
duke@435 | 236 | enum { |
duke@435 | 237 | // default sizes; make them slightly smaller than 2**k to guard against |
duke@435 | 238 | // buddy-system style malloc implementations |
duke@435 | 239 | #ifdef _LP64 |
duke@435 | 240 | slack = 40, // [RGV] Not sure if this is right, but make it |
duke@435 | 241 | // a multiple of 8. |
duke@435 | 242 | #else |
duke@435 | 243 | slack = 20, // suspected sizeof(Chunk) + internal malloc headers |
duke@435 | 244 | #endif |
duke@435 | 245 | |
duke@435 | 246 | init_size = 1*K - slack, // Size of first chunk |
duke@435 | 247 | medium_size= 10*K - slack, // Size of medium-sized chunk |
duke@435 | 248 | size = 32*K - slack, // Default size of an Arena chunk (following the first) |
duke@435 | 249 | non_pool_size = init_size + 32 // An initial size which is not one of above |
duke@435 | 250 | }; |
duke@435 | 251 | |
duke@435 | 252 | void chop(); // Chop this chunk |
duke@435 | 253 | void next_chop(); // Chop next chunk |
duke@435 | 254 | static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); } |
duke@435 | 255 | |
duke@435 | 256 | size_t length() const { return _len; } |
duke@435 | 257 | Chunk* next() const { return _next; } |
duke@435 | 258 | void set_next(Chunk* n) { _next = n; } |
duke@435 | 259 | // Boundaries of data area (possibly unused) |
duke@435 | 260 | char* bottom() const { return ((char*) this) + aligned_overhead_size(); } |
duke@435 | 261 | char* top() const { return bottom() + _len; } |
duke@435 | 262 | bool contains(char* p) const { return bottom() <= p && p <= top(); } |
duke@435 | 263 | |
duke@435 | 264 | // Start the chunk_pool cleaner task |
duke@435 | 265 | static void start_chunk_pool_cleaner_task(); |
bobv@2036 | 266 | |
bobv@2036 | 267 | static void clean_chunk_pool(); |
duke@435 | 268 | }; |
duke@435 | 269 | |
duke@435 | 270 | //------------------------------Arena------------------------------------------ |
duke@435 | 271 | // Fast allocation of memory |
zgu@3900 | 272 | class Arena : public CHeapObj<mtNone|otArena> { |
duke@435 | 273 | protected: |
duke@435 | 274 | friend class ResourceMark; |
duke@435 | 275 | friend class HandleMark; |
duke@435 | 276 | friend class NoHandleMark; |
never@3138 | 277 | friend class VMStructs; |
never@3138 | 278 | |
duke@435 | 279 | Chunk *_first; // First chunk |
duke@435 | 280 | Chunk *_chunk; // current chunk |
duke@435 | 281 | char *_hwm, *_max; // High water mark and max in current chunk |
duke@435 | 282 | void* grow(size_t x); // Get a new Chunk of at least size x |
zgu@3900 | 283 | size_t _size_in_bytes; // Size of arena (used for native memory tracking) |
zgu@3900 | 284 | |
kvn@2557 | 285 | NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start |
duke@435 | 286 | friend class AllocStats; |
duke@435 | 287 | debug_only(void* malloc(size_t size);) |
duke@435 | 288 | debug_only(void* internal_malloc_4(size_t x);) |
kvn@2557 | 289 | NOT_PRODUCT(void inc_bytes_allocated(size_t x);) |
kamg@2589 | 290 | |
kamg@2589 | 291 | void signal_out_of_memory(size_t request, const char* whence) const; |
kamg@2589 | 292 | |
kamg@2589 | 293 | void check_for_overflow(size_t request, const char* whence) const { |
kamg@2589 | 294 | if (UINTPTR_MAX - request < (uintptr_t)_hwm) { |
kamg@2589 | 295 | signal_out_of_memory(request, whence); |
kamg@2589 | 296 | } |
kamg@2589 | 297 | } |
kamg@2589 | 298 | |
duke@435 | 299 | public: |
duke@435 | 300 | Arena(); |
duke@435 | 301 | Arena(size_t init_size); |
duke@435 | 302 | Arena(Arena *old); |
duke@435 | 303 | ~Arena(); |
duke@435 | 304 | void destruct_contents(); |
duke@435 | 305 | char* hwm() const { return _hwm; } |
duke@435 | 306 | |
zgu@3900 | 307 | // new operators |
zgu@3900 | 308 | void* operator new (size_t size); |
zgu@3900 | 309 | void* operator new (size_t size, const std::nothrow_t& nothrow_constant); |
zgu@3900 | 310 | |
zgu@3900 | 311 | // dynamic memory type tagging |
zgu@3900 | 312 | void* operator new(size_t size, MEMFLAGS flags); |
zgu@3900 | 313 | void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags); |
zgu@3900 | 314 | void operator delete(void* p); |
zgu@3900 | 315 | |
duke@435 | 316 | // Fast allocate in the arena. Common case is: pointer test + increment. |
duke@435 | 317 | void* Amalloc(size_t x) { |
duke@435 | 318 | assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2"); |
duke@435 | 319 | x = ARENA_ALIGN(x); |
duke@435 | 320 | debug_only(if (UseMallocOnly) return malloc(x);) |
kamg@2589 | 321 | check_for_overflow(x, "Arena::Amalloc"); |
kvn@2557 | 322 | NOT_PRODUCT(inc_bytes_allocated(x);) |
duke@435 | 323 | if (_hwm + x > _max) { |
duke@435 | 324 | return grow(x); |
duke@435 | 325 | } else { |
duke@435 | 326 | char *old = _hwm; |
duke@435 | 327 | _hwm += x; |
duke@435 | 328 | return old; |
duke@435 | 329 | } |
duke@435 | 330 | } |
duke@435 | 331 | // Further assume size is padded out to words |
duke@435 | 332 | void *Amalloc_4(size_t x) { |
duke@435 | 333 | assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); |
duke@435 | 334 | debug_only(if (UseMallocOnly) return malloc(x);) |
kamg@2589 | 335 | check_for_overflow(x, "Arena::Amalloc_4"); |
kvn@2557 | 336 | NOT_PRODUCT(inc_bytes_allocated(x);) |
duke@435 | 337 | if (_hwm + x > _max) { |
duke@435 | 338 | return grow(x); |
duke@435 | 339 | } else { |
duke@435 | 340 | char *old = _hwm; |
duke@435 | 341 | _hwm += x; |
duke@435 | 342 | return old; |
duke@435 | 343 | } |
duke@435 | 344 | } |
duke@435 | 345 | |
duke@435 | 346 | // Allocate with 'double' alignment. It is 8 bytes on sparc. |
duke@435 | 347 | // In other cases Amalloc_D() should be the same as Amalloc_4(). |
duke@435 | 348 | void* Amalloc_D(size_t x) { |
duke@435 | 349 | assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); |
duke@435 | 350 | debug_only(if (UseMallocOnly) return malloc(x);) |
duke@435 | 351 | #if defined(SPARC) && !defined(_LP64) |
duke@435 | 352 | #define DALIGN_M1 7 |
duke@435 | 353 | size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm; |
duke@435 | 354 | x += delta; |
duke@435 | 355 | #endif |
kamg@2589 | 356 | check_for_overflow(x, "Arena::Amalloc_D"); |
kvn@2557 | 357 | NOT_PRODUCT(inc_bytes_allocated(x);) |
duke@435 | 358 | if (_hwm + x > _max) { |
duke@435 | 359 | return grow(x); // grow() returns a result aligned >= 8 bytes. |
duke@435 | 360 | } else { |
duke@435 | 361 | char *old = _hwm; |
duke@435 | 362 | _hwm += x; |
duke@435 | 363 | #if defined(SPARC) && !defined(_LP64) |
duke@435 | 364 | old += delta; // align to 8-bytes |
duke@435 | 365 | #endif |
duke@435 | 366 | return old; |
duke@435 | 367 | } |
duke@435 | 368 | } |
duke@435 | 369 | |
duke@435 | 370 | // Fast delete in area. Common case is: NOP (except for storage reclaimed) |
duke@435 | 371 | void Afree(void *ptr, size_t size) { |
duke@435 | 372 | #ifdef ASSERT |
duke@435 | 373 | if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory |
duke@435 | 374 | if (UseMallocOnly) return; |
duke@435 | 375 | #endif |
duke@435 | 376 | if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr; |
duke@435 | 377 | } |
duke@435 | 378 | |
duke@435 | 379 | void *Arealloc( void *old_ptr, size_t old_size, size_t new_size ); |
duke@435 | 380 | |
duke@435 | 381 | // Move contents of this arena into an empty arena |
duke@435 | 382 | Arena *move_contents(Arena *empty_arena); |
duke@435 | 383 | |
duke@435 | 384 | // Determine if pointer belongs to this Arena or not. |
duke@435 | 385 | bool contains( const void *ptr ) const; |
duke@435 | 386 | |
duke@435 | 387 | // Total of all chunks in use (not thread-safe) |
duke@435 | 388 | size_t used() const; |
duke@435 | 389 | |
duke@435 | 390 | // Total # of bytes used |
zgu@3900 | 391 | size_t size_in_bytes() const { return _size_in_bytes; }; |
zgu@3900 | 392 | void set_size_in_bytes(size_t size); |
zgu@3900 | 393 | |
duke@435 | 394 | static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN; |
duke@435 | 395 | static void free_all(char** start, char** end) PRODUCT_RETURN; |
duke@435 | 396 | |
zgu@3900 | 397 | // how many arena instances |
zgu@3900 | 398 | NOT_PRODUCT(static volatile jint _instance_count;) |
duke@435 | 399 | private: |
duke@435 | 400 | // Reset this Arena to empty, access will trigger grow if necessary |
duke@435 | 401 | void reset(void) { |
duke@435 | 402 | _first = _chunk = NULL; |
duke@435 | 403 | _hwm = _max = NULL; |
zgu@3900 | 404 | set_size_in_bytes(0); |
duke@435 | 405 | } |
duke@435 | 406 | }; |
duke@435 | 407 | |
duke@435 | 408 | // One of the following macros must be used when allocating |
duke@435 | 409 | // an array or object from an arena |
jcoomes@2191 | 410 | #define NEW_ARENA_ARRAY(arena, type, size) \ |
jcoomes@2191 | 411 | (type*) (arena)->Amalloc((size) * sizeof(type)) |
duke@435 | 412 | |
jcoomes@2191 | 413 | #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \ |
jcoomes@2191 | 414 | (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \ |
jcoomes@2191 | 415 | (new_size) * sizeof(type) ) |
duke@435 | 416 | |
jcoomes@2191 | 417 | #define FREE_ARENA_ARRAY(arena, type, old, size) \ |
jcoomes@2191 | 418 | (arena)->Afree((char*)(old), (size) * sizeof(type)) |
duke@435 | 419 | |
jcoomes@2191 | 420 | #define NEW_ARENA_OBJ(arena, type) \ |
duke@435 | 421 | NEW_ARENA_ARRAY(arena, type, 1) |
duke@435 | 422 | |
duke@435 | 423 | |
duke@435 | 424 | //%note allocation_1 |
duke@435 | 425 | extern char* resource_allocate_bytes(size_t size); |
duke@435 | 426 | extern char* resource_allocate_bytes(Thread* thread, size_t size); |
duke@435 | 427 | extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size); |
duke@435 | 428 | extern void resource_free_bytes( char *old, size_t size ); |
duke@435 | 429 | |
duke@435 | 430 | //---------------------------------------------------------------------- |
duke@435 | 431 | // Base class for objects allocated in the resource area per default. |
duke@435 | 432 | // Optionally, objects may be allocated on the C heap with |
duke@435 | 433 | // new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena) |
duke@435 | 434 | // ResourceObj's can be allocated within other objects, but don't use |
duke@435 | 435 | // new or delete (allocation_type is unknown). If new is used to allocate, |
duke@435 | 436 | // use delete to deallocate. |
duke@435 | 437 | class ResourceObj ALLOCATION_SUPER_CLASS_SPEC { |
duke@435 | 438 | public: |
kvn@2040 | 439 | enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 }; |
kvn@2043 | 440 | static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN; |
duke@435 | 441 | #ifdef ASSERT |
duke@435 | 442 | private: |
kvn@2040 | 443 | // When this object is allocated on stack the new() operator is not |
kvn@2040 | 444 | // called but garbage on stack may look like a valid allocation_type. |
kvn@2040 | 445 | // Store negated 'this' pointer when new() is called to distinguish cases. |
kvn@2357 | 446 | // Use second array's element for verification value to distinguish garbage. |
kvn@2357 | 447 | uintptr_t _allocation_t[2]; |
kvn@2357 | 448 | bool is_type_set() const; |
duke@435 | 449 | public: |
kvn@2043 | 450 | allocation_type get_allocation_type() const; |
kvn@2043 | 451 | bool allocated_on_stack() const { return get_allocation_type() == STACK_OR_EMBEDDED; } |
kvn@2043 | 452 | bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; } |
kvn@2043 | 453 | bool allocated_on_C_heap() const { return get_allocation_type() == C_HEAP; } |
kvn@2043 | 454 | bool allocated_on_arena() const { return get_allocation_type() == ARENA; } |
kvn@2040 | 455 | ResourceObj(); // default construtor |
kvn@2040 | 456 | ResourceObj(const ResourceObj& r); // default copy construtor |
kvn@2040 | 457 | ResourceObj& operator=(const ResourceObj& r); // default copy assignment |
kvn@2040 | 458 | ~ResourceObj(); |
duke@435 | 459 | #endif // ASSERT |
duke@435 | 460 | |
duke@435 | 461 | public: |
zgu@3900 | 462 | void* operator new(size_t size, allocation_type type, MEMFLAGS flags); |
duke@435 | 463 | void* operator new(size_t size, Arena *arena) { |
duke@435 | 464 | address res = (address)arena->Amalloc(size); |
kvn@2040 | 465 | DEBUG_ONLY(set_allocation_type(res, ARENA);) |
duke@435 | 466 | return res; |
duke@435 | 467 | } |
duke@435 | 468 | void* operator new(size_t size) { |
duke@435 | 469 | address res = (address)resource_allocate_bytes(size); |
kvn@2040 | 470 | DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
duke@435 | 471 | return res; |
duke@435 | 472 | } |
duke@435 | 473 | void operator delete(void* p); |
duke@435 | 474 | }; |
duke@435 | 475 | |
duke@435 | 476 | // One of the following macros must be used when allocating an array |
duke@435 | 477 | // or object to determine whether it should reside in the C heap on in |
duke@435 | 478 | // the resource area. |
duke@435 | 479 | |
duke@435 | 480 | #define NEW_RESOURCE_ARRAY(type, size)\ |
duke@435 | 481 | (type*) resource_allocate_bytes((size) * sizeof(type)) |
duke@435 | 482 | |
duke@435 | 483 | #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\ |
duke@435 | 484 | (type*) resource_allocate_bytes(thread, (size) * sizeof(type)) |
duke@435 | 485 | |
duke@435 | 486 | #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\ |
duke@435 | 487 | (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) ) |
duke@435 | 488 | |
duke@435 | 489 | #define FREE_RESOURCE_ARRAY(type, old, size)\ |
duke@435 | 490 | resource_free_bytes((char*)(old), (size) * sizeof(type)) |
duke@435 | 491 | |
duke@435 | 492 | #define FREE_FAST(old)\ |
duke@435 | 493 | /* nop */ |
duke@435 | 494 | |
duke@435 | 495 | #define NEW_RESOURCE_OBJ(type)\ |
duke@435 | 496 | NEW_RESOURCE_ARRAY(type, 1) |
duke@435 | 497 | |
zgu@3900 | 498 | #define NEW_C_HEAP_ARRAY(type, size, memflags)\ |
zgu@3900 | 499 | (type*) (AllocateHeap((size) * sizeof(type), memflags)) |
duke@435 | 500 | |
zgu@3900 | 501 | #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ |
zgu@3900 | 502 | (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) |
duke@435 | 503 | |
zgu@3900 | 504 | #define FREE_C_HEAP_ARRAY(type,old,memflags) \ |
zgu@3900 | 505 | FreeHeap((char*)(old), memflags) |
duke@435 | 506 | |
zgu@3900 | 507 | #define NEW_C_HEAP_OBJ(type, memflags)\ |
zgu@3900 | 508 | NEW_C_HEAP_ARRAY(type, 1, memflags) |
zgu@3900 | 509 | |
zgu@3900 | 510 | |
zgu@3900 | 511 | #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ |
zgu@3900 | 512 | (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) |
zgu@3900 | 513 | |
zgu@3900 | 514 | #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\ |
zgu@3900 | 515 | (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc)) |
zgu@3900 | 516 | |
zgu@3900 | 517 | #define NEW_C_HEAP_OBJ2(type, memflags, pc)\ |
zgu@3900 | 518 | NEW_C_HEAP_ARRAY2(type, 1, memflags, pc) |
zgu@3900 | 519 | |
duke@435 | 520 | |
duke@435 | 521 | extern bool warn_new_operator; |
duke@435 | 522 | |
duke@435 | 523 | // for statistics |
duke@435 | 524 | #ifndef PRODUCT |
duke@435 | 525 | class AllocStats : StackObj { |
kvn@2557 | 526 | julong start_mallocs, start_frees; |
kvn@2557 | 527 | julong start_malloc_bytes, start_mfree_bytes, start_res_bytes; |
duke@435 | 528 | public: |
duke@435 | 529 | AllocStats(); |
duke@435 | 530 | |
kvn@2557 | 531 | julong num_mallocs(); // since creation of receiver |
kvn@2557 | 532 | julong alloc_bytes(); |
kvn@2557 | 533 | julong num_frees(); |
kvn@2557 | 534 | julong free_bytes(); |
kvn@2557 | 535 | julong resource_bytes(); |
duke@435 | 536 | void print(); |
duke@435 | 537 | }; |
duke@435 | 538 | #endif |
duke@435 | 539 | |
duke@435 | 540 | |
duke@435 | 541 | //------------------------------ReallocMark--------------------------------- |
duke@435 | 542 | // Code which uses REALLOC_RESOURCE_ARRAY should check an associated |
duke@435 | 543 | // ReallocMark, which is declared in the same scope as the reallocated |
duke@435 | 544 | // pointer. Any operation that could __potentially__ cause a reallocation |
duke@435 | 545 | // should check the ReallocMark. |
duke@435 | 546 | class ReallocMark: public StackObj { |
duke@435 | 547 | protected: |
duke@435 | 548 | NOT_PRODUCT(int _nesting;) |
duke@435 | 549 | |
duke@435 | 550 | public: |
duke@435 | 551 | ReallocMark() PRODUCT_RETURN; |
duke@435 | 552 | void check() PRODUCT_RETURN; |
duke@435 | 553 | }; |
stefank@2314 | 554 | |
stefank@2314 | 555 | #endif // SHARE_VM_MEMORY_ALLOCATION_HPP |