Skip to content

Commit e52a80f

Browse files
committed
Memory: Fix review findings
1 parent bec7e29 commit e52a80f

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

doc/Memory.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The scratch allocator preallocates a memory block which can be used in your prog
1414
This will be done when clearing the allocator. All allocations will be invalidated.
1515

1616
### Usecases
17-
You need to sove any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away
17+
You need to save any kind of algorithms. For this you need to work with dynamic allocations, which will be thrown away
1818
after finishing your work.
1919

2020
### Examples

include/cppcore/Memory/TScratchAllocator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class TScratchAllocator {
4444

4545
/// @brief The class constructor with the pool size.
4646
/// @param[in] numItems The buffer size.
47-
TScratchAllocator(size_t numItems);
47+
explicit TScratchAllocator(size_t numItems);
4848

4949
/// @brief The class destructor.
5050
~TScratchAllocator();
@@ -91,7 +91,7 @@ inline TScratchAllocator<T>::TScratchAllocator() :
9191

9292
template<class T>
9393
inline TScratchAllocator<T>::TScratchAllocator(size_t numItems) :
94-
mBlock(nullptr), mSize(0u), mIndex(0u) {
94+
mBlock(nullptr), mSize(numItems), mIndex(0u) {
9595
reserve(numItems);
9696
}
9797

@@ -121,7 +121,7 @@ void TScratchAllocator<T>::reserve(size_t size) {
121121

122122
template<class T>
123123
inline void TScratchAllocator<T>::clear() {
124-
delete mBlock;
124+
delete [] mBlock;
125125
mBlock = nullptr;
126126
mSize = 0u;
127127
}
Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,66 @@
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+
-----------------------------------------------------------------------------------------------*/
123
#include <cppcore/Memory/TScratchAllocator.h>
224

325
#include <gtest/gtest.h>
426

527
using namespace cppcore;
628

7-
class TScratchAllocatorTest : public testing::Test {};
29+
class TScratchAllocatorTest : public testing::Test {
30+
public:
31+
static constexpr size_t BufferSize = 1024u;
32+
};
833

934
TEST_F(TScratchAllocatorTest, CreateTest) {
1035
bool ok( true );
1136
try {
12-
ScratchAllocator myAllocator(1024);
37+
ScratchAllocator myAllocator(BufferSize);
1338
} catch ( ... ) {
1439
ok = false;
1540
}
1641
EXPECT_TRUE( ok );
1742
}
1843

1944
TEST_F(TScratchAllocatorTest, AllocTest) {
20-
ScratchAllocator myAllocator(1024);
45+
ScratchAllocator myAllocator(BufferSize);
2146
char *ptr1 = myAllocator.alloc(512);
2247
EXPECT_NE(ptr1, nullptr);
23-
EXPECT_EQ(myAllocator.capacity(), 1024);
48+
EXPECT_EQ(myAllocator.capacity(), BufferSize);
2449
EXPECT_EQ(myAllocator.freeMem(), 512);
50+
EXPECT_EQ(myAllocator.reservedMem(), 512);
2551

2652
char *ptr2 = myAllocator.alloc(600);
2753
EXPECT_EQ(ptr2, nullptr);
54+
55+
myAllocator.clear();
56+
}
57+
58+
TEST_F(TScratchAllocatorTest, ClearTest) {
59+
ScratchAllocator myAllocator(BufferSize);
60+
EXPECT_EQ(myAllocator.capacity(), BufferSize);
61+
EXPECT_EQ(myAllocator.freeMem(), BufferSize);
62+
63+
myAllocator.clear();
64+
EXPECT_EQ(myAllocator.capacity(), 0u);
65+
EXPECT_EQ(myAllocator.freeMem(), 0u);
2866
}

0 commit comments

Comments
 (0)