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