annotate src/share/vm/memory/heap.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 82db0859acbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_heap.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 size_t CodeHeap::header_size() {
a61af66fc99e Initial load
duke
parents:
diff changeset
30 return sizeof(HeapBlock);
a61af66fc99e Initial load
duke
parents:
diff changeset
31 }
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // Implementation of Heap
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 CodeHeap::CodeHeap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 _number_of_committed_segments = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 _number_of_reserved_segments = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 _segment_size = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 _log2_segment_size = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 _next_segment = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 _freelist = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 _free_segments = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 }
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
49 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // setup _segmap pointers for faster indexing
a61af66fc99e Initial load
duke
parents:
diff changeset
51 address p = (address)_segmap.low() + beg;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 address q = (address)_segmap.low() + end;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // initialize interval
a61af66fc99e Initial load
duke
parents:
diff changeset
54 while (p < q) *p++ = 0xFF;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57
a61af66fc99e Initial load
duke
parents:
diff changeset
58 void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
59 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
60 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // setup _segmap pointers for faster indexing
a61af66fc99e Initial load
duke
parents:
diff changeset
62 address p = (address)_segmap.low() + beg;
a61af66fc99e Initial load
duke
parents:
diff changeset
63 address q = (address)_segmap.low() + end;
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // initialize interval
a61af66fc99e Initial load
duke
parents:
diff changeset
65 int i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 while (p < q) {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 *p++ = i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 if (i == 0xFF) i = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 }
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 static size_t align_to_page_size(size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 const size_t alignment = (size_t)os::vm_page_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 assert(is_power_of_2(alignment), "no kidding ???");
a61af66fc99e Initial load
duke
parents:
diff changeset
76 return (size + alignment - 1) & ~(alignment - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 static size_t align_to_allocation_size(size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 const size_t alignment = (size_t)os::vm_allocation_granularity();
a61af66fc99e Initial load
duke
parents:
diff changeset
82 assert(is_power_of_2(alignment), "no kidding ???");
a61af66fc99e Initial load
duke
parents:
diff changeset
83 return (size + alignment - 1) & ~(alignment - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
84 }
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 void CodeHeap::on_code_mapping(char* base, size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 #ifdef LINUX
a61af66fc99e Initial load
duke
parents:
diff changeset
89 extern void linux_wrap_code(char* base, size_t size);
a61af66fc99e Initial load
duke
parents:
diff changeset
90 linux_wrap_code(base, size);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 bool CodeHeap::reserve(size_t reserved_size, size_t committed_size,
a61af66fc99e Initial load
duke
parents:
diff changeset
96 size_t segment_size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 assert(reserved_size >= committed_size, "reserved < committed");
a61af66fc99e Initial load
duke
parents:
diff changeset
98 assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
a61af66fc99e Initial load
duke
parents:
diff changeset
99 assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 _segment_size = segment_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
102 _log2_segment_size = exact_log2(segment_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 // Reserve and initialize space for _memory.
a61af66fc99e Initial load
duke
parents:
diff changeset
105 const size_t page_size = os::page_size_for_region(committed_size,
a61af66fc99e Initial load
duke
parents:
diff changeset
106 reserved_size, 8);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 const size_t granularity = os::vm_allocation_granularity();
a61af66fc99e Initial load
duke
parents:
diff changeset
108 const size_t r_align = MAX2(page_size, granularity);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 const size_t r_size = align_size_up(reserved_size, r_align);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 const size_t c_size = align_size_up(committed_size, page_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 :
a61af66fc99e Initial load
duke
parents:
diff changeset
113 MAX2(page_size, granularity);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 ReservedSpace rs(r_size, rs_align, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
115 os::trace_page_sizes("code heap", committed_size, reserved_size, page_size,
a61af66fc99e Initial load
duke
parents:
diff changeset
116 rs.base(), rs.size());
a61af66fc99e Initial load
duke
parents:
diff changeset
117 if (!_memory.initialize(rs, c_size)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 }
a61af66fc99e Initial load
duke
parents:
diff changeset
120
a61af66fc99e Initial load
duke
parents:
diff changeset
121 on_code_mapping(_memory.low(), _memory.committed_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
122 _number_of_committed_segments = number_of_segments(_memory.committed_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
123 _number_of_reserved_segments = number_of_segments(_memory.reserved_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
124 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // reserve space for _segmap
a61af66fc99e Initial load
duke
parents:
diff changeset
127 if (!_segmap.initialize(align_to_page_size(_number_of_reserved_segments), align_to_page_size(_number_of_committed_segments))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit enough space for segment map");
a61af66fc99e Initial load
duke
parents:
diff changeset
131 assert(_segmap.reserved_size() >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
a61af66fc99e Initial load
duke
parents:
diff changeset
132 assert(_segmap.reserved_size() >= _segmap.committed_size() , "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // initialize remaining instance variables
a61af66fc99e Initial load
duke
parents:
diff changeset
135 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
136 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140 void CodeHeap::release() {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 Unimplemented();
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 bool CodeHeap::expand_by(size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // expand _memory space
a61af66fc99e Initial load
duke
parents:
diff changeset
147 size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if (dm > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 char* base = _memory.low() + _memory.committed_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
150 if (!_memory.expand_by(dm)) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 on_code_mapping(base, dm);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 size_t i = _number_of_committed_segments;
a61af66fc99e Initial load
duke
parents:
diff changeset
153 _number_of_committed_segments = number_of_segments(_memory.committed_size());
a61af66fc99e Initial load
duke
parents:
diff changeset
154 assert(_number_of_reserved_segments == number_of_segments(_memory.reserved_size()), "number of reserved segments should not change");
a61af66fc99e Initial load
duke
parents:
diff changeset
155 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // expand _segmap space
a61af66fc99e Initial load
duke
parents:
diff changeset
157 size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
158 if (ds > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 if (!_segmap.expand_by(ds)) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
162 // initialize additional segmap entries
a61af66fc99e Initial load
duke
parents:
diff changeset
163 mark_segmap_as_free(i, _number_of_committed_segments);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 void CodeHeap::shrink_by(size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
170 Unimplemented();
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 void CodeHeap::clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
175 _next_segment = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
176 mark_segmap_as_free(0, _number_of_committed_segments);
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 void* CodeHeap::allocate(size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 size_t length = number_of_segments(size + sizeof(HeapBlock));
a61af66fc99e Initial load
duke
parents:
diff changeset
182 assert(length *_segment_size >= sizeof(FreeBlock), "not enough room for FreeList");
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 // First check if we can satify request from freelist
a61af66fc99e Initial load
duke
parents:
diff changeset
185 debug_only(verify());
a61af66fc99e Initial load
duke
parents:
diff changeset
186 HeapBlock* block = search_freelist(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 debug_only(if (VerifyCodeCacheOften) verify());
a61af66fc99e Initial load
duke
parents:
diff changeset
188 if (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 assert(block->length() >= length && block->length() < length + CodeCacheMinBlockLength, "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
190 assert(!block->free(), "must be marked free");
a61af66fc99e Initial load
duke
parents:
diff changeset
191 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
192 memset((void *)block->allocated_space(), badCodeHeapNewVal, size);
a61af66fc99e Initial load
duke
parents:
diff changeset
193 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return block->allocated_space();
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196
a61af66fc99e Initial load
duke
parents:
diff changeset
197 if (length < CodeCacheMinBlockLength) {
a61af66fc99e Initial load
duke
parents:
diff changeset
198 length = CodeCacheMinBlockLength;
a61af66fc99e Initial load
duke
parents:
diff changeset
199 }
a61af66fc99e Initial load
duke
parents:
diff changeset
200 if (_next_segment + length <= _number_of_committed_segments) {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 mark_segmap_as_used(_next_segment, _next_segment + length);
a61af66fc99e Initial load
duke
parents:
diff changeset
202 HeapBlock* b = block_at(_next_segment);
a61af66fc99e Initial load
duke
parents:
diff changeset
203 b->initialize(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
204 _next_segment += length;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
206 memset((void *)b->allocated_space(), badCodeHeapNewVal, size);
a61af66fc99e Initial load
duke
parents:
diff changeset
207 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
208 return b->allocated_space();
a61af66fc99e Initial load
duke
parents:
diff changeset
209 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }
a61af66fc99e Initial load
duke
parents:
diff changeset
213
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 void CodeHeap::deallocate(void* p) {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 assert(p == find_start(p), "illegal deallocation");
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // Find start of HeapBlock
a61af66fc99e Initial load
duke
parents:
diff changeset
218 HeapBlock* b = (((HeapBlock *)p) - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 assert(b->allocated_space() == p, "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
220 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
221 memset((void *)b->allocated_space(),
a61af66fc99e Initial load
duke
parents:
diff changeset
222 badCodeHeapFreeVal,
a61af66fc99e Initial load
duke
parents:
diff changeset
223 size(b->length()) - sizeof(HeapBlock));
a61af66fc99e Initial load
duke
parents:
diff changeset
224 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
225 add_to_freelist(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 debug_only(if (VerifyCodeCacheOften) verify());
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 void* CodeHeap::find_start(void* p) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 if (!contains(p)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 size_t i = segment_for(p);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 address b = (address)_segmap.low();
a61af66fc99e Initial load
duke
parents:
diff changeset
237 if (b[i] == 0xFF) {
a61af66fc99e Initial load
duke
parents:
diff changeset
238 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240 while (b[i] > 0) i -= (int)b[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
241 HeapBlock* h = block_at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
242 if (h->free()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
244 }
a61af66fc99e Initial load
duke
parents:
diff changeset
245 return h->allocated_space();
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248
a61af66fc99e Initial load
duke
parents:
diff changeset
249 size_t CodeHeap::alignment_unit() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
250 // this will be a power of two
a61af66fc99e Initial load
duke
parents:
diff changeset
251 return _segment_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253
a61af66fc99e Initial load
duke
parents:
diff changeset
254
a61af66fc99e Initial load
duke
parents:
diff changeset
255 size_t CodeHeap::alignment_offset() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
256 // The lowest address in any allocated block will be
a61af66fc99e Initial load
duke
parents:
diff changeset
257 // equal to alignment_offset (mod alignment_unit).
a61af66fc99e Initial load
duke
parents:
diff changeset
258 return sizeof(HeapBlock) & (_segment_size - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
259 }
a61af66fc99e Initial load
duke
parents:
diff changeset
260
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // Finds the next free heapblock. If the current one is free, that it returned
a61af66fc99e Initial load
duke
parents:
diff changeset
262 void* CodeHeap::next_free(HeapBlock *b) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 // Since free blocks are merged, there is max. on free block
a61af66fc99e Initial load
duke
parents:
diff changeset
264 // between two used ones
a61af66fc99e Initial load
duke
parents:
diff changeset
265 if (b != NULL && b->free()) b = next_block(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
266 assert(b == NULL || !b->free(), "must be in use or at end of heap");
a61af66fc99e Initial load
duke
parents:
diff changeset
267 return (b == NULL) ? NULL : b->allocated_space();
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // Returns the first used HeapBlock
a61af66fc99e Initial load
duke
parents:
diff changeset
271 HeapBlock* CodeHeap::first_block() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 if (_next_segment > 0)
a61af66fc99e Initial load
duke
parents:
diff changeset
273 return block_at(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
274 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276
a61af66fc99e Initial load
duke
parents:
diff changeset
277 HeapBlock *CodeHeap::block_start(void *q) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 HeapBlock* b = (HeapBlock*)find_start(q);
a61af66fc99e Initial load
duke
parents:
diff changeset
279 if (b == NULL) return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
280 return b - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 }
a61af66fc99e Initial load
duke
parents:
diff changeset
282
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // Returns the next Heap block an offset into one
a61af66fc99e Initial load
duke
parents:
diff changeset
284 HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 if (b == NULL) return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 size_t i = segment_for(b) + b->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
287 if (i < _next_segment)
a61af66fc99e Initial load
duke
parents:
diff changeset
288 return block_at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 }
a61af66fc99e Initial load
duke
parents:
diff changeset
291
a61af66fc99e Initial load
duke
parents:
diff changeset
292
a61af66fc99e Initial load
duke
parents:
diff changeset
293 // Returns current capacity
a61af66fc99e Initial load
duke
parents:
diff changeset
294 size_t CodeHeap::capacity() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 return _memory.committed_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
296 }
a61af66fc99e Initial load
duke
parents:
diff changeset
297
a61af66fc99e Initial load
duke
parents:
diff changeset
298 size_t CodeHeap::max_capacity() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 return _memory.reserved_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301
a61af66fc99e Initial load
duke
parents:
diff changeset
302 size_t CodeHeap::allocated_capacity() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 // Start with the committed size in _memory;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 size_t l = _memory.committed_size();
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 // Subtract the committed, but unused, segments
a61af66fc99e Initial load
duke
parents:
diff changeset
307 l -= size(_number_of_committed_segments - _next_segment);
a61af66fc99e Initial load
duke
parents:
diff changeset
308
a61af66fc99e Initial load
duke
parents:
diff changeset
309 // Subtract the size of the freelist
a61af66fc99e Initial load
duke
parents:
diff changeset
310 l -= size(_free_segments);
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 return l;
a61af66fc99e Initial load
duke
parents:
diff changeset
313 }
a61af66fc99e Initial load
duke
parents:
diff changeset
314
a61af66fc99e Initial load
duke
parents:
diff changeset
315 // Free list management
a61af66fc99e Initial load
duke
parents:
diff changeset
316
a61af66fc99e Initial load
duke
parents:
diff changeset
317 FreeBlock *CodeHeap::following_block(FreeBlock *b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
318 return (FreeBlock*)(((address)b) + _segment_size * b->length());
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // Inserts block b after a
a61af66fc99e Initial load
duke
parents:
diff changeset
322 void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 assert(a != NULL && b != NULL, "must be real pointers");
a61af66fc99e Initial load
duke
parents:
diff changeset
324
a61af66fc99e Initial load
duke
parents:
diff changeset
325 // Link b into the list after a
a61af66fc99e Initial load
duke
parents:
diff changeset
326 b->set_link(a->link());
a61af66fc99e Initial load
duke
parents:
diff changeset
327 a->set_link(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
328
a61af66fc99e Initial load
duke
parents:
diff changeset
329 // See if we can merge blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
330 merge_right(b); // Try to make b bigger
a61af66fc99e Initial load
duke
parents:
diff changeset
331 merge_right(a); // Try to make a include b
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333
a61af66fc99e Initial load
duke
parents:
diff changeset
334 // Try to merge this block with the following block
a61af66fc99e Initial load
duke
parents:
diff changeset
335 void CodeHeap::merge_right(FreeBlock *a) {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 assert(a->free(), "must be a free block");
a61af66fc99e Initial load
duke
parents:
diff changeset
337 if (following_block(a) == a->link()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 assert(a->link() != NULL && a->link()->free(), "must be free too");
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // Update block a to include the following block
a61af66fc99e Initial load
duke
parents:
diff changeset
340 a->set_length(a->length() + a->link()->length());
a61af66fc99e Initial load
duke
parents:
diff changeset
341 a->set_link(a->link()->link());
a61af66fc99e Initial load
duke
parents:
diff changeset
342 // Update find_start map
a61af66fc99e Initial load
duke
parents:
diff changeset
343 size_t beg = segment_for(a);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 mark_segmap_as_used(beg, beg + a->length());
a61af66fc99e Initial load
duke
parents:
diff changeset
345 }
a61af66fc99e Initial load
duke
parents:
diff changeset
346 }
a61af66fc99e Initial load
duke
parents:
diff changeset
347
a61af66fc99e Initial load
duke
parents:
diff changeset
348 void CodeHeap::add_to_freelist(HeapBlock *a) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 FreeBlock* b = (FreeBlock*)a;
a61af66fc99e Initial load
duke
parents:
diff changeset
350 assert(b != _freelist, "cannot be removed twice");
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 // Mark as free and update free space count
a61af66fc99e Initial load
duke
parents:
diff changeset
353 _free_segments += b->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
354 b->set_free();
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356 // First element in list?
a61af66fc99e Initial load
duke
parents:
diff changeset
357 if (_freelist == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 _freelist = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
359 b->set_link(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
360 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 }
a61af66fc99e Initial load
duke
parents:
diff changeset
362
a61af66fc99e Initial load
duke
parents:
diff changeset
363 // Scan for right place to put into list. List
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // is sorted by increasing addresseses
a61af66fc99e Initial load
duke
parents:
diff changeset
365 FreeBlock* prev = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
366 FreeBlock* cur = _freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 while(cur != NULL && cur < b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
368 assert(prev == NULL || prev < cur, "must be ordered");
a61af66fc99e Initial load
duke
parents:
diff changeset
369 prev = cur;
a61af66fc99e Initial load
duke
parents:
diff changeset
370 cur = cur->link();
a61af66fc99e Initial load
duke
parents:
diff changeset
371 }
a61af66fc99e Initial load
duke
parents:
diff changeset
372
a61af66fc99e Initial load
duke
parents:
diff changeset
373 assert( (prev == NULL && b < _freelist) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
374 (prev < b && (cur == NULL || b < cur)), "list must be ordered");
a61af66fc99e Initial load
duke
parents:
diff changeset
375
a61af66fc99e Initial load
duke
parents:
diff changeset
376 if (prev == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
377 // Insert first in list
a61af66fc99e Initial load
duke
parents:
diff changeset
378 b->set_link(_freelist);
a61af66fc99e Initial load
duke
parents:
diff changeset
379 _freelist = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
380 merge_right(_freelist);
a61af66fc99e Initial load
duke
parents:
diff changeset
381 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 insert_after(prev, b);
a61af66fc99e Initial load
duke
parents:
diff changeset
383 }
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385
a61af66fc99e Initial load
duke
parents:
diff changeset
386 // Search freelist for an entry on the list with the best fit
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // Return NULL if no one was found
a61af66fc99e Initial load
duke
parents:
diff changeset
388 FreeBlock* CodeHeap::search_freelist(size_t length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 FreeBlock *best_block = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
390 FreeBlock *best_prev = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
391 size_t best_length = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
392
a61af66fc99e Initial load
duke
parents:
diff changeset
393 // Search for smallest block which is bigger than length
a61af66fc99e Initial load
duke
parents:
diff changeset
394 FreeBlock *prev = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
395 FreeBlock *cur = _freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
396 while(cur != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
397 size_t l = cur->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
398 if (l >= length && (best_block == NULL || best_length > l)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // Remember best block, its previous element, and its length
a61af66fc99e Initial load
duke
parents:
diff changeset
400 best_block = cur;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 best_prev = prev;
a61af66fc99e Initial load
duke
parents:
diff changeset
402 best_length = best_block->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
403 }
a61af66fc99e Initial load
duke
parents:
diff changeset
404
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // Next element in list
a61af66fc99e Initial load
duke
parents:
diff changeset
406 prev = cur;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 cur = cur->link();
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
a61af66fc99e Initial load
duke
parents:
diff changeset
409
a61af66fc99e Initial load
duke
parents:
diff changeset
410 if (best_block == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
411 // None found
a61af66fc99e Initial load
duke
parents:
diff changeset
412 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
413 }
a61af66fc99e Initial load
duke
parents:
diff changeset
414
a61af66fc99e Initial load
duke
parents:
diff changeset
415 assert((best_prev == NULL && _freelist == best_block ) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
416 (best_prev != NULL && best_prev->link() == best_block), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
417
a61af66fc99e Initial load
duke
parents:
diff changeset
418 // Exact (or at least good enough) fit. Remove from list.
a61af66fc99e Initial load
duke
parents:
diff changeset
419 // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
a61af66fc99e Initial load
duke
parents:
diff changeset
420 if (best_length < length + CodeCacheMinBlockLength) {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 length = best_length;
a61af66fc99e Initial load
duke
parents:
diff changeset
422 if (best_prev == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 assert(_freelist == best_block, "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
424 _freelist = _freelist->link();
a61af66fc99e Initial load
duke
parents:
diff changeset
425 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
426 // Unmap element
a61af66fc99e Initial load
duke
parents:
diff changeset
427 best_prev->set_link(best_block->link());
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
430 // Truncate block and return a pointer to the following block
a61af66fc99e Initial load
duke
parents:
diff changeset
431 best_block->set_length(best_length - length);
a61af66fc99e Initial load
duke
parents:
diff changeset
432 best_block = following_block(best_block);
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // Set used bit and length on new block
a61af66fc99e Initial load
duke
parents:
diff changeset
434 size_t beg = segment_for(best_block);
a61af66fc99e Initial load
duke
parents:
diff changeset
435 mark_segmap_as_used(beg, beg + length);
a61af66fc99e Initial load
duke
parents:
diff changeset
436 best_block->set_length(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
437 }
a61af66fc99e Initial load
duke
parents:
diff changeset
438
a61af66fc99e Initial load
duke
parents:
diff changeset
439 best_block->set_used();
a61af66fc99e Initial load
duke
parents:
diff changeset
440 _free_segments -= length;
a61af66fc99e Initial load
duke
parents:
diff changeset
441 return best_block;
a61af66fc99e Initial load
duke
parents:
diff changeset
442 }
a61af66fc99e Initial load
duke
parents:
diff changeset
443
a61af66fc99e Initial load
duke
parents:
diff changeset
444 //----------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
445 // Non-product code
a61af66fc99e Initial load
duke
parents:
diff changeset
446
a61af66fc99e Initial load
duke
parents:
diff changeset
447 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
448
a61af66fc99e Initial load
duke
parents:
diff changeset
449 void CodeHeap::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
450 tty->print_cr("The Heap");
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
a61af66fc99e Initial load
duke
parents:
diff changeset
452
a61af66fc99e Initial load
duke
parents:
diff changeset
453 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
454
a61af66fc99e Initial load
duke
parents:
diff changeset
455 void CodeHeap::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // Count the number of blocks on the freelist, and the amount of space
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // represented.
a61af66fc99e Initial load
duke
parents:
diff changeset
458 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
459 size_t len = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
460 for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
461 len += b->length();
a61af66fc99e Initial load
duke
parents:
diff changeset
462 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
463 }
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 // Verify that freelist contains the right amount of free space
a61af66fc99e Initial load
duke
parents:
diff changeset
466 guarantee(len == _free_segments, "wrong freelist");
a61af66fc99e Initial load
duke
parents:
diff changeset
467
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // Verify that the number of free blocks is not out of hand.
a61af66fc99e Initial load
duke
parents:
diff changeset
469 static int free_block_threshold = 10000;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 if (count > free_block_threshold) {
a61af66fc99e Initial load
duke
parents:
diff changeset
471 warning("CodeHeap: # of free blocks > %d", free_block_threshold);
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // Double the warning limit
a61af66fc99e Initial load
duke
parents:
diff changeset
473 free_block_threshold *= 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
474 }
a61af66fc99e Initial load
duke
parents:
diff changeset
475
a61af66fc99e Initial load
duke
parents:
diff changeset
476 // Verify that the freelist contains the same number of free blocks that is
a61af66fc99e Initial load
duke
parents:
diff changeset
477 // found on the full list.
a61af66fc99e Initial load
duke
parents:
diff changeset
478 for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 if (h->free()) count--;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 }
a61af66fc99e Initial load
duke
parents:
diff changeset
481 guarantee(count == 0, "missing free blocks");
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }