Path: blob/master/test/hotspot/gtest/metaspace/metaspaceGtestSparseArray.hpp
41144 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_METASPACEGTESTSPARSEARRAY_HPP26#define GTEST_METASPACE_METASPACEGTESTSPARSEARRAY_HPP2728#include "memory/allocation.hpp"29#include "runtime/os.hpp"30#include "utilities/debug.hpp"31#include "metaspaceGtestCommon.hpp"32#include "metaspaceGtestRangeHelpers.hpp"3334/////// SparseArray<T> ////////////////3536// Throughout these tests we need to keep track of allocated items (ranges of metaspace memory, metachunks, ..)37// and be able to random-access them. Makes sense to have a helper for that.38template <class T>39class SparseArray : public StackObj {4041T* const _slots;42const int _num;4344// For convenience: a range covering all possible slot indices.45const IntRange _index_range;4647bool contains(int index) const {48return _index_range.contains(index);49}5051// Check slot intex for oob52void check_index(int i) const {53assert(contains(i), "Sanity");54}5556// Swap the content of two slots.57void swap(int i1, int i2) {58check_index(i1);59check_index(i2);60T tmp = _slots[i1];61_slots[i1] = _slots[i2];62_slots[i2] = tmp;63}6465enum condition_t { cond_null = 0, cond_non_null = 1, cond_dontcare = 2 };6667// Helper for next_matching_slot68bool slot_matches(int slot, condition_t c) const {69switch(c) {70case cond_null: return _slots[slot] == NULL;71case cond_non_null: return _slots[slot] != NULL;72case cond_dontcare: return true;73}74ShouldNotReachHere();75return false;76}7778// Starting at (including) index, find the next matching slot. Returns index or -1 if none found.79int next_matching_slot(int slot, condition_t c) const {80while(slot < _num) {81if (slot_matches(slot, c)) {82return slot;83}84slot++;85}86return -1;87}8889public:9091SparseArray(int num) :92_slots(NEW_C_HEAP_ARRAY(T, num, mtInternal)),93_num(num),94_index_range(num)95{96for (int i = 0; i < _num; i++) {97_slots[i] = NULL;98}99}100101T at(int i) { return _slots[i]; }102const T at(int i) const { return _slots[i]; }103void set_at(int i, T e) { _slots[i] = e; }104105int size() const { return _num; }106107bool slot_is_null(int i) const { check_index(i); return _slots[i] == NULL; }108109DEBUG_ONLY(void check_slot_is_null(int i) const { assert(slot_is_null(i), "Slot %d is not null", i); })110DEBUG_ONLY(void check_slot_is_not_null(int i) const { assert(!slot_is_null(i), "Slot %d is null", i); })111112// Shuffle all elements randomly113void shuffle() {114for (int i = 0; i < _num; i++) {115swap(i, random_slot_index());116}117}118119// Reverse elements120void reverse() {121for (int i = 0; i < _num / 2; i++) {122swap(i, _num - i);123}124}125126int first_slot() const { return 0; }127int next_slot(int index) const { return index == _index_range.highest() ? -1 : index + 1; }128129int first_non_null_slot() const { return next_matching_slot(0, cond_non_null); }130int next_non_null_slot(int index) const { return next_matching_slot(index + 1, cond_non_null); }131132int first_null_slot() const { return next_matching_slot(0, cond_null); }133int next_null_slot(int index) const { return next_matching_slot(index + 1, cond_null); }134135// Return a random slot index.136int random_slot_index() const {137return _index_range.random_value();138}139140int random_non_null_slot_index() const {141int i = next_non_null_slot(_index_range.random_value());142if (i == -1) {143i = first_non_null_slot();144}145return i;146}147148int random_null_slot_index() const {149int i = next_null_slot(_index_range.random_value());150if (i == -1) {151i = first_null_slot();152}153return i;154}155156IntRange random_slot_range() const {157return _index_range.random_subrange();158}159160};161162#endif // GTEST_METASPACE_METASPACEGTESTSPARSEARRAY_HPP163164165166