coleenp@622: /* coleenp@622: * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved. coleenp@622: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@622: * coleenp@622: * This code is free software; you can redistribute it and/or modify it coleenp@622: * under the terms of the GNU General Public License version 2 only, as coleenp@622: * published by the Free Software Foundation. coleenp@622: * coleenp@622: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@622: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@622: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@622: * version 2 for more details (a copy is included in the LICENSE file that coleenp@622: * accompanied this code). coleenp@622: * coleenp@622: * You should have received a copy of the GNU General Public License version coleenp@622: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@622: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@622: * coleenp@622: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, coleenp@622: * CA 95054 USA or visit www.sun.com if you need additional information or coleenp@622: * have any questions. coleenp@622: * coleenp@622: */ coleenp@622: coleenp@622: // coleenp@622: // Free block maintenance for Concurrent Mark Sweep Generation coleenp@622: // coleenp@622: // The main data structure for free blocks are coleenp@622: // . an indexed array of small free blocks, and coleenp@622: // . a dictionary of large free blocks coleenp@622: // coleenp@622: coleenp@622: // No virtuals in FreeChunk (don't want any vtables). coleenp@622: coleenp@622: // A FreeChunk is merely a chunk that can be in a doubly linked list coleenp@622: // and has a size field. NOTE: FreeChunks are distinguished from allocated coleenp@622: // objects in two ways (by the sweeper), depending on whether the VM is 32 or coleenp@622: // 64 bits. coleenp@622: // In 32 bits or 64 bits without CompressedOops, the second word (prev) has the coleenp@622: // LSB set to indicate a free chunk; allocated objects' klass() pointers coleenp@622: // don't have their LSB set. The corresponding bit in the CMSBitMap is coleenp@622: // set when the chunk is allocated. There are also blocks that "look free" coleenp@622: // but are not part of the free list and should not be coalesced into larger coleenp@622: // free blocks. These free blocks have their two LSB's set. coleenp@622: coleenp@622: class FreeChunk VALUE_OBJ_CLASS_SPEC { coleenp@622: friend class VMStructs; coleenp@622: // For 64 bit compressed oops, the markOop encodes both the size and the coleenp@622: // indication that this is a FreeChunk and not an object. coleenp@622: volatile size_t _size; coleenp@622: FreeChunk* _prev; coleenp@622: FreeChunk* _next; coleenp@622: coleenp@622: markOop mark() const volatile { return (markOop)_size; } coleenp@622: void set_mark(markOop m) { _size = (size_t)m; } coleenp@622: coleenp@622: public: coleenp@622: NOT_PRODUCT(static const size_t header_size();) coleenp@622: coleenp@622: // Returns "true" if the address indicates that the block represents coleenp@622: // a free chunk. coleenp@622: static bool indicatesFreeChunk(const HeapWord* addr) { coleenp@622: // Force volatile read from addr because value might change between coleenp@622: // calls. We really want the read of _mark and _prev from this pointer coleenp@622: // to be volatile but making the fields volatile causes all sorts of coleenp@622: // compilation errors. coleenp@622: return ((volatile FreeChunk*)addr)->isFree(); coleenp@622: } coleenp@622: coleenp@622: bool isFree() const volatile { coleenp@622: LP64_ONLY(if (UseCompressedOops) return mark()->is_cms_free_chunk(); else) coleenp@622: return (((intptr_t)_prev) & 0x1) == 0x1; coleenp@622: } coleenp@622: bool cantCoalesce() const { coleenp@622: assert(isFree(), "can't get coalesce bit on not free"); coleenp@622: return (((intptr_t)_prev) & 0x2) == 0x2; coleenp@622: } coleenp@622: void dontCoalesce() { coleenp@622: // the block should be free coleenp@622: assert(isFree(), "Should look like a free block"); coleenp@622: _prev = (FreeChunk*)(((intptr_t)_prev) | 0x2); coleenp@622: } coleenp@622: FreeChunk* prev() const { coleenp@622: return (FreeChunk*)(((intptr_t)_prev) & ~(0x3)); coleenp@622: } coleenp@622: coleenp@622: debug_only(void* prev_addr() const { return (void*)&_prev; }) coleenp@622: coleenp@622: size_t size() const volatile { coleenp@622: LP64_ONLY(if (UseCompressedOops) return mark()->get_size(); else ) coleenp@622: return _size; coleenp@622: } coleenp@622: void setSize(size_t sz) { coleenp@622: LP64_ONLY(if (UseCompressedOops) set_mark(markOopDesc::set_size_and_free(sz)); else ) coleenp@622: _size = sz; coleenp@622: } coleenp@622: coleenp@622: FreeChunk* next() const { return _next; } coleenp@622: coleenp@622: void linkAfter(FreeChunk* ptr) { coleenp@622: linkNext(ptr); coleenp@622: if (ptr != NULL) ptr->linkPrev(this); coleenp@622: } coleenp@622: void linkAfterNonNull(FreeChunk* ptr) { coleenp@622: assert(ptr != NULL, "precondition violation"); coleenp@622: linkNext(ptr); coleenp@622: ptr->linkPrev(this); coleenp@622: } coleenp@622: void linkNext(FreeChunk* ptr) { _next = ptr; } coleenp@622: void linkPrev(FreeChunk* ptr) { coleenp@622: LP64_ONLY(if (UseCompressedOops) _prev = ptr; else) coleenp@622: _prev = (FreeChunk*)((intptr_t)ptr | 0x1); coleenp@622: } coleenp@622: void clearPrev() { _prev = NULL; } coleenp@622: void clearNext() { _next = NULL; } coleenp@622: void markNotFree() { coleenp@622: LP64_ONLY(if (UseCompressedOops) set_mark(markOopDesc::prototype());) coleenp@622: // Also set _prev to null coleenp@622: _prev = NULL; coleenp@622: } coleenp@622: coleenp@622: // Return the address past the end of this chunk coleenp@622: HeapWord* end() const { return ((HeapWord*) this) + size(); } coleenp@622: coleenp@622: // debugging coleenp@622: void verify() const PRODUCT_RETURN; coleenp@622: void verifyList() const PRODUCT_RETURN; coleenp@622: void mangleAllocated(size_t size) PRODUCT_RETURN; coleenp@622: void mangleFreed(size_t size) PRODUCT_RETURN; coleenp@622: }; coleenp@622: coleenp@622: // Alignment helpers etc. coleenp@622: #define numQuanta(x,y) ((x+y-1)/y) coleenp@622: enum AlignmentConstants { coleenp@622: MinChunkSize = numQuanta(sizeof(FreeChunk), MinObjAlignmentInBytes) * MinObjAlignment coleenp@622: }; coleenp@622: