1 /* |
1 /* |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2014, 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. |
25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
27 |
27 |
28 #include "runtime/atomic.inline.hpp" |
28 #include "runtime/atomic.inline.hpp" |
29 #include "runtime/os.hpp" |
29 #include "runtime/os.hpp" |
|
30 #include "services/memTracker.hpp" |
30 |
31 |
31 // Explicit C-heap memory management |
32 // Explicit C-heap memory management |
32 |
33 |
33 void trace_heap_malloc(size_t size, const char* name, void *p); |
34 void trace_heap_malloc(size_t size, const char* name, void *p); |
34 void trace_heap_free(void *p); |
35 void trace_heap_free(void *p); |
47 #endif |
48 #endif |
48 } |
49 } |
49 #endif |
50 #endif |
50 |
51 |
51 // allocate using malloc; will fail if no memory available |
52 // allocate using malloc; will fail if no memory available |
52 inline char* AllocateHeap(size_t size, MEMFLAGS flags, address pc = 0, |
53 inline char* AllocateHeap(size_t size, MEMFLAGS flags, |
|
54 const NativeCallStack& stack, |
53 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
55 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
54 if (pc == 0) { |
56 char* p = (char*) os::malloc(size, flags, stack); |
55 pc = CURRENT_PC; |
|
56 } |
|
57 char* p = (char*) os::malloc(size, flags, pc); |
|
58 #ifdef ASSERT |
57 #ifdef ASSERT |
59 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p); |
58 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p); |
60 #endif |
59 #endif |
61 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
60 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
62 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap"); |
61 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap"); |
63 } |
62 } |
64 return p; |
63 return p; |
65 } |
64 } |
|
65 inline char* AllocateHeap(size_t size, MEMFLAGS flags, |
|
66 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
|
67 return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode); |
|
68 } |
66 |
69 |
67 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flags, |
70 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag, |
68 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
71 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
69 char* p = (char*) os::realloc(old, size, flags, CURRENT_PC); |
72 char* p = (char*) os::realloc(old, size, flag, CURRENT_PC); |
70 #ifdef ASSERT |
73 #ifdef ASSERT |
71 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p); |
74 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p); |
72 #endif |
75 #endif |
73 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
76 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
74 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap"); |
77 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap"); |
83 os::free(p, memflags); |
86 os::free(p, memflags); |
84 } |
87 } |
85 |
88 |
86 |
89 |
87 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, |
90 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, |
88 address caller_pc) throw() { |
91 const NativeCallStack& stack) throw() { |
89 void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC)); |
92 void* p = (void*)AllocateHeap(size, F, stack); |
|
93 #ifdef ASSERT |
|
94 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
|
95 #endif |
|
96 return p; |
|
97 } |
|
98 |
|
99 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() { |
|
100 return CHeapObj<F>::operator new(size, CALLER_PC); |
|
101 } |
|
102 |
|
103 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, |
|
104 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() { |
|
105 void* p = (void*)AllocateHeap(size, F, stack, |
|
106 AllocFailStrategy::RETURN_NULL); |
90 #ifdef ASSERT |
107 #ifdef ASSERT |
91 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
108 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
92 #endif |
109 #endif |
93 return p; |
110 return p; |
94 } |
111 } |
95 |
112 |
96 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, |
113 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, |
97 const std::nothrow_t& nothrow_constant, address caller_pc) throw() { |
114 const std::nothrow_t& nothrow_constant) throw() { |
98 void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC), |
115 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); |
99 AllocFailStrategy::RETURN_NULL); |
|
100 #ifdef ASSERT |
|
101 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
|
102 #endif |
|
103 return p; |
|
104 } |
116 } |
105 |
117 |
106 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
118 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
107 address caller_pc) throw() { |
119 const NativeCallStack& stack) throw() { |
108 return CHeapObj<F>::operator new(size, caller_pc); |
120 return CHeapObj<F>::operator new(size, stack); |
|
121 } |
|
122 |
|
123 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size) |
|
124 throw() { |
|
125 return CHeapObj<F>::operator new(size, CALLER_PC); |
109 } |
126 } |
110 |
127 |
111 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
128 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
112 const std::nothrow_t& nothrow_constant, address caller_pc) throw() { |
129 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() { |
113 return CHeapObj<F>::operator new(size, nothrow_constant, caller_pc); |
130 return CHeapObj<F>::operator new(size, nothrow_constant, stack); |
|
131 } |
|
132 |
|
133 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
|
134 const std::nothrow_t& nothrow_constant) throw() { |
|
135 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); |
114 } |
136 } |
115 |
137 |
116 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ |
138 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ |
117 FreeHeap(p, F); |
139 FreeHeap(p, F); |
118 } |
140 } |