src/share/vm/memory/allocation.inline.hpp

Print this page
rev 6150 : 8037959: BitMap::resize frees old map before copying memory if !in_resource_area
Summary: Add reallocate functionality to ArrayAllocator and use it from BitMap::resize
Reviewed-by:


 105 
 106 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 107       address caller_pc) throw() {
 108     return CHeapObj<F>::operator new(size, caller_pc);
 109 }
 110 
 111 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 112   const std::nothrow_t&  nothrow_constant, address caller_pc) throw() {
 113     return CHeapObj<F>::operator new(size, nothrow_constant, caller_pc);
 114 }
 115 
 116 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
 117     FreeHeap(p, F);
 118 }
 119 
 120 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
 121     FreeHeap(p, F);
 122 }
 123 
 124 template <class E, MEMFLAGS F>
 125 E* ArrayAllocator<E, F>::allocate(size_t length) {
 126   assert(_addr == NULL, "Already in use");
 127 
 128   _size = sizeof(E) * length;
 129   _use_malloc = _size < ArrayAllocatorMallocLimit;
 130 
 131   if (_use_malloc) {
 132     _addr = AllocateHeap(_size, F);
 133     if (_addr == NULL && _size >=  (size_t)os::vm_allocation_granularity()) {
 134       // malloc failed let's try with mmap instead
 135       _use_malloc = false;
 136     } else {
 137       return (E*)_addr;
 138     }
 139   }
 140 
 141   int alignment = os::vm_allocation_granularity();
 142   _size = align_size_up(_size, alignment);
 143 
 144   _addr = os::reserve_memory(_size, NULL, alignment, F);
 145   if (_addr == NULL) {
 146     vm_exit_out_of_memory(_size, OOM_MMAP_ERROR, "Allocator (reserve)");
 147   }
 148 
 149   os::commit_memory_or_exit(_addr, _size, !ExecMem, "Allocator (commit)");










 150 
 151   return (E*)_addr;















 152 }
 153 
 154 template<class E, MEMFLAGS F>
 155 void ArrayAllocator<E, F>::free() {
 156   if (_addr != NULL) {
 157     if (_use_malloc) {
 158       FreeHeap(_addr, F);
 159     } else {
 160       os::release_memory(_addr, _size);
 161     }
 162     _addr = NULL;
 163   }
 164 }
 165 
 166 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP


 105 
 106 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 107       address caller_pc) throw() {
 108     return CHeapObj<F>::operator new(size, caller_pc);
 109 }
 110 
 111 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 112   const std::nothrow_t&  nothrow_constant, address caller_pc) throw() {
 113     return CHeapObj<F>::operator new(size, nothrow_constant, caller_pc);
 114 }
 115 
 116 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
 117     FreeHeap(p, F);
 118 }
 119 
 120 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
 121     FreeHeap(p, F);
 122 }
 123 
 124 template <class E, MEMFLAGS F>
 125 char* ArrayAllocator<E, F>::allocate_inner(size_t &size, bool &use_malloc) {
 126   char* addr = NULL;



 127 
 128   if (use_malloc) {
 129     addr = AllocateHeap(size, F);
 130     if (addr == NULL && size >= (size_t)os::vm_allocation_granularity()) {
 131       // malloc failed let's try with mmap instead
 132       use_malloc = false;
 133     } else {
 134       return addr;
 135     }
 136   }
 137 
 138   int alignment = os::vm_allocation_granularity();
 139   size = align_size_up(size, alignment);
 140 
 141   addr = os::reserve_memory(size, NULL, alignment, F);
 142   if (addr == NULL) {
 143     vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)");
 144   }
 145 
 146   os::commit_memory_or_exit(addr, size, !ExecMem, "Allocator (commit)");
 147   return addr;
 148 }
 149 
 150 template <class E, MEMFLAGS F>
 151 E* ArrayAllocator<E, F>::allocate(size_t length) {
 152   assert(_addr == NULL, "Already in use");
 153 
 154   _size = sizeof(E) * length;
 155   _use_malloc = should_use_malloc(_size);
 156   _addr = allocate_inner(_size, _use_malloc);
 157 
 158   return (E*)_addr;
 159 }
 160 
 161 template <class E, MEMFLAGS F>
 162 E* ArrayAllocator<E, F>::reallocate(size_t new_length) {
 163   size_t new_size = sizeof(E) * new_length;
 164   bool use_malloc = should_use_malloc(new_size);
 165   char* new_addr = allocate_inner(new_size, use_malloc);
 166 
 167   memcpy(new_addr, _addr, MIN2(new_size, _size));
 168 
 169   free();
 170   _size = new_size;
 171   _use_malloc = use_malloc;
 172   _addr = new_addr;
 173   return (E*)new_addr;
 174 }
 175 
 176 template<class E, MEMFLAGS F>
 177 void ArrayAllocator<E, F>::free() {
 178   if (_addr != NULL) {
 179     if (_use_malloc) {
 180       FreeHeap(_addr, F);
 181     } else {
 182       os::release_memory(_addr, _size);
 183     }
 184     _addr = NULL;
 185   }
 186 }
 187 
 188 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP