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