Path: blob/master/test/hotspot/gtest/metaspace/metaspaceGtestContexts.hpp
41145 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP26#define GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/chunklevel.hpp"30#include "memory/metaspace/metachunk.hpp"31#include "memory/metaspace/testHelpers.hpp"32#include "metaspaceGtestCommon.hpp"3334using metaspace::Metachunk;35using metaspace::chunklevel_t;36using namespace metaspace::chunklevel;3738class MetaspaceGtestContext : public metaspace::MetaspaceTestContext {39public:40MetaspaceGtestContext(size_t commit_limit = 0, size_t reserve_limit = 0) :41metaspace::MetaspaceTestContext("gtest-metaspace-context", commit_limit, reserve_limit)42{}43};4445class ChunkGtestContext : public MetaspaceGtestContext {4647int _num_chunks_allocated;4849void checked_alloc_chunk_0(Metachunk** p_return_value, chunklevel_t preferred_level,50chunklevel_t max_level, size_t min_committed_size);5152// Test pattern established when allocating from the chunk with allocate_from_chunk_with_tests().53void test_pattern(Metachunk* c, size_t word_size);54void test_pattern(Metachunk* c) { test_pattern(c, c->used_words()); }5556public:5758ChunkGtestContext(size_t commit_limit = 0, size_t reserve_limit = 0) :59MetaspaceGtestContext(commit_limit, reserve_limit),60_num_chunks_allocated(0)61{}6263/////6465// Note: all test functions return void and return values are by pointer ref; this is awkward but otherwise we cannot66// use gtest ASSERT macros inside those functions.6768// Allocate a chunk (you do not know if it will succeed).69void alloc_chunk(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {70checked_alloc_chunk_0(p_return_value, preferred_level, max_level, min_committed_size);71}7273// Allocate a chunk; do not expect success, but if it succeeds, test the chunk.74void alloc_chunk(Metachunk** p_return_value, chunklevel_t level) {75alloc_chunk(p_return_value, level, level, word_size_for_level(level));76}7778// Allocate a chunk; it must succeed. Test the chunk.79void alloc_chunk_expect_success(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {80checked_alloc_chunk_0(p_return_value, preferred_level, max_level, min_committed_size);81ASSERT_NOT_NULL(*p_return_value);82}8384// Allocate a chunk; it must succeed. Test the chunk.85void alloc_chunk_expect_success(Metachunk** p_return_value, chunklevel_t level) {86alloc_chunk_expect_success(p_return_value, level, level, word_size_for_level(level));87}8889// Allocate a chunk but expect it to fail.90void alloc_chunk_expect_failure(chunklevel_t preferred_level, chunklevel_t max_level, size_t min_committed_size) {91Metachunk* c = NULL;92checked_alloc_chunk_0(&c, preferred_level, max_level, min_committed_size);93ASSERT_NULL(c);94}9596// Allocate a chunk but expect it to fail.97void alloc_chunk_expect_failure(chunklevel_t level) {98return alloc_chunk_expect_failure(level, level, word_size_for_level(level));99}100101/////102103void return_chunk(Metachunk* c);104105/////106107// Allocates from a chunk; also, fills allocated area with test pattern which will be tested with test_pattern().108void allocate_from_chunk(MetaWord** p_return_value, Metachunk* c, size_t word_size);109110// Convenience function: allocate from chunk for when you don't care for the result pointer111void allocate_from_chunk(Metachunk* c, size_t word_size) {112MetaWord* dummy;113allocate_from_chunk(&dummy, c, word_size);114}115116void commit_chunk_with_test(Metachunk* c, size_t additional_size);117void commit_chunk_expect_failure(Metachunk* c, size_t additional_size);118119void uncommit_chunk_with_test(Metachunk* c);120121};122123#endif // GTEST_METASPACE_METASPACE_GTESTCONTEXTS_HPP124125126127