Skip to content

Commit

Permalink
Delete unmarked backings WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Borthwick committed Oct 30, 2015
1 parent 1555fff commit bbc7ca3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
8 changes: 6 additions & 2 deletions GCPointer/src/GCPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace gc
Deleter& deleter;
uint refCount;
bool marked;
bool deleted;

void deletePointee()
{
Expand Down Expand Up @@ -186,8 +187,11 @@ namespace gc
{
if (backing && --backing->refCount == 0)
{
delete (T*) backing->to;
delete backing;
if (!gc_pool<T>::instance().isCollecting)
{
delete (T*) backing->to;
delete backing;
}
}

backing = nullptr;
Expand Down
41 changes: 16 additions & 25 deletions GCPointer/src/GCPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "GCPointer.h"
#include <map>
#include <set>

namespace gc
{
Expand All @@ -23,11 +24,14 @@ namespace gc
friend size_t live_pointer_count();

using OwnerPointerMap = std::multimap<const OwnerType*, gc_ptr_base*>;
using BackingSet = std::set<gc_ptr_base::Backing*>;
using MapIt = typename OwnerPointerMap::iterator;
using Range = std::pair<MapIt, MapIt>;

OwnerPointerMap owned;
BackingSet backings;
MapIt lastInsertion;
bool isCollecting = false;

void reset()
{
Expand All @@ -37,6 +41,8 @@ namespace gc
void add(gc_ptr_base& ptr)
{
lastInsertion = owned.insert({ ptr.owner, &ptr });
if (ptr.backing)
backings.insert(ptr.backing);
}

void remove(gc_ptr_base& ptr)
Expand Down Expand Up @@ -74,35 +80,20 @@ namespace gc

void deleteUnmarked()
{
for (auto it = owned.begin(); it != owned.end(); )
isCollecting = true; // TODO: Lock?

for (auto it : backings)
{
gc_ptr_base& ptr = *it->second;
if (ptr.backing && !ptr.backing->marked)
{
auto* backing = ptr.backing;
nullifyPointersTo(backing->to);
backing->deletePointee();
delete backing;

// Iterator now invalid, start again
it = owned.begin();
}
else
++it;
gc_ptr_base::Backing& backing = *it;
if (!backing.marked)
backing.deletePointee();
}

//TODO: Remove invalid backings

isCollecting = false;
}

void nullifyPointersTo(OwnerType* pointee)
{
for (auto entry : owned)
{
gc_ptr_base& p = *entry.second;
if (p.backing && (p.backing->to == pointee))
{
p.backing = nullptr;
}
}
}

void unmarkAll()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/GCPointerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,4 @@ TEST_F(GCPointerTest, ownersNeedNotBeGCObjects)
p->selfReference = p;
}
collectGarbage();
}
}

0 comments on commit bbc7ca3

Please sign in to comment.