1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_MEMORY_HEAP_HPP 26 #define SHARE_VM_MEMORY_HEAP_HPP 27 28 #include "code/codeBlob.hpp" 29 #include "memory/allocation.hpp" 30 #include "runtime/virtualspace.hpp" 31 32 // Blocks 33 34 class HeapBlock VALUE_OBJ_CLASS_SPEC { 35 friend class VMStructs; 36 37 public: 38 struct Header { 39 size_t _length; // the length in segments 40 bool _used; // Used bit 41 }; 42 43 protected: 44 union { 45 Header _header; 46 int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ]; 47 // pad to 0 mod 8 48 }; 49 50 public: 51 // Initialization 52 void initialize(size_t length) { _header._length = length; set_used(); } 53 54 // Accessors 55 void* allocated_space() const { return (void*)(this + 1); } 56 size_t length() const { return _header._length; } 57 58 // Used/free 59 void set_used() { _header._used = true; } 60 void set_free() { _header._used = false; } 61 bool free() { return !_header._used; } 62 }; 63 64 class FreeBlock: public HeapBlock { 65 friend class VMStructs; 66 protected: 67 FreeBlock* _link; 68 69 public: 70 // Initialization 71 void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; } 72 73 // Merging 74 void set_length(size_t l) { _header._length = l; } 75 76 // Accessors 77 FreeBlock* link() const { return _link; } 78 void set_link(FreeBlock* link) { _link = link; } 79 }; 80 81 class CodeHeap : public CHeapObj<mtCode> { 82 friend class VMStructs; 83 private: 84 VirtualSpace _memory; // the memory holding the blocks 85 VirtualSpace _segmap; // the memory holding the segment map 86 87 size_t _number_of_committed_segments; 88 size_t _number_of_reserved_segments; 89 size_t _segment_size; 90 int _log2_segment_size; 91 92 size_t _next_segment; 93 94 FreeBlock* _freelist; 95 size_t _freelist_segments; // No. of segments in freelist 96 size_t _max_allocated_capacity; // Peak capacity that was allocated during lifetime of the heap 97 98 const char* _name; // Name of the CodeHeap 99 const int _code_blob_type; // CodeBlobType it contains 100 bool _was_full; // True if CodeHeap was full during runtime 101 102 // Helper functions 103 size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; } 104 size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; } 105 106 size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; } 107 HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); } 108 109 void mark_segmap_as_free(size_t beg, size_t end); 110 void mark_segmap_as_used(size_t beg, size_t end); 111 112 // Freelist management helpers 113 FreeBlock* following_block(FreeBlock *b); 114 void insert_after(FreeBlock* a, FreeBlock* b); 115 void merge_right (FreeBlock* a); 116 117 // Toplevel freelist management 118 void add_to_freelist(HeapBlock *b); 119 FreeBlock* search_freelist(size_t length, bool is_critical); 120 121 // Iteration helpers 122 void* next_free(HeapBlock* b) const; 123 HeapBlock* first_block() const; 124 HeapBlock* next_block(HeapBlock* b) const; 125 HeapBlock* block_start(void* p) const; 126 127 // to perform additional actions on creation of executable code 128 void on_code_mapping(char* base, size_t size); 129 130 public: 131 CodeHeap(const char* name, const int code_blob_type); 132 133 // Heap extents 134 bool reserve(ReservedSpace rs, size_t committed_size, size_t segment_size); 135 void release(); // releases all allocated memory 136 bool expand_by(size_t size); // expands commited memory by size 137 void shrink_by(size_t size); // shrinks commited memory by size 138 void clear(); // clears all heap contents 139 140 // Memory allocation 141 void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL 142 void deallocate(void* p); // deallocates a block 143 144 // Attributes 145 char* low_boundary() const { return _memory.low_boundary (); } 146 char* high() const { return _memory.high(); } 147 char* high_boundary() const { return _memory.high_boundary(); } 148 149 bool contains(const void* p) const { return low_boundary() <= p && p < high(); } 150 void* find_start(void* p) const; // returns the block containing p or NULL 151 size_t alignment_unit() const; // alignment of any block 152 size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit 153 static size_t header_size(); // returns the header size for each heap block 154 155 // Iteration 156 157 // returns the first block or NULL 158 void* first() const { return next_free(first_block()); } 159 // returns the next block given a block p or NULL 160 void* next(void* p) const { return next_free(next_block(block_start(p))); } 161 162 // Statistics 163 size_t capacity() const; 164 size_t max_capacity() const; 165 size_t allocated_capacity() const; 166 size_t max_allocated_capacity() const { return _max_allocated_capacity; } 167 size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); } 168 169 // Returns true if the CodeHeap contains CodeBlobs of the given type 170 bool accepts(int code_blob_type) const { return (_code_blob_type == code_blob_type); } 171 172 // Debugging / Profiling 173 const char* name() const { return _name; } 174 bool was_full() { return _was_full; } 175 void report_full() { _was_full = true; } 176 177 private: 178 size_t heap_unallocated_capacity() const; 179 180 public: 181 // Debugging 182 void verify(); 183 void print() PRODUCT_RETURN; 184 }; 185 186 #endif // SHARE_VM_MEMORY_HEAP_HPP