memRegion.cpp revision 0:a61af66fc99e
190075Sobrien/*
290075Sobrien * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
390075Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
790075Sobrien * published by the Free Software Foundation.
890075Sobrien *
990075Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1090075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1390075Sobrien * accompanied this code).
1490075Sobrien *
1590075Sobrien * You should have received a copy of the GNU General Public License version
1690075Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1790075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1890075Sobrien *
1990075Sobrien * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2090075Sobrien * CA 95054 USA or visit www.sun.com if you need additional information or
2190075Sobrien * have any questions.
2290075Sobrien *
2390075Sobrien */
2490075Sobrien
2590075Sobrien// A very simple data structure representing a contigous word-aligned
2690075Sobrien// region of address space.
2790075Sobrien
2890075Sobrien#include "incls/_precompiled.incl"
2990075Sobrien#include "incls/_memRegion.cpp.incl"
3090075Sobrien
3190075SobrienMemRegion MemRegion::intersection(const MemRegion mr2) const {
3290075Sobrien  MemRegion res;
3390075Sobrien  HeapWord* res_start = MAX2(start(), mr2.start());
3490075Sobrien  HeapWord* res_end   = MIN2(end(),   mr2.end());
3590075Sobrien  if (res_start < res_end) {
3690075Sobrien    res.set_start(res_start);
3790075Sobrien    res.set_end(res_end);
3890075Sobrien  }
3990075Sobrien  return res;
4090075Sobrien}
41117395Skan
42117395SkanMemRegion MemRegion::_union(const MemRegion mr2) const {
43117395Skan  // If one region is empty, return the other
44117395Skan  if (is_empty()) return mr2;
4590075Sobrien  if (mr2.is_empty()) return MemRegion(start(), end());
46117395Skan
4790075Sobrien  // Otherwise, regions must overlap or be adjacent
4890075Sobrien  assert(((start() <= mr2.start()) && (end() >= mr2.start())) ||
4990075Sobrien         ((mr2.start() <= start()) && (mr2.end() >= start())),
5090075Sobrien             "non-adjacent or overlapping regions");
5190075Sobrien  MemRegion res;
5290075Sobrien  HeapWord* res_start = MIN2(start(), mr2.start());
53117395Skan  HeapWord* res_end   = MAX2(end(),   mr2.end());
54117395Skan  res.set_start(res_start);
55117395Skan  res.set_end(res_end);
5690075Sobrien  return res;
5790075Sobrien}
5890075Sobrien
5990075SobrienMemRegion MemRegion::minus(const MemRegion mr2) const {
6090075Sobrien  // There seem to be 6 cases:
6190075Sobrien  //                  |this MemRegion|
6290075Sobrien  // |strictly below|
6390075Sobrien  //   |overlap beginning|
6490075Sobrien  //                    |interior|
6590075Sobrien  //                        |overlap ending|
6690075Sobrien  //                                   |strictly above|
6790075Sobrien  //              |completely overlapping|
6890075Sobrien  // We can't deal with an interior case because it would
6990075Sobrien  // produce two disjoint regions as a result.
7090075Sobrien  // We aren't trying to be optimal in the number of tests below,
7190075Sobrien  // but the order is important to distinguish the strictly cases
72  // from the overlapping cases.
73  if (mr2.end() <= start()) {
74    // strictly below
75    return MemRegion(start(), end());
76  }
77  if (mr2.start() <= start() && mr2.end() <= end()) {
78    // overlap beginning
79    return MemRegion(mr2.end(), end());
80  }
81  if (mr2.start() >= end()) {
82    // strictly above
83    return MemRegion(start(), end());
84  }
85  if (mr2.start() >= start() && mr2.end() >= end()) {
86    // overlap ending
87    return MemRegion(start(), mr2.start());
88  }
89  if (mr2.start() <= start() && mr2.end() >= end()) {
90    // completely overlapping
91    return MemRegion();
92  }
93  if (mr2.start() > start() && mr2.end() < end()) {
94    // interior
95    guarantee(false, "MemRegion::minus, but interior");
96    return MemRegion();
97  }
98  ShouldNotReachHere();
99  return MemRegion();
100}
101