Path: blob/master/src/hotspot/share/memory/memRegion.hpp
40949 views
/*1* Copyright (c) 2000, 2019, 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#ifndef SHARE_MEMORY_MEMREGION_HPP25#define SHARE_MEMORY_MEMREGION_HPP2627#include "memory/allocation.hpp"28#include "utilities/debug.hpp"29#include "utilities/globalDefinitions.hpp"3031// A very simple data structure representing a contigous region32// region of address space.3334// Note that MemRegions are typically passed by value, not by reference.35// The intent is that they remain very small and contain no36// objects. The copy constructor and destructor must be trivial,37// to support optimization for pass-by-value.38// These should almost never be allocated in heap but we do39// create MemRegions (in CardTable and G1CMRootMemRegions) on the heap so operator40// new and operator new [] were added for these special cases.4142class MemRegion {43friend class VMStructs;44private:45HeapWord* _start;46size_t _word_size;4748public:49MemRegion() : _start(NULL), _word_size(0) {};50MemRegion(HeapWord* start, size_t word_size) :51_start(start), _word_size(word_size) {};52MemRegion(HeapWord* start, HeapWord* end) :53_start(start), _word_size(pointer_delta(end, start)) {54assert(end >= start, "incorrect constructor arguments");55}56MemRegion(MetaWord* start, MetaWord* end) :57_start((HeapWord*)start), _word_size(pointer_delta(end, start)) {58assert(end >= start, "incorrect constructor arguments");59}6061MemRegion intersection(const MemRegion mr2) const;62// regions must overlap or be adjacent63MemRegion _union(const MemRegion mr2) const;64// minus will fail a guarantee if mr2 is interior to this,65// since there's no way to return 2 disjoint regions.66MemRegion minus(const MemRegion mr2) const;6768HeapWord* start() const { return _start; }69HeapWord* end() const { return _start + _word_size; }70HeapWord* last() const { return _start + _word_size - 1; }7172void set_start(HeapWord* start) { _start = start; }73void set_end(HeapWord* end) { _word_size = pointer_delta(end, _start); }74void set_word_size(size_t word_size) {75_word_size = word_size;76}7778bool contains(const MemRegion mr2) const {79return _start <= mr2._start && end() >= mr2.end();80}81bool contains(const void* addr) const {82return addr >= (void*)_start && addr < (void*)end();83}84bool equals(const MemRegion mr2) const {85// first disjunct since we do not have a canonical empty set86return ((is_empty() && mr2.is_empty()) ||87(start() == mr2.start() && end() == mr2.end()));88}8990size_t byte_size() const { return _word_size * sizeof(HeapWord); }91size_t word_size() const { return _word_size; }9293bool is_empty() const { return word_size() == 0; }9495// Creates and initializes an array of MemRegions of the given length.96static MemRegion* create_array(size_t length, MEMFLAGS flags);97static void destroy_array(MemRegion* array, size_t length);98};99100// For iteration over MemRegion's.101102class MemRegionClosure : public StackObj {103public:104virtual void do_MemRegion(MemRegion mr) = 0;105};106107// A ResourceObj version of MemRegionClosure108109class MemRegionClosureRO: public MemRegionClosure {110public:111void* operator new(size_t size, ResourceObj::allocation_type type, MEMFLAGS flags) throw() {112return ResourceObj::operator new(size, type, flags);113}114void* operator new(size_t size, Arena *arena) throw() {115return ResourceObj::operator new(size, arena);116}117void* operator new(size_t size) throw() {118return ResourceObj::operator new(size);119}120121void operator delete(void* p) {} // nothing to do122};123124#endif // SHARE_MEMORY_MEMREGION_HPP125126127