diff --git a/include/decodeless/allocator.hpp b/include/decodeless/allocator.hpp index 11d1330..8352424 100644 --- a/include/decodeless/allocator.hpp +++ b/include/decodeless/allocator.hpp @@ -79,7 +79,7 @@ class linear_memory_resource { linear_memory_resource(size_t initialSize, ResOrAlloc&& parent) requires memory_resource : m_parent(std::move(parent)) - , m_begin(allocate_bytes(m_parent, initialSize)) + , m_begin(initialSize != 0 ? allocate_bytes(m_parent, initialSize) : nullptr) , m_next(reinterpret_cast(m_begin)) , m_end(reinterpret_cast(m_begin) + initialSize) { if constexpr (nonrealloc_memory_resource) { diff --git a/test/src/allocator.cpp b/test/src/allocator.cpp index 1bc4d8b..1a5c39e 100644 --- a/test/src/allocator.cpp +++ b/test/src/allocator.cpp @@ -203,12 +203,30 @@ TEST_F(Allocate, EmptyNonrealloc) { EXPECT_EQ(memory.capacity(), 42); } -TEST_F(Allocate, EmptyRealloc) { +TEST_F(Allocate, EmptyReallocDefault) { linear_memory_resource memory; EXPECT_EQ(memory.size(), 0); EXPECT_EQ(memory.capacity(), 0); } +TEST_F(Allocate, EmptyRealloc) { + linear_memory_resource memory{ReallocNullMemoryResource()}; + EXPECT_EQ(memory.size(), 0); + EXPECT_EQ(memory.capacity(), 0); +} + +TEST_F(Allocate, ZeroInitialRealloc) { + linear_memory_resource memory{0, ReallocNullAllocator()}; + EXPECT_EQ(memory.size(), 0); + EXPECT_EQ(memory.capacity(), 0); +} + +TEST_F(Allocate, ZeroInitialReallocMemoryResource) { + linear_memory_resource memory{0, ReallocNullMemoryResource()}; + EXPECT_EQ(memory.size(), 0); + EXPECT_EQ(memory.capacity(), 0); +} + TEST_F(Allocate, Truncate) { linear_memory_resource memory; std::ignore = memory.allocate(1, 1);