1 /*
 2  * Copyright (c) 2023 Red Hat, Inc. All rights reserved.
 3  * Copyright (c) 2023, 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_METABLOCK_INLINE_HPP
27 #define SHARE_MEMORY_METASPACE_METABLOCK_INLINE_HPP
28 
29 #include "memory/metaspace/metablock.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/align.hpp"
32 #include "utilities/ostream.hpp"
33 #include "utilities/debug.hpp"
34 
35 class outputStream;
36 
37 namespace metaspace {
38 
39 inline MetaBlock MetaBlock::split_off_tail(size_t tailsize) {
40   if (is_empty() || tailsize == 0) {
41     return MetaBlock();
42   }
43   assert(tailsize <= _word_size, "invalid split point for block "
44          METABLOCKFORMAT ": %zu", METABLOCKFORMATARGS(*this), tailsize);
45   const size_t new_size = _word_size - tailsize;
46   MetaBlock tail(_base + new_size, tailsize);
47   _word_size = new_size;
48   if (_word_size == 0) {
49     _base = nullptr;
50   }
51   return tail;
52 }
53 
54 inline void MetaBlock::print_on(outputStream* st) const {
55   st->print(METABLOCKFORMAT, METABLOCKFORMATARGS(*this));
56 }
57 
58 // Convenience functions
59 inline bool MetaBlock::is_aligned_base(size_t alignment_words) const {
60   return is_aligned(_base, alignment_words * BytesPerWord);
61 }
62 
63 inline bool MetaBlock::is_aligned_size(size_t alignment_words) const {
64   return is_aligned(_word_size, alignment_words);
65 }
66 
67 // some convenience asserts
68 #define assert_block_base_aligned(block, alignment_words) \
69   assert(block.is_aligned_base(alignment_words), "Block wrong base alignment " METABLOCKFORMAT, METABLOCKFORMATARGS(block));
70 
71 #define assert_block_size_aligned(block, alignment_words) \
72   assert(block.is_aligned_size(alignment_words), "Block wrong size alignment " METABLOCKFORMAT, METABLOCKFORMATARGS(block));
73 
74 #define assert_block_larger_or_equal(block, x) \
75   assert(block.word_size() >= x, "Block too small " METABLOCKFORMAT, METABLOCKFORMATARGS(block));
76 
77 #ifdef ASSERT
78 inline void MetaBlock::verify() const {
79   assert( (_base == nullptr && _word_size == 0) ||
80           (_base != nullptr && _word_size > 0),
81           "block invalid " METABLOCKFORMAT, METABLOCKFORMATARGS(*this));
82 }
83 #endif
84 
85 } // namespace metaspace
86 
87 #endif // SHARE_MEMORY_METASPACE_METABLOCK_INLINE_HPP