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