Skip to content

Commit b9caf66

Browse files
committed
code refactor + cleanup
1 parent f14fe99 commit b9caf66

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

Source/Library/MemoryManager.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
#include <setjmp.h>
22

3-
#include "MemoryManager.hpp"
43
#include "DetectOs.hpp"
4+
#include "MemoryManager.hpp"
55

66
#ifdef WINDOWS
7-
87
#include <windows.h>
98

109
#include <processthreadsapi.h>
11-
1210
#endif
13-
#ifdef LINUX
1411

12+
#ifdef LINUX
1513
#include <pthread.h>
16-
1714
#endif
1815

1916
namespace sd
@@ -32,7 +29,6 @@ namespace sd
3229
}
3330

3431
#ifdef WINDOWS
35-
3632
auto getStackBounds()
3733
{
3834
auto rsp = getStackRsp();
@@ -43,7 +39,6 @@ namespace sd
4339
}
4440
#endif
4541
#ifdef LINUX
46-
4742
auto getStackBounds()
4843
{
4944
auto rsp = getStackRsp();
@@ -83,8 +78,8 @@ namespace sd
8378

8479
void MemoryManager::clear()
8580
{
86-
_register.forEach([this](Object &object) { destroy(object); });
87-
_register.clear();
81+
_objectRegister.forEach([this](Object &object) { destroy(object); });
82+
_objectRegister.clear();
8883
}
8984

9085
void MemoryManager::destroy(Object &object)
@@ -104,7 +99,7 @@ namespace sd
10499
{
105100
auto ptr = worklist.back();
106101
worklist.pop_back();
107-
auto &object = _register.getObject(ptr);
102+
auto &object = _objectRegister.getObject(ptr);
108103

109104
if (object.isMarked())
110105
{
@@ -120,7 +115,7 @@ namespace sd
120115

121116
void MemoryManager::sweep()
122117
{
123-
_register.unregisterIf([this](Object &object) {
118+
_objectRegister.unregisterIf([this](Object &object) {
124119
if (object.isMarked())
125120
{
126121
object.unmark();
@@ -146,7 +141,7 @@ namespace sd
146141
while (rsp < top)
147142
{
148143
auto address = (void *)*reinterpret_cast<void **>(rsp);
149-
if (_register.contains(address))
144+
if (_objectRegister.contains(address))
150145
{
151146
result.emplace_back(address);
152147
}
@@ -164,7 +159,7 @@ namespace sd
164159
while (p < end)
165160
{
166161
auto address = (void *)*reinterpret_cast<void **>(p);
167-
if (_register.contains(address))
162+
if (_objectRegister.contains(address))
168163
{
169164
result.emplace_back(address);
170165
}

Source/Library/h/MemoryManager.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ namespace sd
5353
class Metadata
5454
{
5555
private:
56-
bool _marked;
57-
const TypeInfo *_typeInfo;
56+
bool _marked = false;
57+
const TypeInfo *_typeInfo = nullptr;
5858

5959
public:
6060
Metadata(TypeInfo *typeInfo) : _marked(false), _typeInfo(typeInfo) {}
@@ -65,7 +65,7 @@ namespace sd
6565
};
6666

6767
private:
68-
void *_ptr;
68+
void *_ptr = nullptr;
6969
Metadata _metadata;
7070

7171
Object(void *ptr, Metadata metadata) : _ptr(ptr), _metadata(metadata) {}
@@ -76,8 +76,7 @@ namespace sd
7676

7777
template <class T, class... Args> static Object create(Args &&...params)
7878
{
79-
auto ptr = Type<T>::create(std::forward<Args>(params)...);
80-
return Object{ptr, {Type<T>::getInfo()}};
79+
return Object{Type<T>::create(std::forward<Args>(params)...), {Type<T>::getInfo()}};
8180
};
8281

8382
void *getRawPtr() const { return _ptr; }
@@ -122,7 +121,7 @@ namespace sd
122121
};
123122
#pragma endregion
124123

125-
ObjectsRegister _register;
124+
ObjectsRegister _objectRegister;
126125

127126
size_t _allocatedMemory = 0;
128127
size_t _memoryLimit = 1 * 1024 * 1024; // ~1MB
@@ -147,7 +146,7 @@ namespace sd
147146
{
148147
auto object = Object::create<T>(std::forward<Args>(params)...);
149148
_allocatedMemory += object.getSize();
150-
_register.registerNew(object);
149+
_objectRegister.registerNew(object);
151150
if (isGBCollectionNeeded())
152151
{
153152
garbageCollect();

Tests/MemoryManagerTest.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ TEST_F(MemoryManagerTest, ManagerShouldCollectObjectsWithCircleReferences)
121121
TEST_F(MemoryManagerTest, ManagerShouldCollectSomeObjects)
122122
{
123123
auto circle = make(nullptr);
124-
auto circle2 = make(circle);
125-
circle->ptr = circle2;
124+
circle->ptr = make(circle);
126125
{
127126

128127
auto toRemove = make(nullptr);
@@ -140,10 +139,24 @@ TEST_F(MemoryManagerTest, ManagerShouldCollectSomeObjects)
140139

141140
sd::MemoryManager::instance().garbageCollect();
142141

143-
EXPECT_FALSE(wasCollected({circle, circle2}));
142+
EXPECT_FALSE(wasCollected({circle, circle->ptr}));
144143
EXPECT_LE(1, collectedCnt());
145144
}
146145

146+
TEST_F(MemoryManagerTest, ManagerShouldNotCollectInnerObjects)
147+
{
148+
auto circle = make(nullptr);
149+
circle->ptr = make(nullptr);
150+
circle->ptr->ptr = make(nullptr);
151+
circle->ptr->ptr->ptr = make(nullptr);
152+
circle->ptr->ptr->ptr->ptr = make(nullptr);
153+
154+
sd::MemoryManager::instance().garbageCollect();
155+
156+
EXPECT_FALSE(
157+
wasCollected({circle, circle->ptr, circle->ptr->ptr, circle->ptr->ptr->ptr, circle->ptr->ptr->ptr->ptr}));
158+
}
159+
147160
TEST_F(MemoryManagerTest, ManagerShouldNotCollectAutomaticallySomeObjects)
148161
{
149162
auto limit = 500 * 1024 / sizeof(ExampleClass);

0 commit comments

Comments
 (0)