Tue, 07 Oct 2008 11:01:35 -0700
Merge
duke@435 | 1 | /* |
xdono@772 | 2 | * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "incls/_precompiled.incl" |
duke@435 | 26 | #include "incls/_psVirtualspace.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | // PSVirtualSpace |
duke@435 | 29 | |
duke@435 | 30 | PSVirtualSpace::PSVirtualSpace(ReservedSpace rs, size_t alignment) : |
duke@435 | 31 | _alignment(alignment) |
duke@435 | 32 | { |
duke@435 | 33 | set_reserved(rs); |
duke@435 | 34 | set_committed(reserved_low_addr(), reserved_low_addr()); |
duke@435 | 35 | DEBUG_ONLY(verify()); |
duke@435 | 36 | } |
duke@435 | 37 | |
duke@435 | 38 | PSVirtualSpace::PSVirtualSpace(ReservedSpace rs) : |
duke@435 | 39 | _alignment(os::vm_page_size()) |
duke@435 | 40 | { |
duke@435 | 41 | set_reserved(rs); |
duke@435 | 42 | set_committed(reserved_low_addr(), reserved_low_addr()); |
duke@435 | 43 | DEBUG_ONLY(verify()); |
duke@435 | 44 | } |
duke@435 | 45 | |
duke@435 | 46 | // Deprecated. |
duke@435 | 47 | PSVirtualSpace::PSVirtualSpace(): _alignment(os::vm_page_size()) { |
duke@435 | 48 | } |
duke@435 | 49 | |
duke@435 | 50 | // Deprecated. |
duke@435 | 51 | bool PSVirtualSpace::initialize(ReservedSpace rs, |
duke@435 | 52 | size_t commit_size) { |
duke@435 | 53 | set_reserved(rs); |
duke@435 | 54 | set_committed(reserved_low_addr(), reserved_low_addr()); |
duke@435 | 55 | |
duke@435 | 56 | // Commit to initial size. |
duke@435 | 57 | assert(commit_size <= rs.size(), "commit_size too big"); |
duke@435 | 58 | bool result = commit_size > 0 ? expand_by(commit_size) : true; |
duke@435 | 59 | DEBUG_ONLY(verify()); |
duke@435 | 60 | return result; |
duke@435 | 61 | } |
duke@435 | 62 | |
duke@435 | 63 | PSVirtualSpace::~PSVirtualSpace() { |
duke@435 | 64 | release(); |
duke@435 | 65 | } |
duke@435 | 66 | |
duke@435 | 67 | bool PSVirtualSpace::contains(void* p) const { |
duke@435 | 68 | char* const cp = (char*)p; |
duke@435 | 69 | return cp >= committed_low_addr() && cp < committed_high_addr(); |
duke@435 | 70 | } |
duke@435 | 71 | |
duke@435 | 72 | void PSVirtualSpace::release() { |
duke@435 | 73 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
coleenp@672 | 74 | // This may not release memory it didn't reserve. |
coleenp@672 | 75 | // Use rs.release() to release the underlying memory instead. |
duke@435 | 76 | _reserved_low_addr = _reserved_high_addr = NULL; |
duke@435 | 77 | _committed_low_addr = _committed_high_addr = NULL; |
duke@435 | 78 | _special = false; |
duke@435 | 79 | } |
duke@435 | 80 | |
duke@435 | 81 | bool PSVirtualSpace::expand_by(size_t bytes, bool pre_touch) { |
duke@435 | 82 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 83 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 84 | |
duke@435 | 85 | if (uncommitted_size() < bytes) { |
duke@435 | 86 | return false; |
duke@435 | 87 | } |
duke@435 | 88 | |
duke@435 | 89 | char* const base_addr = committed_high_addr(); |
duke@435 | 90 | bool result = special() || os::commit_memory(base_addr, bytes, alignment()); |
duke@435 | 91 | if (result) { |
duke@435 | 92 | _committed_high_addr += bytes; |
duke@435 | 93 | } |
duke@435 | 94 | |
duke@435 | 95 | if (pre_touch || AlwaysPreTouch) { |
duke@435 | 96 | for (char* curr = base_addr; |
duke@435 | 97 | curr < _committed_high_addr; |
duke@435 | 98 | curr += os::vm_page_size()) { |
duke@435 | 99 | char tmp = *curr; |
duke@435 | 100 | *curr = 0; |
duke@435 | 101 | } |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | return result; |
duke@435 | 105 | } |
duke@435 | 106 | |
duke@435 | 107 | bool PSVirtualSpace::shrink_by(size_t bytes) { |
duke@435 | 108 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 109 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 110 | |
duke@435 | 111 | if (committed_size() < bytes) { |
duke@435 | 112 | return false; |
duke@435 | 113 | } |
duke@435 | 114 | |
duke@435 | 115 | char* const base_addr = committed_high_addr() - bytes; |
duke@435 | 116 | bool result = special() || os::uncommit_memory(base_addr, bytes); |
duke@435 | 117 | if (result) { |
duke@435 | 118 | _committed_high_addr -= bytes; |
duke@435 | 119 | } |
duke@435 | 120 | |
duke@435 | 121 | return result; |
duke@435 | 122 | } |
duke@435 | 123 | |
duke@435 | 124 | size_t |
duke@435 | 125 | PSVirtualSpace::expand_into(PSVirtualSpace* other_space, size_t bytes) { |
duke@435 | 126 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 127 | assert(grows_up(), "this space must grow up"); |
duke@435 | 128 | assert(other_space->grows_down(), "other space must grow down"); |
duke@435 | 129 | assert(reserved_high_addr() == other_space->reserved_low_addr(), |
duke@435 | 130 | "spaces not contiguous"); |
duke@435 | 131 | assert(special() == other_space->special(), "one space is special, the other is not"); |
duke@435 | 132 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 133 | DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space)); |
duke@435 | 134 | |
duke@435 | 135 | size_t bytes_needed = bytes; |
duke@435 | 136 | |
duke@435 | 137 | // First use the uncommitted region in this space. |
duke@435 | 138 | size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed); |
duke@435 | 139 | if (tmp_bytes > 0) { |
duke@435 | 140 | if (expand_by(tmp_bytes)) { |
duke@435 | 141 | bytes_needed -= tmp_bytes; |
duke@435 | 142 | } else { |
duke@435 | 143 | return 0; |
duke@435 | 144 | } |
duke@435 | 145 | } |
duke@435 | 146 | |
duke@435 | 147 | // Next take from the uncommitted region in the other space, and commit it. |
duke@435 | 148 | tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed); |
duke@435 | 149 | if (tmp_bytes > 0) { |
duke@435 | 150 | char* const commit_base = committed_high_addr(); |
duke@435 | 151 | if (other_space->special() || |
duke@435 | 152 | os::commit_memory(commit_base, tmp_bytes, alignment())) { |
duke@435 | 153 | // Reduce the reserved region in the other space. |
duke@435 | 154 | other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes, |
duke@435 | 155 | other_space->reserved_high_addr(), |
duke@435 | 156 | other_space->special()); |
duke@435 | 157 | |
duke@435 | 158 | // Grow both reserved and committed in this space. |
duke@435 | 159 | _reserved_high_addr += tmp_bytes; |
duke@435 | 160 | _committed_high_addr += tmp_bytes; |
duke@435 | 161 | bytes_needed -= tmp_bytes; |
duke@435 | 162 | } else { |
duke@435 | 163 | return bytes - bytes_needed; |
duke@435 | 164 | } |
duke@435 | 165 | } |
duke@435 | 166 | |
duke@435 | 167 | // Finally take from the already committed region in the other space. |
duke@435 | 168 | tmp_bytes = bytes_needed; |
duke@435 | 169 | if (tmp_bytes > 0) { |
duke@435 | 170 | // Reduce both committed and reserved in the other space. |
duke@435 | 171 | other_space->set_committed(other_space->committed_low_addr() + tmp_bytes, |
duke@435 | 172 | other_space->committed_high_addr()); |
duke@435 | 173 | other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes, |
duke@435 | 174 | other_space->reserved_high_addr(), |
duke@435 | 175 | other_space->special()); |
duke@435 | 176 | |
duke@435 | 177 | // Grow both reserved and committed in this space. |
duke@435 | 178 | _reserved_high_addr += tmp_bytes; |
duke@435 | 179 | _committed_high_addr += tmp_bytes; |
duke@435 | 180 | } |
duke@435 | 181 | |
duke@435 | 182 | return bytes; |
duke@435 | 183 | } |
duke@435 | 184 | |
duke@435 | 185 | #ifndef PRODUCT |
duke@435 | 186 | bool PSVirtualSpace::is_aligned(size_t value, size_t align) { |
duke@435 | 187 | const size_t tmp_value = value + align - 1; |
duke@435 | 188 | const size_t mask = ~(align - 1); |
duke@435 | 189 | return (tmp_value & mask) == value; |
duke@435 | 190 | } |
duke@435 | 191 | |
duke@435 | 192 | bool PSVirtualSpace::is_aligned(size_t value) const { |
duke@435 | 193 | return is_aligned(value, alignment()); |
duke@435 | 194 | } |
duke@435 | 195 | |
duke@435 | 196 | bool PSVirtualSpace::is_aligned(char* value) const { |
duke@435 | 197 | return is_aligned((size_t)value); |
duke@435 | 198 | } |
duke@435 | 199 | |
duke@435 | 200 | void PSVirtualSpace::verify() const { |
duke@435 | 201 | assert(is_aligned(alignment(), os::vm_page_size()), "bad alignment"); |
duke@435 | 202 | assert(is_aligned(reserved_low_addr()), "bad reserved_low_addr"); |
duke@435 | 203 | assert(is_aligned(reserved_high_addr()), "bad reserved_high_addr"); |
duke@435 | 204 | assert(is_aligned(committed_low_addr()), "bad committed_low_addr"); |
duke@435 | 205 | assert(is_aligned(committed_high_addr()), "bad committed_high_addr"); |
duke@435 | 206 | |
duke@435 | 207 | // Reserved region must be non-empty or both addrs must be 0. |
duke@435 | 208 | assert(reserved_low_addr() < reserved_high_addr() || |
duke@435 | 209 | reserved_low_addr() == NULL && reserved_high_addr() == NULL, |
duke@435 | 210 | "bad reserved addrs"); |
duke@435 | 211 | assert(committed_low_addr() <= committed_high_addr(), "bad committed addrs"); |
duke@435 | 212 | |
duke@435 | 213 | if (grows_up()) { |
duke@435 | 214 | assert(reserved_low_addr() == committed_low_addr(), "bad low addrs"); |
duke@435 | 215 | assert(reserved_high_addr() >= committed_high_addr(), "bad high addrs"); |
duke@435 | 216 | } else { |
duke@435 | 217 | assert(reserved_high_addr() == committed_high_addr(), "bad high addrs"); |
duke@435 | 218 | assert(reserved_low_addr() <= committed_low_addr(), "bad low addrs"); |
duke@435 | 219 | } |
duke@435 | 220 | } |
duke@435 | 221 | |
duke@435 | 222 | void PSVirtualSpace::print() const { |
duke@435 | 223 | gclog_or_tty->print_cr("virtual space [" PTR_FORMAT "]: alignment=" |
duke@435 | 224 | SIZE_FORMAT "K grows %s%s", |
duke@435 | 225 | this, alignment() / K, grows_up() ? "up" : "down", |
duke@435 | 226 | special() ? " (pinned in memory)" : ""); |
duke@435 | 227 | gclog_or_tty->print_cr(" reserved=" SIZE_FORMAT "K" |
duke@435 | 228 | " [" PTR_FORMAT "," PTR_FORMAT "]" |
duke@435 | 229 | " committed=" SIZE_FORMAT "K" |
duke@435 | 230 | " [" PTR_FORMAT "," PTR_FORMAT "]", |
duke@435 | 231 | reserved_size() / K, |
duke@435 | 232 | reserved_low_addr(), reserved_high_addr(), |
duke@435 | 233 | committed_size() / K, |
duke@435 | 234 | committed_low_addr(), committed_high_addr()); |
duke@435 | 235 | } |
duke@435 | 236 | #endif // #ifndef PRODUCT |
duke@435 | 237 | |
duke@435 | 238 | void PSVirtualSpace::print_space_boundaries_on(outputStream* st) const { |
duke@435 | 239 | st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ")", |
duke@435 | 240 | low_boundary(), high(), high_boundary()); |
duke@435 | 241 | } |
duke@435 | 242 | |
duke@435 | 243 | PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs, |
duke@435 | 244 | size_t alignment) : |
duke@435 | 245 | PSVirtualSpace(alignment) |
duke@435 | 246 | { |
duke@435 | 247 | set_reserved(rs); |
duke@435 | 248 | set_committed(reserved_high_addr(), reserved_high_addr()); |
duke@435 | 249 | DEBUG_ONLY(verify()); |
duke@435 | 250 | } |
duke@435 | 251 | |
duke@435 | 252 | PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs) { |
duke@435 | 253 | set_reserved(rs); |
duke@435 | 254 | set_committed(reserved_high_addr(), reserved_high_addr()); |
duke@435 | 255 | DEBUG_ONLY(verify()); |
duke@435 | 256 | } |
duke@435 | 257 | |
duke@435 | 258 | bool PSVirtualSpaceHighToLow::expand_by(size_t bytes, bool pre_touch) { |
duke@435 | 259 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 260 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 261 | |
duke@435 | 262 | if (uncommitted_size() < bytes) { |
duke@435 | 263 | return false; |
duke@435 | 264 | } |
duke@435 | 265 | |
duke@435 | 266 | char* const base_addr = committed_low_addr() - bytes; |
duke@435 | 267 | bool result = special() || os::commit_memory(base_addr, bytes, alignment()); |
duke@435 | 268 | if (result) { |
duke@435 | 269 | _committed_low_addr -= bytes; |
duke@435 | 270 | } |
duke@435 | 271 | |
duke@435 | 272 | if (pre_touch || AlwaysPreTouch) { |
duke@435 | 273 | for (char* curr = base_addr; |
duke@435 | 274 | curr < _committed_high_addr; |
duke@435 | 275 | curr += os::vm_page_size()) { |
duke@435 | 276 | char tmp = *curr; |
duke@435 | 277 | *curr = 0; |
duke@435 | 278 | } |
duke@435 | 279 | } |
duke@435 | 280 | |
duke@435 | 281 | return result; |
duke@435 | 282 | } |
duke@435 | 283 | |
duke@435 | 284 | bool PSVirtualSpaceHighToLow::shrink_by(size_t bytes) { |
duke@435 | 285 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 286 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 287 | |
duke@435 | 288 | if (committed_size() < bytes) { |
duke@435 | 289 | return false; |
duke@435 | 290 | } |
duke@435 | 291 | |
duke@435 | 292 | char* const base_addr = committed_low_addr(); |
duke@435 | 293 | bool result = special() || os::uncommit_memory(base_addr, bytes); |
duke@435 | 294 | if (result) { |
duke@435 | 295 | _committed_low_addr += bytes; |
duke@435 | 296 | } |
duke@435 | 297 | |
duke@435 | 298 | return result; |
duke@435 | 299 | } |
duke@435 | 300 | |
duke@435 | 301 | size_t PSVirtualSpaceHighToLow::expand_into(PSVirtualSpace* other_space, |
duke@435 | 302 | size_t bytes) { |
duke@435 | 303 | assert(is_aligned(bytes), "arg not aligned"); |
duke@435 | 304 | assert(grows_down(), "this space must grow down"); |
duke@435 | 305 | assert(other_space->grows_up(), "other space must grow up"); |
duke@435 | 306 | assert(reserved_low_addr() == other_space->reserved_high_addr(), |
duke@435 | 307 | "spaces not contiguous"); |
duke@435 | 308 | assert(special() == other_space->special(), "one space is special in memory, the other is not"); |
duke@435 | 309 | DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); |
duke@435 | 310 | DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space)); |
duke@435 | 311 | |
duke@435 | 312 | size_t bytes_needed = bytes; |
duke@435 | 313 | |
duke@435 | 314 | // First use the uncommitted region in this space. |
duke@435 | 315 | size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed); |
duke@435 | 316 | if (tmp_bytes > 0) { |
duke@435 | 317 | if (expand_by(tmp_bytes)) { |
duke@435 | 318 | bytes_needed -= tmp_bytes; |
duke@435 | 319 | } else { |
duke@435 | 320 | return 0; |
duke@435 | 321 | } |
duke@435 | 322 | } |
duke@435 | 323 | |
duke@435 | 324 | // Next take from the uncommitted region in the other space, and commit it. |
duke@435 | 325 | tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed); |
duke@435 | 326 | if (tmp_bytes > 0) { |
duke@435 | 327 | char* const commit_base = committed_low_addr() - tmp_bytes; |
duke@435 | 328 | if (other_space->special() || |
duke@435 | 329 | os::commit_memory(commit_base, tmp_bytes, alignment())) { |
duke@435 | 330 | // Reduce the reserved region in the other space. |
duke@435 | 331 | other_space->set_reserved(other_space->reserved_low_addr(), |
duke@435 | 332 | other_space->reserved_high_addr() - tmp_bytes, |
duke@435 | 333 | other_space->special()); |
duke@435 | 334 | |
duke@435 | 335 | // Grow both reserved and committed in this space. |
duke@435 | 336 | _reserved_low_addr -= tmp_bytes; |
duke@435 | 337 | _committed_low_addr -= tmp_bytes; |
duke@435 | 338 | bytes_needed -= tmp_bytes; |
duke@435 | 339 | } else { |
duke@435 | 340 | return bytes - bytes_needed; |
duke@435 | 341 | } |
duke@435 | 342 | } |
duke@435 | 343 | |
duke@435 | 344 | // Finally take from the already committed region in the other space. |
duke@435 | 345 | tmp_bytes = bytes_needed; |
duke@435 | 346 | if (tmp_bytes > 0) { |
duke@435 | 347 | // Reduce both committed and reserved in the other space. |
duke@435 | 348 | other_space->set_committed(other_space->committed_low_addr(), |
duke@435 | 349 | other_space->committed_high_addr() - tmp_bytes); |
duke@435 | 350 | other_space->set_reserved(other_space->reserved_low_addr(), |
duke@435 | 351 | other_space->reserved_high_addr() - tmp_bytes, |
duke@435 | 352 | other_space->special()); |
duke@435 | 353 | |
duke@435 | 354 | // Grow both reserved and committed in this space. |
duke@435 | 355 | _reserved_low_addr -= tmp_bytes; |
duke@435 | 356 | _committed_low_addr -= tmp_bytes; |
duke@435 | 357 | } |
duke@435 | 358 | |
duke@435 | 359 | return bytes; |
duke@435 | 360 | } |
duke@435 | 361 | |
duke@435 | 362 | void |
duke@435 | 363 | PSVirtualSpaceHighToLow::print_space_boundaries_on(outputStream* st) const { |
duke@435 | 364 | st->print_cr(" (" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT "]", |
duke@435 | 365 | high_boundary(), low(), low_boundary()); |
duke@435 | 366 | } |