GarbageCollation的实现

    先吐槽下GC的bug有多无力...vs永远只会告诉你堆损坏...
    我的这个GC用的是mark-sweep.本来想用mark-compact的, 但后来发现用mark-compact无法处理this指针变动之类的问题, 于是偷懒用了最简单的mark-sweep.
    原理就不重复了, 直接贴代码(只贴申明, 全部代码后面会提供下载)
//GarbageCollation.h
#ifndef __XIAOGC_GARBAGECOLLATION__
#define __XIAOGC_GARBAGECOLLATION__

namespace XiaoGC
{
class GarbageCollation
{
private:
static size_t thresholdValue;
static size_t usedArea;

friend class Node;

/**
* 标记已用的节点
*/
static void mark(Node* node);
/**
* 移除没用的节点
*/
static void sweep();
public:
/**
* 初始化
*/
static void init(size_t threshold);
/**
* 用来代替std::malloc
*/
static void* gcMalloc(size_t size);
/**
* 手动开启GC
*/
static void runGC();
};
}

#endif

//GCGraph.h    引用图相关的结构(对象是点, 指针是边)
#ifndef __XIAOGC_GCGRAPH__
#define __XIAOGC_GCGRAPH__

#include "SmartPointer.h"

namespace XiaoGC
{
class Node;

class Edge
{
private:
Edge *next, *previous;

friend class Node;
friend class GarbageCollation;
protected:
Node *ip;
Edge();
public:
Edge(Node *parent, Node *child);
~Edge();

void init(Node *parent, Node *child);
};

class Node
{
private:
Node *nextNode;
Edge headEdge, tailEdge;
bool used;
size_t size;

friend class Edge;
friend class GarbageCollation;
public:
void init(Node* next);
};
}

#endif 

//SmartPointer.h 用来代替普通指针的类(这个类最好不要new)
#ifndef __XIAOGC_SMARTPOINTER__
#define __XIAOGC_SMARTPOINTER__

#include "GCGraph.h"
#include "TemplateStaticValue.h"

namespace XiaoGC
{
template <typename T>
class SmartPointer
: public Edge
{
public:
/**
* 父指针, 如果这个指针是栈指针, 则parent为0, 如果这个指针是某个类的成员指针,则parent为this
*/
explicit SmartPointer(void* parent)
: Edge()
{
if(parent == 0)
this->init(TemplateStaticValue::mainNode, 0);
else
this->init(((Node*)parent) - 1, 0);
}
/**
* 同上, 只不过可以同时初始化指向的对象
*/
explicit SmartPointer(void* parent, T* child)
: Edge()
{
if(parent == 0)
this->init(TemplateStaticValue::mainNode, ((Node*)child) - 1);
else
this->init(((Node*)parent) - 1, ((Node*)child) - 1);
}

explicit SmartPointer(const SmartPointer& pointer)
: Edge()
{
this->ip = pointer->ip;

this->next = pointer->next;
pointer->next = this;
this->next->previous = this;
this->previous = pointer;
}

~SmartPointer()
{
}

public:
T* operator -> ()
{
return (T*) (this->ip + 1);
}

T& operator * ()
{
return *((T*) (this->ip + 1));
}

SmartPointer& operator = (const SmartPointer& pointer)
{
this->ip = pointer.ip;
return *this;
}

SmartPointer& operator = (const T* ptr)
{
this->ip = (Node*)ptr - 1;
return *this;
}

bool operator == (const SmartPointer& pointer)
{
return this->ip == pointer.ip;
}
};
}

#endif 

//TemplateStaticValue.h 就名字的意思
#ifndef __XIAOGC_TEMPLATESTATICVALUE__
#define __XIAOGC_TEMPLATESTATICVALUE__

#include "GCGraph.h"

namespace XiaoGC
{
class Node;
class TemplateStaticValue
{
private:
static Node *mainNode;

friend class GarbageCollation;
template<typename T>
friend class SmartPointer;
};
}

#endif 


http://pan.baidu.com/s/1jG9RTGU 
http://download.csdn.net/detail/zz67tta/7880055

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值