Path: blob/master/src/hotspot/share/memory/arena.hpp
40949 views
/*1* Copyright (c) 2017, 2021, 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_MEMORY_ARENA_HPP25#define SHARE_MEMORY_ARENA_HPP2627#include "memory/allocation.hpp"28#include "runtime/globals.hpp"29#include "utilities/globalDefinitions.hpp"30#include "utilities/powerOfTwo.hpp"3132#include <new>3334// The byte alignment to be used by Arena::Amalloc. See bugid 4169348.35// Note: this value must be a power of 23637#define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)3839#define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)40#define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))41#define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)4243//------------------------------Chunk------------------------------------------44// Linked list of raw memory chunks45class Chunk: CHeapObj<mtChunk> {4647private:48Chunk* _next; // Next Chunk in list49const size_t _len; // Size of this Chunk50public:51void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw();52void operator delete(void* p);53Chunk(size_t length);5455enum {56// default sizes; make them slightly smaller than 2**k to guard against57// buddy-system style malloc implementations58#ifdef _LP6459slack = 40, // [RGV] Not sure if this is right, but make it60// a multiple of 8.61#else62slack = 20, // suspected sizeof(Chunk) + internal malloc headers63#endif6465tiny_size = 256 - slack, // Size of first chunk (tiny)66init_size = 1*K - slack, // Size of first chunk (normal aka small)67medium_size= 10*K - slack, // Size of medium-sized chunk68size = 32*K - slack, // Default size of an Arena chunk (following the first)69non_pool_size = init_size + 32 // An initial size which is not one of above70};7172void chop(); // Chop this chunk73void next_chop(); // Chop next chunk74static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }75static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); }7677size_t length() const { return _len; }78Chunk* next() const { return _next; }79void set_next(Chunk* n) { _next = n; }80// Boundaries of data area (possibly unused)81char* bottom() const { return ((char*) this) + aligned_overhead_size(); }82char* top() const { return bottom() + _len; }83bool contains(char* p) const { return bottom() <= p && p <= top(); }8485// Start the chunk_pool cleaner task86static void start_chunk_pool_cleaner_task();87};8889//------------------------------Arena------------------------------------------90// Fast allocation of memory91class Arena : public CHeapObj<mtNone> {92protected:93friend class HandleMark;94friend class NoHandleMark;95friend class VMStructs;9697MEMFLAGS _flags; // Memory tracking flags9899Chunk *_first; // First chunk100Chunk *_chunk; // current chunk101char *_hwm, *_max; // High water mark and max in current chunk102// Get a new Chunk of at least size x103void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);104size_t _size_in_bytes; // Size of arena (used for native memory tracking)105106debug_only(void* malloc(size_t size);)107debug_only(void* internal_malloc_4(size_t x);)108109void signal_out_of_memory(size_t request, const char* whence) const;110111bool check_for_overflow(size_t request, const char* whence,112AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const {113if (UINTPTR_MAX - request < (uintptr_t)_hwm) {114if (alloc_failmode == AllocFailStrategy::RETURN_NULL) {115return false;116}117signal_out_of_memory(request, whence);118}119return true;120}121122public:123Arena(MEMFLAGS memflag);124Arena(MEMFLAGS memflag, size_t init_size);125~Arena();126void destruct_contents();127char* hwm() const { return _hwm; }128129// new operators130void* operator new (size_t size) throw();131void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw();132133// dynamic memory type tagging134void* operator new(size_t size, MEMFLAGS flags) throw();135void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw();136void operator delete(void* p);137138// Fast allocate in the arena. Common case is: pointer test + increment.139void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {140assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");141x = ARENA_ALIGN(x);142debug_only(if (UseMallocOnly) return malloc(x);)143if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode))144return NULL;145if (_hwm + x > _max) {146return grow(x, alloc_failmode);147} else {148char *old = _hwm;149_hwm += x;150return old;151}152}153// Further assume size is padded out to words154void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {155assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );156debug_only(if (UseMallocOnly) return malloc(x);)157if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode))158return NULL;159if (_hwm + x > _max) {160return grow(x, alloc_failmode);161} else {162char *old = _hwm;163_hwm += x;164return old;165}166}167168// Allocate with 'double' alignment. It is 8 bytes on sparc.169// In other cases Amalloc_D() should be the same as Amalloc_4().170void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {171assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );172debug_only(if (UseMallocOnly) return malloc(x);)173if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode))174return NULL;175if (_hwm + x > _max) {176return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes.177} else {178char *old = _hwm;179_hwm += x;180return old;181}182}183184// Fast delete in area. Common case is: NOP (except for storage reclaimed)185bool Afree(void *ptr, size_t size) {186if (ptr == NULL) {187return true; // as with free(3), freeing NULL is a noop.188}189#ifdef ASSERT190if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory191if (UseMallocOnly) return true;192#endif193if (((char*)ptr) + size == _hwm) {194_hwm = (char*)ptr;195return true;196} else {197// Unable to fast free, so we just drop it.198return false;199}200}201202void *Arealloc( void *old_ptr, size_t old_size, size_t new_size,203AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);204205// Move contents of this arena into an empty arena206Arena *move_contents(Arena *empty_arena);207208// Determine if pointer belongs to this Arena or not.209bool contains( const void *ptr ) const;210211// Total of all chunks in use (not thread-safe)212size_t used() const;213214// Total # of bytes used215size_t size_in_bytes() const { return _size_in_bytes; };216void set_size_in_bytes(size_t size);217218static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;219static void free_all(char** start, char** end) PRODUCT_RETURN;220221private:222// Reset this Arena to empty, access will trigger grow if necessary223void reset(void) {224_first = _chunk = NULL;225_hwm = _max = NULL;226set_size_in_bytes(0);227}228};229230// One of the following macros must be used when allocating231// an array or object from an arena232#define NEW_ARENA_ARRAY(arena, type, size) \233(type*) (arena)->Amalloc((size) * sizeof(type))234235#define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \236(type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \237(new_size) * sizeof(type) )238239#define FREE_ARENA_ARRAY(arena, type, old, size) \240(arena)->Afree((char*)(old), (size) * sizeof(type))241242#define NEW_ARENA_OBJ(arena, type) \243NEW_ARENA_ARRAY(arena, type, 1)244245#endif // SHARE_MEMORY_ARENA_HPP246247248