J. Duke | 8153779 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
Zhengyu Gu | f0cf82f | 2014-08-07 12:18:58 -0700 | [diff] [blame] | 2 | * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
J. Duke | 8153779 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
Erik Trimble | ba7c173 | 2010-05-27 19:08:38 -0700 | [diff] [blame] | 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. |
J. Duke | 8153779 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | * |
| 23 | */ |
| 24 | |
Stefan Karlsson | 8006fe8 | 2010-11-23 13:22:55 -0800 | [diff] [blame] | 25 | #include "precompiled.hpp" |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 26 | #include "memory/allocation.hpp" |
| 27 | #include "memory/allocation.inline.hpp" |
Stefan Karlsson | 8006fe8 | 2010-11-23 13:22:55 -0800 | [diff] [blame] | 28 | #include "memory/memRegion.hpp" |
| 29 | #include "runtime/globals.hpp" |
| 30 | |
J. Duke | 8153779 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 31 | // A very simple data structure representing a contigous word-aligned |
| 32 | // region of address space. |
| 33 | |
J. Duke | 8153779 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 34 | MemRegion MemRegion::intersection(const MemRegion mr2) const { |
| 35 | MemRegion res; |
| 36 | HeapWord* res_start = MAX2(start(), mr2.start()); |
| 37 | HeapWord* res_end = MIN2(end(), mr2.end()); |
| 38 | if (res_start < res_end) { |
| 39 | res.set_start(res_start); |
| 40 | res.set_end(res_end); |
| 41 | } |
| 42 | return res; |
| 43 | } |
| 44 | |
| 45 | MemRegion MemRegion::_union(const MemRegion mr2) const { |
| 46 | // If one region is empty, return the other |
| 47 | if (is_empty()) return mr2; |
| 48 | if (mr2.is_empty()) return MemRegion(start(), end()); |
| 49 | |
| 50 | // Otherwise, regions must overlap or be adjacent |
| 51 | assert(((start() <= mr2.start()) && (end() >= mr2.start())) || |
| 52 | ((mr2.start() <= start()) && (mr2.end() >= start())), |
| 53 | "non-adjacent or overlapping regions"); |
| 54 | MemRegion res; |
| 55 | HeapWord* res_start = MIN2(start(), mr2.start()); |
| 56 | HeapWord* res_end = MAX2(end(), mr2.end()); |
| 57 | res.set_start(res_start); |
| 58 | res.set_end(res_end); |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | MemRegion MemRegion::minus(const MemRegion mr2) const { |
| 63 | // There seem to be 6 cases: |
| 64 | // |this MemRegion| |
| 65 | // |strictly below| |
| 66 | // |overlap beginning| |
| 67 | // |interior| |
| 68 | // |overlap ending| |
| 69 | // |strictly above| |
| 70 | // |completely overlapping| |
| 71 | // We can't deal with an interior case because it would |
| 72 | // produce two disjoint regions as a result. |
| 73 | // We aren't trying to be optimal in the number of tests below, |
| 74 | // but the order is important to distinguish the strictly cases |
| 75 | // from the overlapping cases. |
| 76 | if (mr2.end() <= start()) { |
| 77 | // strictly below |
| 78 | return MemRegion(start(), end()); |
| 79 | } |
| 80 | if (mr2.start() <= start() && mr2.end() <= end()) { |
| 81 | // overlap beginning |
| 82 | return MemRegion(mr2.end(), end()); |
| 83 | } |
| 84 | if (mr2.start() >= end()) { |
| 85 | // strictly above |
| 86 | return MemRegion(start(), end()); |
| 87 | } |
| 88 | if (mr2.start() >= start() && mr2.end() >= end()) { |
| 89 | // overlap ending |
| 90 | return MemRegion(start(), mr2.start()); |
| 91 | } |
| 92 | if (mr2.start() <= start() && mr2.end() >= end()) { |
| 93 | // completely overlapping |
| 94 | return MemRegion(); |
| 95 | } |
| 96 | if (mr2.start() > start() && mr2.end() < end()) { |
| 97 | // interior |
| 98 | guarantee(false, "MemRegion::minus, but interior"); |
| 99 | return MemRegion(); |
| 100 | } |
| 101 | ShouldNotReachHere(); |
| 102 | return MemRegion(); |
| 103 | } |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 104 | |
Lois Foltan | fca9e37 | 2013-08-29 18:56:29 -0400 | [diff] [blame] | 105 | void* MemRegion::operator new(size_t size) throw() { |
Zhengyu Gu | f0cf82f | 2014-08-07 12:18:58 -0700 | [diff] [blame] | 106 | return (address)AllocateHeap(size, mtGC, CURRENT_PC, |
| 107 | AllocFailStrategy::RETURN_NULL); |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Lois Foltan | fca9e37 | 2013-08-29 18:56:29 -0400 | [diff] [blame] | 110 | void* MemRegion::operator new [](size_t size) throw() { |
Zhengyu Gu | f0cf82f | 2014-08-07 12:18:58 -0700 | [diff] [blame] | 111 | return (address)AllocateHeap(size, mtGC, CURRENT_PC, |
| 112 | AllocFailStrategy::RETURN_NULL); |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 113 | } |
| 114 | void MemRegion::operator delete(void* p) { |
Max Ockner | 91dbd4f | 2014-12-01 12:16:15 -0500 | [diff] [blame] | 115 | FreeHeap(p); |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void MemRegion::operator delete [](void* p) { |
Max Ockner | 91dbd4f | 2014-12-01 12:16:15 -0500 | [diff] [blame] | 119 | FreeHeap(p); |
Yumin Qi | 98151c3 | 2013-05-14 09:41:12 -0700 | [diff] [blame] | 120 | } |
| 121 | |