1 /*
   2  * Copyright (c) 2000, 2014, 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 #include "precompiled.hpp"
  26 #include "memory/allocation.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/memRegion.hpp"
  29 #include "runtime/globals.hpp"
  30 
  31 // A very simple data structure representing a contigous word-aligned
  32 // region of address space.
  33 
  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 }
 104 
 105 void* MemRegion::operator new(size_t size) throw() {
 106   return (address)AllocateHeap(size, mtGC, CURRENT_PC,
 107     AllocFailStrategy::RETURN_NULL);
 108 }
 109 
 110 void* MemRegion::operator new [](size_t size) throw() {
 111   return (address)AllocateHeap(size, mtGC, CURRENT_PC,
 112     AllocFailStrategy::RETURN_NULL);
 113 }
 114 void  MemRegion::operator delete(void* p) {
 115   FreeHeap(p);
 116 }
 117 
 118 void  MemRegion::operator delete [](void* p) {
 119   FreeHeap(p);
 120 }