Path: blob/master/src/hotspot/share/memory/heap.hpp
40949 views
/*1* Copyright (c) 1997, 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_HEAP_HPP25#define SHARE_MEMORY_HEAP_HPP2627#include "code/codeBlob.hpp"28#include "memory/allocation.hpp"29#include "memory/virtualspace.hpp"30#include "utilities/macros.hpp"3132// Blocks3334class HeapBlock {35friend class VMStructs;3637public:38struct Header {39size_t _length; // the length in segments40bool _used; // Used bit41};4243protected:44union {45Header _header;46int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];47// pad to 0 mod 848};4950public:51// Initialization52void initialize(size_t length) { _header._length = length; set_used(); }53// Merging/splitting54void set_length(size_t length) { _header._length = length; }5556// Accessors57void* allocated_space() const { return (void*)(this + 1); }58size_t length() const { return _header._length; }5960// Used/free61void set_used() { _header._used = true; }62void set_free() { _header._used = false; }63bool free() { return !_header._used; }64};6566class FreeBlock: public HeapBlock {67friend class VMStructs;68protected:69FreeBlock* _link;7071public:72// Initialization73void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; }7475// Accessors76FreeBlock* link() const { return _link; }77void set_link(FreeBlock* link) { _link = link; }78};7980class CodeHeap : public CHeapObj<mtCode> {81friend class VMStructs;82protected:83VirtualSpace _memory; // the memory holding the blocks84VirtualSpace _segmap; // the memory holding the segment map8586size_t _number_of_committed_segments;87size_t _number_of_reserved_segments;88size_t _segment_size;89int _log2_segment_size;9091size_t _next_segment;9293FreeBlock* _freelist;94FreeBlock* _last_insert_point; // last insert point in add_to_freelist95size_t _freelist_segments; // No. of segments in freelist96int _freelist_length;97size_t _max_allocated_capacity; // Peak capacity that was allocated during lifetime of the heap9899const char* _name; // Name of the CodeHeap100const int _code_blob_type; // CodeBlobType it contains101int _blob_count; // Number of CodeBlobs102int _nmethod_count; // Number of nmethods103int _adapter_count; // Number of adapters104int _full_count; // Number of times the code heap was full105int _fragmentation_count; // #FreeBlock joins without fully initializing segment map elements.106107enum { free_sentinel = 0xFF };108static const int fragmentation_limit = 10000; // defragment after that many potential fragmentations.109static const int freelist_limit = 100; // improve insert point search if list is longer than this limit.110static char segmap_template[free_sentinel+1];111112// Helper functions113size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }114size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }115116size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }117bool is_segment_unused(int val) const { return val == free_sentinel; }118void* address_for(size_t i) const { return (void*)(_memory.low() + segments_to_size(i)); }119void* find_block_for(void* p) const;120HeapBlock* block_at(size_t i) const { return (HeapBlock*)address_for(i); }121122// These methods take segment map indices as range boundaries123void mark_segmap_as_free(size_t beg, size_t end);124void mark_segmap_as_used(size_t beg, size_t end, bool is_FreeBlock_join);125void invalidate(size_t beg, size_t end, size_t header_bytes);126void clear(size_t beg, size_t end);127void clear(); // clears all heap contents128static void init_segmap_template();129130// Freelist management helpers131FreeBlock* following_block(FreeBlock* b);132void insert_after(FreeBlock* a, FreeBlock* b);133bool merge_right (FreeBlock* a);134135// Toplevel freelist management136void add_to_freelist(HeapBlock* b);137HeapBlock* search_freelist(size_t length);138139// Iteration helpers140void* next_used(HeapBlock* b) const;141HeapBlock* block_start(void* p) const;142143// to perform additional actions on creation of executable code144void on_code_mapping(char* base, size_t size);145146public:147CodeHeap(const char* name, const int code_blob_type);148149// Heap extents150bool reserve(ReservedSpace rs, size_t committed_size, size_t segment_size);151bool expand_by(size_t size); // expands committed memory by size152153// Memory allocation154void* allocate (size_t size); // Allocate 'size' bytes in the code cache or return NULL155void deallocate(void* p); // Deallocate memory156// Free the tail of segments allocated by the last call to 'allocate()' which exceed 'used_size'.157// ATTENTION: this is only safe to use if there was no other call to 'allocate()' after158// 'p' was allocated. Only intended for freeing memory which would be otherwise159// wasted after the interpreter generation because we don't know the interpreter size160// beforehand and we also can't easily relocate the interpreter to a new location.161void deallocate_tail(void* p, size_t used_size);162163// Boundaries of committed space.164char* low() const { return _memory.low(); }165char* high() const { return _memory.high(); }166// Boundaries of reserved space.167char* low_boundary() const { return _memory.low_boundary(); }168char* high_boundary() const { return _memory.high_boundary(); }169170// Containment means "contained in committed space".171bool contains(const void* p) const { return low() <= p && p < high(); }172bool contains_blob(const CodeBlob* blob) const {173return contains((void*)blob);174}175176virtual void* find_start(void* p) const; // returns the block containing p or NULL177virtual CodeBlob* find_blob_unsafe(void* start) const;178size_t alignment_unit() const; // alignment of any block179size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit180static size_t header_size() { return sizeof(HeapBlock); } // returns the header size for each heap block181182size_t segment_size() const { return _segment_size; } // for CodeHeapState183HeapBlock* first_block() const; // for CodeHeapState184HeapBlock* next_block(HeapBlock* b) const; // for CodeHeapState185HeapBlock* split_block(HeapBlock* b, size_t split_seg); // split one block into two186187FreeBlock* freelist() const { return _freelist; } // for CodeHeapState188189size_t allocated_in_freelist() const { return _freelist_segments * CodeCacheSegmentSize; }190int freelist_length() const { return _freelist_length; } // number of elements in the freelist191192// returns the first block or NULL193virtual void* first() const { return next_used(first_block()); }194// returns the next block given a block p or NULL195virtual void* next(void* p) const { return next_used(next_block(block_start(p))); }196197// Statistics198size_t capacity() const;199size_t max_capacity() const;200int allocated_segments() const;201size_t allocated_capacity() const;202size_t max_allocated_capacity() const { return _max_allocated_capacity; }203size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }204205// Returns true if the CodeHeap contains CodeBlobs of the given type206bool accepts(int code_blob_type) const { return (_code_blob_type == CodeBlobType::All) ||207(_code_blob_type == code_blob_type); }208int code_blob_type() const { return _code_blob_type; }209210// Debugging / Profiling211const char* name() const { return _name; }212int blob_count() { return _blob_count; }213int nmethod_count() { return _nmethod_count; }214void set_nmethod_count(int count) { _nmethod_count = count; }215int adapter_count() { return _adapter_count; }216void set_adapter_count(int count) { _adapter_count = count; }217int full_count() { return _full_count; }218void report_full() { _full_count++; }219220private:221size_t heap_unallocated_capacity() const;222int defrag_segmap(bool do_defrag);223int segmap_hops(size_t beg, size_t end);224225public:226// Debugging227void verify() PRODUCT_RETURN;228void print() PRODUCT_RETURN;229};230231#endif // SHARE_MEMORY_HEAP_HPP232233234