-
Notifications
You must be signed in to change notification settings - Fork 4
Feature: Add a scratch allocator #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e97ff1
Memory: Add scratch allocator + it unittests.
kullingk bec7e29
Memory: Add scratch allocator doc.
kullingk e52a80f
Memory: Fix review findings
kullingk 7889884
Memory: Fix typo
kullingk 34a8f13
Update Memory.md; Fix typo
kimkulling a7a2e6a
Update Memory.md
kimkulling 393f7fd
Update TScratchAllocator.h: Fix review findings
kimkulling a99d03a
Update TScratchAllocator.h
kimkulling 2120436
Update TStackAllocator.h: Fix review findings
kimkulling 1ff473d
Update TScratchAllocator.h
kimkulling aa39d7f
Update TScratchAllocator.h: fix review findings
kimkulling bea8a32
Update TScratchAllocator.h
kimkulling c688e34
Update TScratchAllocator.h
kimkulling 40dd2a8
Update TScratchAllocator.h
kimkulling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
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 <cppcore/CPPCoreCommon.h> | ||
#include <memory> | ||
#include <limits> | ||
|
||
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 T> | ||
class TScratchAllocator { | ||
public: | ||
/// @brief The default class constructor. | ||
TScratchAllocator(); | ||
|
||
/// @brief The class constructor with the pool size. | ||
/// @param[in] numItems The buffer size. | ||
explicit 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 = nullptr; | ||
size_t mSize; | ||
size_t mIndex; | ||
}; | ||
|
||
template<class T> | ||
inline TScratchAllocator<T>::TScratchAllocator() : | ||
mSize(0u), mIndex(0u) { | ||
// empty | ||
} | ||
|
||
template<class T> | ||
inline TScratchAllocator<T>::TScratchAllocator(size_t numItems) : | ||
mSize(numItems), mIndex(0u) { | ||
reserve(numItems); | ||
} | ||
|
||
template<class T> | ||
inline TScratchAllocator<T>::~TScratchAllocator() { | ||
clear(); | ||
} | ||
|
||
template<class T> | ||
inline T *TScratchAllocator<T>::alloc(size_t numItems) { | ||
if (numItems == 0) { | ||
return nullptr; | ||
} | ||
|
||
// Check for overflow | ||
if (numItems > std::numeric_limits<size_t>::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; | ||
} | ||
|
||
template<class T> | ||
void TScratchAllocator<T>::reserve(size_t size) { | ||
clear(); | ||
mBlock = new T[size]; | ||
mSize = size; | ||
mIndex = 0u; | ||
} | ||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
template<class T> | ||
inline void TScratchAllocator<T>::clear() { | ||
delete [] mBlock; | ||
mBlock = nullptr; | ||
mSize = 0u; | ||
} | ||
|
||
template<class T> | ||
inline size_t TScratchAllocator<T>::capacity() const { | ||
return mSize; | ||
} | ||
|
||
template<class T> | ||
inline size_t TScratchAllocator<T>::reservedMem() const { | ||
return mIndex; | ||
} | ||
|
||
template<class T> | ||
inline size_t TScratchAllocator<T>::freeMem() const { | ||
return (mSize - mIndex); | ||
} | ||
|
||
using ScratchAllocator = TScratchAllocator<char>; | ||
|
||
} // namespace cppcore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
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 <cppcore/Memory/TScratchAllocator.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace cppcore; | ||
|
||
class TScratchAllocatorTest : public testing::Test { | ||
public: | ||
static constexpr size_t BufferSize = 1024u; | ||
}; | ||
|
||
TEST_F(TScratchAllocatorTest, CreateTest) { | ||
bool ok( true ); | ||
try { | ||
ScratchAllocator myAllocator(BufferSize); | ||
} catch ( ... ) { | ||
ok = false; | ||
} | ||
EXPECT_TRUE( ok ); | ||
} | ||
|
||
TEST_F(TScratchAllocatorTest, AllocTest) { | ||
ScratchAllocator myAllocator(BufferSize); | ||
char *ptr1 = myAllocator.alloc(512); | ||
EXPECT_NE(ptr1, nullptr); | ||
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(); | ||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.