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_ALIGNMENT_HPP
27 #define SHARE_MEMORY_METASPACE_ALIGNMENT_HPP
28 
29 #include "memory/metaspace/chunklevel.hpp"
30 #include "memory/metaspace/freeBlocks.hpp"
31 #include "utilities/align.hpp"
32 #include "utilities/powerOfTwo.hpp"
33 #include "utilities/globalDefinitions.hpp"
34 
35 namespace metaspace {
36 
37 // The minimal alignment: good enough to store structures with 64bit wide members (also on 32-bit).
38 // Should we ever store longer values, revise.
39 static const int LogMetaspaceMinimalAlignment = LogBytesPerLong;
40 static const int MetaspaceMinAlignmentBytes = 1 << LogMetaspaceMinimalAlignment;
41 static const int MetaspaceMinAlignmentWords = MetaspaceMinAlignmentBytes / BytesPerWord;
42 
43 // The maximum possible alignment is the smallest chunk size (note that the buddy allocator places
44 // chunks at chunk-size-aligned boundaries, therefore the start address is guaranteed to be aligned).
45 // We cannot guarantee allocation alignment beyond this value.
46 static const int MetaspaceMaxAlignmentWords = chunklevel::MIN_CHUNK_WORD_SIZE;
47 
48 // Given a net allocation word size and an alignment value, return the raw word size we actually
49 // allocate internally.
50 inline size_t get_raw_word_size_for_requested_word_size(size_t net_word_size,
51                                                         size_t alignment_words) {
52 
53   // The alignment should be between the minimum alignment but cannot be larger than the smallest chunk size
54   assert(is_power_of_2(alignment_words), "invalid alignment");
55   assert(alignment_words >= MetaspaceMinAlignmentWords &&
56          alignment_words <= MetaspaceMaxAlignmentWords,
57          "invalid alignment (" SIZE_FORMAT ")", alignment_words);
58 
59   // Deallocated metablocks are kept in a binlist which means blocks need to have
60   // a minimal size
61   size_t raw_word_size = MAX2(net_word_size, FreeBlocks::MinWordSize);
62 
63   raw_word_size = align_up(raw_word_size, alignment_words);
64 
65   return raw_word_size;
66 }
67 
68 } // namespace metaspace
69 
70 #endif // SHARE_MEMORY_METASPACE_ALIGNMENT_HPP