Skip to content

Commit fb07336

Browse files
committed
fix zero initial size for memory resource parent
1 parent 772bcb7 commit fb07336

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

include/decodeless/allocator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class linear_memory_resource {
7171
linear_memory_resource(size_t initialSize, ResOrAlloc&& parent)
7272
requires memory_resource<ResOrAlloc>
7373
: m_parent(std::move(parent))
74-
, m_begin(allocate_bytes(m_parent, initialSize))
74+
, m_begin(initialSize != 0 ? allocate_bytes(m_parent, initialSize) : initialSize)
7575
, m_next(reinterpret_cast<uintptr_t>(m_begin))
7676
, m_end(reinterpret_cast<uintptr_t>(m_begin) + initialSize) {
7777
if constexpr (nonrealloc_memory_resource<ResOrAlloc>) {

test/src/allocator.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,30 @@ TEST_F(Allocate, EmptyNonrealloc) {
203203
EXPECT_EQ(memory.capacity(), 42);
204204
}
205205

206-
TEST_F(Allocate, EmptyRealloc) {
206+
TEST_F(Allocate, EmptyReallocDefault) {
207207
linear_memory_resource<ReallocNullAllocator> memory;
208208
EXPECT_EQ(memory.size(), 0);
209209
EXPECT_EQ(memory.capacity(), 0);
210210
}
211211

212+
TEST_F(Allocate, EmptyRealloc) {
213+
linear_memory_resource<ReallocNullMemoryResource> memory{ReallocNullMemoryResource()};
214+
EXPECT_EQ(memory.size(), 0);
215+
EXPECT_EQ(memory.capacity(), 0);
216+
}
217+
218+
TEST_F(Allocate, ZeroInitialRealloc) {
219+
linear_memory_resource<ReallocNullAllocator> memory{0, ReallocNullAllocator()};
220+
EXPECT_EQ(memory.size(), 0);
221+
EXPECT_EQ(memory.capacity(), 0);
222+
}
223+
224+
TEST_F(Allocate, ZeroInitialReallocMemoryResource) {
225+
linear_memory_resource<ReallocNullMemoryResource> memory{0, ReallocNullMemoryResource()};
226+
EXPECT_EQ(memory.size(), 0);
227+
EXPECT_EQ(memory.capacity(), 0);
228+
}
229+
212230
TEST_F(Allocate, Truncate) {
213231
linear_memory_resource<ReallocNullAllocator> memory;
214232
std::ignore = memory.allocate(1, 1);

0 commit comments

Comments
 (0)