aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_MEMORY_HEAP_HPP aoqi@0: #define SHARE_VM_MEMORY_HEAP_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "runtime/virtualspace.hpp" aoqi@0: aoqi@0: // Blocks aoqi@0: aoqi@0: class HeapBlock VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class VMStructs; aoqi@0: aoqi@0: public: aoqi@0: struct Header { aoqi@0: size_t _length; // the length in segments aoqi@0: bool _used; // Used bit aoqi@0: }; aoqi@0: aoqi@0: protected: aoqi@0: union { aoqi@0: Header _header; aoqi@0: int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ]; aoqi@0: // pad to 0 mod 8 aoqi@0: }; aoqi@0: aoqi@0: public: aoqi@0: // Initialization aoqi@0: void initialize(size_t length) { _header._length = length; set_used(); } aoqi@0: aoqi@0: // Accessors aoqi@0: void* allocated_space() const { return (void*)(this + 1); } aoqi@0: size_t length() const { return _header._length; } aoqi@0: aoqi@0: // Used/free aoqi@0: void set_used() { _header._used = true; } aoqi@0: void set_free() { _header._used = false; } aoqi@0: bool free() { return !_header._used; } aoqi@0: }; aoqi@0: aoqi@0: class FreeBlock: public HeapBlock { aoqi@0: friend class VMStructs; aoqi@0: protected: aoqi@0: FreeBlock* _link; aoqi@0: aoqi@0: public: aoqi@0: // Initialization aoqi@0: void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; } aoqi@0: aoqi@0: // Merging aoqi@0: void set_length(size_t l) { _header._length = l; } aoqi@0: aoqi@0: // Accessors aoqi@0: FreeBlock* link() const { return _link; } aoqi@0: void set_link(FreeBlock* link) { _link = link; } aoqi@0: }; aoqi@0: aoqi@0: class CodeHeap : public CHeapObj { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: VirtualSpace _memory; // the memory holding the blocks aoqi@0: VirtualSpace _segmap; // the memory holding the segment map aoqi@0: aoqi@0: size_t _number_of_committed_segments; aoqi@0: size_t _number_of_reserved_segments; aoqi@0: size_t _segment_size; aoqi@0: int _log2_segment_size; aoqi@0: aoqi@0: size_t _next_segment; aoqi@0: aoqi@0: FreeBlock* _freelist; aoqi@0: size_t _freelist_segments; // No. of segments in freelist aoqi@0: aoqi@0: // Helper functions aoqi@0: size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; } aoqi@0: size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; } aoqi@0: aoqi@0: size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; } aoqi@0: HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); } aoqi@0: aoqi@0: void mark_segmap_as_free(size_t beg, size_t end); aoqi@0: void mark_segmap_as_used(size_t beg, size_t end); aoqi@0: aoqi@0: // Freelist management helpers aoqi@0: FreeBlock* following_block(FreeBlock *b); aoqi@0: void insert_after(FreeBlock* a, FreeBlock* b); aoqi@0: void merge_right (FreeBlock* a); aoqi@0: aoqi@0: // Toplevel freelist management aoqi@0: void add_to_freelist(HeapBlock *b); aoqi@0: FreeBlock* search_freelist(size_t length, bool is_critical); aoqi@0: aoqi@0: // Iteration helpers aoqi@0: void* next_free(HeapBlock* b) const; aoqi@0: HeapBlock* first_block() const; aoqi@0: HeapBlock* next_block(HeapBlock* b) const; aoqi@0: HeapBlock* block_start(void* p) const; aoqi@0: aoqi@0: // to perform additional actions on creation of executable code aoqi@0: void on_code_mapping(char* base, size_t size); aoqi@0: aoqi@0: public: aoqi@0: CodeHeap(); aoqi@0: aoqi@0: // Heap extents aoqi@0: bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size); aoqi@0: void release(); // releases all allocated memory aoqi@0: bool expand_by(size_t size); // expands commited memory by size aoqi@0: void shrink_by(size_t size); // shrinks commited memory by size aoqi@0: void clear(); // clears all heap contents aoqi@0: aoqi@0: // Memory allocation aoqi@0: void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL aoqi@0: void deallocate(void* p); // deallocates a block aoqi@0: aoqi@0: // Attributes aoqi@0: char* low_boundary() const { return _memory.low_boundary (); } aoqi@0: char* high() const { return _memory.high(); } aoqi@0: char* high_boundary() const { return _memory.high_boundary(); } aoqi@0: aoqi@0: bool contains(const void* p) const { return low_boundary() <= p && p < high(); } aoqi@0: void* find_start(void* p) const; // returns the block containing p or NULL aoqi@0: size_t alignment_unit() const; // alignment of any block aoqi@0: size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit aoqi@0: static size_t header_size(); // returns the header size for each heap block aoqi@0: aoqi@0: // Iteration aoqi@0: aoqi@0: // returns the first block or NULL aoqi@0: void* first() const { return next_free(first_block()); } aoqi@0: // returns the next block given a block p or NULL aoqi@0: void* next(void* p) const { return next_free(next_block(block_start(p))); } aoqi@0: aoqi@0: // Statistics aoqi@0: size_t capacity() const; aoqi@0: size_t max_capacity() const; aoqi@0: size_t allocated_capacity() const; aoqi@0: size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); } aoqi@0: aoqi@0: private: aoqi@0: size_t heap_unallocated_capacity() const; aoqi@0: aoqi@0: public: aoqi@0: // Debugging aoqi@0: void verify(); aoqi@0: void print() PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_HEAP_HPP