1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/concurrentMarkSweep/freeList.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,304 @@ 1.4 +/* 1.5 + * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_freeList.cpp.incl" 1.30 + 1.31 +// Free list. A FreeList is used to access a linked list of chunks 1.32 +// of space in the heap. The head and tail are maintained so that 1.33 +// items can be (as in the current implementation) added at the 1.34 +// at the tail of the list and removed from the head of the list to 1.35 +// maintain a FIFO queue. 1.36 + 1.37 +FreeList::FreeList() : 1.38 + _head(NULL), _tail(NULL) 1.39 +#ifdef ASSERT 1.40 + , _protecting_lock(NULL) 1.41 +#endif 1.42 +{ 1.43 + _size = 0; 1.44 + _count = 0; 1.45 + _hint = 0; 1.46 + init_statistics(); 1.47 +} 1.48 + 1.49 +FreeList::FreeList(FreeChunk* fc) : 1.50 + _head(fc), _tail(fc) 1.51 +#ifdef ASSERT 1.52 + , _protecting_lock(NULL) 1.53 +#endif 1.54 +{ 1.55 + _size = fc->size(); 1.56 + _count = 1; 1.57 + _hint = 0; 1.58 + init_statistics(); 1.59 +#ifndef PRODUCT 1.60 + _allocation_stats.set_returnedBytes(size() * HeapWordSize); 1.61 +#endif 1.62 +} 1.63 + 1.64 +FreeList::FreeList(HeapWord* addr, size_t size) : 1.65 + _head((FreeChunk*) addr), _tail((FreeChunk*) addr) 1.66 +#ifdef ASSERT 1.67 + , _protecting_lock(NULL) 1.68 +#endif 1.69 +{ 1.70 + assert(size > sizeof(FreeChunk), "size is too small"); 1.71 + head()->setSize(size); 1.72 + _size = size; 1.73 + _count = 1; 1.74 + init_statistics(); 1.75 +#ifndef PRODUCT 1.76 + _allocation_stats.set_returnedBytes(_size * HeapWordSize); 1.77 +#endif 1.78 +} 1.79 + 1.80 +void FreeList::reset(size_t hint) { 1.81 + set_count(0); 1.82 + set_head(NULL); 1.83 + set_tail(NULL); 1.84 + set_hint(hint); 1.85 +} 1.86 + 1.87 +void FreeList::init_statistics() { 1.88 + _allocation_stats.initialize(); 1.89 +} 1.90 + 1.91 +FreeChunk* FreeList::getChunkAtHead() { 1.92 + assert_proper_lock_protection(); 1.93 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.94 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.95 + FreeChunk* fc = head(); 1.96 + if (fc != NULL) { 1.97 + FreeChunk* nextFC = fc->next(); 1.98 + if (nextFC != NULL) { 1.99 + // The chunk fc being removed has a "next". Set the "next" to the 1.100 + // "prev" of fc. 1.101 + nextFC->linkPrev(NULL); 1.102 + } else { // removed tail of list 1.103 + link_tail(NULL); 1.104 + } 1.105 + link_head(nextFC); 1.106 + decrement_count(); 1.107 + } 1.108 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.109 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.110 + return fc; 1.111 +} 1.112 + 1.113 + 1.114 +void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) { 1.115 + assert_proper_lock_protection(); 1.116 + assert(fl->count() == 0, "Precondition"); 1.117 + if (count() > 0) { 1.118 + int k = 1; 1.119 + fl->set_head(head()); n--; 1.120 + FreeChunk* tl = head(); 1.121 + while (tl->next() != NULL && n > 0) { 1.122 + tl = tl->next(); n--; k++; 1.123 + } 1.124 + assert(tl != NULL, "Loop Inv."); 1.125 + 1.126 + // First, fix up the list we took from. 1.127 + FreeChunk* new_head = tl->next(); 1.128 + set_head(new_head); 1.129 + set_count(count() - k); 1.130 + if (new_head == NULL) { 1.131 + set_tail(NULL); 1.132 + } else { 1.133 + new_head->linkPrev(NULL); 1.134 + } 1.135 + // Now we can fix up the tail. 1.136 + tl->linkNext(NULL); 1.137 + // And return the result. 1.138 + fl->set_tail(tl); 1.139 + fl->set_count(k); 1.140 + } 1.141 +} 1.142 + 1.143 +// Remove this chunk from the list 1.144 +void FreeList::removeChunk(FreeChunk*fc) { 1.145 + assert_proper_lock_protection(); 1.146 + assert(head() != NULL, "Remove from empty list"); 1.147 + assert(fc != NULL, "Remove a NULL chunk"); 1.148 + assert(size() == fc->size(), "Wrong list"); 1.149 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.150 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.151 + 1.152 + FreeChunk* prevFC = fc->prev(); 1.153 + FreeChunk* nextFC = fc->next(); 1.154 + if (nextFC != NULL) { 1.155 + // The chunk fc being removed has a "next". Set the "next" to the 1.156 + // "prev" of fc. 1.157 + nextFC->linkPrev(prevFC); 1.158 + } else { // removed tail of list 1.159 + link_tail(prevFC); 1.160 + } 1.161 + if (prevFC == NULL) { // removed head of list 1.162 + link_head(nextFC); 1.163 + assert(nextFC == NULL || nextFC->prev() == NULL, 1.164 + "Prev of head should be NULL"); 1.165 + } else { 1.166 + prevFC->linkNext(nextFC); 1.167 + assert(tail() != prevFC || prevFC->next() == NULL, 1.168 + "Next of tail should be NULL"); 1.169 + } 1.170 + decrement_count(); 1.171 +#define TRAP_CODE 1 1.172 +#if TRAP_CODE 1.173 + if (head() == NULL) { 1.174 + guarantee(tail() == NULL, "INVARIANT"); 1.175 + guarantee(count() == 0, "INVARIANT"); 1.176 + } 1.177 +#endif 1.178 + // clear next and prev fields of fc, debug only 1.179 + NOT_PRODUCT( 1.180 + fc->linkPrev(NULL); 1.181 + fc->linkNext(NULL); 1.182 + ) 1.183 + assert(fc->isFree(), "Should still be a free chunk"); 1.184 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.185 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.186 + assert(head() == NULL || head()->size() == size(), "wrong item on list"); 1.187 + assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); 1.188 +} 1.189 + 1.190 +// Add this chunk at the head of the list. 1.191 +void FreeList::returnChunkAtHead(FreeChunk* chunk, bool record_return) { 1.192 + assert_proper_lock_protection(); 1.193 + assert(chunk != NULL, "insert a NULL chunk"); 1.194 + assert(size() == chunk->size(), "Wrong size"); 1.195 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.196 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.197 + 1.198 + FreeChunk* oldHead = head(); 1.199 + assert(chunk != oldHead, "double insertion"); 1.200 + chunk->linkAfter(oldHead); 1.201 + link_head(chunk); 1.202 + if (oldHead == NULL) { // only chunk in list 1.203 + assert(tail() == NULL, "inconsistent FreeList"); 1.204 + link_tail(chunk); 1.205 + } 1.206 + increment_count(); // of # of chunks in list 1.207 + DEBUG_ONLY( 1.208 + if (record_return) { 1.209 + increment_returnedBytes_by(size()*HeapWordSize); 1.210 + } 1.211 + ) 1.212 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.213 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.214 + assert(head() == NULL || head()->size() == size(), "wrong item on list"); 1.215 + assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); 1.216 +} 1.217 + 1.218 +void FreeList::returnChunkAtHead(FreeChunk* chunk) { 1.219 + assert_proper_lock_protection(); 1.220 + returnChunkAtHead(chunk, true); 1.221 +} 1.222 + 1.223 +// Add this chunk at the tail of the list. 1.224 +void FreeList::returnChunkAtTail(FreeChunk* chunk, bool record_return) { 1.225 + assert_proper_lock_protection(); 1.226 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.227 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.228 + assert(chunk != NULL, "insert a NULL chunk"); 1.229 + assert(size() == chunk->size(), "wrong size"); 1.230 + 1.231 + FreeChunk* oldTail = tail(); 1.232 + assert(chunk != oldTail, "double insertion"); 1.233 + if (oldTail != NULL) { 1.234 + oldTail->linkAfter(chunk); 1.235 + } else { // only chunk in list 1.236 + assert(head() == NULL, "inconsistent FreeList"); 1.237 + link_head(chunk); 1.238 + } 1.239 + link_tail(chunk); 1.240 + increment_count(); // of # of chunks in list 1.241 + DEBUG_ONLY( 1.242 + if (record_return) { 1.243 + increment_returnedBytes_by(size()*HeapWordSize); 1.244 + } 1.245 + ) 1.246 + assert(head() == NULL || head()->prev() == NULL, "list invariant"); 1.247 + assert(tail() == NULL || tail()->next() == NULL, "list invariant"); 1.248 + assert(head() == NULL || head()->size() == size(), "wrong item on list"); 1.249 + assert(tail() == NULL || tail()->size() == size(), "wrong item on list"); 1.250 +} 1.251 + 1.252 +void FreeList::returnChunkAtTail(FreeChunk* chunk) { 1.253 + returnChunkAtTail(chunk, true); 1.254 +} 1.255 + 1.256 +void FreeList::prepend(FreeList* fl) { 1.257 + assert_proper_lock_protection(); 1.258 + if (fl->count() > 0) { 1.259 + if (count() == 0) { 1.260 + set_head(fl->head()); 1.261 + set_tail(fl->tail()); 1.262 + set_count(fl->count()); 1.263 + } else { 1.264 + // Both are non-empty. 1.265 + FreeChunk* fl_tail = fl->tail(); 1.266 + FreeChunk* this_head = head(); 1.267 + assert(fl_tail->next() == NULL, "Well-formedness of fl"); 1.268 + fl_tail->linkNext(this_head); 1.269 + this_head->linkPrev(fl_tail); 1.270 + set_head(fl->head()); 1.271 + set_count(count() + fl->count()); 1.272 + } 1.273 + fl->set_head(NULL); 1.274 + fl->set_tail(NULL); 1.275 + fl->set_count(0); 1.276 + } 1.277 +} 1.278 + 1.279 +// verifyChunkInFreeLists() is used to verify that an item is in this free list. 1.280 +// It is used as a debugging aid. 1.281 +bool FreeList::verifyChunkInFreeLists(FreeChunk* fc) const { 1.282 + // This is an internal consistency check, not part of the check that the 1.283 + // chunk is in the free lists. 1.284 + guarantee(fc->size() == size(), "Wrong list is being searched"); 1.285 + FreeChunk* curFC = head(); 1.286 + while (curFC) { 1.287 + // This is an internal consistency check. 1.288 + guarantee(size() == curFC->size(), "Chunk is in wrong list."); 1.289 + if (fc == curFC) { 1.290 + return true; 1.291 + } 1.292 + curFC = curFC->next(); 1.293 + } 1.294 + return false; 1.295 +} 1.296 + 1.297 +#ifndef PRODUCT 1.298 +void FreeList::assert_proper_lock_protection_work() const { 1.299 +#ifdef ASSERT 1.300 + if (_protecting_lock != NULL && 1.301 + SharedHeap::heap()->n_par_threads() > 0) { 1.302 + // Should become an assert. 1.303 + guarantee(_protecting_lock->owned_by_self(), "FreeList RACE DETECTED"); 1.304 + } 1.305 +#endif 1.306 +} 1.307 +#endif