Skip to content

Commit 229d1ad

Browse files
committed
Merge two MockMemoryManager classes to a single one
Change-Id: I4b994b21a2423f1f3c077e847483766798c8d7f9
1 parent ff9b36b commit 229d1ad

File tree

3 files changed

+62
-63
lines changed

3 files changed

+62
-63
lines changed

unit_tests/memory_manager/memory_manager_allocate_in_preferred_pool_tests.cpp

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include "runtime/memory_manager/os_agnostic_memory_manager.h"
24+
#include "unit_tests/mocks/mock_memory_manager.h"
2425

2526
#include "test.h"
2627
#include "gtest/gtest.h"
@@ -32,50 +33,10 @@ class MemoryManagerGetAlloctionDataTest : public testing::TestWithParam<Graphics
3233
void TearDown() override {}
3334
};
3435

35-
class MockOsAgnosticMemoryManager : public OsAgnosticMemoryManager {
36-
public:
37-
using MemoryManager::allocateGraphicsMemory;
38-
using MemoryManager::getAllocationData;
39-
MockOsAgnosticMemoryManager(bool enable64kbPages) : OsAgnosticMemoryManager(enable64kbPages) {
40-
}
41-
GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override {
42-
allocationCreated = true;
43-
return OsAgnosticMemoryManager::allocateGraphicsMemory(size, alignment, forcePin, uncacheable);
44-
}
45-
GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) override {
46-
allocation64kbPageCreated = true;
47-
preferRenderCompressedFlagPassed = preferRenderCompressed;
48-
return OsAgnosticMemoryManager::allocateGraphicsMemory64kb(size, alignment, forcePin, preferRenderCompressed);
49-
}
50-
51-
GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override {
52-
if (failInDevicePool) {
53-
status = AllocationStatus::RetryInNonDevicePool;
54-
return nullptr;
55-
}
56-
if (failInDevicePoolWithError) {
57-
status = AllocationStatus::Error;
58-
return nullptr;
59-
}
60-
61-
auto allocation = OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(allocationData, status);
62-
if (allocation) {
63-
allocationInDevicePoolCreated = true;
64-
}
65-
return allocation;
66-
}
67-
bool allocationCreated = false;
68-
bool allocation64kbPageCreated = false;
69-
bool allocationInDevicePoolCreated = false;
70-
bool failInDevicePool = false;
71-
bool failInDevicePoolWithError = false;
72-
bool preferRenderCompressedFlagPassed = false;
73-
};
74-
7536
TEST(MemoryManagerGetAlloctionDataTest, givenMustBeZeroCopyAndAllocateMemoryFlagsAndNullptrWhenAllocationDataIsQueriedThenCorrectFlagsAndSizeAreSet) {
7637
AllocationData allocData;
7738

78-
MockOsAgnosticMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
39+
MockMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
7940

8041
EXPECT_TRUE(allocData.flags.mustBeZeroCopy);
8142
EXPECT_TRUE(allocData.flags.useSystemMemory);
@@ -86,7 +47,7 @@ TEST(MemoryManagerGetAlloctionDataTest, givenMustBeZeroCopyAndAllocateMemoryFlag
8647
TEST(MemoryManagerGetAlloctionDataTest, givenMustBeZeroCopyFlagFalseWhenAllocationDataIsQueriedThenMustBeZeroCopyAndUseSystemMemoryFlagsAreNotSet) {
8748
AllocationData allocData;
8849

89-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
50+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
9051

9152
EXPECT_FALSE(allocData.flags.mustBeZeroCopy);
9253
EXPECT_FALSE(allocData.flags.useSystemMemory);
@@ -97,22 +58,22 @@ TEST(MemoryManagerGetAlloctionDataTest, givenMustBeZeroCopyFlagFalseWhenAllocati
9758
TEST(MemoryManagerGetAlloctionDataTest, givenAllocateMemoryFlagTrueWhenHostPtrIsNotNullThenAllocationDataHasHostPtrNulled) {
9859
AllocationData allocData;
9960
char memory = 0;
100-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, &memory, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER);
61+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, &memory, sizeof(memory), GraphicsAllocation::AllocationType::BUFFER);
10162

10263
EXPECT_EQ(sizeof(memory), allocData.size);
10364
EXPECT_EQ(nullptr, allocData.hostPtr);
10465
}
10566

10667
TEST(MemoryManagerGetAlloctionDataTest, givenForcePinFlagTrueWhenAllocationDataIsQueriedThenCorrectFlagIsSet) {
10768
AllocationData allocData;
108-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, true, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
69+
MockMemoryManager::getAllocationData(allocData, false, true, true, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
10970

11071
EXPECT_TRUE(allocData.flags.forcePin);
11172
}
11273

11374
TEST(MemoryManagerGetAlloctionDataTest, givenUncacheableFlagTrueWhenAllocationDataIsQueriedThenCorrectFlagIsSet) {
11475
AllocationData allocData;
115-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, true, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
76+
MockMemoryManager::getAllocationData(allocData, false, true, false, true, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
11677

11778
EXPECT_TRUE(allocData.flags.uncacheable);
11879
}
@@ -123,7 +84,7 @@ TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, givenAllocatio
12384
AllocationData allocData;
12485

12586
auto allocType = GetParam();
126-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
87+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
12788

12889
EXPECT_TRUE(allocData.flags.allow32Bit);
12990
EXPECT_TRUE(allocData.flags.allow64kbPages);
@@ -133,11 +94,11 @@ TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, givenAllocatio
13394
TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesAllowedTest, given64kbAllowedAllocationTypeWhenAllocatingThenPreferRenderCompressionOnlyForSpecificTypes) {
13495
auto allocType = GetParam();
13596
AllocationData allocData;
136-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
97+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
13798
bool bufferCompressedType = (allocType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED);
13899
EXPECT_TRUE(allocData.flags.allow64kbPages);
139100

140-
MockOsAgnosticMemoryManager mockMemoryManager(true);
101+
MockMemoryManager mockMemoryManager(true);
141102
auto allocation = mockMemoryManager.allocateGraphicsMemory(allocData);
142103

143104
EXPECT_TRUE(mockMemoryManager.allocation64kbPageCreated);
@@ -152,7 +113,7 @@ TEST_P(MemoryManagerGetAlloctionData32BitAnd64kbPagesNotAllowedTest, givenAlloca
152113
AllocationData allocData;
153114

154115
auto allocType = GetParam();
155-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
116+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, allocType);
156117

157118
EXPECT_FALSE(allocData.flags.allow32Bit);
158119
EXPECT_FALSE(allocData.flags.allow64kbPages);
@@ -189,7 +150,7 @@ TEST(MemoryManagerTest, givenForced32BitSetWhenGraphicsMemoryFor32BitAllowedType
189150
memoryManager.setForce32BitAllocations(true);
190151

191152
AllocationData allocData;
192-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
153+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
193154

194155
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
195156
ASSERT_NE(nullptr, allocation);
@@ -209,7 +170,7 @@ TEST(MemoryManagerTest, givenForced32BitEnabledWhenGraphicsMemorywihtoutAllow32B
209170
memoryManager.setForce32BitAllocations(true);
210171

211172
AllocationData allocData;
212-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
173+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
213174
allocData.flags.allow32Bit = false;
214175

215176
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
@@ -224,7 +185,7 @@ TEST(MemoryManagerTest, givenForced32BitDisabledWhenGraphicsMemoryWith32BitFlagF
224185
memoryManager.setForce32BitAllocations(false);
225186

226187
AllocationData allocData;
227-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
188+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
228189

229190
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
230191
ASSERT_NE(nullptr, allocation);
@@ -236,7 +197,7 @@ TEST(MemoryManagerTest, givenForced32BitDisabledWhenGraphicsMemoryWith32BitFlagF
236197
TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryMustBeZeroCopyAndIsAllocatedWithNullptrForBufferThen64kbAllocationIsReturned) {
237198
OsAgnosticMemoryManager memoryManager(true);
238199
AllocationData allocData;
239-
MockOsAgnosticMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
200+
MockMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
240201

241202
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
242203
ASSERT_NE(nullptr, allocation);
@@ -249,9 +210,9 @@ TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryMustBeZeroCopyAnd
249210
}
250211

251212
TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryWithoutAllow64kbPagesFlagsIsAllocatedThenNon64kbAllocationIsReturned) {
252-
MockOsAgnosticMemoryManager memoryManager(true);
213+
MockMemoryManager memoryManager(true);
253214
AllocationData allocData;
254-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
215+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
255216
allocData.flags.allow64kbPages = false;
256217

257218
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
@@ -263,9 +224,9 @@ TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryWithoutAllow64kbP
263224
}
264225

265226
TEST(MemoryManagerTest, givenDisabled64kbPagesWhenGraphicsMemoryMustBeZeroCopyAndIsAllocatedWithNullptrForBufferThenNon64kbAllocationIsReturned) {
266-
MockOsAgnosticMemoryManager memoryManager(false);
227+
MockMemoryManager memoryManager(false);
267228
AllocationData allocData;
268-
MockOsAgnosticMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
229+
MockMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
269230

270231
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
271232
ASSERT_NE(nullptr, allocation);
@@ -281,7 +242,7 @@ TEST(MemoryManagerTest, givenForced32BitAndEnabled64kbPagesWhenGraphicsMemoryMus
281242
memoryManager.setForce32BitAllocations(true);
282243

283244
AllocationData allocData;
284-
MockOsAgnosticMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
245+
MockMemoryManager::getAllocationData(allocData, true, true, false, false, nullptr, 10, GraphicsAllocation::AllocationType::BUFFER);
285246

286247
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
287248
ASSERT_NE(nullptr, allocation);
@@ -298,7 +259,7 @@ TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryIsAllocatedWithHo
298259
OsAgnosticMemoryManager memoryManager(true);
299260
AllocationData allocData;
300261
char memory[1];
301-
MockOsAgnosticMemoryManager::getAllocationData(allocData, true, false, false, false, &memory, 1, GraphicsAllocation::AllocationType::BUFFER);
262+
MockMemoryManager::getAllocationData(allocData, true, false, false, false, &memory, 1, GraphicsAllocation::AllocationType::BUFFER);
302263

303264
auto allocation = memoryManager.allocateGraphicsMemory(allocData);
304265
ASSERT_NE(nullptr, allocation);
@@ -309,9 +270,9 @@ TEST(MemoryManagerTest, givenEnabled64kbPagesWhenGraphicsMemoryIsAllocatedWithHo
309270
}
310271

311272
TEST(MemoryManagerTest, givenMemoryManagerWhenGraphicsMemoryAllocationInDevicePoolFailsThenFallbackAllocationIsReturned) {
312-
MockOsAgnosticMemoryManager memoryManager(false);
273+
MockMemoryManager memoryManager(false);
313274
AllocationData allocData;
314-
MockOsAgnosticMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER);
275+
MockMemoryManager::getAllocationData(allocData, false, true, false, false, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER);
315276

316277
memoryManager.failInDevicePool = true;
317278

@@ -324,15 +285,15 @@ TEST(MemoryManagerTest, givenMemoryManagerWhenGraphicsMemoryAllocationInDevicePo
324285
}
325286

326287
TEST(MemoryManagerTest, givenMemoryManagerWhenZeroCopyFlagIsNotSetThenAllocateGraphicsMemoryInPreferredPoolCanAllocateInDevicePool) {
327-
MockOsAgnosticMemoryManager memoryManager(false);
288+
MockMemoryManager memoryManager(false);
328289

329290
auto allocation = memoryManager.allocateGraphicsMemoryInPreferredPool(false, true, false, false, nullptr, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER);
330291
EXPECT_NE(nullptr, allocation);
331292
memoryManager.freeGraphicsMemory(allocation);
332293
}
333294

334295
TEST(MemoryManagerTest, givenMemoryManagerWhenZeroCopyFlagIsNotSetAndAllocateInDevicePoolFailsWithErrorThenAllocateGraphicsMemoryInPreferredPoolReturnsNullptr) {
335-
MockOsAgnosticMemoryManager memoryManager(false);
296+
MockMemoryManager memoryManager(false);
336297

337298
memoryManager.failInDevicePoolWithError = true;
338299

unit_tests/mocks/mock_memory_manager.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ GraphicsAllocation *MockMemoryManager::peekAllocationListHead() {
6565
}
6666

6767
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) {
68+
allocation64kbPageCreated = true;
69+
preferRenderCompressedFlagPassed = preferRenderCompressed;
70+
6871
auto allocation = OsAgnosticMemoryManager::allocateGraphicsMemory64kb(size, alignment, forcePin, preferRenderCompressed);
6972
if (allocation) {
7073
allocation->gmm = new Gmm(allocation->getUnderlyingBuffer(), size, false, preferRenderCompressed);
@@ -73,4 +76,26 @@ GraphicsAllocation *MockMemoryManager::allocateGraphicsMemory64kb(size_t size, s
7376
return allocation;
7477
}
7578

79+
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) {
80+
if (failInDevicePool) {
81+
status = AllocationStatus::RetryInNonDevicePool;
82+
return nullptr;
83+
}
84+
if (failInDevicePoolWithError) {
85+
status = AllocationStatus::Error;
86+
return nullptr;
87+
}
88+
89+
auto allocation = OsAgnosticMemoryManager::allocateGraphicsMemoryInDevicePool(allocationData, status);
90+
if (allocation) {
91+
allocationInDevicePoolCreated = true;
92+
}
93+
return allocation;
94+
}
95+
96+
GraphicsAllocation *MockMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
97+
allocationCreated = true;
98+
return OsAgnosticMemoryManager::allocateGraphicsMemory(size, alignment, forcePin, uncacheable);
99+
}
100+
76101
} // namespace OCLRT

unit_tests/mocks/mock_memory_manager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ namespace OCLRT {
2929

3030
class MockMemoryManager : public OsAgnosticMemoryManager {
3131
public:
32+
using MemoryManager::allocateGraphicsMemory;
33+
using MemoryManager::getAllocationData;
34+
3235
MockMemoryManager() = default;
3336
MockMemoryManager(bool enable64pages) : OsAgnosticMemoryManager(enable64pages) {}
3437
GraphicsAllocation *allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) override;
@@ -40,6 +43,16 @@ class MockMemoryManager : public OsAgnosticMemoryManager {
4043
void setDevice(Device *device);
4144
bool isAllocationListEmpty();
4245
GraphicsAllocation *peekAllocationListHead();
46+
47+
GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override;
48+
GraphicsAllocation *allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) override;
49+
50+
bool allocationCreated = false;
51+
bool allocation64kbPageCreated = false;
52+
bool allocationInDevicePoolCreated = false;
53+
bool failInDevicePool = false;
54+
bool failInDevicePoolWithError = false;
55+
bool preferRenderCompressedFlagPassed = false;
4356
};
4457

4558
class GMockMemoryManager : public MockMemoryManager {

0 commit comments

Comments
 (0)