先吐槽下GC的bug有多无力...vs永远只会告诉你堆损坏...
我的这个GC用的是mark-sweep.本来想用mark-compact的, 但后来发现用mark-compact无法处理this指针变动之类的问题, 于是偷懒用了最简单的mark-sweep.
原理就不重复了, 直接贴代码(只贴申明, 全部代码后面会提供下载)
//GarbageCollation.h
我的这个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 引用图相关的结构(对象是点, 指针是边)
#endif
//SmartPointer.h 用来代替普通指针的类(这个类最好不要new)
#endif
//TemplateStaticValue.h 就名字的意思
#endif
http://pan.baidu.com/s/1jG9RTGU
http://download.csdn.net/detail/zz67tta/7880055//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);
};
}
//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;
}
};
}
//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;
};
}
http://pan.baidu.com/s/1jG9RTGU