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 #include "precompiled.hpp"
27 #include "memory/metaspace/metablock.inline.hpp"
28 //#define LOG_PLEASE
29 #include "metaspaceGtestCommon.hpp"
30 
31 using metaspace::MetaBlock;
32 
33 
34 #define CHECK_BLOCK_EMPTY(block) { \
35   EXPECT_TRUE(block.is_empty()); \
36   DEBUG_ONLY(block.verify()); \
37 }
38 
39 #define CHECK_BLOCK(block, expected_base, expected_size) { \
40     EXPECT_EQ(block.base(), (MetaWord*)expected_base); \
41     EXPECT_EQ((size_t)expected_size, block.word_size()); \
42     EXPECT_EQ(block.end(), expected_base + expected_size); \
43     DEBUG_ONLY(block.verify()); \
44 }
45 
46 static constexpr uintptr_t large_pointer = NOT_LP64(0x99999990) LP64_ONLY(0x9999999999999990ULL);
47 
48 TEST(metaspace, MetaBlock_1) {
49   MetaBlock bl;
50   CHECK_BLOCK_EMPTY(bl);
51 }
52 
53 TEST(metaspace, MetaBlock_2) {
54   MetaWord* const p = (MetaWord*)large_pointer;
55   constexpr size_t s = G;
56   MetaBlock bl(p, s);
57   CHECK_BLOCK(bl, p, s);
58 }
59 
60 TEST(metaspace, MetaBlock_3) {
61   MetaWord* const p = (MetaWord*)large_pointer;
62   MetaBlock bl(p, 0);
63   CHECK_BLOCK_EMPTY(bl);
64 }
65 
66 TEST_VM(metaspace, MetaBlock_4) {
67   MetaWord* const p = (MetaWord*)large_pointer;
68   MetaBlock bl(p, G);
69   CHECK_BLOCK(bl, p, G);
70 
71   MetaBlock bl_copy = bl, bl2;
72 
73   bl2 = bl.split_off_tail(M);
74   CHECK_BLOCK(bl, p, G - M);
75   CHECK_BLOCK(bl2, p + G - M, M);
76 
77   bl = bl_copy;
78 
79 bl.print_on(tty);
80 bl2.print_on(tty);
81   bl2 = bl.split_off_tail(G);
82   bl.print_on(tty);
83   bl2.print_on(tty);
84 
85   ASSERT_EQ(bl2, bl_copy);
86   ASSERT_TRUE(bl.is_empty());
87 
88   bl = bl_copy;
89 
90   bl2 = bl.split_off_tail(0);
91   ASSERT_EQ(bl, bl_copy);
92   ASSERT_TRUE(bl2.is_empty());
93 
94   MetaBlock empty;
95   bl = empty.split_off_tail(0);
96   ASSERT_TRUE(bl.is_empty());
97 }