src/share/vm/memory/heap.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_HEAP_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_HEAP_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/virtualspace.hpp"
aoqi@0 30
aoqi@0 31 // Blocks
aoqi@0 32
aoqi@0 33 class HeapBlock VALUE_OBJ_CLASS_SPEC {
aoqi@0 34 friend class VMStructs;
aoqi@0 35
aoqi@0 36 public:
aoqi@0 37 struct Header {
aoqi@0 38 size_t _length; // the length in segments
aoqi@0 39 bool _used; // Used bit
aoqi@0 40 };
aoqi@0 41
aoqi@0 42 protected:
aoqi@0 43 union {
aoqi@0 44 Header _header;
aoqi@0 45 int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];
aoqi@0 46 // pad to 0 mod 8
aoqi@0 47 };
aoqi@0 48
aoqi@0 49 public:
aoqi@0 50 // Initialization
aoqi@0 51 void initialize(size_t length) { _header._length = length; set_used(); }
aoqi@0 52
aoqi@0 53 // Accessors
aoqi@0 54 void* allocated_space() const { return (void*)(this + 1); }
aoqi@0 55 size_t length() const { return _header._length; }
aoqi@0 56
aoqi@0 57 // Used/free
aoqi@0 58 void set_used() { _header._used = true; }
aoqi@0 59 void set_free() { _header._used = false; }
aoqi@0 60 bool free() { return !_header._used; }
aoqi@0 61 };
aoqi@0 62
aoqi@0 63 class FreeBlock: public HeapBlock {
aoqi@0 64 friend class VMStructs;
aoqi@0 65 protected:
aoqi@0 66 FreeBlock* _link;
aoqi@0 67
aoqi@0 68 public:
aoqi@0 69 // Initialization
aoqi@0 70 void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; }
aoqi@0 71
aoqi@0 72 // Merging
aoqi@0 73 void set_length(size_t l) { _header._length = l; }
aoqi@0 74
aoqi@0 75 // Accessors
aoqi@0 76 FreeBlock* link() const { return _link; }
aoqi@0 77 void set_link(FreeBlock* link) { _link = link; }
aoqi@0 78 };
aoqi@0 79
aoqi@0 80 class CodeHeap : public CHeapObj<mtCode> {
aoqi@0 81 friend class VMStructs;
aoqi@0 82 private:
aoqi@0 83 VirtualSpace _memory; // the memory holding the blocks
aoqi@0 84 VirtualSpace _segmap; // the memory holding the segment map
aoqi@0 85
aoqi@0 86 size_t _number_of_committed_segments;
aoqi@0 87 size_t _number_of_reserved_segments;
aoqi@0 88 size_t _segment_size;
aoqi@0 89 int _log2_segment_size;
aoqi@0 90
aoqi@0 91 size_t _next_segment;
aoqi@0 92
aoqi@0 93 FreeBlock* _freelist;
aoqi@0 94 size_t _freelist_segments; // No. of segments in freelist
aoqi@0 95
aoqi@0 96 // Helper functions
aoqi@0 97 size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
aoqi@0 98 size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
aoqi@0 99
aoqi@0 100 size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }
aoqi@0 101 HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
aoqi@0 102
aoqi@0 103 void mark_segmap_as_free(size_t beg, size_t end);
aoqi@0 104 void mark_segmap_as_used(size_t beg, size_t end);
aoqi@0 105
aoqi@0 106 // Freelist management helpers
aoqi@0 107 FreeBlock* following_block(FreeBlock *b);
aoqi@0 108 void insert_after(FreeBlock* a, FreeBlock* b);
aoqi@0 109 void merge_right (FreeBlock* a);
aoqi@0 110
aoqi@0 111 // Toplevel freelist management
aoqi@0 112 void add_to_freelist(HeapBlock *b);
aoqi@0 113 FreeBlock* search_freelist(size_t length, bool is_critical);
aoqi@0 114
aoqi@0 115 // Iteration helpers
aoqi@0 116 void* next_free(HeapBlock* b) const;
aoqi@0 117 HeapBlock* first_block() const;
aoqi@0 118 HeapBlock* next_block(HeapBlock* b) const;
aoqi@0 119 HeapBlock* block_start(void* p) const;
aoqi@0 120
aoqi@0 121 // to perform additional actions on creation of executable code
aoqi@0 122 void on_code_mapping(char* base, size_t size);
aoqi@0 123
aoqi@0 124 public:
aoqi@0 125 CodeHeap();
aoqi@0 126
aoqi@0 127 // Heap extents
aoqi@0 128 bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size);
aoqi@0 129 void release(); // releases all allocated memory
aoqi@0 130 bool expand_by(size_t size); // expands commited memory by size
aoqi@0 131 void shrink_by(size_t size); // shrinks commited memory by size
aoqi@0 132 void clear(); // clears all heap contents
aoqi@0 133
aoqi@0 134 // Memory allocation
aoqi@0 135 void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL
aoqi@0 136 void deallocate(void* p); // deallocates a block
aoqi@0 137
aoqi@0 138 // Attributes
aoqi@0 139 char* low_boundary() const { return _memory.low_boundary (); }
aoqi@0 140 char* high() const { return _memory.high(); }
aoqi@0 141 char* high_boundary() const { return _memory.high_boundary(); }
aoqi@0 142
aoqi@0 143 bool contains(const void* p) const { return low_boundary() <= p && p < high(); }
aoqi@0 144 void* find_start(void* p) const; // returns the block containing p or NULL
aoqi@0 145 size_t alignment_unit() const; // alignment of any block
aoqi@0 146 size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit
aoqi@0 147 static size_t header_size(); // returns the header size for each heap block
aoqi@0 148
aoqi@0 149 // Iteration
aoqi@0 150
aoqi@0 151 // returns the first block or NULL
aoqi@0 152 void* first() const { return next_free(first_block()); }
aoqi@0 153 // returns the next block given a block p or NULL
aoqi@0 154 void* next(void* p) const { return next_free(next_block(block_start(p))); }
aoqi@0 155
aoqi@0 156 // Statistics
aoqi@0 157 size_t capacity() const;
aoqi@0 158 size_t max_capacity() const;
aoqi@0 159 size_t allocated_capacity() const;
aoqi@0 160 size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }
aoqi@0 161
aoqi@0 162 private:
aoqi@0 163 size_t heap_unallocated_capacity() const;
aoqi@0 164
aoqi@0 165 public:
aoqi@0 166 // Debugging
aoqi@0 167 void verify();
aoqi@0 168 void print() PRODUCT_RETURN;
aoqi@0 169 };
aoqi@0 170
aoqi@0 171 #endif // SHARE_VM_MEMORY_HEAP_HPP

mercurial