24
#include "precompiled.hpp"
28
#include "gc/z/zAddress.inline.hpp"
29
#include "gc/z/zGlobals.hpp"
30
#include "gc/z/zList.inline.hpp"
31
#include "gc/z/zMemory.inline.hpp"
32
#include "gc/z/zSyscall_windows.hpp"
33
#include "gc/z/zVirtualMemory.hpp"
34
#include "runtime/os.hpp"
35
#include "unittest.hpp"
37
using namespace testing;
39
#define EXPECT_ALLOC_OK(offset) EXPECT_NE(offset, zoffset(UINTPTR_MAX))
41
class ZMapperTest : public Test {
43
static constexpr size_t ZMapperTestReservationSize = 32 * M;
45
static bool _initialized;
46
static ZMemoryManager* _va;
48
ZVirtualMemoryManager* _vmm;
51
bool reserve_for_test() {
53
_vmm->pd_initialize_before_reserve();
56
if (!_vmm->pd_reserve(ZOffset::address_unsafe(zoffset(0)), ZMapperTestReservationSize)) {
61
_va->free(zoffset(0), ZMapperTestReservationSize);
64
_vmm->pd_initialize_after_reserve();
69
virtual void SetUp() {
71
if (!ZSyscall::is_supported()) {
72
GTEST_SKIP() << "Requires Windows version 1803 or later";
76
ZSyscall::initialize();
77
ZGlobalsPointers::initialize();
80
_vmm = (ZVirtualMemoryManager*)os::malloc(sizeof(ZVirtualMemoryManager), mtTest);
83
_va = new (&_vmm->_manager) ZMemoryManager();
86
if (!reserve_for_test()) {
87
GTEST_SKIP() << "Failed to reserve address space";
94
virtual void TearDown() {
95
if (!ZSyscall::is_supported()) {
101
_vmm->pd_unreserve(ZOffset::address_unsafe(zoffset(0)), 0);
106
static void test_alloc_low_address() {
108
zoffset bottom = _va->alloc_low_address(ZGranuleSize);
109
EXPECT_ALLOC_OK(bottom);
111
_va->free(bottom, ZGranuleSize);
114
bottom = _va->alloc_low_address(ZGranuleSize * 3);
115
EXPECT_ALLOC_OK(bottom);
117
_va->free(bottom, ZGranuleSize * 3);
120
bottom = _va->alloc_low_address(ZGranuleSize);
121
EXPECT_ALLOC_OK(bottom);
123
zoffset next = _va->alloc_low_address(ZGranuleSize);
124
EXPECT_ALLOC_OK(next);
126
_va->free(bottom, ZGranuleSize);
127
_va->free(next, ZGranuleSize);
130
static void test_alloc_high_address() {
132
zoffset high = _va->alloc_high_address(ZGranuleSize);
133
EXPECT_ALLOC_OK(high);
135
zoffset prev = _va->alloc_high_address(ZGranuleSize);
136
EXPECT_ALLOC_OK(prev);
138
_va->free(high, ZGranuleSize);
139
_va->free(prev, ZGranuleSize);
142
high = _va->alloc_high_address(ZGranuleSize * 2);
143
EXPECT_ALLOC_OK(high);
145
_va->free(high, ZGranuleSize * 2);
148
static void test_alloc_whole_area() {
150
zoffset bottom = _va->alloc_low_address(ZMapperTestReservationSize);
151
EXPECT_ALLOC_OK(bottom);
154
_va->free(bottom, ZGranuleSize * 4);
155
_va->free(bottom + ZGranuleSize * 6, ZGranuleSize * 6);
157
zoffset offset = _va->alloc_low_address(ZGranuleSize * 4);
158
EXPECT_ALLOC_OK(offset);
160
offset = _va->alloc_low_address(ZGranuleSize * 6);
161
EXPECT_ALLOC_OK(offset);
164
_va->free(bottom, ZMapperTestReservationSize);
166
bottom = _va->alloc_low_address(ZMapperTestReservationSize);
167
EXPECT_ALLOC_OK(bottom);
169
_va->free(bottom, ZMapperTestReservationSize);
173
bool ZMapperTest::_initialized = false;
174
ZMemoryManager* ZMapperTest::_va = nullptr;
176
TEST_VM_F(ZMapperTest, test_alloc_low_address) {
177
test_alloc_low_address();
180
TEST_VM_F(ZMapperTest, test_alloc_high_address) {
181
test_alloc_high_address();
184
TEST_VM_F(ZMapperTest, test_alloc_whole_area) {
185
test_alloc_whole_area();