Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/heap.hpp
32285 views
/*1* Copyright (c) 1997, 2013, 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_HEAP_HPP25#define SHARE_VM_MEMORY_HEAP_HPP2627#include "memory/allocation.hpp"28#include "runtime/virtualspace.hpp"2930// Blocks3132class HeapBlock VALUE_OBJ_CLASS_SPEC {33friend class VMStructs;3435public:36struct Header {37size_t _length; // the length in segments38bool _used; // Used bit39};4041protected:42union {43Header _header;44int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];45// pad to 0 mod 846};4748public:49// Initialization50void initialize(size_t length) { _header._length = length; set_used(); }5152// Accessors53void* allocated_space() const { return (void*)(this + 1); }54size_t length() const { return _header._length; }5556// Used/free57void set_used() { _header._used = true; }58void set_free() { _header._used = false; }59bool free() { return !_header._used; }60};6162class FreeBlock: public HeapBlock {63friend class VMStructs;64protected:65FreeBlock* _link;6667public:68// Initialization69void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; }7071// Merging72void set_length(size_t l) { _header._length = l; }7374// Accessors75FreeBlock* link() const { return _link; }76void set_link(FreeBlock* link) { _link = link; }77};7879class CodeHeap : public CHeapObj<mtCode> {80friend class VMStructs;81private:82VirtualSpace _memory; // the memory holding the blocks83VirtualSpace _segmap; // the memory holding the segment map8485size_t _number_of_committed_segments;86size_t _number_of_reserved_segments;87size_t _segment_size;88int _log2_segment_size;8990size_t _next_segment;9192FreeBlock* _freelist;93size_t _freelist_segments; // No. of segments in freelist9495// Helper functions96size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }97size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }9899size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }100HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }101102void mark_segmap_as_free(size_t beg, size_t end);103void mark_segmap_as_used(size_t beg, size_t end);104105// Freelist management helpers106FreeBlock* following_block(FreeBlock *b);107void insert_after(FreeBlock* a, FreeBlock* b);108void merge_right (FreeBlock* a);109110// Toplevel freelist management111void add_to_freelist(HeapBlock *b);112FreeBlock* search_freelist(size_t length, bool is_critical);113114// Iteration helpers115void* next_free(HeapBlock* b) const;116HeapBlock* first_block() const;117HeapBlock* next_block(HeapBlock* b) const;118HeapBlock* block_start(void* p) const;119120// to perform additional actions on creation of executable code121void on_code_mapping(char* base, size_t size);122123public:124CodeHeap();125126// Heap extents127bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size);128void release(); // releases all allocated memory129bool expand_by(size_t size); // expands commited memory by size130void shrink_by(size_t size); // shrinks commited memory by size131void clear(); // clears all heap contents132133// Memory allocation134void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL135void deallocate(void* p); // deallocates a block136137// Attributes138char* low_boundary() const { return _memory.low_boundary (); }139char* high() const { return _memory.high(); }140char* high_boundary() const { return _memory.high_boundary(); }141142bool contains(const void* p) const { return low_boundary() <= p && p < high(); }143void* find_start(void* p) const; // returns the block containing p or NULL144size_t alignment_unit() const; // alignment of any block145size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit146static size_t header_size(); // returns the header size for each heap block147148// Iteration149150// returns the first block or NULL151void* first() const { return next_free(first_block()); }152// returns the next block given a block p or NULL153void* next(void* p) const { return next_free(next_block(block_start(p))); }154155// Statistics156size_t capacity() const;157size_t max_capacity() const;158size_t allocated_capacity() const;159size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }160161private:162size_t heap_unallocated_capacity() const;163164public:165// Debugging166void verify();167void print() PRODUCT_RETURN;168};169170#endif // SHARE_VM_MEMORY_HEAP_HPP171172173