Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/allocation.inline.hpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP25#define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP2627#include "runtime/atomic.inline.hpp"28#include "runtime/os.hpp"29#include "services/memTracker.hpp"3031// Explicit C-heap memory management3233void trace_heap_malloc(size_t size, const char* name, void *p);34void trace_heap_free(void *p);3536#ifndef PRODUCT37// Increments unsigned long value for statistics (not atomic on MP).38inline void inc_stat_counter(volatile julong* dest, julong add_value) {39#if defined(SPARC) || defined(X86) || defined(AARCH64)40// Sparc, X86 and AArch64 have atomic jlong (8 bytes) instructions41julong value = Atomic::load((volatile jlong*)dest);42value += add_value;43Atomic::store((jlong)value, (volatile jlong*)dest);44#else45// possible word-tearing during load/store46*dest += add_value;47#endif48}49#endif5051// allocate using malloc; will fail if no memory available52inline char* AllocateHeap(size_t size, MEMFLAGS flags,53const NativeCallStack& stack,54AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {55char* p = (char*) os::malloc(size, flags, stack);56#ifdef ASSERT57if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);58#endif59if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {60vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");61}62return p;63}6465#ifdef __GNUC__66__attribute__((always_inline))67#endif68inline char* AllocateHeap(size_t size, MEMFLAGS flags,69AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {70return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);71}7273#ifdef __GNUC__74__attribute__((always_inline))75#endif76inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,77AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {78char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);79#ifdef ASSERT80if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);81#endif82if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {83vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");84}85return p;86}8788inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) {89#ifdef ASSERT90if (PrintMallocFree) trace_heap_free(p);91#endif92os::free(p, memflags);93}949596template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,97const NativeCallStack& stack) throw() {98void* p = (void*)AllocateHeap(size, F, stack);99#ifdef ASSERT100if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);101#endif102return p;103}104105template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() {106return CHeapObj<F>::operator new(size, CALLER_PC);107}108109template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,110const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {111void* p = (void*)AllocateHeap(size, F, stack,112AllocFailStrategy::RETURN_NULL);113#ifdef ASSERT114if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);115#endif116return p;117}118119template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,120const std::nothrow_t& nothrow_constant) throw() {121return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);122}123124template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,125const NativeCallStack& stack) throw() {126return CHeapObj<F>::operator new(size, stack);127}128129template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size)130throw() {131return CHeapObj<F>::operator new(size, CALLER_PC);132}133134template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,135const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {136return CHeapObj<F>::operator new(size, nothrow_constant, stack);137}138139template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,140const std::nothrow_t& nothrow_constant) throw() {141return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);142}143144template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){145FreeHeap(p, F);146}147148template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){149FreeHeap(p, F);150}151152template <class E, MEMFLAGS F>153E* ArrayAllocator<E, F>::allocate(size_t length) {154assert(_addr == NULL, "Already in use");155156_size = sizeof(E) * length;157_use_malloc = _size < ArrayAllocatorMallocLimit;158159if (_use_malloc) {160_addr = AllocateHeap(_size, F);161if (_addr == NULL && _size >= (size_t)os::vm_allocation_granularity()) {162// malloc failed let's try with mmap instead163_use_malloc = false;164} else {165return (E*)_addr;166}167}168169int alignment = os::vm_allocation_granularity();170_size = align_size_up(_size, alignment);171172_addr = os::reserve_memory(_size, NULL, alignment, F MACOS_AARCH64_ONLY(, !ExecMem));173if (_addr == NULL) {174vm_exit_out_of_memory(_size, OOM_MMAP_ERROR, "Allocator (reserve)");175}176177os::commit_memory_or_exit(_addr, _size, !ExecMem, "Allocator (commit)");178179return (E*)_addr;180}181182template<class E, MEMFLAGS F>183void ArrayAllocator<E, F>::free() {184if (_addr != NULL) {185if (_use_malloc) {186FreeHeap(_addr, F);187} else {188os::release_memory(_addr, _size);189}190_addr = NULL;191}192}193194#endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP195196197