diff options
| author | Stefan Boberg <[email protected]> | 2024-11-19 09:59:04 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-11-19 09:59:04 +0100 |
| commit | e335ca10a6c6a1e37b155e2155f5c5908c0272ae (patch) | |
| tree | 78b32384df0e58b22052dcfa7990c16921c6b650 /src/zencore/memory.cpp | |
| parent | 5.5.12 (diff) | |
| download | zen-e335ca10a6c6a1e37b155e2155f5c5908c0272ae.tar.xz zen-e335ca10a6c6a1e37b155e2155f5c5908c0272ae.zip | |
memory/string support cleanup and additions (#220)
* removed unused memory classes
* added align.h alignment helpers used in upcoming changes
* added char16_t StringLength
* avoid memory alloc in SetCurrentThreadName
* added command line parsing helpers to zencore/commandline.h
* removed IoBuffer direct VirtualAlloc path
Diffstat (limited to 'src/zencore/memory.cpp')
| -rw-r--r-- | src/zencore/memory.cpp | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/src/zencore/memory.cpp b/src/zencore/memory.cpp index 808c9fcb6..a0d911786 100644 --- a/src/zencore/memory.cpp +++ b/src/zencore/memory.cpp @@ -52,28 +52,6 @@ AlignedFreeImpl(void* ptr) ////////////////////////////////////////////////////////////////////////// -MemoryArena::MemoryArena() -{ -} - -MemoryArena::~MemoryArena() -{ -} - -void* -MemoryArena::Alloc(size_t Size, size_t Alignment) -{ - return AlignedAllocImpl(Size, Alignment); -} - -void -MemoryArena::Free(void* ptr) -{ - AlignedFreeImpl(ptr); -} - -////////////////////////////////////////////////////////////////////////// - void* Memory::Alloc(size_t Size, size_t Alignment) { @@ -87,105 +65,12 @@ Memory::Free(void* ptr) } ////////////////////////////////////////////////////////////////////////// - -ChunkingLinearAllocator::ChunkingLinearAllocator(uint64_t ChunkSize, uint64_t ChunkAlignment) -: m_ChunkSize(ChunkSize) -, m_ChunkAlignment(ChunkAlignment) -{ -} - -ChunkingLinearAllocator::~ChunkingLinearAllocator() -{ - Reset(); -} - -void -ChunkingLinearAllocator::Reset() -{ - for (void* ChunkEntry : m_ChunkList) - { - Memory::Free(ChunkEntry); - } - m_ChunkList.clear(); - - m_ChunkCursor = nullptr; - m_ChunkBytesRemain = 0; -} - -void* -ChunkingLinearAllocator::Alloc(size_t Size, size_t Alignment) -{ - ZEN_ASSERT_SLOW(zen::IsPow2(Alignment)); - - // This could be improved in a bunch of ways - // - // * We pessimistically allocate memory even though there may be enough memory available for a single allocation due to the way we take - // alignment into account below - // * The block allocation size could be chosen to minimize slack for the case when multiple oversize allocations are made rather than - // minimizing the number of chunks - // * ... - - const uint64_t AllocationSize = zen::RoundUp(Size, Alignment); - - if (m_ChunkBytesRemain < (AllocationSize + Alignment - 1)) - { - const uint64_t ChunkSize = zen::RoundUp(zen::Max(m_ChunkSize, Size), m_ChunkSize); - void* ChunkPtr = Memory::Alloc(ChunkSize, m_ChunkAlignment); - if (!ChunkPtr) - { - ThrowOutOfMemory(fmt::format("failed allocating {:#x} bytes aligned to {:#x}", ChunkSize, m_ChunkAlignment)); - } - m_ChunkCursor = reinterpret_cast<uint8_t*>(ChunkPtr); - m_ChunkBytesRemain = ChunkSize; - m_ChunkList.push_back(ChunkPtr); - } - - const uint64_t AlignFixup = (Alignment - reinterpret_cast<uintptr_t>(m_ChunkCursor)) & (Alignment - 1); - void* ReturnPtr = m_ChunkCursor + AlignFixup; - const uint64_t Delta = AlignFixup + AllocationSize; - - ZEN_ASSERT_SLOW(m_ChunkBytesRemain >= Delta); - - m_ChunkCursor += Delta; - m_ChunkBytesRemain -= Delta; - - ZEN_ASSERT_SLOW(IsPointerAligned(ReturnPtr, Alignment)); - - return ReturnPtr; -} - -////////////////////////////////////////////////////////////////////////// // // Unit tests // #if ZEN_WITH_TESTS -TEST_CASE("ChunkingLinearAllocator") -{ - ChunkingLinearAllocator Allocator(4096); - - void* p1 = Allocator.Alloc(1, 1); - void* p2 = Allocator.Alloc(1, 1); - - CHECK(p1 != p2); - - void* p3 = Allocator.Alloc(1, 4); - CHECK(IsPointerAligned(p3, 4)); - - void* p3_2 = Allocator.Alloc(1, 4); - CHECK(IsPointerAligned(p3_2, 4)); - - void* p4 = Allocator.Alloc(1, 8); - CHECK(IsPointerAligned(p4, 8)); - - for (int i = 0; i < 100; ++i) - { - void* p0 = Allocator.Alloc(64); - ZEN_UNUSED(p0); - } -} - TEST_CASE("MemoryView") { { |