2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2018-01-16 07:48:01 +01:00
|
|
|
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
|
|
|
|
#define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
|
|
|
|
|
2016-08-21 20:56:37 -04:00
|
|
|
#include "runtime/atomic.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/os.hpp"
|
2014-08-07 12:18:58 -07:00
|
|
|
#include "services/memTracker.hpp"
|
2017-07-05 11:33:17 +02:00
|
|
|
#include "utilities/align.hpp"
|
2016-06-21 19:29:39 -04:00
|
|
|
#include "utilities/globalDefinitions.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Explicit C-heap memory management
|
|
|
|
|
2011-02-10 19:34:48 -08:00
|
|
|
#ifndef PRODUCT
|
2011-02-07 10:34:39 -08:00
|
|
|
// Increments unsigned long value for statistics (not atomic on MP).
|
|
|
|
inline void inc_stat_counter(volatile julong* dest, julong add_value) {
|
2011-02-10 19:34:48 -08:00
|
|
|
#if defined(SPARC) || defined(X86)
|
|
|
|
// Sparc and X86 have atomic jlong (8 bytes) instructions
|
2018-02-11 03:12:15 -05:00
|
|
|
julong value = Atomic::load(dest);
|
2011-02-07 10:34:39 -08:00
|
|
|
value += add_value;
|
2018-02-11 03:12:15 -05:00
|
|
|
Atomic::store(value, dest);
|
2011-02-10 19:34:48 -08:00
|
|
|
#else
|
|
|
|
// possible word-tearing during load/store
|
|
|
|
*dest += add_value;
|
|
|
|
#endif
|
2011-02-07 10:34:39 -08:00
|
|
|
}
|
2011-02-10 19:34:48 -08:00
|
|
|
#endif
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// allocate using malloc; will fail if no memory available
|
2014-08-07 12:18:58 -07:00
|
|
|
inline char* AllocateHeap(size_t size, MEMFLAGS flags,
|
|
|
|
const NativeCallStack& stack,
|
2012-10-17 17:36:48 +02:00
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
|
2014-08-07 12:18:58 -07:00
|
|
|
char* p = (char*) os::malloc(size, flags, stack);
|
2013-04-30 11:56:52 -07:00
|
|
|
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
|
|
|
|
vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
2015-04-28 19:04:39 +09:00
|
|
|
|
2016-06-21 19:29:39 -04:00
|
|
|
ALWAYSINLINE char* AllocateHeap(size_t size, MEMFLAGS flags,
|
2014-08-07 12:18:58 -07:00
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
|
|
|
|
return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2016-06-21 19:29:39 -04:00
|
|
|
ALWAYSINLINE char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,
|
2012-10-17 17:36:48 +02:00
|
|
|
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
|
2014-08-07 12:18:58 -07:00
|
|
|
char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);
|
2013-04-30 11:56:52 -07:00
|
|
|
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
|
|
|
|
vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:16:15 -05:00
|
|
|
inline void FreeHeap(void* p) {
|
|
|
|
os::free(p);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
|
2014-08-07 12:18:58 -07:00
|
|
|
const NativeCallStack& stack) throw() {
|
2018-02-02 09:34:11 -05:00
|
|
|
return (void*)AllocateHeap(size, F, stack);
|
2014-08-07 12:18:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, CALLER_PC);
|
|
|
|
}
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
|
2018-02-02 09:34:11 -05:00
|
|
|
const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {
|
|
|
|
return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);
|
|
|
|
}
|
2014-08-07 12:18:58 -07:00
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
|
|
|
|
const std::nothrow_t& nothrow_constant) throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
|
|
|
|
const NativeCallStack& stack) throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size)
|
|
|
|
throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, CALLER_PC);
|
2013-05-14 09:41:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
|
2014-08-07 12:18:58 -07:00
|
|
|
const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, nothrow_constant, stack);
|
2013-05-14 09:41:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
|
2014-08-07 12:18:58 -07:00
|
|
|
const std::nothrow_t& nothrow_constant) throw() {
|
|
|
|
return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
|
2012-06-28 17:03:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
|
2014-12-01 12:16:15 -05:00
|
|
|
FreeHeap(p);
|
2013-05-14 09:41:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
|
2014-12-01 12:16:15 -05:00
|
|
|
FreeHeap(p);
|
2012-06-28 17:03:16 -04:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
size_t MmapArrayAllocator<E>::size_for(size_t length) {
|
2016-03-09 12:44:12 +01:00
|
|
|
size_t size = length * sizeof(E);
|
2013-04-08 07:49:28 +02:00
|
|
|
int alignment = os::vm_allocation_granularity();
|
2017-07-04 15:58:10 +02:00
|
|
|
return align_up(size, alignment);
|
2016-03-09 12:44:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* MmapArrayAllocator<E>::allocate_or_null(size_t length, MEMFLAGS flags) {
|
2016-09-15 16:44:19 +02:00
|
|
|
size_t size = size_for(length);
|
|
|
|
int alignment = os::vm_allocation_granularity();
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
char* addr = os::reserve_memory(size, NULL, alignment, flags);
|
2016-09-15 16:44:19 +02:00
|
|
|
if (addr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-16 07:48:01 +01:00
|
|
|
if (os::commit_memory(addr, size, !ExecMem)) {
|
2016-09-15 16:44:19 +02:00
|
|
|
return (E*)addr;
|
|
|
|
} else {
|
|
|
|
os::release_memory(addr, size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* MmapArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
|
2016-03-09 17:03:04 +01:00
|
|
|
size_t size = size_for(length);
|
2016-03-09 12:44:12 +01:00
|
|
|
int alignment = os::vm_allocation_granularity();
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
char* addr = os::reserve_memory(size, NULL, alignment, flags);
|
2014-04-02 14:17:34 +02:00
|
|
|
if (addr == NULL) {
|
|
|
|
vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)");
|
2013-04-08 07:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 14:17:34 +02:00
|
|
|
os::commit_memory_or_exit(addr, size, !ExecMem, "Allocator (commit)");
|
2016-03-09 12:44:12 +01:00
|
|
|
|
|
|
|
return (E*)addr;
|
2014-04-02 14:17:34 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
void MmapArrayAllocator<E>::free(E* addr, size_t length) {
|
2016-03-09 17:03:04 +01:00
|
|
|
bool result = os::release_memory((char*)addr, size_for(length));
|
|
|
|
assert(result, "Failed to release memory");
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
size_t MallocArrayAllocator<E>::size_for(size_t length) {
|
2016-03-09 17:03:04 +01:00
|
|
|
return length * sizeof(E);
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* MallocArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
|
|
|
|
return (E*)AllocateHeap(size_for(length), flags);
|
2016-03-09 17:03:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template<class E>
|
|
|
|
void MallocArrayAllocator<E>::free(E* addr, size_t /*length*/) {
|
2016-03-09 17:03:04 +01:00
|
|
|
FreeHeap(addr);
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
bool ArrayAllocator<E>::should_use_malloc(size_t length) {
|
|
|
|
return MallocArrayAllocator<E>::size_for(length) < ArrayAllocatorMallocLimit;
|
2016-03-09 17:03:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* ArrayAllocator<E>::allocate_malloc(size_t length, MEMFLAGS flags) {
|
|
|
|
return MallocArrayAllocator<E>::allocate(length, flags);
|
2016-03-09 17:03:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* ArrayAllocator<E>::allocate_mmap(size_t length, MEMFLAGS flags) {
|
|
|
|
return MmapArrayAllocator<E>::allocate(length, flags);
|
2016-03-09 17:03:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* ArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
|
2016-03-09 12:44:12 +01:00
|
|
|
if (should_use_malloc(length)) {
|
2017-07-21 21:01:59 -04:00
|
|
|
return allocate_malloc(length, flags);
|
2016-03-09 12:44:12 +01:00
|
|
|
}
|
2013-04-08 07:49:28 +02:00
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
return allocate_mmap(length, flags);
|
2013-04-08 07:49:28 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template <class E>
|
|
|
|
E* ArrayAllocator<E>::reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags) {
|
2016-03-09 12:44:12 +01:00
|
|
|
E* new_addr = (new_length > 0)
|
2017-07-21 21:01:59 -04:00
|
|
|
? allocate(new_length, flags)
|
2016-03-09 12:44:12 +01:00
|
|
|
: NULL;
|
2014-04-02 14:17:34 +02:00
|
|
|
|
2016-03-09 12:44:12 +01:00
|
|
|
if (new_addr != NULL && old_addr != NULL) {
|
|
|
|
memcpy(new_addr, old_addr, MIN2(old_length, new_length) * sizeof(E));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_addr != NULL) {
|
|
|
|
free(old_addr, old_length);
|
|
|
|
}
|
2014-04-02 14:17:34 +02:00
|
|
|
|
2016-03-09 12:44:12 +01:00
|
|
|
return new_addr;
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template<class E>
|
|
|
|
void ArrayAllocator<E>::free_malloc(E* addr, size_t length) {
|
|
|
|
MallocArrayAllocator<E>::free(addr, length);
|
2016-03-09 12:44:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template<class E>
|
|
|
|
void ArrayAllocator<E>::free_mmap(E* addr, size_t length) {
|
|
|
|
MmapArrayAllocator<E>::free(addr, length);
|
2014-04-02 14:17:34 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:01:59 -04:00
|
|
|
template<class E>
|
|
|
|
void ArrayAllocator<E>::free(E* addr, size_t length) {
|
2016-03-09 12:44:12 +01:00
|
|
|
if (addr != NULL) {
|
|
|
|
if (should_use_malloc(length)) {
|
|
|
|
free_malloc(addr, length);
|
2013-04-08 07:49:28 +02:00
|
|
|
} else {
|
2016-03-09 12:44:12 +01:00
|
|
|
free_mmap(addr, length);
|
2013-04-08 07:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 17:03:16 -04:00
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
|