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