author | stefank |
Fri, 14 Feb 2014 09:29:56 +0100 | |
changeset 22883 | 5378704451dc |
parent 20730 | 59f433c86d9d |
child 24424 | 2658d7834c6e |
permissions | -rw-r--r-- |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
1 |
/* |
20729 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
4 |
* |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
8 |
* |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
13 |
* accompanied this code). |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
14 |
* |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
18 |
* |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
21 |
* questions. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
22 |
* |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
23 |
*/ |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
24 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
25 |
#include "precompiled.hpp" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
26 |
#include "memory/allocation.hpp" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
27 |
#include "memory/metachunk.hpp" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
28 |
#include "utilities/copy.hpp" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
29 |
#include "utilities/debug.hpp" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
30 |
|
17101 | 31 |
class VirtualSpaceNode; |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
32 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
33 |
const size_t metadata_chunk_initialize = 0xf7f7f7f7; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
34 |
|
20729 | 35 |
size_t Metachunk::object_alignment() { |
20730
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
36 |
// Must align pointers and sizes to 8, |
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
37 |
// so that 64 bit types get correctly aligned. |
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
38 |
const size_t alignment = 8; |
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
39 |
|
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
40 |
// Make sure that the Klass alignment also agree. |
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
41 |
STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes); |
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
42 |
|
59f433c86d9d
8026392: Metachunks and Metablocks are using a too large alignment
stefank
parents:
20729
diff
changeset
|
43 |
return alignment; |
20729 | 44 |
} |
45 |
||
46 |
size_t Metachunk::overhead() { |
|
47 |
return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord; |
|
48 |
} |
|
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
49 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
50 |
// Metachunk methods |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
51 |
|
17101 | 52 |
Metachunk::Metachunk(size_t word_size, |
20729 | 53 |
VirtualSpaceNode* container) |
54 |
: Metabase<Metachunk>(word_size), |
|
17101 | 55 |
_top(NULL), |
56 |
_container(container) |
|
57 |
{ |
|
20729 | 58 |
_top = initial_top(); |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
59 |
#ifdef ASSERT |
20729 | 60 |
set_is_tagged_free(false); |
17101 | 61 |
size_t data_word_size = pointer_delta(end(), |
20729 | 62 |
_top, |
17101 | 63 |
sizeof(MetaWord)); |
20729 | 64 |
Copy::fill_to_words((HeapWord*)_top, |
17101 | 65 |
data_word_size, |
66 |
metadata_chunk_initialize); |
|
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
67 |
#endif |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
68 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
69 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
70 |
MetaWord* Metachunk::allocate(size_t word_size) { |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
71 |
MetaWord* result = NULL; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
72 |
// If available, bump the pointer to allocate. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
73 |
if (free_word_size() >= word_size) { |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
74 |
result = _top; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
75 |
_top = _top + word_size; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
76 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
77 |
return result; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
78 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
79 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
80 |
// _bottom points to the start of the chunk including the overhead. |
15086
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
81 |
size_t Metachunk::used_word_size() const { |
20729 | 82 |
return pointer_delta(_top, bottom(), sizeof(MetaWord)); |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
83 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
84 |
|
15086
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
85 |
size_t Metachunk::free_word_size() const { |
20729 | 86 |
return pointer_delta(end(), _top, sizeof(MetaWord)); |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
87 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
88 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
89 |
void Metachunk::print_on(outputStream* st) const { |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
90 |
st->print_cr("Metachunk:" |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
91 |
" bottom " PTR_FORMAT " top " PTR_FORMAT |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
92 |
" end " PTR_FORMAT " size " SIZE_FORMAT, |
20729 | 93 |
bottom(), _top, end(), word_size()); |
15086
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
94 |
if (Verbose) { |
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
95 |
st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT, |
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
96 |
used_word_size(), free_word_size()); |
2bfd799e9147
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
14635
diff
changeset
|
97 |
} |
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
98 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
99 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
100 |
#ifndef PRODUCT |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
101 |
void Metachunk::mangle() { |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
102 |
// Mangle the payload of the chunk and not the links that |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
103 |
// maintain list of chunks. |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
104 |
HeapWord* start = (HeapWord*)(bottom() + overhead()); |
20729 | 105 |
size_t size = word_size() - overhead(); |
106 |
Copy::fill_to_words(start, size, metadata_chunk_initialize); |
|
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
107 |
} |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
108 |
#endif // PRODUCT |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
109 |
|
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
110 |
void Metachunk::verify() { |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
111 |
#ifdef ASSERT |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
112 |
// Cannot walk through the blocks unless the blocks have |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
113 |
// headers with sizes. |
20729 | 114 |
assert(bottom() <= _top && |
115 |
_top <= (MetaWord*)end(), |
|
14635
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
116 |
"Chunk has been smashed"); |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
117 |
#endif |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
118 |
return; |
6a3c85b8111c
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
119 |
} |
20729 | 120 |
|
121 |
/////////////// Unit tests /////////////// |
|
122 |
||
123 |
#ifndef PRODUCT |
|
124 |
||
125 |
class TestMetachunk { |
|
126 |
public: |
|
127 |
static void test() { |
|
128 |
size_t size = 2 * 1024 * 1024; |
|
129 |
void* memory = malloc(size); |
|
130 |
assert(memory != NULL, "Failed to malloc 2MB"); |
|
131 |
||
132 |
Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL); |
|
133 |
||
134 |
assert(metachunk->bottom() == (MetaWord*)metachunk, "assert"); |
|
135 |
assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert"); |
|
136 |
||
137 |
// Check sizes |
|
138 |
assert(metachunk->size() == metachunk->word_size(), "assert"); |
|
139 |
assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(), |
|
140 |
sizeof(MetaWord*)), "assert"); |
|
141 |
||
142 |
// Check usage |
|
143 |
assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); |
|
144 |
assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); |
|
145 |
assert(metachunk->top() == metachunk->initial_top(), "assert"); |
|
146 |
assert(metachunk->is_empty(), "assert"); |
|
147 |
||
148 |
// Allocate |
|
149 |
size_t alloc_size = 64; // Words |
|
150 |
assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert"); |
|
151 |
||
152 |
MetaWord* mem = metachunk->allocate(alloc_size); |
|
153 |
||
154 |
// Check post alloc |
|
155 |
assert(mem == metachunk->initial_top(), "assert"); |
|
156 |
assert(mem + alloc_size == metachunk->top(), "assert"); |
|
157 |
assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert"); |
|
158 |
assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); |
|
159 |
assert(!metachunk->is_empty(), "assert"); |
|
160 |
||
161 |
// Clear chunk |
|
162 |
metachunk->reset_empty(); |
|
163 |
||
164 |
// Check post clear |
|
165 |
assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); |
|
166 |
assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); |
|
167 |
assert(metachunk->top() == metachunk->initial_top(), "assert"); |
|
168 |
assert(metachunk->is_empty(), "assert"); |
|
169 |
||
170 |
free(memory); |
|
171 |
} |
|
172 |
}; |
|
173 |
||
174 |
void TestMetachunk_test() { |
|
175 |
TestMetachunk::test(); |
|
176 |
} |
|
177 |
||
178 |
#endif |