Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/memory/arena.hpp
40949 views
1
/*
2
* Copyright (c) 2017, 2021, 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
#ifndef SHARE_MEMORY_ARENA_HPP
26
#define SHARE_MEMORY_ARENA_HPP
27
28
#include "memory/allocation.hpp"
29
#include "runtime/globals.hpp"
30
#include "utilities/globalDefinitions.hpp"
31
#include "utilities/powerOfTwo.hpp"
32
33
#include <new>
34
35
// The byte alignment to be used by Arena::Amalloc. See bugid 4169348.
36
// Note: this value must be a power of 2
37
38
#define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
39
40
#define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
41
#define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
42
#define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
43
44
//------------------------------Chunk------------------------------------------
45
// Linked list of raw memory chunks
46
class Chunk: CHeapObj<mtChunk> {
47
48
private:
49
Chunk* _next; // Next Chunk in list
50
const size_t _len; // Size of this Chunk
51
public:
52
void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw();
53
void operator delete(void* p);
54
Chunk(size_t length);
55
56
enum {
57
// default sizes; make them slightly smaller than 2**k to guard against
58
// buddy-system style malloc implementations
59
#ifdef _LP64
60
slack = 40, // [RGV] Not sure if this is right, but make it
61
// a multiple of 8.
62
#else
63
slack = 20, // suspected sizeof(Chunk) + internal malloc headers
64
#endif
65
66
tiny_size = 256 - slack, // Size of first chunk (tiny)
67
init_size = 1*K - slack, // Size of first chunk (normal aka small)
68
medium_size= 10*K - slack, // Size of medium-sized chunk
69
size = 32*K - slack, // Default size of an Arena chunk (following the first)
70
non_pool_size = init_size + 32 // An initial size which is not one of above
71
};
72
73
void chop(); // Chop this chunk
74
void next_chop(); // Chop next chunk
75
static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
76
static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); }
77
78
size_t length() const { return _len; }
79
Chunk* next() const { return _next; }
80
void set_next(Chunk* n) { _next = n; }
81
// Boundaries of data area (possibly unused)
82
char* bottom() const { return ((char*) this) + aligned_overhead_size(); }
83
char* top() const { return bottom() + _len; }
84
bool contains(char* p) const { return bottom() <= p && p <= top(); }
85
86
// Start the chunk_pool cleaner task
87
static void start_chunk_pool_cleaner_task();
88
};
89
90
//------------------------------Arena------------------------------------------
91
// Fast allocation of memory
92
class Arena : public CHeapObj<mtNone> {
93
protected:
94
friend class HandleMark;
95
friend class NoHandleMark;
96
friend class VMStructs;
97
98
MEMFLAGS _flags; // Memory tracking flags
99
100
Chunk *_first; // First chunk
101
Chunk *_chunk; // current chunk
102
char *_hwm, *_max; // High water mark and max in current chunk
103
// Get a new Chunk of at least size x
104
void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
105
size_t _size_in_bytes; // Size of arena (used for native memory tracking)
106
107
debug_only(void* malloc(size_t size);)
108
debug_only(void* internal_malloc_4(size_t x);)
109
110
void signal_out_of_memory(size_t request, const char* whence) const;
111
112
bool check_for_overflow(size_t request, const char* whence,
113
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const {
114
if (UINTPTR_MAX - request < (uintptr_t)_hwm) {
115
if (alloc_failmode == AllocFailStrategy::RETURN_NULL) {
116
return false;
117
}
118
signal_out_of_memory(request, whence);
119
}
120
return true;
121
}
122
123
public:
124
Arena(MEMFLAGS memflag);
125
Arena(MEMFLAGS memflag, size_t init_size);
126
~Arena();
127
void destruct_contents();
128
char* hwm() const { return _hwm; }
129
130
// new operators
131
void* operator new (size_t size) throw();
132
void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw();
133
134
// dynamic memory type tagging
135
void* operator new(size_t size, MEMFLAGS flags) throw();
136
void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw();
137
void operator delete(void* p);
138
139
// Fast allocate in the arena. Common case is: pointer test + increment.
140
void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
141
assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
142
x = ARENA_ALIGN(x);
143
debug_only(if (UseMallocOnly) return malloc(x);)
144
if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode))
145
return NULL;
146
if (_hwm + x > _max) {
147
return grow(x, alloc_failmode);
148
} else {
149
char *old = _hwm;
150
_hwm += x;
151
return old;
152
}
153
}
154
// Further assume size is padded out to words
155
void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
156
assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
157
debug_only(if (UseMallocOnly) return malloc(x);)
158
if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode))
159
return NULL;
160
if (_hwm + x > _max) {
161
return grow(x, alloc_failmode);
162
} else {
163
char *old = _hwm;
164
_hwm += x;
165
return old;
166
}
167
}
168
169
// Allocate with 'double' alignment. It is 8 bytes on sparc.
170
// In other cases Amalloc_D() should be the same as Amalloc_4().
171
void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
172
assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
173
debug_only(if (UseMallocOnly) return malloc(x);)
174
if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode))
175
return NULL;
176
if (_hwm + x > _max) {
177
return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes.
178
} else {
179
char *old = _hwm;
180
_hwm += x;
181
return old;
182
}
183
}
184
185
// Fast delete in area. Common case is: NOP (except for storage reclaimed)
186
bool Afree(void *ptr, size_t size) {
187
if (ptr == NULL) {
188
return true; // as with free(3), freeing NULL is a noop.
189
}
190
#ifdef ASSERT
191
if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
192
if (UseMallocOnly) return true;
193
#endif
194
if (((char*)ptr) + size == _hwm) {
195
_hwm = (char*)ptr;
196
return true;
197
} else {
198
// Unable to fast free, so we just drop it.
199
return false;
200
}
201
}
202
203
void *Arealloc( void *old_ptr, size_t old_size, size_t new_size,
204
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
205
206
// Move contents of this arena into an empty arena
207
Arena *move_contents(Arena *empty_arena);
208
209
// Determine if pointer belongs to this Arena or not.
210
bool contains( const void *ptr ) const;
211
212
// Total of all chunks in use (not thread-safe)
213
size_t used() const;
214
215
// Total # of bytes used
216
size_t size_in_bytes() const { return _size_in_bytes; };
217
void set_size_in_bytes(size_t size);
218
219
static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) PRODUCT_RETURN;
220
static void free_all(char** start, char** end) PRODUCT_RETURN;
221
222
private:
223
// Reset this Arena to empty, access will trigger grow if necessary
224
void reset(void) {
225
_first = _chunk = NULL;
226
_hwm = _max = NULL;
227
set_size_in_bytes(0);
228
}
229
};
230
231
// One of the following macros must be used when allocating
232
// an array or object from an arena
233
#define NEW_ARENA_ARRAY(arena, type, size) \
234
(type*) (arena)->Amalloc((size) * sizeof(type))
235
236
#define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size) \
237
(type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \
238
(new_size) * sizeof(type) )
239
240
#define FREE_ARENA_ARRAY(arena, type, old, size) \
241
(arena)->Afree((char*)(old), (size) * sizeof(type))
242
243
#define NEW_ARENA_OBJ(arena, type) \
244
NEW_ARENA_ARRAY(arena, type, 1)
245
246
#endif // SHARE_MEMORY_ARENA_HPP
247
248