Skip to content

Commit 8e97ff1

Browse files
committed
Memory: Add scratch allocator + it unittests.
1 parent dbdecc6 commit 8e97ff1

File tree

9 files changed

+184
-13
lines changed

9 files changed

+184
-13
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ IF( CPPCORE_BUILD_UNITTESTS )
150150
SET( cppcore_memory_test_src
151151
test/memory/TStackAllocatorTest.cpp
152152
test/memory/TPoolAllocatorTest.cpp
153+
test/memory/TScratchAllocatorTest.cpp
153154
)
154155

155156
SET( cppcore_random_test_src
@@ -163,7 +164,7 @@ IF( CPPCORE_BUILD_UNITTESTS )
163164
SOURCE_GROUP( code\\container FILES ${cppcore_container_test_src} )
164165
SOURCE_GROUP( code\\memory FILES ${cppcore_memory_test_src} )
165166
SOURCE_GROUP( code\\random FILES ${cppcore_random_test_src} )
166-
167+
167168
# Prevent overriding the parent project's compiler/linker
168169
# settings on Windows
169170
SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

include/cppcore/IO/FileSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class FileSystem {
4949
/// @brief The class constructor with the location.
5050
/// @param[in] location The root location.
5151
FileSystem(const char *location);
52-
52+
5353
/// @brief The class destructor.
5454
~FileSystem() = default;
55-
55+
5656
/// @brief Will perform a refresh.
5757
void refresh();
58-
58+
5959
/// @brief Will return the free disk info.
6060
/// @return the File-system space.
6161
FSSpace *getFreeDiskSpace();

include/cppcore/Memory/TDefaultAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,4 @@ inline void TDefaultAllocator<T>::dumpAllocations(std::string &allocs) {
129129
// empty
130130
}
131131

132-
} // Namespace cppcore
132+
} // namespace cppcore

