Path: blob/master/src/hotspot/share/memory/memRegion.cpp
40949 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.hpp"26#include "memory/allocation.inline.hpp"27#include "memory/memRegion.hpp"28#include "runtime/globals.hpp"2930// A very simple data structure representing a contigous word-aligned31// region of address space.3233MemRegion MemRegion::intersection(const MemRegion mr2) const {34MemRegion res;35HeapWord* res_start = MAX2(start(), mr2.start());36HeapWord* res_end = MIN2(end(), mr2.end());37if (res_start < res_end) {38res.set_start(res_start);39res.set_end(res_end);40}41return res;42}4344MemRegion MemRegion::_union(const MemRegion mr2) const {45// If one region is empty, return the other46if (is_empty()) return mr2;47if (mr2.is_empty()) return MemRegion(start(), end());4849// Otherwise, regions must overlap or be adjacent50assert(((start() <= mr2.start()) && (end() >= mr2.start())) ||51((mr2.start() <= start()) && (mr2.end() >= start())),52"non-adjacent or overlapping regions");53MemRegion res;54HeapWord* res_start = MIN2(start(), mr2.start());55HeapWord* res_end = MAX2(end(), mr2.end());56res.set_start(res_start);57res.set_end(res_end);58return res;59}6061MemRegion MemRegion::minus(const MemRegion mr2) const {62// There seem to be 6 cases:63// |this MemRegion|64// |strictly below|65// |overlap beginning|66// |interior|67// |overlap ending|68// |strictly above|69// |completely overlapping|70// We can't deal with an interior case because it would71// produce two disjoint regions as a result.72// We aren't trying to be optimal in the number of tests below,73// but the order is important to distinguish the strictly cases74// from the overlapping cases.75if (mr2.end() <= start()) {76// strictly below77return MemRegion(start(), end());78}79if (mr2.start() <= start() && mr2.end() <= end()) {80// overlap beginning81return MemRegion(mr2.end(), end());82}83if (mr2.start() >= end()) {84// strictly above85return MemRegion(start(), end());86}87if (mr2.start() >= start() && mr2.end() >= end()) {88// overlap ending89return MemRegion(start(), mr2.start());90}91if (mr2.start() <= start() && mr2.end() >= end()) {92// completely overlapping93return MemRegion();94}95if (mr2.start() > start() && mr2.end() < end()) {96// interior97guarantee(false, "MemRegion::minus, but interior");98return MemRegion();99}100ShouldNotReachHere();101return MemRegion();102}103104MemRegion* MemRegion::create_array(size_t length, MEMFLAGS flags) {105MemRegion* result = NEW_C_HEAP_ARRAY(MemRegion, length, flags);106for (size_t i = 0; i < length; i++) {107::new (&result[i]) MemRegion();108}109return result;110}111112void MemRegion::destroy_array(MemRegion* array, size_t length) {113if (array == NULL) {114return;115}116for (size_t i = 0; i < length; i++) {117array[i].~MemRegion();118}119FREE_C_HEAP_ARRAY(MemRegion, array);120}121122