Restored the monotonic allocator

This commit is contained in:
Benoit Blanchon
2018-11-30 14:27:33 +01:00
parent 637f7a5bfa
commit 45f4e5ac20
23 changed files with 35 additions and 609 deletions

View File

@ -60,15 +60,6 @@ TEST_CASE("MemoryPool::allocFrozenString()") {
REQUIRE(a->value == b->value);
}
SECTION("Returns same address after freeString()") {
StringSlot *a = pool.allocFrozenString(1);
pool.freeString(a);
StringSlot *b = pool.allocFrozenString(1);
REQUIRE(a == b);
REQUIRE(a->value == b->value);
}
SECTION("Can use full capacity when fresh") {
StringSlot *a = pool.allocFrozenString(longestString);
@ -84,60 +75,3 @@ TEST_CASE("MemoryPool::allocFrozenString()") {
REQUIRE(a != 0);
}
}
TEST_CASE("MemoryPool::freeString()") {
const size_t poolCapacity = 512;
const size_t longestString = poolCapacity - sizeof(StringSlot);
char buffer[poolCapacity];
MemoryPool pool(buffer, poolCapacity);
static const size_t testStringSize =
(poolCapacity - sizeof(StringSlot) * 4 - sizeof(VariantSlot) * 4) / 4;
SECTION("Restores full capacity") {
StringSlot *strings[4];
VariantSlot *variants[4];
for (int i = 0; i < 4; i++) {
strings[i] = pool.allocFrozenString(testStringSize);
REQUIRE(strings[i] != 0);
variants[i] = pool.allocVariant();
REQUIRE(variants[i] != 0);
}
// In random order
pool.freeString(strings[2]);
pool.freeVariant(variants[3]);
pool.freeVariant(variants[0]);
pool.freeString(strings[0]);
pool.freeVariant(variants[1]);
pool.freeString(strings[1]);
pool.freeVariant(variants[2]);
pool.freeString(strings[3]);
StringSlot *b = pool.allocFrozenString(longestString);
REQUIRE(b != 0);
REQUIRE(b->size == longestString);
}
SECTION("Move strings") {
StringSlot *a = pool.allocFrozenString(6);
strcpy(a->value, "hello");
StringSlot *b = pool.allocFrozenString(7);
strcpy(b->value, "world!");
pool.freeString(a);
REQUIRE(b->size == 7);
REQUIRE(b->value == std::string("world!"));
REQUIRE(a->value == b->value);
}
SECTION("Accepts non-frozen string") {
StringSlot *a = pool.allocExpandableString();
pool.freeString(a);
REQUIRE(pool.size() == 0);
}
}