blob: 5b2c5f7fc48cf81fb1bbc5a3132deb9f75d30e9d [file] [log] [blame]
J. Duke81537792007-12-01 00:00:00 +00001/*
Zhengyu Guf0cf82f2014-08-07 12:18:58 -07002 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
J. Duke81537792007-12-01 00:00:00 +00003 * 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 Trimbleba7c1732010-05-27 19:08:38 -070019 * 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. Duke81537792007-12-01 00:00:00 +000022 *
23 */
24
Stefan Karlsson8006fe82010-11-23 13:22:55 -080025#include "precompiled.hpp"
Yumin Qi98151c32013-05-14 09:41:12 -070026#include "memory/allocation.hpp"
27#include "memory/allocation.inline.hpp"
Stefan Karlsson8006fe82010-11-23 13:22:55 -080028#include "memory/memRegion.hpp"
29#include "runtime/globals.hpp"
30
J. Duke81537792007-12-01 00:00:00 +000031// A very simple data structure representing a contigous word-aligned
32// region of address space.
33
J. Duke81537792007-12-01 00:00:00 +000034MemRegion 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
45MemRegion 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
62MemRegion 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 Qi98151c32013-05-14 09:41:12 -0700104
Lois Foltanfca9e372013-08-29 18:56:29 -0400105void* MemRegion::operator new(size_t size) throw() {
Zhengyu Guf0cf82f2014-08-07 12:18:58 -0700106 return (address)AllocateHeap(size, mtGC, CURRENT_PC,
107 AllocFailStrategy::RETURN_NULL);
Yumin Qi98151c32013-05-14 09:41:12 -0700108}
109
Lois Foltanfca9e372013-08-29 18:56:29 -0400110void* MemRegion::operator new [](size_t size) throw() {
Zhengyu Guf0cf82f2014-08-07 12:18:58 -0700111 return (address)AllocateHeap(size, mtGC, CURRENT_PC,
112 AllocFailStrategy::RETURN_NULL);
Yumin Qi98151c32013-05-14 09:41:12 -0700113}
114void MemRegion::operator delete(void* p) {
Max Ockner91dbd4f2014-12-01 12:16:15 -0500115 FreeHeap(p);
Yumin Qi98151c32013-05-14 09:41:12 -0700116}
117
118void MemRegion::operator delete [](void* p) {
Max Ockner91dbd4f2014-12-01 12:16:15 -0500119 FreeHeap(p);
Yumin Qi98151c32013-05-14 09:41:12 -0700120}
121