mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-20 18:29:23 +00:00
Add SlabAllocator and tests
Bug: dawn:340 Change-Id: I6fa1948261e8e6f91324464dade3e9954bd833e5 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/15861 Reviewed-by: Jiawei Shao <jiawei.shao@intel.com> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
35645a601a
commit
e5534c4419
115
src/tests/unittests/PlacementAllocatedTests.cpp
Normal file
115
src/tests/unittests/PlacementAllocatedTests.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
// Copyright 2020 The Dawn Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "common/PlacementAllocated.h"
|
||||
|
||||
using namespace testing;
|
||||
|
||||
namespace {
|
||||
|
||||
enum class DestructedClass {
|
||||
Foo,
|
||||
Bar,
|
||||
};
|
||||
|
||||
class MockDestructor {
|
||||
public:
|
||||
MOCK_METHOD2(Call, void(void*, DestructedClass));
|
||||
};
|
||||
|
||||
std::unique_ptr<StrictMock<MockDestructor>> mockDestructor;
|
||||
|
||||
class PlacementAllocatedTests : public Test {
|
||||
void SetUp() override {
|
||||
mockDestructor = std::make_unique<StrictMock<MockDestructor>>();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
mockDestructor = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
struct Foo : PlacementAllocated {
|
||||
virtual ~Foo() {
|
||||
mockDestructor->Call(this, DestructedClass::Foo);
|
||||
}
|
||||
};
|
||||
|
||||
struct Bar : Foo {
|
||||
~Bar() override {
|
||||
mockDestructor->Call(this, DestructedClass::Bar);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// Test that deletion calls the destructor and does not free memory.
|
||||
TEST_F(PlacementAllocatedTests, DeletionDoesNotFreeMemory) {
|
||||
void* ptr = malloc(sizeof(Foo));
|
||||
|
||||
Foo* foo = new (ptr) Foo();
|
||||
|
||||
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo));
|
||||
delete foo;
|
||||
|
||||
// Touch the memory, this shouldn't crash.
|
||||
static_assert(sizeof(Foo) >= sizeof(uint32_t), "");
|
||||
*reinterpret_cast<uint32_t*>(foo) = 42;
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
// Test that destructing an instance of a derived class calls the derived, then base destructor, and
|
||||
// does not free memory.
|
||||
TEST_F(PlacementAllocatedTests, DeletingDerivedClassCallsBaseDestructor) {
|
||||
void* ptr = malloc(sizeof(Bar));
|
||||
|
||||
Bar* bar = new (ptr) Bar();
|
||||
|
||||
{
|
||||
InSequence s;
|
||||
EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Bar));
|
||||
EXPECT_CALL(*mockDestructor, Call(bar, DestructedClass::Foo));
|
||||
delete bar;
|
||||
}
|
||||
|
||||
// Touch the memory, this shouldn't crash.
|
||||
static_assert(sizeof(Bar) >= sizeof(uint32_t), "");
|
||||
*reinterpret_cast<uint32_t*>(bar) = 42;
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
// Test that destructing an instance of a base class calls the derived, then base destructor, and
|
||||
// does not free memory.
|
||||
TEST_F(PlacementAllocatedTests, DeletingBaseClassCallsDerivedDestructor) {
|
||||
void* ptr = malloc(sizeof(Bar));
|
||||
|
||||
Foo* foo = new (ptr) Bar();
|
||||
|
||||
{
|
||||
InSequence s;
|
||||
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Bar));
|
||||
EXPECT_CALL(*mockDestructor, Call(foo, DestructedClass::Foo));
|
||||
delete foo;
|
||||
}
|
||||
|
||||
// Touch the memory, this shouldn't crash.
|
||||
static_assert(sizeof(Bar) >= sizeof(uint32_t), "");
|
||||
*reinterpret_cast<uint32_t*>(foo) = 42;
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
180
src/tests/unittests/SlabAllocatorTests.cpp
Normal file
180
src/tests/unittests/SlabAllocatorTests.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
// Copyright 2020 The Dawn Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "common/Math.h"
|
||||
#include "common/SlabAllocator.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct Foo : public PlacementAllocated {
|
||||
Foo(int value) : value(value) {
|
||||
}
|
||||
|
||||
int value;
|
||||
};
|
||||
|
||||
struct alignas(256) AlignedFoo : public Foo {
|
||||
using Foo::Foo;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// Test that a slab allocator of a single object works.
|
||||
TEST(SlabAllocatorTests, Single) {
|
||||
SlabAllocator<Foo> allocator(1);
|
||||
|
||||
Foo* obj = allocator.Allocate(4);
|
||||
EXPECT_EQ(obj->value, 4);
|
||||
|
||||
allocator.Deallocate(obj);
|
||||
}
|
||||
|
||||
// Allocate multiple objects and check their data is correct.
|
||||
TEST(SlabAllocatorTests, AllocateSequential) {
|
||||
// Check small alignment
|
||||
{
|
||||
SlabAllocator<Foo> allocator(5);
|
||||
|
||||
std::vector<Foo*> objects;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
auto* ptr = allocator.Allocate(i);
|
||||
EXPECT_TRUE(std::find(objects.begin(), objects.end(), ptr) == objects.end());
|
||||
objects.push_back(ptr);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// Check that the value is correct and hasn't been trampled.
|
||||
EXPECT_EQ(objects[i]->value, i);
|
||||
|
||||
// Check that the alignment is correct.
|
||||
EXPECT_TRUE(IsPtrAligned(objects[i], alignof(Foo)));
|
||||
}
|
||||
|
||||
// Deallocate all of the objects.
|
||||
for (Foo* object : objects) {
|
||||
allocator.Deallocate(object);
|
||||
}
|
||||
}
|
||||
|
||||
// Check large alignment
|
||||
{
|
||||
SlabAllocator<AlignedFoo> allocator(9);
|
||||
|
||||
std::vector<AlignedFoo*> objects;
|
||||
for (int i = 0; i < 21; ++i) {
|
||||
auto* ptr = allocator.Allocate(i);
|
||||
EXPECT_TRUE(std::find(objects.begin(), objects.end(), ptr) == objects.end());
|
||||
objects.push_back(ptr);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 21; ++i) {
|
||||
// Check that the value is correct and hasn't been trampled.
|
||||
EXPECT_EQ(objects[i]->value, i);
|
||||
|
||||
// Check that the alignment is correct.
|
||||
EXPECT_TRUE(IsPtrAligned(objects[i], 256));
|
||||
}
|
||||
|
||||
// Deallocate all of the objects.
|
||||
for (AlignedFoo* object : objects) {
|
||||
allocator.Deallocate(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test that when reallocating a number of objects <= pool size, all memory is reused.
|
||||
TEST(SlabAllocatorTests, ReusesFreedMemory) {
|
||||
SlabAllocator<Foo> allocator(17);
|
||||
|
||||
// Allocate a number of objects.
|
||||
std::set<Foo*> objects;
|
||||
for (int i = 0; i < 17; ++i) {
|
||||
EXPECT_TRUE(objects.insert(allocator.Allocate(i)).second);
|
||||
}
|
||||
|
||||
// Deallocate all of the objects.
|
||||
for (Foo* object : objects) {
|
||||
allocator.Deallocate(object);
|
||||
}
|
||||
|
||||
std::set<Foo*> reallocatedObjects;
|
||||
// Allocate objects again. All of the pointers should be the same.
|
||||
for (int i = 0; i < 17; ++i) {
|
||||
Foo* ptr = allocator.Allocate(i);
|
||||
EXPECT_TRUE(reallocatedObjects.insert(ptr).second);
|
||||
EXPECT_TRUE(std::find(objects.begin(), objects.end(), ptr) != objects.end());
|
||||
}
|
||||
|
||||
// Deallocate all of the objects.
|
||||
for (Foo* object : objects) {
|
||||
allocator.Deallocate(object);
|
||||
}
|
||||
}
|
||||
|
||||
// Test many allocations and deallocations. Meant to catch corner cases with partially
|
||||
// empty slabs.
|
||||
TEST(SlabAllocatorTests, AllocateDeallocateMany) {
|
||||
SlabAllocator<Foo> allocator(17);
|
||||
|
||||
std::set<Foo*> objects;
|
||||
std::set<Foo*> set3;
|
||||
std::set<Foo*> set7;
|
||||
|
||||
// Allocate many objects.
|
||||
for (uint32_t i = 0; i < 800; ++i) {
|
||||
Foo* object = allocator.Allocate(i);
|
||||
EXPECT_TRUE(objects.insert(object).second);
|
||||
|
||||
if (i % 3 == 0) {
|
||||
set3.insert(object);
|
||||
} else if (i % 7 == 0) {
|
||||
set7.insert(object);
|
||||
}
|
||||
}
|
||||
|
||||
// Deallocate every 3rd object.
|
||||
for (Foo* object : set3) {
|
||||
allocator.Deallocate(object);
|
||||
objects.erase(object);
|
||||
}
|
||||
|
||||
// Allocate many more objects
|
||||
for (uint32_t i = 0; i < 800; ++i) {
|
||||
Foo* object = allocator.Allocate(i);
|
||||
EXPECT_TRUE(objects.insert(object).second);
|
||||
|
||||
if (i % 7 == 0) {
|
||||
set7.insert(object);
|
||||
}
|
||||
}
|
||||
|
||||
// Deallocate every 7th object from the first and second rounds of allocation.
|
||||
for (Foo* object : set7) {
|
||||
allocator.Deallocate(object);
|
||||
objects.erase(object);
|
||||
}
|
||||
|
||||
// Allocate objects again
|
||||
for (uint32_t i = 0; i < 800; ++i) {
|
||||
Foo* object = allocator.Allocate(i);
|
||||
EXPECT_TRUE(objects.insert(object).second);
|
||||
}
|
||||
|
||||
// Deallocate the rest of the objects
|
||||
for (Foo* object : objects) {
|
||||
allocator.Deallocate(object);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user