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