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 GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP
27 #define GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP
28 
29 #include "memory/allocation.hpp"
30 #include "memory/metaspace/chunklevel.hpp"
31 #include "memory/metaspace/metachunk.hpp"
32 #include "memory/metaspace/testHelpers.hpp"
33 #include "metaspaceGtestCommon.hpp"
34 
35 using metaspace::Metachunk;
36 using metaspace::chunklevel_t;
37 using namespace metaspace::chunklevel;
38 
39 class MetaspaceGtestContext : public metaspace::MetaspaceTestContext {
40 public:
MetaspaceGtestContext(size_t commit_limit=0,size_t reserve_limit=0)41   MetaspaceGtestContext(size_t commit_limit = 0, size_t reserve_limit = 0) :
42     metaspace::MetaspaceTestContext("gtest-metaspace-context", commit_limit, reserve_limit)
43   {}
44 };
45 
46 class ChunkGtestContext : public MetaspaceGtestContext {
47 
48   int _num_chunks_allocated;
49 
50   void checked_alloc_chunk_0(Metachunk** p_return_value, chunklevel_t preferred_level,
51                              chunklevel_t max_level, size_t min_committed_size);
52 
53   // Test pattern established when allocating from the chunk with allocate_from_chunk_with_tests().
54   void test_pattern(Metachunk* c, size_t word_size);
test_pattern(Metachunk * c)55   void test_pattern(Metachunk* c) { test_pattern(c, c->used_words()); }
56 
57 public:
58 
ChunkGtestContext(size_t commit_limit=0,size_t reserve_limit=0)59   ChunkGtestContext(size_t commit_limit = 0, size_t reserve_limit = 0) :
60     MetaspaceGtestContext(commit_limit, reserve_limit),
61     _num_chunks_allocated(0)
62   {}
63 
64   /////
65 
66   // Note: all test functions return void and return values are by pointer ref; this is awkward but otherwise we cannot
67   // use gtest ASSERT macros inside those functions.
68 
69   // Allocate a chunk (you do not know if it will succeed).
alloc_chunk(Metachunk ** p_return_value,chunklevel_t preferred_level,chunklevel_t max_level,size_t min_committed_size)70   void alloc_chunk(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {
71     checked_alloc_chunk_0(p_return_value, preferred_level, max_level, min_committed_size);
72   }
73 
74   // Allocate a chunk; do not expect success, but if it succeeds, test the chunk.
alloc_chunk(Metachunk ** p_return_value,chunklevel_t level)75   void alloc_chunk(Metachunk** p_return_value, chunklevel_t level) {
76     alloc_chunk(p_return_value, level, level, word_size_for_level(level));
77   }
78 
79   // Allocate a chunk; it must succeed. Test the chunk.
alloc_chunk_expect_success(Metachunk ** p_return_value,chunklevel_t preferred_level,chunklevel_t max_level,size_t min_committed_size)80   void alloc_chunk_expect_success(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {
81     checked_alloc_chunk_0(p_return_value, preferred_level, max_level, min_committed_size);
82     ASSERT_NOT_NULL(*p_return_value);
83   }
84 
85   // Allocate a chunk; it must succeed. Test the chunk.
alloc_chunk_expect_success(Metachunk ** p_return_value,chunklevel_t level)86   void alloc_chunk_expect_success(Metachunk** p_return_value, chunklevel_t level) {
87     alloc_chunk_expect_success(p_return_value, level, level, word_size_for_level(level));
88   }
89 
90   // Allocate a chunk but expect it to fail.
alloc_chunk_expect_failure(chunklevel_t preferred_level,chunklevel_t max_level,size_t min_committed_size)91   void alloc_chunk_expect_failure(chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {
92     Metachunk* c = NULL;
93     checked_alloc_chunk_0(&c, preferred_level, max_level, min_committed_size);
94     ASSERT_NULL(c);
95   }
96 
97   // Allocate a chunk but expect it to fail.
alloc_chunk_expect_failure(chunklevel_t level)98   void alloc_chunk_expect_failure(chunklevel_t level) {
99     return alloc_chunk_expect_failure(level, level, word_size_for_level(level));
100   }
101 
102   /////
103 
104   void return_chunk(Metachunk* c);
105 
106   /////
107 
108   // Allocates from a chunk; also, fills allocated area with test pattern which will be tested with test_pattern().
109   void allocate_from_chunk(MetaWord** p_return_value, Metachunk* c, size_t word_size);
110 
111   // Convenience function: allocate from chunk for when you don't care for the result pointer
allocate_from_chunk(Metachunk * c,size_t word_size)112   void allocate_from_chunk(Metachunk* c, size_t word_size) {
113     MetaWord* dummy;
114     allocate_from_chunk(&dummy, c, word_size);
115   }
116 
117   void commit_chunk_with_test(Metachunk* c, size_t additional_size);
118   void commit_chunk_expect_failure(Metachunk* c, size_t additional_size);
119 
120   void uncommit_chunk_with_test(Metachunk* c);
121 
122 };
123 
124 #endif // GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP
125 
126