void FreeList::getFirstNChunksFromList(size_t n, FreeList* fl) { assert_proper_lock_protection(); assert(fl->count() == 0, "Precondition"); if (count() > 0) { int k = 1; fl->set_head(head()); n--; FreeChunk* tl = head(); while (tl->next() != NULL && n > 0) { tl = tl->next(); n--; k++; } assert(tl != NULL, "Loop Inv."); // First, fix up the list we took from. FreeChunk* new_head = tl->next(); set_head(new_head); set_count(count() - k); if (new_head == NULL) { set_tail(NULL); } else { new_head->linkPrev(NULL); } // Now we can fix up the tail. tl->linkNext(NULL); // And return the result. fl->set_tail(tl); fl->set_count(k); } }
// Remove this chunk from the list void FreeList::removeChunk(FreeChunk*fc) { assert_proper_lock_protection(); assert(head() != NULL, "Remove from empty list"); assert(fc != NULL, "Remove a NULL chunk"); assert(size() == fc->size(), "Wrong list"); assert(head() == NULL || head()->prev() == NULL, "list invariant"); assert(tail() == NULL || tail()->next() == NULL, "list invariant"); FreeChunk* prevFC = fc->prev(); FreeChunk* nextFC = fc->next(); if (nextFC != NULL) { // The chunk fc being removed has a "next". Set the "next" to the // "prev" of fc. nextFC->linkPrev(prevFC); } else { // removed tail of list link_tail(prevFC); } if (prevFC == NULL) { // removed head of list link_head(nextFC); assert(nextFC == NULL || nextFC->prev() == NULL, "Prev of head should be NULL"); } else { prevFC->linkNext(nextFC); assert(tail() != prevFC || prevFC->next() == NULL, "Next of tail should be NULL"); } decrement_count(); assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0, "H/T/C Inconsistency"); // clear next and prev fields of fc, debug only NOT_PRODUCT( fc->linkPrev(NULL); fc->linkNext(NULL); )