|
1 /* |
|
2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 # include "incls/_precompiled.incl" |
|
26 # include "incls/_mutableSpace.cpp.incl" |
|
27 |
|
28 void MutableSpace::initialize(MemRegion mr, bool clear_space) { |
|
29 HeapWord* bottom = mr.start(); |
|
30 HeapWord* end = mr.end(); |
|
31 |
|
32 assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end), |
|
33 "invalid space boundaries"); |
|
34 set_bottom(bottom); |
|
35 set_end(end); |
|
36 |
|
37 if (clear_space) clear(); |
|
38 } |
|
39 |
|
40 void MutableSpace::clear() { |
|
41 set_top(bottom()); |
|
42 if (ZapUnusedHeapArea) mangle_unused_area(); |
|
43 } |
|
44 |
|
45 // This version requires locking. */ |
|
46 HeapWord* MutableSpace::allocate(size_t size) { |
|
47 assert(Heap_lock->owned_by_self() || |
|
48 (SafepointSynchronize::is_at_safepoint() && |
|
49 Thread::current()->is_VM_thread()), |
|
50 "not locked"); |
|
51 HeapWord* obj = top(); |
|
52 if (pointer_delta(end(), obj) >= size) { |
|
53 HeapWord* new_top = obj + size; |
|
54 set_top(new_top); |
|
55 assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top), |
|
56 "checking alignment"); |
|
57 return obj; |
|
58 } else { |
|
59 return NULL; |
|
60 } |
|
61 } |
|
62 |
|
63 // This version is lock-free. |
|
64 HeapWord* MutableSpace::cas_allocate(size_t size) { |
|
65 do { |
|
66 HeapWord* obj = top(); |
|
67 if (pointer_delta(end(), obj) >= size) { |
|
68 HeapWord* new_top = obj + size; |
|
69 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); |
|
70 // result can be one of two: |
|
71 // the old top value: the exchange succeeded |
|
72 // otherwise: the new value of the top is returned. |
|
73 if (result != obj) { |
|
74 continue; // another thread beat us to the allocation, try again |
|
75 } |
|
76 assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top), |
|
77 "checking alignment"); |
|
78 return obj; |
|
79 } else { |
|
80 return NULL; |
|
81 } |
|
82 } while (true); |
|
83 } |
|
84 |
|
85 // Try to deallocate previous allocation. Returns true upon success. |
|
86 bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) { |
|
87 HeapWord* expected_top = obj + size; |
|
88 return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top; |
|
89 } |
|
90 |
|
91 void MutableSpace::oop_iterate(OopClosure* cl) { |
|
92 HeapWord* obj_addr = bottom(); |
|
93 HeapWord* t = top(); |
|
94 // Could call objects iterate, but this is easier. |
|
95 while (obj_addr < t) { |
|
96 obj_addr += oop(obj_addr)->oop_iterate(cl); |
|
97 } |
|
98 } |
|
99 |
|
100 void MutableSpace::object_iterate(ObjectClosure* cl) { |
|
101 HeapWord* p = bottom(); |
|
102 while (p < top()) { |
|
103 cl->do_object(oop(p)); |
|
104 p += oop(p)->size(); |
|
105 } |
|
106 } |
|
107 |
|
108 void MutableSpace::print_short() const { print_short_on(tty); } |
|
109 void MutableSpace::print_short_on( outputStream* st) const { |
|
110 st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K, |
|
111 (int) ((double) used_in_bytes() * 100 / capacity_in_bytes())); |
|
112 } |
|
113 |
|
114 void MutableSpace::print() const { print_on(tty); } |
|
115 void MutableSpace::print_on(outputStream* st) const { |
|
116 MutableSpace::print_short_on(st); |
|
117 st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")", |
|
118 bottom(), top(), end()); |
|
119 } |
|
120 |
|
121 void MutableSpace::verify(bool allow_dirty) const { |
|
122 HeapWord* p = bottom(); |
|
123 HeapWord* t = top(); |
|
124 HeapWord* prev_p = NULL; |
|
125 while (p < t) { |
|
126 oop(p)->verify(); |
|
127 prev_p = p; |
|
128 p += oop(p)->size(); |
|
129 } |
|
130 guarantee(p == top(), "end of last object must match end of space"); |
|
131 } |