0N/A/*
1827N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_MEMORY_HEAP_HPP
1879N/A#define SHARE_VM_MEMORY_HEAP_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "runtime/virtualspace.hpp"
1879N/A
0N/A// Blocks
0N/A
0N/Aclass HeapBlock VALUE_OBJ_CLASS_SPEC {
0N/A friend class VMStructs;
0N/A
0N/A public:
0N/A struct Header {
0N/A size_t _length; // the length in segments
0N/A bool _used; // Used bit
0N/A };
0N/A
0N/A protected:
0N/A union {
0N/A Header _header;
0N/A int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];
0N/A // pad to 0 mod 8
0N/A };
0N/A
0N/A public:
0N/A // Initialization
0N/A void initialize(size_t length) { _header._length = length; set_used(); }
0N/A
0N/A // Accessors
0N/A void* allocated_space() const { return (void*)(this + 1); }
0N/A size_t length() const { return _header._length; }
0N/A
0N/A // Used/free
0N/A void set_used() { _header._used = true; }
0N/A void set_free() { _header._used = false; }
0N/A bool free() { return !_header._used; }
0N/A};
0N/A
0N/Aclass FreeBlock: public HeapBlock {
0N/A friend class VMStructs;
0N/A protected:
0N/A FreeBlock* _link;
0N/A
0N/A public:
0N/A // Initialization
0N/A void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; }
0N/A
0N/A // Merging
0N/A void set_length(size_t l) { _header._length = l; }
0N/A
0N/A // Accessors
0N/A FreeBlock* link() const { return _link; }
0N/A void set_link(FreeBlock* link) { _link = link; }
0N/A};
0N/A
3863N/Aclass CodeHeap : public CHeapObj<mtCode> {
0N/A friend class VMStructs;
0N/A private:
0N/A VirtualSpace _memory; // the memory holding the blocks
0N/A VirtualSpace _segmap; // the memory holding the segment map
0N/A
0N/A size_t _number_of_committed_segments;
0N/A size_t _number_of_reserved_segments;
0N/A size_t _segment_size;
0N/A int _log2_segment_size;
0N/A
0N/A size_t _next_segment;
0N/A
0N/A FreeBlock* _freelist;
0N/A size_t _free_segments; // No. of segments in freelist
0N/A
0N/A // Helper functions
0N/A size_t number_of_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
0N/A size_t size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
0N/A
0N/A size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }
0N/A HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
0N/A
0N/A void mark_segmap_as_free(size_t beg, size_t end);
0N/A void mark_segmap_as_used(size_t beg, size_t end);
0N/A
0N/A // Freelist management helpers
0N/A FreeBlock* following_block(FreeBlock *b);
0N/A void insert_after(FreeBlock* a, FreeBlock* b);
0N/A void merge_right (FreeBlock* a);
0N/A
0N/A // Toplevel freelist management
0N/A void add_to_freelist(HeapBlock *b);
0N/A FreeBlock* search_freelist(size_t length);
0N/A
0N/A // Iteration helpers
0N/A void* next_free(HeapBlock* b) const;
0N/A HeapBlock* first_block() const;
0N/A HeapBlock* next_block(HeapBlock* b) const;
0N/A HeapBlock* block_start(void* p) const;
0N/A
0N/A // to perform additional actions on creation of executable code
0N/A void on_code_mapping(char* base, size_t size);
0N/A
0N/A public:
0N/A CodeHeap();
0N/A
0N/A // Heap extents
0N/A bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size);
0N/A void release(); // releases all allocated memory
0N/A bool expand_by(size_t size); // expands commited memory by size
0N/A void shrink_by(size_t size); // shrinks commited memory by size
0N/A void clear(); // clears all heap contents
0N/A
0N/A // Memory allocation
0N/A void* allocate (size_t size); // allocates a block of size or returns NULL
0N/A void deallocate(void* p); // deallocates a block
0N/A
0N/A // Attributes
0N/A void* begin() const { return _memory.low (); }
0N/A void* end() const { return _memory.high(); }
0N/A bool contains(void* p) const { return begin() <= p && p < end(); }
0N/A void* find_start(void* p) const; // returns the block containing p or NULL
0N/A size_t alignment_unit() const; // alignment of any block
0N/A size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit
0N/A static size_t header_size(); // returns the header size for each heap block
0N/A
0N/A // Returns reserved area high and low addresses
0N/A char *low_boundary() const { return _memory.low_boundary (); }
1827N/A char *high() const { return _memory.high(); }
0N/A char *high_boundary() const { return _memory.high_boundary(); }
0N/A
0N/A // Iteration
0N/A
0N/A // returns the first block or NULL
0N/A void* first() const { return next_free(first_block()); }
0N/A // returns the next block given a block p or NULL
0N/A void* next(void* p) const { return next_free(next_block(block_start(p))); }
0N/A
0N/A // Statistics
0N/A size_t capacity() const;
0N/A size_t max_capacity() const;
0N/A size_t allocated_capacity() const;
0N/A size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }
1979N/A size_t largest_free_block() const;
0N/A
0N/A // Debugging
0N/A void verify();
0N/A void print() PRODUCT_RETURN;
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_MEMORY_HEAP_HPP