include/cppcore/Memory/TPoolAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class TPoolAllocator {
9494

9595
/// @brief Will reset the allocator.
9696
void reset();
97-
97+
9898
/// No copying allowed
9999
CPPCORE_NONE_COPYING(TPoolAllocator)
100100

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014-2024 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
-----------------------------------------------------------------------------------------------*/
23+
#pragma once
24+
25+
#include <cppcore/CPPCoreCommon.h>
26+
27+
namespace cppcore {
28+
29+
//-------------------------------------------------------------------------------------------------
30+
/// @class TScratchAllocator
31+
/// @ingroup CPPCore
32+
///
33+
/// @brief A simple scratch allocator.
34+
///
35+
/// You can use it to manage all the dynamic allocations without caring about cleaning it up. All
36+
/// Allocations will be a one-shot alloc, which do not need to get released. When calling clear
37+
/// all allocation will be invalidated and cannot be used anymore.
38+
//-------------------------------------------------------------------------------------------------
39+
template<class T>
40+
class TScratchAllocator {
41+
public:
42+
/// @brief The default class constructor.
43+
TScratchAllocator();
44+
45+
/// @brief The class constructor with the pool size.
46+
/// @param[in] numItems The buffer size.
47+
TScratchAllocator(size_t numItems);
48+
49+
/// @brief The class destructor.
50+
~TScratchAllocator();
51+
52+
/// @brief Will allocate the number of items.
53+
/// @param[in] numItems The number of items.
54+
/// @return Pointr showing to the scratch buffer.
55+
T *alloc(size_t numItems);
56+
57+
/// @brief Will reserve the pool.
58+
/// @apram[in] size The pool size to reserve.
59+
void reserve(size_t size);
60+
61+
/// @brief Will clear the pool, memory will be deallocated.
62+
/// @note All instances which are currently is use will get invalid. PLease use with care.
63+
void clear();
64+
65+
/// @brief Returns the capacity of items in the pool allocator.
66+
/// @return The capacity.
67+
size_t capacity() const;
68+
69+
/// @brief Will return the reserved memory in bytes.
70+
/// @return The reserved memory in bytes.
71+
size_t reservedMem() const;
72+
73+
/// @brief Will return the free memory in the pool in bytes.
74+
/// @return The free memory in bytes.
75+
size_t freeMem() const;
76+
77+
/// No copying allowed
78+
CPPCORE_NONE_COPYING(TScratchAllocator)
79+
80+
private:
81+
T *mBlock;
82+
size_t mSize;
83+
size_t mIndex;
84+
};
85+
86+
template<class T>
87+
inline TScratchAllocator<T>::TScratchAllocator() :
88+
mBlock(nullptr), mSize(0u), mIndex(0u) {
89+
// empty
90+
}
91+
92+
template<class T>
93+
inline TScratchAllocator<T>::TScratchAllocator(size_t numItems) :
94+
mBlock(nullptr), mSize(0u), mIndex(0u) {
95+
reserve(numItems);
96+
}
97+
98+
template<class T>
99+
inline TScratchAllocator<T>::~TScratchAllocator() {
100+
clear();
101+
}
102+
103+
template<class T>
104+
inline T *TScratchAllocator<T>::alloc(size_t numItems) {
105+
if ((mIndex + numItems) > mSize) {
106+
return nullptr;
107+
}
108+
109+
T *ptr = &mBlock[mIndex];
110+
mIndex += numItems;
111+
return ptr;
112+
}
113+
114+
template<class T>
115+
void TScratchAllocator<T>::reserve(size_t size) {
116+
clear();
117+
mBlock = new T[size];
118+
mSize = size;
119+
mIndex = 0u;
120+
}
121+
122+
template<class T>
123+
inline void TScratchAllocator<T>::clear() {
124+
delete mBlock;
125+
mBlock = nullptr;
126+
mSize = 0u;
127+
}
128+
129+
template<class T>
130+
inline size_t TScratchAllocator<T>::capacity() const {
131+
return mSize;
132+
}
133+
134+
template<class T>
135+
size_t TScratchAllocator<T>::reservedMem() const {
136+
return mIndex;
137+
}
138+
139+
template<class T>
140+
size_t TScratchAllocator<T>::freeMem() const {
141+
return (mSize - mIndex);
142+
}
143+
144+
using ScratchAllocator = TScratchAllocator<char>;
145+
146+
} // namespace cppcore

include/cppcore/Memory/TStackAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ inline void TStackAllocator<T>::clear() {
185185
m_capacity = 0;
186186
m_top = 0;
187187
}
188-
188+
189189
template <class T>
190190
inline void TStackAllocator<T>::reset() {
191191
m_top = 0;

test/Random/RandomGeneratorTest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525

2626
using namespace cppcore;
2727

28-
class RandomGeneratorTest : public testing::Test {
29-
// empty
30-
};
28+
class RandomGeneratorTest : public testing::Test {};
3129

3230
TEST_F( RandomGeneratorTest, getTest ) {
3331
RandomGenerator generator;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cppcore/Memory/TScratchAllocator.h>
2+
3+
#include <gtest/gtest.h>
4+
5+
using namespace cppcore;
6+
7+
class TScratchAllocatorTest : public testing::Test {};
8+
9+
TEST_F(TScratchAllocatorTest, CreateTest) {
10+
bool ok( true );
11+
try {
12+
ScratchAllocator myAllocator(1024);
13+
} catch ( ... ) {
14+
ok = false;
15+
}
16+
EXPECT_TRUE( ok );
17+
}
18+
19+
TEST_F(TScratchAllocatorTest, AllocTest) {
20+
ScratchAllocator myAllocator(1024);
21+
char *ptr1 = myAllocator.alloc(512);
22+
EXPECT_NE(ptr1, nullptr);
23+
EXPECT_EQ(myAllocator.capacity(), 1024);
24+
EXPECT_EQ(myAllocator.freeMem(), 512);
25+
26+
char *ptr2 = myAllocator.alloc(600);
27+
EXPECT_EQ(ptr2, nullptr);
28+
}

test/memory/TStackAllocatorTest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

2727
using namespace cppcore;
2828

29-
class TStackAllocatorTest : public testing::Test {
30-
protected:
31-
};
29+
class TStackAllocatorTest : public testing::Test {};
3230

3331
TEST_F( TStackAllocatorTest, CreateTest ) {
3432
bool ok( true );

0 commit comments

Comments
 (0)