1 /*
   2  * Copyright (c) 2000, 2004, Oracle and/or its affiliates. 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 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.
  22  *
  23  */
  24 
  25 // A very simple data structure representing a contigous word-aligned
  26 // region of address space.
  27 
  28 #include "incls/_precompiled.incl"
  29 #include "incls/_memRegion.cpp.incl"
  30 
  31 MemRegion MemRegion::intersection(const MemRegion mr2) const {
  32   MemRegion res;
  33   HeapWord* res_start = MAX2(start(), mr2.start());
  34   HeapWord* res_end   = MIN2(end(),   mr2.end());
  35   if (res_start < res_end) {
  36     res.set_start(res_start);
  37     res.set_end(res_end);
  38   }
  39   return res;
  40 }
  41 
  42 MemRegion MemRegion::_union(const MemRegion mr2) const {
  43   // If one region is empty, return the other
  44   if (is_empty()) return mr2;
  45   if (mr2.is_empty()) return MemRegion(start(), end());
  46 
  47   // Otherwise, regions must overlap or be adjacent
  48   assert(((start() <= mr2.start()) && (end() >= mr2.start())) ||
  49          ((mr2.start() <= start()) && (mr2.end() >= start())),
  50              "non-adjacent or overlapping regions");
  51   MemRegion res;
  52   HeapWord* res_start = MIN2(start(), mr2.start());
  53   HeapWord* res_end   = MAX2(end(),   mr2.end());
  54   res.set_start(res_start);
  55   res.set_end(res_end);
  56   return res;
  57 }
  58 
  59 MemRegion MemRegion::minus(const MemRegion mr2) const {
  60   // There seem to be 6 cases:
  61   //                  |this MemRegion|
  62   // |strictly below|
  63   //   |overlap beginning|
  64   //                    |interior|
  65   //                        |overlap ending|
  66   //                                   |strictly above|
  67   //              |completely overlapping|
  68   // We can't deal with an interior case because it would
  69   // produce two disjoint regions as a result.
  70   // We aren't trying to be optimal in the number of tests below,
  71   // 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 }