Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/memory/metaspace/chunkHeaderPool.hpp
40957 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2020 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#ifndef SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP
27
#define SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP
28
29
#include "memory/allocation.hpp"
30
#include "memory/metaspace/counters.hpp"
31
#include "memory/metaspace/metachunk.hpp"
32
#include "memory/metaspace/metachunkList.hpp"
33
#include "utilities/debug.hpp"
34
#include "utilities/globalDefinitions.hpp"
35
36
namespace metaspace {
37
38
// Chunk headers (Metachunk objects) are separate entities from their payload.
39
// Since they are allocated and released frequently in the course of buddy allocation
40
// (splitting, merging chunks happens often) we want allocation of them fast. Therefore
41
// we keep them in a simple pool (somewhat like a primitive slab allocator).
42
43
class ChunkHeaderPool : public CHeapObj<mtMetaspace> {
44
45
static const int SlabCapacity = 128;
46
47
struct Slab : public CHeapObj<mtMetaspace> {
48
Slab* _next;
49
int _top;
50
Metachunk _elems [SlabCapacity];
51
Slab() : _next(NULL), _top(0) {
52
for (int i = 0; i < SlabCapacity; i++) {
53
_elems[i].clear();
54
}
55
}
56
};
57
58
IntCounter _num_slabs;
59
Slab* _first_slab;
60
Slab* _current_slab;
61
62
IntCounter _num_handed_out;
63
64
MetachunkList _freelist;
65
66
void allocate_new_slab();
67
68
static ChunkHeaderPool* _chunkHeaderPool;
69
70
public:
71
72
ChunkHeaderPool();
73
74
~ChunkHeaderPool();
75
76
// Allocates a Metachunk structure. The structure is uninitialized.
77
Metachunk* allocate_chunk_header() {
78
DEBUG_ONLY(verify());
79
80
Metachunk* c = NULL;
81
c = _freelist.remove_first();
82
assert(c == NULL || c->is_dead(), "Not a freelist chunk header?");
83
if (c == NULL) {
84
if (_current_slab == NULL ||
85
_current_slab->_top == SlabCapacity) {
86
allocate_new_slab();
87
assert(_current_slab->_top < SlabCapacity, "Sanity");
88
}
89
c = _current_slab->_elems + _current_slab->_top;
90
_current_slab->_top++;
91
}
92
_num_handed_out.increment();
93
// By contract, the returned structure is uninitialized.
94
// Zap to make this clear.
95
DEBUG_ONLY(c->zap_header(0xBB);)
96
97
return c;
98
}
99
100
void return_chunk_header(Metachunk* c) {
101
// We only ever should return free chunks, since returning chunks
102
// happens only on merging and merging only works with free chunks.
103
assert(c != NULL && c->is_free(), "Sanity");
104
#ifdef ASSERT
105
// In debug, fill dead header with pattern.
106
c->zap_header(0xCC);
107
c->set_next(NULL);
108
c->set_prev(NULL);
109
#endif
110
c->set_dead();
111
_freelist.add(c);
112
_num_handed_out.decrement();
113
}
114
115
// Returns number of allocated elements.
116
int used() const { return _num_handed_out.get(); }
117
118
// Returns number of elements in free list.
119
int freelist_size() const { return _freelist.count(); }
120
121
// Returns size of memory used.
122
size_t memory_footprint_words() const;
123
124
DEBUG_ONLY(void verify() const;)
125
126
static void initialize();
127
128
// Returns reference to the one global chunk header pool.
129
static ChunkHeaderPool* pool() { return _chunkHeaderPool; }
130
131
};
132
133
} // namespace metaspace
134
135
#endif // SHARE_MEMORY_METASPACE_CHUNKHEADERPOOL_HPP
136
137