|
1 /* |
|
2 * Copyright (c) 2019, SAP SE. All rights reserved. |
|
3 * Copyright (c) 2019, Oracle and/or its affiliates. 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_BLOCKLISTARRAY_INLINE_HPP |
|
27 #define SHARE_MEMORY_METASPACE_BLOCKLISTARRAY_INLINE_HPP |
|
28 |
|
29 #include "memory/allocation.hpp" |
|
30 #include "memory/metaspace/blockListArray.hpp" |
|
31 #include "utilities/debug.hpp" |
|
32 #include "utilities/globalDefinitions.hpp" |
|
33 #include "utilities/ostream.hpp" |
|
34 |
|
35 namespace metaspace { |
|
36 |
|
37 |
|
38 // Starting at (including) pos, find the position of the next 1 bit. |
|
39 // Return -1 if not found. |
|
40 int BlockListFreeMap::find_next_set_bit(int pos) const { |
|
41 |
|
42 if (get_bit(pos)) { |
|
43 return pos; |
|
44 } |
|
45 mask_type m2 = _mask; |
|
46 int pos2 = pos + 1; |
|
47 m2 >>= pos2; |
|
48 if (m2 > 0) { |
|
49 while ((m2 & (mask_type)1) == 0) { |
|
50 m2 >>= 1; |
|
51 pos2 ++; |
|
52 } |
|
53 return pos2; |
|
54 } |
|
55 return -1; |
|
56 |
|
57 } |
|
58 |
|
59 /////////////////////////////////////// |
|
60 |
|
61 template <size_t min_word_size, size_t spread, int num_bins> |
|
62 void BlockListArray<min_word_size, spread, num_bins>::put(MetaWord* p, size_t word_size) { |
|
63 assert(word_size >= minimal_word_size() && word_size < maximal_word_size(), "Invalid word size"); |
|
64 block_t* b = (block_t*)p; |
|
65 int bno = bin_for_size(word_size); |
|
66 assert(bno >= 0 && bno < num_bins, "Sanity"); |
|
67 assert(b != _bins[bno], "double add?"); |
|
68 b->next = _bins[bno]; |
|
69 b->size = word_size; |
|
70 _bins[bno] = b; |
|
71 _map.set_bit(bno); |
|
72 } |
|
73 |
|
74 template <size_t min_word_size, size_t spread, int num_bins> |
|
75 block_t* BlockListArray<min_word_size, spread, num_bins>::get(size_t word_size) { |
|
76 // Adjust size for spread (we need the bin number which guarantees word_size). |
|
77 word_size += (spread - 1); |
|
78 if (word_size >= maximal_word_size()) { |
|
79 return NULL; |
|
80 } |
|
81 int bno = bin_for_size(word_size); |
|
82 bno = _map.find_next_set_bit(bno); |
|
83 if (bno != -1) { |
|
84 assert(bno >= 0 && bno < num_bins, "Sanity"); |
|
85 assert(_bins[bno] != NULL, "Sanity"); |
|
86 block_t* b = _bins[bno]; |
|
87 _bins[bno] = b->next; |
|
88 if (_bins[bno] == NULL) { |
|
89 _map.clr_bit(bno); |
|
90 } |
|
91 return b; |
|
92 } |
|
93 return NULL; |
|
94 } |
|
95 |
|
96 #ifdef ASSERT |
|
97 template <size_t min_word_size, size_t spread, int num_bins> |
|
98 void BlockListArray<min_word_size, spread, num_bins>::verify() const { |
|
99 for (int i = 0; i < num_bins; i ++) { |
|
100 assert(_map.get_bit(i) == (_bins[i] != NULL), "Sanity"); |
|
101 const size_t min_size = minimal_word_size_in_bin(i); |
|
102 const size_t max_size = maximal_word_size_in_bin(i); |
|
103 for(block_t* b = _bins[i]; b != NULL; b = b->next) { |
|
104 assert(b->size >= min_size && b->size < max_size, "Sanity"); |
|
105 } |
|
106 } |
|
107 } |
|
108 #endif // ASSERT |
|
109 |
|
110 |
|
111 template <size_t min_word_size, size_t spread, int num_bins> |
|
112 void BlockListArray<min_word_size, spread, num_bins>::statistics(block_stats_t* stats) const { |
|
113 for (int i = 0; i < num_bins; i ++) { |
|
114 for(block_t* b = _bins[i]; b != NULL; b = b->next) { |
|
115 stats->num_blocks ++; |
|
116 stats->word_size += b->size; |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 template <size_t min_word_size, size_t spread, int num_bins> |
|
122 void BlockListArray<min_word_size, spread, num_bins>::print(outputStream* st) const { |
|
123 bool first = true; |
|
124 for (int i = 0; i < num_bins; i ++) { |
|
125 int n = 0; |
|
126 for(block_t* b = _bins[i]; b != NULL; b = b->next) { |
|
127 n ++; |
|
128 } |
|
129 if (n > 0) { |
|
130 if (!first) { |
|
131 st->print(", "); |
|
132 } else { |
|
133 first = false; |
|
134 } |
|
135 st->print(SIZE_FORMAT "=%d", minimal_word_size_in_bin(i), n); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 } // namespace metaspace |
|
141 |
|
142 #endif // SHARE_MEMORY_METASPACE_BLOCKLISTARRAY_INLINE_HPP |