memRegion.hpp revision 4668:f9be75d21404
1221828Sgrehan/*
2221828Sgrehan * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3221828Sgrehan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4221828Sgrehan *
5221828Sgrehan * This code is free software; you can redistribute it and/or modify it
6221828Sgrehan * under the terms of the GNU General Public License version 2 only, as
7221828Sgrehan * published by the Free Software Foundation.
8221828Sgrehan *
9221828Sgrehan * This code is distributed in the hope that it will be useful, but WITHOUT
10221828Sgrehan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11221828Sgrehan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12221828Sgrehan * version 2 for more details (a copy is included in the LICENSE file that
13221828Sgrehan * accompanied this code).
14221828Sgrehan *
15221828Sgrehan * You should have received a copy of the GNU General Public License version
16221828Sgrehan * 2 along with this work; if not, write to the Free Software Foundation,
17221828Sgrehan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18221828Sgrehan *
19221828Sgrehan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20221828Sgrehan * or visit www.oracle.com if you need additional information or have any
21221828Sgrehan * questions.
22221828Sgrehan *
23221828Sgrehan */
24221828Sgrehan
25221828Sgrehan#ifndef SHARE_VM_MEMORY_MEMREGION_HPP
26221828Sgrehan#define SHARE_VM_MEMORY_MEMREGION_HPP
27221828Sgrehan
28221828Sgrehan#include "memory/allocation.hpp"
29221828Sgrehan#include "utilities/debug.hpp"
30221828Sgrehan#include "utilities/globalDefinitions.hpp"
31221828Sgrehan
32221828Sgrehan// A very simple data structure representing a contigous region
33221828Sgrehan// region of address space.
34221828Sgrehan
35221828Sgrehan// Note that MemRegions are passed by value, not by reference.
36221828Sgrehan// The intent is that they remain very small and contain no
37221828Sgrehan// objects. _ValueObj should never be allocated in heap but we do
38221828Sgrehan// create MemRegions (in CardTableModRefBS) in heap so operator
39221828Sgrehan// new and operator new [] added for this special case.
40221828Sgrehan
41221828Sgrehanclass MetaWord;
42221828Sgrehan
43221828Sgrehanclass MemRegion VALUE_OBJ_CLASS_SPEC {
44221828Sgrehan  friend class VMStructs;
45221828Sgrehanprivate:
46221828Sgrehan  HeapWord* _start;
47221828Sgrehan  size_t    _word_size;
48221828Sgrehan
49221828Sgrehanpublic:
50221828Sgrehan  MemRegion() : _start(NULL), _word_size(0) {};
51221828Sgrehan  MemRegion(HeapWord* start, size_t word_size) :
52221828Sgrehan    _start(start), _word_size(word_size) {};
53221828Sgrehan  MemRegion(HeapWord* start, HeapWord* end) :
54221828Sgrehan    _start(start), _word_size(pointer_delta(end, start)) {
55221828Sgrehan    assert(end >= start, "incorrect constructor arguments");
56221828Sgrehan  }
57221828Sgrehan  MemRegion(MetaWord* start, MetaWord* end) :
58221828Sgrehan    _start((HeapWord*)start), _word_size(pointer_delta(end, start)) {
59221828Sgrehan    assert(end >= start, "incorrect constructor arguments");
60221828Sgrehan  }
61221828Sgrehan
62221828Sgrehan  MemRegion(const MemRegion& mr): _start(mr._start), _word_size(mr._word_size) {}
63221828Sgrehan
64221828Sgrehan  MemRegion intersection(const MemRegion mr2) const;
65221828Sgrehan  // regions must overlap or be adjacent
66221828Sgrehan  MemRegion _union(const MemRegion mr2) const;
67221828Sgrehan  // minus will fail a guarantee if mr2 is interior to this,
68221828Sgrehan  // since there's no way to return 2 disjoint regions.
69221828Sgrehan  MemRegion minus(const MemRegion mr2) const;
70221828Sgrehan
71221828Sgrehan  HeapWord* start() const { return _start; }
72221828Sgrehan  HeapWord* end() const   { return _start + _word_size; }
73221828Sgrehan  HeapWord* last() const  { return _start + _word_size - 1; }
74221828Sgrehan
75221828Sgrehan  void set_start(HeapWord* start) { _start = start; }
76221828Sgrehan  void set_end(HeapWord* end)     { _word_size = pointer_delta(end, _start); }
77221828Sgrehan  void set_word_size(size_t word_size) {
78221828Sgrehan    _word_size = word_size;
79221828Sgrehan  }
80221828Sgrehan
81221828Sgrehan  bool contains(const MemRegion mr2) const {
82221828Sgrehan    return _start <= mr2._start && end() >= mr2.end();
83221828Sgrehan  }
84221828Sgrehan  bool contains(const void* addr) const {
85221828Sgrehan    return addr >= (void*)_start && addr < (void*)end();
86221828Sgrehan  }
87221828Sgrehan  bool equals(const MemRegion mr2) const {
88221828Sgrehan    // first disjunct since we do not have a canonical empty set
89221828Sgrehan    return ((is_empty() && mr2.is_empty()) ||
90221828Sgrehan            (start() == mr2.start() && end() == mr2.end()));
91221828Sgrehan  }
92221828Sgrehan
93221828Sgrehan  size_t byte_size() const { return _word_size * sizeof(HeapWord); }
94221828Sgrehan  size_t word_size() const { return _word_size; }
95221828Sgrehan
96221828Sgrehan  bool is_empty() const { return word_size() == 0; }
97221828Sgrehan  void* operator new(size_t size);
98221828Sgrehan  void* operator new [](size_t size);
99221828Sgrehan  void  operator delete(void* p);
100221828Sgrehan  void  operator delete [](void* p);
101221828Sgrehan};
102221828Sgrehan
103221828Sgrehan// For iteration over MemRegion's.
104221828Sgrehan
105221828Sgrehanclass MemRegionClosure : public StackObj {
106221828Sgrehanpublic:
107221828Sgrehan  virtual void do_MemRegion(MemRegion mr) = 0;
108221828Sgrehan};
109221828Sgrehan
110221828Sgrehan// A ResourceObj version of MemRegionClosure
111221828Sgrehan
112221828Sgrehanclass MemRegionClosureRO: public MemRegionClosure {
113221828Sgrehanpublic:
114221828Sgrehan  void* operator new(size_t size, ResourceObj::allocation_type type, MEMFLAGS flags) {
115221828Sgrehan        return ResourceObj::operator new(size, type, flags);
116221828Sgrehan  }
117221828Sgrehan  void* operator new(size_t size, Arena *arena) {
118221828Sgrehan        return ResourceObj::operator new(size, arena);
119221828Sgrehan  }
120221828Sgrehan  void* operator new(size_t size) {
121221828Sgrehan        return ResourceObj::operator new(size);
122239042Sneel  }
123221828Sgrehan
124221828Sgrehan  void  operator delete(void* p) {} // nothing to do
125221828Sgrehan};
126239042Sneel
127221828Sgrehan#endif // SHARE_VM_MEMORY_MEMREGION_HPP
128221828Sgrehan