1 | /* |
2 | * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. |
8 | * |
9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 | * version 2 for more details (a copy is included in the LICENSE file that |
13 | * accompanied this code). |
14 | * |
15 | * You should have received a copy of the GNU General Public License version |
16 | * 2 along with this work; if not, write to the Free Software Foundation, |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
18 | * |
19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 | * or visit www.oracle.com if you need additional information or have any |
21 | * questions. |
22 | * |
23 | */ |
24 | |
25 | #ifndef SHARE_MEMORY_FREELIST_INLINE_HPP |
26 | #define SHARE_MEMORY_FREELIST_INLINE_HPP |
27 | |
28 | #include "gc/shared/collectedHeap.hpp" |
29 | #include "memory/freeList.hpp" |
30 | #include "runtime/globals.hpp" |
31 | #include "runtime/mutex.hpp" |
32 | #include "runtime/vmThread.hpp" |
33 | #include "utilities/macros.hpp" |
34 | |
35 | // Free list. A FreeList is used to access a linked list of chunks |
36 | // of space in the heap. The head and tail are maintained so that |
37 | // items can be (as in the current implementation) added at the |
38 | // at the tail of the list and removed from the head of the list to |
39 | // maintain a FIFO queue. |
40 | |
41 | template <class Chunk> |
42 | FreeList<Chunk>::FreeList() : |
43 | _head(NULL), _tail(NULL) |
44 | #ifdef ASSERT |
45 | , _protecting_lock(NULL) |
46 | #endif |
47 | { |
48 | _size = 0; |
49 | _count = 0; |
50 | } |
51 | |
52 | template <class Chunk> |
53 | void FreeList<Chunk>::link_head(Chunk* v) { |
54 | assert_proper_lock_protection(); |
55 | set_head(v); |
56 | // If this method is not used (just set the head instead), |
57 | // this check can be avoided. |
58 | if (v != NULL) { |
59 | v->link_prev(NULL); |
60 | } |
61 | } |
62 | |
63 | |
64 | |
65 | template <class Chunk> |
66 | void FreeList<Chunk>::reset() { |
67 | // Don't set the _size to 0 because this method is |
68 | // used with a existing list that has a size but which has |
69 | // been emptied. |
70 | // Don't clear the _protecting_lock of an existing list. |
71 | set_count(0); |
72 | set_head(NULL); |
73 | set_tail(NULL); |
74 | } |
75 | |
76 | template <class Chunk> |
77 | void FreeList<Chunk>::initialize() { |
78 | #ifdef ASSERT |
79 | // Needed early because it might be checked in other initializing code. |
80 | set_protecting_lock(NULL); |
81 | #endif |
82 | reset(); |
83 | set_size(0); |
84 | } |
85 | |
86 | template <class Chunk_t> |
87 | Chunk_t* FreeList<Chunk_t>::get_chunk_at_head() { |
88 | assert_proper_lock_protection(); |
89 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
90 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
91 | Chunk_t* fc = head(); |
92 | if (fc != NULL) { |
93 | Chunk_t* nextFC = fc->next(); |
94 | if (nextFC != NULL) { |
95 | // The chunk fc being removed has a "next". Set the "next" to the |
96 | // "prev" of fc. |
97 | nextFC->link_prev(NULL); |
98 | } else { // removed tail of list |
99 | link_tail(NULL); |
100 | } |
101 | link_head(nextFC); |
102 | decrement_count(); |
103 | } |
104 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
105 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
106 | return fc; |
107 | } |
108 | |
109 | |
110 | template <class Chunk> |
111 | void FreeList<Chunk>::getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl) { |
112 | assert_proper_lock_protection(); |
113 | assert(fl->count() == 0, "Precondition" ); |
114 | if (count() > 0) { |
115 | int k = 1; |
116 | fl->set_head(head()); n--; |
117 | Chunk* tl = head(); |
118 | while (tl->next() != NULL && n > 0) { |
119 | tl = tl->next(); n--; k++; |
120 | } |
121 | assert(tl != NULL, "Loop Inv." ); |
122 | |
123 | // First, fix up the list we took from. |
124 | Chunk* new_head = tl->next(); |
125 | set_head(new_head); |
126 | set_count(count() - k); |
127 | if (new_head == NULL) { |
128 | set_tail(NULL); |
129 | } else { |
130 | new_head->link_prev(NULL); |
131 | } |
132 | // Now we can fix up the tail. |
133 | tl->link_next(NULL); |
134 | // And return the result. |
135 | fl->set_tail(tl); |
136 | fl->set_count(k); |
137 | } |
138 | } |
139 | |
140 | // Remove this chunk from the list |
141 | template <class Chunk> |
142 | void FreeList<Chunk>::remove_chunk(Chunk*fc) { |
143 | assert_proper_lock_protection(); |
144 | assert(head() != NULL, "Remove from empty list" ); |
145 | assert(fc != NULL, "Remove a NULL chunk" ); |
146 | assert(size() == fc->size(), "Wrong list" ); |
147 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
148 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
149 | |
150 | Chunk* prevFC = fc->prev(); |
151 | Chunk* nextFC = fc->next(); |
152 | if (nextFC != NULL) { |
153 | // The chunk fc being removed has a "next". Set the "next" to the |
154 | // "prev" of fc. |
155 | nextFC->link_prev(prevFC); |
156 | } else { // removed tail of list |
157 | link_tail(prevFC); |
158 | } |
159 | if (prevFC == NULL) { // removed head of list |
160 | link_head(nextFC); |
161 | assert(nextFC == NULL || nextFC->prev() == NULL, |
162 | "Prev of head should be NULL" ); |
163 | } else { |
164 | prevFC->link_next(nextFC); |
165 | assert(tail() != prevFC || prevFC->next() == NULL, |
166 | "Next of tail should be NULL" ); |
167 | } |
168 | decrement_count(); |
169 | assert(((head() == NULL) + (tail() == NULL) + (count() == 0)) % 3 == 0, |
170 | "H/T/C Inconsistency" ); |
171 | // clear next and prev fields of fc, debug only |
172 | NOT_PRODUCT( |
173 | fc->link_prev(NULL); |
174 | fc->link_next(NULL); |
175 | ) |
176 | assert(fc->is_free(), "Should still be a free chunk" ); |
177 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
178 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
179 | assert(head() == NULL || head()->size() == size(), "wrong item on list" ); |
180 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list" ); |
181 | } |
182 | |
183 | // Add this chunk at the head of the list. |
184 | template <class Chunk> |
185 | void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk, bool record_return) { |
186 | assert_proper_lock_protection(); |
187 | assert(chunk != NULL, "insert a NULL chunk" ); |
188 | assert(size() == chunk->size(), "Wrong size" ); |
189 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
190 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
191 | |
192 | Chunk* oldHead = head(); |
193 | assert(chunk != oldHead, "double insertion" ); |
194 | chunk->link_after(oldHead); |
195 | link_head(chunk); |
196 | if (oldHead == NULL) { // only chunk in list |
197 | assert(tail() == NULL, "inconsistent FreeList" ); |
198 | link_tail(chunk); |
199 | } |
200 | increment_count(); // of # of chunks in list |
201 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
202 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
203 | assert(head() == NULL || head()->size() == size(), "wrong item on list" ); |
204 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list" ); |
205 | } |
206 | |
207 | template <class Chunk> |
208 | void FreeList<Chunk>::return_chunk_at_head(Chunk* chunk) { |
209 | assert_proper_lock_protection(); |
210 | return_chunk_at_head(chunk, true); |
211 | } |
212 | |
213 | // Add this chunk at the tail of the list. |
214 | template <class Chunk> |
215 | void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk, bool record_return) { |
216 | assert_proper_lock_protection(); |
217 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
218 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
219 | assert(chunk != NULL, "insert a NULL chunk" ); |
220 | assert(size() == chunk->size(), "wrong size" ); |
221 | |
222 | Chunk* oldTail = tail(); |
223 | assert(chunk != oldTail, "double insertion" ); |
224 | if (oldTail != NULL) { |
225 | oldTail->link_after(chunk); |
226 | } else { // only chunk in list |
227 | assert(head() == NULL, "inconsistent FreeList" ); |
228 | link_head(chunk); |
229 | } |
230 | link_tail(chunk); |
231 | increment_count(); // of # of chunks in list |
232 | assert(head() == NULL || head()->prev() == NULL, "list invariant" ); |
233 | assert(tail() == NULL || tail()->next() == NULL, "list invariant" ); |
234 | assert(head() == NULL || head()->size() == size(), "wrong item on list" ); |
235 | assert(tail() == NULL || tail()->size() == size(), "wrong item on list" ); |
236 | } |
237 | |
238 | template <class Chunk> |
239 | void FreeList<Chunk>::return_chunk_at_tail(Chunk* chunk) { |
240 | return_chunk_at_tail(chunk, true); |
241 | } |
242 | |
243 | template <class Chunk> |
244 | void FreeList<Chunk>::prepend(FreeList<Chunk>* fl) { |
245 | assert_proper_lock_protection(); |
246 | if (fl->count() > 0) { |
247 | if (count() == 0) { |
248 | set_head(fl->head()); |
249 | set_tail(fl->tail()); |
250 | set_count(fl->count()); |
251 | } else { |
252 | // Both are non-empty. |
253 | Chunk* fl_tail = fl->tail(); |
254 | Chunk* this_head = head(); |
255 | assert(fl_tail->next() == NULL, "Well-formedness of fl" ); |
256 | fl_tail->link_next(this_head); |
257 | this_head->link_prev(fl_tail); |
258 | set_head(fl->head()); |
259 | set_count(count() + fl->count()); |
260 | } |
261 | fl->set_head(NULL); |
262 | fl->set_tail(NULL); |
263 | fl->set_count(0); |
264 | } |
265 | } |
266 | |
267 | // verify_chunk_in_free_lists() is used to verify that an item is in this free list. |
268 | // It is used as a debugging aid. |
269 | template <class Chunk> |
270 | bool FreeList<Chunk>::verify_chunk_in_free_list(Chunk* fc) const { |
271 | // This is an internal consistency check, not part of the check that the |
272 | // chunk is in the free lists. |
273 | guarantee(fc->size() == size(), "Wrong list is being searched" ); |
274 | Chunk* curFC = head(); |
275 | while (curFC) { |
276 | // This is an internal consistency check. |
277 | guarantee(size() == curFC->size(), "Chunk is in wrong list." ); |
278 | if (fc == curFC) { |
279 | return true; |
280 | } |
281 | curFC = curFC->next(); |
282 | } |
283 | return false; |
284 | } |
285 | |
286 | #ifdef ASSERT |
287 | template <class Chunk> |
288 | void FreeList<Chunk>::assert_proper_lock_protection_work() const { |
289 | // Nothing to do if the list has no assigned protecting lock |
290 | if (protecting_lock() == NULL) { |
291 | return; |
292 | } |
293 | |
294 | Thread* thr = Thread::current(); |
295 | if (thr->is_VM_thread() || thr->is_ConcurrentGC_thread()) { |
296 | // assert that we are holding the freelist lock |
297 | } else if (thr->is_GC_task_thread()) { |
298 | assert(protecting_lock()->owned_by_self(), "FreeList RACE DETECTED" ); |
299 | } else if (thr->is_Java_thread()) { |
300 | assert(!SafepointSynchronize::is_at_safepoint(), "Should not be executing" ); |
301 | } else { |
302 | ShouldNotReachHere(); // unaccounted thread type? |
303 | } |
304 | } |
305 | #endif |
306 | |
307 | // Print the "label line" for free list stats. |
308 | template <class Chunk> |
309 | void FreeList<Chunk>::print_labels_on(outputStream* st, const char* c) { |
310 | st->print("%16s\t" , c); |
311 | st->print("%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" |
312 | "%14s\t" "%14s\t" "%14s\t" "%14s\t" "%14s\t" "\n" , |
313 | "bfrsurp" , "surplus" , "desired" , "prvSwep" , "bfrSwep" , |
314 | "count" , "cBirths" , "cDeaths" , "sBirths" , "sDeaths" ); |
315 | } |
316 | |
317 | // Print the AllocationStats for the given free list. If the second argument |
318 | // to the call is a non-null string, it is printed in the first column; |
319 | // otherwise, if the argument is null (the default), then the size of the |
320 | // (free list) block is printed in the first column. |
321 | template <class Chunk_t> |
322 | void FreeList<Chunk_t>::print_on(outputStream* st, const char* c) const { |
323 | if (c != NULL) { |
324 | st->print("%16s" , c); |
325 | } else { |
326 | st->print(SIZE_FORMAT_W(16), size()); |
327 | } |
328 | } |
329 | |
330 | #endif // SHARE_MEMORY_FREELIST_INLINE_HPP |
331 | |