coleenp@622: /* mikael@4153: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. coleenp@622: * coleenp@622: */ coleenp@622: stefank@2314: #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_FREECHUNK_HPP stefank@2314: #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_FREECHUNK_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/memRegion.hpp" stefank@2314: #include "oops/markOop.hpp" stefank@2314: #include "runtime/mutex.hpp" stefank@2314: #include "utilities/debug.hpp" stefank@2314: #include "utilities/globalDefinitions.hpp" stefank@2314: #include "utilities/ostream.hpp" stefank@2314: 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. jmasa@3732: return ((volatile FreeChunk*)addr)->is_free(); coleenp@622: } coleenp@622: jmasa@3732: bool is_free() 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 { jmasa@3732: assert(is_free(), "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 jmasa@3732: assert(is_free(), "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; }) jmasa@698: debug_only(void* next_addr() const { return (void*)&_next; }) jmasa@698: debug_only(void* size_addr() const { return (void*)&_size; }) 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: } jmasa@3732: void set_size(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: jmasa@3732: void link_after(FreeChunk* ptr) { jmasa@3732: link_next(ptr); jmasa@3732: if (ptr != NULL) ptr->link_prev(this); coleenp@622: } jmasa@3732: void link_next(FreeChunk* ptr) { _next = ptr; } jmasa@3732: void link_prev(FreeChunk* ptr) { ysr@2071: LP64_ONLY(if (UseCompressedOops) _prev = ptr; else) ysr@2071: _prev = (FreeChunk*)((intptr_t)ptr | 0x1); coleenp@622: } jmasa@3732: void clear_next() { _next = NULL; } coleenp@622: void markNotFree() { ysr@2071: // Set _prev (klass) to null before (if) clearing the mark word below ysr@2071: _prev = NULL; ysr@2071: #ifdef _LP64 ysr@2071: if (UseCompressedOops) { ysr@2071: OrderAccess::storestore(); ysr@2071: set_mark(markOopDesc::prototype()); ysr@2071: } ysr@2071: #endif jmasa@3732: assert(!is_free(), "Error"); coleenp@622: } coleenp@622: coleenp@622: // Return the address past the end of this chunk jmasa@4196: uintptr_t* end() const { return ((uintptr_t*) 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; ysr@1580: ysr@1580: void print_on(outputStream* st); coleenp@622: }; coleenp@622: kvn@1926: extern size_t MinChunkSize; coleenp@622: stefank@2314: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_FREECHUNK_HPP