From 8e97ff1747184e06bbd19046f44cc37022342ea3 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 14:56:44 +0100 Subject: [PATCH 01/14] Memory: Add scratch allocator + it unittests. --- CMakeLists.txt | 3 +- include/cppcore/IO/FileSystem.h | 6 +- include/cppcore/Memory/TDefaultAllocator.h | 2 +- include/cppcore/Memory/TPoolAllocator.h | 2 +- include/cppcore/Memory/TScratchAllocator.h | 146 +++++++++++++++++++++ include/cppcore/Memory/TStackAllocator.h | 2 +- test/Random/RandomGeneratorTest.cpp | 4 +- test/memory/TScratchAllocatorTest.cpp | 28 ++++ test/memory/TStackAllocatorTest.cpp | 4 +- 9 files changed, 184 insertions(+), 13 deletions(-) create mode 100644 include/cppcore/Memory/TScratchAllocator.h create mode 100644 test/memory/TScratchAllocatorTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e4c7892..2564b94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,6 +150,7 @@ IF( CPPCORE_BUILD_UNITTESTS ) SET( cppcore_memory_test_src test/memory/TStackAllocatorTest.cpp test/memory/TPoolAllocatorTest.cpp + test/memory/TScratchAllocatorTest.cpp ) SET( cppcore_random_test_src @@ -163,7 +164,7 @@ IF( CPPCORE_BUILD_UNITTESTS ) SOURCE_GROUP( code\\container FILES ${cppcore_container_test_src} ) SOURCE_GROUP( code\\memory FILES ${cppcore_memory_test_src} ) SOURCE_GROUP( code\\random FILES ${cppcore_random_test_src} ) - + # Prevent overriding the parent project's compiler/linker # settings on Windows SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE) diff --git a/include/cppcore/IO/FileSystem.h b/include/cppcore/IO/FileSystem.h index 0072fef..79f11fd 100644 --- a/include/cppcore/IO/FileSystem.h +++ b/include/cppcore/IO/FileSystem.h @@ -49,13 +49,13 @@ class FileSystem { /// @brief The class constructor with the location. /// @param[in] location The root location. FileSystem(const char *location); - + /// @brief The class destructor. ~FileSystem() = default; - + /// @brief Will perform a refresh. void refresh(); - + /// @brief Will return the free disk info. /// @return the File-system space. FSSpace *getFreeDiskSpace(); diff --git a/include/cppcore/Memory/TDefaultAllocator.h b/include/cppcore/Memory/TDefaultAllocator.h index 4af1b93..64bbe13 100644 --- a/include/cppcore/Memory/TDefaultAllocator.h +++ b/include/cppcore/Memory/TDefaultAllocator.h @@ -129,4 +129,4 @@ inline void TDefaultAllocator::dumpAllocations(std::string &allocs) { // empty } -} // Namespace cppcore +} // namespace cppcore diff --git a/include/cppcore/Memory/TPoolAllocator.h b/include/cppcore/Memory/TPoolAllocator.h index d1a5c6e..5d91c6c 100644 --- a/include/cppcore/Memory/TPoolAllocator.h +++ b/include/cppcore/Memory/TPoolAllocator.h @@ -94,7 +94,7 @@ class TPoolAllocator { /// @brief Will reset the allocator. void reset(); - + /// No copying allowed CPPCORE_NONE_COPYING(TPoolAllocator) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h new file mode 100644 index 0000000..ff9ea25 --- /dev/null +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -0,0 +1,146 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2024 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ +#pragma once + +#include + +namespace cppcore { + +//------------------------------------------------------------------------------------------------- +/// @class TScratchAllocator +/// @ingroup CPPCore +/// +/// @brief A simple scratch allocator. +/// +/// You can use it to manage all the dynamic allocations without caring about cleaning it up. All +/// Allocations will be a one-shot alloc, which do not need to get released. When calling clear +/// all allocation will be invalidated and cannot be used anymore. +//------------------------------------------------------------------------------------------------- +template +class TScratchAllocator { +public: + /// @brief The default class constructor. + TScratchAllocator(); + + /// @brief The class constructor with the pool size. + /// @param[in] numItems The buffer size. + TScratchAllocator(size_t numItems); + + /// @brief The class destructor. + ~TScratchAllocator(); + + /// @brief Will allocate the number of items. + /// @param[in] numItems The number of items. + /// @return Pointr showing to the scratch buffer. + T *alloc(size_t numItems); + + /// @brief Will reserve the pool. + /// @apram[in] size The pool size to reserve. + void reserve(size_t size); + + /// @brief Will clear the pool, memory will be deallocated. + /// @note All instances which are currently is use will get invalid. PLease use with care. + void clear(); + + /// @brief Returns the capacity of items in the pool allocator. + /// @return The capacity. + size_t capacity() const; + + /// @brief Will return the reserved memory in bytes. + /// @return The reserved memory in bytes. + size_t reservedMem() const; + + /// @brief Will return the free memory in the pool in bytes. + /// @return The free memory in bytes. + size_t freeMem() const; + + /// No copying allowed + CPPCORE_NONE_COPYING(TScratchAllocator) + +private: + T *mBlock; + size_t mSize; + size_t mIndex; +}; + +template +inline TScratchAllocator::TScratchAllocator() : + mBlock(nullptr), mSize(0u), mIndex(0u) { + // empty +} + +template +inline TScratchAllocator::TScratchAllocator(size_t numItems) : + mBlock(nullptr), mSize(0u), mIndex(0u) { + reserve(numItems); +} + +template +inline TScratchAllocator::~TScratchAllocator() { + clear(); +} + +template +inline T *TScratchAllocator::alloc(size_t numItems) { + if ((mIndex + numItems) > mSize) { + return nullptr; + } + + T *ptr = &mBlock[mIndex]; + mIndex += numItems; + return ptr; +} + +template +void TScratchAllocator::reserve(size_t size) { + clear(); + mBlock = new T[size]; + mSize = size; + mIndex = 0u; +} + +template +inline void TScratchAllocator::clear() { + delete mBlock; + mBlock = nullptr; + mSize = 0u; +} + +template +inline size_t TScratchAllocator::capacity() const { + return mSize; +} + +template +size_t TScratchAllocator::reservedMem() const { + return mIndex; +} + +template +size_t TScratchAllocator::freeMem() const { + return (mSize - mIndex); +} + +using ScratchAllocator = TScratchAllocator; + +} // namespace cppcore diff --git a/include/cppcore/Memory/TStackAllocator.h b/include/cppcore/Memory/TStackAllocator.h index 219b353..fd1a4db 100644 --- a/include/cppcore/Memory/TStackAllocator.h +++ b/include/cppcore/Memory/TStackAllocator.h @@ -185,7 +185,7 @@ inline void TStackAllocator::clear() { m_capacity = 0; m_top = 0; } - + template inline void TStackAllocator::reset() { m_top = 0; diff --git a/test/Random/RandomGeneratorTest.cpp b/test/Random/RandomGeneratorTest.cpp index 6d32ade..a06656c 100644 --- a/test/Random/RandomGeneratorTest.cpp +++ b/test/Random/RandomGeneratorTest.cpp @@ -25,9 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using namespace cppcore; -class RandomGeneratorTest : public testing::Test { - // empty -}; +class RandomGeneratorTest : public testing::Test {}; TEST_F( RandomGeneratorTest, getTest ) { RandomGenerator generator; diff --git a/test/memory/TScratchAllocatorTest.cpp b/test/memory/TScratchAllocatorTest.cpp new file mode 100644 index 0000000..f6c6856 --- /dev/null +++ b/test/memory/TScratchAllocatorTest.cpp @@ -0,0 +1,28 @@ +#include + +#include + +using namespace cppcore; + +class TScratchAllocatorTest : public testing::Test {}; + +TEST_F(TScratchAllocatorTest, CreateTest) { + bool ok( true ); + try { + ScratchAllocator myAllocator(1024); + } catch ( ... ) { + ok = false; + } + EXPECT_TRUE( ok ); +} + +TEST_F(TScratchAllocatorTest, AllocTest) { + ScratchAllocator myAllocator(1024); + char *ptr1 = myAllocator.alloc(512); + EXPECT_NE(ptr1, nullptr); + EXPECT_EQ(myAllocator.capacity(), 1024); + EXPECT_EQ(myAllocator.freeMem(), 512); + + char *ptr2 = myAllocator.alloc(600); + EXPECT_EQ(ptr2, nullptr); +} diff --git a/test/memory/TStackAllocatorTest.cpp b/test/memory/TStackAllocatorTest.cpp index 87dd4ba..dad0c99 100644 --- a/test/memory/TStackAllocatorTest.cpp +++ b/test/memory/TStackAllocatorTest.cpp @@ -26,9 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using namespace cppcore; -class TStackAllocatorTest : public testing::Test { -protected: -}; +class TStackAllocatorTest : public testing::Test {}; TEST_F( TStackAllocatorTest, CreateTest ) { bool ok( true ); From bec7e293166ab8cf289b8589cb7185d5824c45ea Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 15:02:47 +0100 Subject: [PATCH 02/14] Memory: Add scratch allocator doc. --- doc/Common.md | 6 +++--- doc/Memory.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/doc/Common.md b/doc/Common.md index 6b99003..4e946ae 100644 --- a/doc/Common.md +++ b/doc/Common.md @@ -38,7 +38,7 @@ You need to manage singe bits and you want to avoid dealing with the OR-, AND- a void main() { TBitField bitfield(0); - + // The first bit is set bitfield.setBit(1); if (bitfield.getBit(1)) std::cout "First bit is set" << std:endl; @@ -53,12 +53,12 @@ void main() { ### Usecases ### Examples -## TSharedPtr +## TSharedPtr ### Introduction ### Usecases ### Examples -## Variant +## Variant ### Introduction ### Usecases ### Examples diff --git a/doc/Memory.md b/doc/Memory.md index 9b772cd..6339b94 100644 --- a/doc/Memory.md +++ b/doc/Memory.md @@ -8,5 +8,34 @@ allocation scheme in all containers. ## CPPCore::TPoolAllocator This allocator can be use to create an initial pool of object at the program startup. +## CPPCore::TScratchAllocator +### Introduction +The scratch allocator preallocates a memory block which can be used in your program. You do not have to deallocate any of the allocations. +This will be done when clearing the allocator. All allocations will be invalidated. + +### Usecases +You need to sove any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away +after finishing your work. + +### Examples +```cpp +#include + +using namespace ::cppcore; + +int main() { + // Will work + ScratchAllocator myAllocator(1024); + char *ptr1 = myAllocator.alloc(512); + assert(ptr1 != nullptr); + + // Overrange shall get catched + char *ptr2 = myAllocator.alloc(600); + assert(ptr2 == nullptr); + + return 0; +} +``` + ## CPPCore::TStackAllocator The stack allocator preallocates a memory block which can be used in your program. When deallocating your memory you have to follow the first-in last-out rule. From e52a80fb0e2a0dfea8d3d8731aa6c868d7414a70 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 16:05:26 +0100 Subject: [PATCH 03/14] Memory: Fix review findings --- doc/Memory.md | 2 +- include/cppcore/Memory/TScratchAllocator.h | 6 +-- test/memory/TScratchAllocatorTest.cpp | 46 ++++++++++++++++++++-- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/doc/Memory.md b/doc/Memory.md index 6339b94..c02e64d 100644 --- a/doc/Memory.md +++ b/doc/Memory.md @@ -14,7 +14,7 @@ The scratch allocator preallocates a memory block which can be used in your prog This will be done when clearing the allocator. All allocations will be invalidated. ### Usecases -You need to sove any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away +You need to save any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away after finishing your work. ### Examples diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index ff9ea25..6d14305 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -44,7 +44,7 @@ class TScratchAllocator { /// @brief The class constructor with the pool size. /// @param[in] numItems The buffer size. - TScratchAllocator(size_t numItems); + explicit TScratchAllocator(size_t numItems); /// @brief The class destructor. ~TScratchAllocator(); @@ -91,7 +91,7 @@ inline TScratchAllocator::TScratchAllocator() : template inline TScratchAllocator::TScratchAllocator(size_t numItems) : - mBlock(nullptr), mSize(0u), mIndex(0u) { + mBlock(nullptr), mSize(numItems), mIndex(0u) { reserve(numItems); } @@ -121,7 +121,7 @@ void TScratchAllocator::reserve(size_t size) { template inline void TScratchAllocator::clear() { - delete mBlock; + delete [] mBlock; mBlock = nullptr; mSize = 0u; } diff --git a/test/memory/TScratchAllocatorTest.cpp b/test/memory/TScratchAllocatorTest.cpp index f6c6856..fe0f869 100644 --- a/test/memory/TScratchAllocatorTest.cpp +++ b/test/memory/TScratchAllocatorTest.cpp @@ -1,15 +1,40 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2024 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ #include #include using namespace cppcore; -class TScratchAllocatorTest : public testing::Test {}; +class TScratchAllocatorTest : public testing::Test { +public: + static constexpr size_t BufferSize = 1024u; +}; TEST_F(TScratchAllocatorTest, CreateTest) { bool ok( true ); try { - ScratchAllocator myAllocator(1024); + ScratchAllocator myAllocator(BufferSize); } catch ( ... ) { ok = false; } @@ -17,12 +42,25 @@ TEST_F(TScratchAllocatorTest, CreateTest) { } TEST_F(TScratchAllocatorTest, AllocTest) { - ScratchAllocator myAllocator(1024); + ScratchAllocator myAllocator(BufferSize); char *ptr1 = myAllocator.alloc(512); EXPECT_NE(ptr1, nullptr); - EXPECT_EQ(myAllocator.capacity(), 1024); + EXPECT_EQ(myAllocator.capacity(), BufferSize); EXPECT_EQ(myAllocator.freeMem(), 512); + EXPECT_EQ(myAllocator.reservedMem(), 512); char *ptr2 = myAllocator.alloc(600); EXPECT_EQ(ptr2, nullptr); + + myAllocator.clear(); +} + +TEST_F(TScratchAllocatorTest, ClearTest) { + ScratchAllocator myAllocator(BufferSize); + EXPECT_EQ(myAllocator.capacity(), BufferSize); + EXPECT_EQ(myAllocator.freeMem(), BufferSize); + + myAllocator.clear(); + EXPECT_EQ(myAllocator.capacity(), 0u); + EXPECT_EQ(myAllocator.freeMem(), 0u); } From 78898843199520be33c0c2fd748b9140fdca0a2a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 16:42:17 +0100 Subject: [PATCH 04/14] Memory: Fix typo --- include/cppcore/Memory/TPoolAllocator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cppcore/Memory/TPoolAllocator.h b/include/cppcore/Memory/TPoolAllocator.h index 5d91c6c..427bf32 100644 --- a/include/cppcore/Memory/TPoolAllocator.h +++ b/include/cppcore/Memory/TPoolAllocator.h @@ -283,4 +283,4 @@ void TPoolAllocator::reset() { m_current = m_first; } -} // Namespace cppcore +} // namespace cppcore From 34a8f136024cac24a366e7ef7bb90fe67cfaee63 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 17:43:15 +0100 Subject: [PATCH 05/14] Update Memory.md; Fix typo --- doc/Memory.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Memory.md b/doc/Memory.md index c02e64d..9ea6a9d 100644 --- a/doc/Memory.md +++ b/doc/Memory.md @@ -19,7 +19,7 @@ after finishing your work. ### Examples ```cpp -#include +#include using namespace ::cppcore; From a7a2e6a0e65ad7bbf24f3001778afc22bf864a8e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 17:44:29 +0100 Subject: [PATCH 06/14] Update Memory.md --- doc/Memory.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/Memory.md b/doc/Memory.md index 9ea6a9d..930c189 100644 --- a/doc/Memory.md +++ b/doc/Memory.md @@ -14,8 +14,11 @@ The scratch allocator preallocates a memory block which can be used in your prog This will be done when clearing the allocator. All allocations will be invalidated. ### Usecases -You need to save any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away -after finishing your work. +Common use cases include: +- Temporary allocations in algorithms (e.g., path finding, sorting) +- Frame-based memory management in games +- Scratch space for parsing and serialization +- Short-lived computational tasks with multiple dynamic allocations ### Examples ```cpp From 393f7fdcbbf16ac59a99f33bb71e17c711aa9f12 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 17:48:22 +0100 Subject: [PATCH 07/14] Update TScratchAllocator.h: Fix review findings --- include/cppcore/Memory/TScratchAllocator.h | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index 6d14305..e2ee68c 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -23,6 +23,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #pragma once #include +#include namespace cppcore { @@ -102,6 +103,28 @@ inline TScratchAllocator::~TScratchAllocator() { template inline T *TScratchAllocator::alloc(size_t numItems) { + if (numItems == 0) { + return nullptr; + } + + // Check for overflow + if (numItems > std::numeric_limits::max() / sizeof(T)) { + return nullptr; + } + + if ((mIndex + numItems) > mSize) { + return nullptr; + } + + // Ensure alignment + size_t alignment = alignof(T); + mIndex = (mIndex + alignment - 1) & ~(alignment - 1); + + T *ptr = &mBlock[mIndex]; + mIndex += numItems; + return ptr; +} + if ((mIndex + numItems) > mSize) { return nullptr; } From a99d03a48e691fa39d103c441c20e505276712a0 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 19:35:34 +0100 Subject: [PATCH 08/14] Update TScratchAllocator.h --- include/cppcore/Memory/TScratchAllocator.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index e2ee68c..e049626 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -124,15 +124,6 @@ inline T *TScratchAllocator::alloc(size_t numItems) { mIndex += numItems; return ptr; } - - if ((mIndex + numItems) > mSize) { - return nullptr; - } - - T *ptr = &mBlock[mIndex]; - mIndex += numItems; - return ptr; -} template void TScratchAllocator::reserve(size_t size) { From 2120436a557391f1efffe69e992676554c9c9ad5 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 20:53:28 +0100 Subject: [PATCH 09/14] Update TStackAllocator.h: Fix review findings --- include/cppcore/Memory/TStackAllocator.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/cppcore/Memory/TStackAllocator.h b/include/cppcore/Memory/TStackAllocator.h index fd1a4db..b556727 100644 --- a/include/cppcore/Memory/TStackAllocator.h +++ b/include/cppcore/Memory/TStackAllocator.h @@ -52,7 +52,7 @@ class TStackAllocator { /// @brief The class destructor. ~TStackAllocator(); - /// Will alloc the number of items from the stack. + /// Will allocate the number of items from the stack. /// @param size [in] The requested size of items. T *alloc(size_t size); @@ -184,11 +184,13 @@ inline void TStackAllocator::clear() { m_data = nullptr; m_capacity = 0; m_top = 0; + m_numAllocs = 0; } template inline void TStackAllocator::reset() { m_top = 0; + m_numAllocs = 0; } template From 1ff473ddedbf79013915b732c02f7d934c3eac6f Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 20:57:17 +0100 Subject: [PATCH 10/14] Update TScratchAllocator.h Add move semantics --- include/cppcore/Memory/TScratchAllocator.h | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index e049626..445e2a4 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -42,6 +42,15 @@ class TScratchAllocator { public: /// @brief The default class constructor. TScratchAllocator(); + + /// @brief The move constructor + /// @param other Will be moved to + TScratchAllocator(TScratchAllocator&& other) noexcept; + + /// @brief The move operator + /// @param other Will be moved to + /// @return instance to moved to. + TScratchAllocator& operator = (TScratchAllocator&& other) noexcept /// @brief The class constructor with the pool size. /// @param[in] numItems The buffer size. @@ -146,15 +155,38 @@ inline size_t TScratchAllocator::capacity() const { } template -size_t TScratchAllocator::reservedMem() const { +inline size_t TScratchAllocator::reservedMem() const { return mIndex; } template -size_t TScratchAllocator::freeMem() const { +inline size_t TScratchAllocator::freeMem() const { return (mSize - mIndex); } +template +inline TScratchAllocator(TScratchAllocator&& other) noexcept : + mBlock(other.mBlock), + mSize(other.mSize), + mIndex(other.mIndex) { + other.mBlock = nullptr; + other.mSize = 0; + other.mIndex = 0; +} + +template +inline TScratchAllocator& operator=(TScratchAllocator&& other) noexcept { + if (this != &other) { + clear(); + mBlock = other.mBlock; + mSize = other.mSize; + mIndex = other.mIndex; + other.mBlock = nullptr; + other.mSize = 0; + other.mIndex = 0; + } + return *this; +} using ScratchAllocator = TScratchAllocator; } // namespace cppcore From aa39d7f872197bbbe5b2c3f1e18cd3dca0ac7658 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 20:57:53 +0100 Subject: [PATCH 11/14] Update TScratchAllocator.h: fix review findings --- include/cppcore/Memory/TScratchAllocator.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index 445e2a4..77f8cfa 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -23,6 +23,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #pragma once #include +#include #include namespace cppcore { From bea8a32b7f0d79542388f9f2f338a922d1bd3d2d Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 20:59:19 +0100 Subject: [PATCH 12/14] Update TScratchAllocator.h --- include/cppcore/Memory/TScratchAllocator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index 77f8cfa..cf592c5 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -51,7 +51,7 @@ class TScratchAllocator { /// @brief The move operator /// @param other Will be moved to /// @return instance to moved to. - TScratchAllocator& operator = (TScratchAllocator&& other) noexcept + TScratchAllocator& operator = (TScratchAllocator&& other) noexcept; /// @brief The class constructor with the pool size. /// @param[in] numItems The buffer size. From c688e34470d3e22d82041362b64ba2c2b2207fd9 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 21:01:34 +0100 Subject: [PATCH 13/14] Update TScratchAllocator.h --- include/cppcore/Memory/TScratchAllocator.h | 32 ---------------------- 1 file changed, 32 deletions(-) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index cf592c5..860f50d 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -44,15 +44,6 @@ class TScratchAllocator { /// @brief The default class constructor. TScratchAllocator(); - /// @brief The move constructor - /// @param other Will be moved to - TScratchAllocator(TScratchAllocator&& other) noexcept; - - /// @brief The move operator - /// @param other Will be moved to - /// @return instance to moved to. - TScratchAllocator& operator = (TScratchAllocator&& other) noexcept; - /// @brief The class constructor with the pool size. /// @param[in] numItems The buffer size. explicit TScratchAllocator(size_t numItems); @@ -165,29 +156,6 @@ inline size_t TScratchAllocator::freeMem() const { return (mSize - mIndex); } -template -inline TScratchAllocator(TScratchAllocator&& other) noexcept : - mBlock(other.mBlock), - mSize(other.mSize), - mIndex(other.mIndex) { - other.mBlock = nullptr; - other.mSize = 0; - other.mIndex = 0; -} - -template -inline TScratchAllocator& operator=(TScratchAllocator&& other) noexcept { - if (this != &other) { - clear(); - mBlock = other.mBlock; - mSize = other.mSize; - mIndex = other.mIndex; - other.mBlock = nullptr; - other.mSize = 0; - other.mIndex = 0; - } - return *this; -} using ScratchAllocator = TScratchAllocator; } // namespace cppcore From 40dd2a874cca2bf47169f53d80de0a90fb291281 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 29 Oct 2024 21:04:14 +0100 Subject: [PATCH 14/14] Update TScratchAllocator.h --- include/cppcore/Memory/TScratchAllocator.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/cppcore/Memory/TScratchAllocator.h b/include/cppcore/Memory/TScratchAllocator.h index 860f50d..ed5f48e 100644 --- a/include/cppcore/Memory/TScratchAllocator.h +++ b/include/cppcore/Memory/TScratchAllocator.h @@ -80,20 +80,20 @@ class TScratchAllocator { CPPCORE_NONE_COPYING(TScratchAllocator) private: - T *mBlock; + T *mBlock = nullptr; size_t mSize; size_t mIndex; }; template inline TScratchAllocator::TScratchAllocator() : - mBlock(nullptr), mSize(0u), mIndex(0u) { + mSize(0u), mIndex(0u) { // empty } template inline TScratchAllocator::TScratchAllocator(size_t numItems) : - mBlock(nullptr), mSize(numItems), mIndex(0u) { + mSize(numItems), mIndex(0u) { reserve(numItems); }