1 /* 2 * Copyright (c) 2001, 2015, 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_FREELIST_HPP 26 #define SHARE_VM_MEMORY_FREELIST_HPP 27 28 class CompactibleFreeListSpace; 29 30 // A class for maintaining a free list of Chunk's. The FreeList 31 // maintains a the structure of the list (head, tail, etc.) plus 32 // statistics for allocations from the list. The links between items 33 // are not part of FreeList. The statistics are 34 // used to make decisions about coalescing Chunk's when they 35 // are swept during collection. 36 // 37 // See the corresponding .cpp file for a description of the specifics 38 // for that implementation. 39 40 class Mutex; 41 42 template <class Chunk_t> 43 class FreeList VALUE_OBJ_CLASS_SPEC { 44 friend class CompactibleFreeListSpace; 45 friend class VMStructs; 46 47 private: 48 Chunk_t* _head; // Head of list of free chunks 49 Chunk_t* _tail; // Tail of list of free chunks 50 size_t _size; // Size in Heap words of each chunk 51 ssize_t _count; // Number of entries in list 52 53 protected: 54 55 #ifdef ASSERT 56 Mutex* _protecting_lock; 57 void assert_proper_lock_protection_work() const; 58 #endif 59 60 // Asserts false if the protecting lock (if any) is not held. 61 void assert_proper_lock_protection() const { 62 DEBUG_ONLY(assert_proper_lock_protection_work()); 63 } 64 65 void increment_count() { 66 _count++; 67 } 68 69 void decrement_count() { 70 _count--; 71 assert(_count >= 0, "Count should not be negative"); 72 } 73 74 public: 75 // Constructor 76 // Construct a list without any entries. 77 FreeList(); 78 79 // Do initialization 80 void initialize(); 81 82 // Reset the head, tail, and count of a free list. 83 void reset(); 84 85 // Declare the current free list to be protected by the given lock. 86 #ifdef ASSERT 87 Mutex* protecting_lock() const { return _protecting_lock; } 88 void set_protecting_lock(Mutex* v) { 89 _protecting_lock = v; 90 } 91 #endif 92 93 // Accessors. 94 Chunk_t* head() const { 95 assert_proper_lock_protection(); 96 return _head; 97 } 98 void set_head(Chunk_t* v) { 99 assert_proper_lock_protection(); 100 _head = v; 101 assert(!_head || _head->size() == _size, "bad chunk size"); 102 } 103 // Set the head of the list and set the prev field of non-null 104 // values to NULL. 105 void link_head(Chunk_t* v); 106 107 Chunk_t* tail() const { 108 assert_proper_lock_protection(); 109 return _tail; 110 } 111 void set_tail(Chunk_t* v) { 112 assert_proper_lock_protection(); 113 _tail = v; 114 assert(!_tail || _tail->size() == _size, "bad chunk size"); 115 } 116 // Set the tail of the list and set the next field of non-null 117 // values to NULL. 118 void link_tail(Chunk_t* v) { 119 assert_proper_lock_protection(); 120 set_tail(v); 121 if (v != NULL) { 122 v->clear_next(); 123 } 124 } 125 126 // No locking checks in read-accessors: lock-free reads (only) are benign. 127 // Readers are expected to have the lock if they are doing work that 128 // requires atomicity guarantees in sections of code. 129 size_t size() const { 130 return _size; 131 } 132 void set_size(size_t v) { 133 assert_proper_lock_protection(); 134 _size = v; 135 } 136 ssize_t count() const { return _count; } 137 void set_count(ssize_t v) { _count = v;} 138 139 size_t get_better_size() { return size(); } 140 141 size_t returned_bytes() const { ShouldNotReachHere(); return 0; } 142 void set_returned_bytes(size_t v) {} 143 void increment_returned_bytes_by(size_t v) {} 144 145 // Unlink head of list and return it. Returns NULL if 146 // the list is empty. 147 Chunk_t* get_chunk_at_head(); 148 149 // Remove the first "n" or "count", whichever is smaller, chunks from the 150 // list, setting "fl", which is required to be empty, to point to them. 151 void getFirstNChunksFromList(size_t n, FreeList<Chunk_t>* fl); 152 153 // Unlink this chunk from it's free list 154 void remove_chunk(Chunk_t* fc); 155 156 // Add this chunk to this free list. 157 void return_chunk_at_head(Chunk_t* fc); 158 void return_chunk_at_tail(Chunk_t* fc); 159 160 // Similar to returnChunk* but also records some diagnostic 161 // information. 162 void return_chunk_at_head(Chunk_t* fc, bool record_return); 163 void return_chunk_at_tail(Chunk_t* fc, bool record_return); 164 165 // Prepend "fl" (whose size is required to be the same as that of "this") 166 // to the front of "this" list. 167 void prepend(FreeList<Chunk_t>* fl); 168 169 // Verify that the chunk is in the list. 170 // found. Return NULL if "fc" is not found. 171 bool verify_chunk_in_free_list(Chunk_t* fc) const; 172 173 // Printing support 174 static void print_labels_on(outputStream* st, const char* c); 175 void print_on(outputStream* st, const char* c = NULL) const; 176 }; 177 178 #endif // SHARE_VM_MEMORY_FREELIST_HPP