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