aoqi@0: /* aoqi@0: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: #ifndef SHARE_VM_MEMORY_METACHUNK_HPP aoqi@0: #define SHARE_VM_MEMORY_METACHUNK_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/debug.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: class VirtualSpaceNode; aoqi@0: aoqi@0: // Super class of Metablock and Metachunk to allow them to aoqi@0: // be put on the FreeList and in the BinaryTreeDictionary. aoqi@0: template aoqi@0: class Metabase VALUE_OBJ_CLASS_SPEC { aoqi@0: size_t _word_size; aoqi@0: T* _next; aoqi@0: T* _prev; aoqi@0: aoqi@0: protected: aoqi@0: Metabase(size_t word_size) : _word_size(word_size), _next(NULL), _prev(NULL) {} aoqi@0: aoqi@0: public: aoqi@0: T* next() const { return _next; } aoqi@0: T* prev() const { return _prev; } aoqi@0: void set_next(T* v) { _next = v; assert(v != this, "Boom");} aoqi@0: void set_prev(T* v) { _prev = v; assert(v != this, "Boom");} aoqi@0: void clear_next() { set_next(NULL); } aoqi@0: void clear_prev() { set_prev(NULL); } aoqi@0: aoqi@0: size_t size() const volatile { return _word_size; } aoqi@0: void set_size(size_t v) { _word_size = v; } aoqi@0: aoqi@0: void link_next(T* ptr) { set_next(ptr); } aoqi@0: void link_prev(T* ptr) { set_prev(ptr); } aoqi@0: void link_after(T* ptr) { aoqi@0: link_next(ptr); aoqi@0: if (ptr != NULL) ptr->link_prev((T*)this); aoqi@0: } aoqi@0: aoqi@0: uintptr_t* end() const { return ((uintptr_t*) this) + size(); } aoqi@0: aoqi@0: bool cantCoalesce() const { return false; } aoqi@0: aoqi@0: // Debug support aoqi@0: #ifdef ASSERT aoqi@0: void* prev_addr() const { return (void*)&_prev; } aoqi@0: void* next_addr() const { return (void*)&_next; } aoqi@0: void* size_addr() const { return (void*)&_word_size; } aoqi@0: #endif aoqi@0: bool verify_chunk_in_free_list(T* tc) const { return true; } aoqi@0: bool verify_par_locked() { return true; } aoqi@0: aoqi@0: void assert_is_mangled() const {/* Don't check "\*/} aoqi@0: aoqi@0: bool is_free() { return true; } aoqi@0: }; aoqi@0: aoqi@0: // Metachunk - Quantum of allocation from a Virtualspace aoqi@0: // Metachunks are reused (when freed are put on a global freelist) and aoqi@0: // have no permanent association to a SpaceManager. aoqi@0: aoqi@0: // +--------------+ <- end --+ --+ aoqi@0: // | | | | aoqi@0: // | | | free | aoqi@0: // | | | | aoqi@0: // | | | | size | capacity aoqi@0: // | | | | aoqi@0: // | | <- top -- + | aoqi@0: // | | | | aoqi@0: // | | | used | aoqi@0: // | | | | aoqi@0: // | | | | aoqi@0: // +--------------+ <- bottom --+ --+ aoqi@0: aoqi@0: class Metachunk : public Metabase { aoqi@0: friend class TestMetachunk; aoqi@0: // The VirtualSpaceNode containing this chunk. aoqi@0: VirtualSpaceNode* _container; aoqi@0: aoqi@0: // Current allocation top. aoqi@0: MetaWord* _top; aoqi@0: aoqi@0: DEBUG_ONLY(bool _is_tagged_free;) aoqi@0: aoqi@0: MetaWord* initial_top() const { return (MetaWord*)this + overhead(); } aoqi@0: MetaWord* top() const { return _top; } aoqi@0: aoqi@0: public: aoqi@0: // Metachunks are allocated out of a MetadataVirtualSpace and aoqi@0: // and use some of its space to describe itself (plus alignment aoqi@0: // considerations). Metadata is allocated in the rest of the chunk. aoqi@0: // This size is the overhead of maintaining the Metachunk within aoqi@0: // the space. aoqi@0: aoqi@0: // Alignment of each allocation in the chunks. aoqi@0: static size_t object_alignment(); aoqi@0: aoqi@0: // Size of the Metachunk header, including alignment. aoqi@0: static size_t overhead(); aoqi@0: aoqi@0: Metachunk(size_t word_size , VirtualSpaceNode* container); aoqi@0: aoqi@0: MetaWord* allocate(size_t word_size); aoqi@0: aoqi@0: VirtualSpaceNode* container() const { return _container; } aoqi@0: aoqi@0: MetaWord* bottom() const { return (MetaWord*) this; } aoqi@0: aoqi@0: // Reset top to bottom so chunk can be reused. aoqi@0: void reset_empty() { _top = initial_top(); clear_next(); clear_prev(); } aoqi@0: bool is_empty() { return _top == initial_top(); } aoqi@0: aoqi@0: // used (has been allocated) aoqi@0: // free (available for future allocations) aoqi@0: size_t word_size() const { return size(); } aoqi@0: size_t used_word_size() const; aoqi@0: size_t free_word_size() const; aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: bool is_tagged_free() { return _is_tagged_free; } aoqi@0: void set_is_tagged_free(bool v) { _is_tagged_free = v; } aoqi@0: #endif aoqi@0: aoqi@0: bool contains(const void* ptr) { return bottom() <= ptr && ptr < _top; } aoqi@0: aoqi@0: NOT_PRODUCT(void mangle();) aoqi@0: aoqi@0: void print_on(outputStream* st) const; aoqi@0: void verify(); aoqi@0: }; aoqi@0: aoqi@0: // Metablock is the unit of allocation from a Chunk. aoqi@0: // aoqi@0: // A Metablock may be reused by its SpaceManager but are never moved between aoqi@0: // SpaceManagers. There is no explicit link to the Metachunk aoqi@0: // from which it was allocated. Metablock may be deallocated and aoqi@0: // put on a freelist but the space is never freed, rather aoqi@0: // the Metachunk it is a part of will be deallocated when it's aoqi@0: // associated class loader is collected. aoqi@0: aoqi@0: class Metablock : public Metabase { aoqi@0: friend class VMStructs; aoqi@0: public: aoqi@0: Metablock(size_t word_size) : Metabase(word_size) {} aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_METACHUNK_HPP