Mercurial > hg > truffle
annotate src/share/vm/memory/metachunk.cpp @ 12902:bdfbb1fb19ca
8026391: The Metachunk header wastes memory
Reviewed-by: coleenp, jmasa
author | stefank |
---|---|
date | Tue, 15 Oct 2013 14:28:51 +0200 |
parents | c23dbf0e8ab7 |
children | ec2e26e26183 |
rev | line source |
---|---|
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
1 /* |
12902 | 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
7208
eade6b2e4782
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. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
4 * |
eade6b2e4782
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 |
eade6b2e4782
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 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
8 * |
eade6b2e4782
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 |
eade6b2e4782
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 |
eade6b2e4782
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 |
eade6b2e4782
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 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
13 * accompanied this code). |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
14 * |
eade6b2e4782
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 |
eade6b2e4782
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, |
eade6b2e4782
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. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
18 * |
eade6b2e4782
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 |
eade6b2e4782
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 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
21 * questions. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
22 * |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
23 */ |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
24 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
25 #include "precompiled.hpp" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
26 #include "memory/allocation.hpp" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
27 #include "memory/metachunk.hpp" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
28 #include "utilities/copy.hpp" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
29 #include "utilities/debug.hpp" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
30 |
10175 | 31 class VirtualSpaceNode; |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
32 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
33 const size_t metadata_chunk_initialize = 0xf7f7f7f7; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
34 |
12902 | 35 size_t Metachunk::object_alignment() { |
36 return ARENA_AMALLOC_ALIGNMENT; | |
37 } | |
38 | |
39 size_t Metachunk::overhead() { | |
40 return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord; | |
41 } | |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
42 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
43 // Metachunk methods |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
44 |
10175 | 45 Metachunk::Metachunk(size_t word_size, |
12902 | 46 VirtualSpaceNode* container) |
47 : Metabase<Metachunk>(word_size), | |
10175 | 48 _top(NULL), |
49 _container(container) | |
50 { | |
12902 | 51 _top = initial_top(); |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
52 #ifdef ASSERT |
12902 | 53 set_is_tagged_free(false); |
10175 | 54 size_t data_word_size = pointer_delta(end(), |
12902 | 55 _top, |
10175 | 56 sizeof(MetaWord)); |
12902 | 57 Copy::fill_to_words((HeapWord*)_top, |
10175 | 58 data_word_size, |
59 metadata_chunk_initialize); | |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
60 #endif |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
61 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
62 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
63 MetaWord* Metachunk::allocate(size_t word_size) { |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
64 MetaWord* result = NULL; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
65 // If available, bump the pointer to allocate. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
66 if (free_word_size() >= word_size) { |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
67 result = _top; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
68 _top = _top + word_size; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
69 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
70 return result; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
71 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
72 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
73 // _bottom points to the start of the chunk including the overhead. |
7446
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
74 size_t Metachunk::used_word_size() const { |
12902 | 75 return pointer_delta(_top, bottom(), sizeof(MetaWord)); |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
76 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
77 |
7446
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
78 size_t Metachunk::free_word_size() const { |
12902 | 79 return pointer_delta(end(), _top, sizeof(MetaWord)); |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
80 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
81 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
82 void Metachunk::print_on(outputStream* st) const { |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
83 st->print_cr("Metachunk:" |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
84 " bottom " PTR_FORMAT " top " PTR_FORMAT |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
85 " end " PTR_FORMAT " size " SIZE_FORMAT, |
12902 | 86 bottom(), _top, end(), word_size()); |
7446
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
87 if (Verbose) { |
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
88 st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT, |
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
89 used_word_size(), free_word_size()); |
e51c9860cf66
8005082: NPG: Add specialized Metachunk sizes for reflection and anonymous classloaders
jmasa
parents:
7208
diff
changeset
|
90 } |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
91 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
92 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
93 #ifndef PRODUCT |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
94 void Metachunk::mangle() { |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
95 // Mangle the payload of the chunk and not the links that |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
96 // maintain list of chunks. |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
97 HeapWord* start = (HeapWord*)(bottom() + overhead()); |
12902 | 98 size_t size = word_size() - overhead(); |
99 Copy::fill_to_words(start, size, metadata_chunk_initialize); | |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
100 } |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
101 #endif // PRODUCT |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
102 |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
103 void Metachunk::verify() { |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
104 #ifdef ASSERT |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
105 // Cannot walk through the blocks unless the blocks have |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
106 // headers with sizes. |
12902 | 107 assert(bottom() <= _top && |
108 _top <= (MetaWord*)end(), | |
7208
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
109 "Chunk has been smashed"); |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
110 #endif |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
111 return; |
eade6b2e4782
8003554: NPG: move Metablock and Metachunk code out of metaspace.cpp
jmasa
parents:
diff
changeset
|
112 } |
12902 | 113 |
114 /////////////// Unit tests /////////////// | |
115 | |
116 #ifndef PRODUCT | |
117 | |
118 class TestMetachunk { | |
119 public: | |
120 static void test() { | |
121 size_t size = 2 * 1024 * 1024; | |
122 void* memory = malloc(size); | |
123 assert(memory != NULL, "Failed to malloc 2MB"); | |
124 | |
125 Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL); | |
126 | |
127 assert(metachunk->bottom() == (MetaWord*)metachunk, "assert"); | |
128 assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert"); | |
129 | |
130 // Check sizes | |
131 assert(metachunk->size() == metachunk->word_size(), "assert"); | |
132 assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(), | |
133 sizeof(MetaWord*)), "assert"); | |
134 | |
135 // Check usage | |
136 assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); | |
137 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); | |
138 assert(metachunk->top() == metachunk->initial_top(), "assert"); | |
139 assert(metachunk->is_empty(), "assert"); | |
140 | |
141 // Allocate | |
142 size_t alloc_size = 64; // Words | |
143 assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert"); | |
144 | |
145 MetaWord* mem = metachunk->allocate(alloc_size); | |
146 | |
147 // Check post alloc | |
148 assert(mem == metachunk->initial_top(), "assert"); | |
149 assert(mem + alloc_size == metachunk->top(), "assert"); | |
150 assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert"); | |
151 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); | |
152 assert(!metachunk->is_empty(), "assert"); | |
153 | |
154 // Clear chunk | |
155 metachunk->reset_empty(); | |
156 | |
157 // Check post clear | |
158 assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); | |
159 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); | |
160 assert(metachunk->top() == metachunk->initial_top(), "assert"); | |
161 assert(metachunk->is_empty(), "assert"); | |
162 | |
163 free(memory); | |
164 } | |
165 }; | |
166 | |
167 void TestMetachunk_test() { | |
168 TestMetachunk::test(); | |
169 } | |
170 | |
171 #endif |