
/**//*
文件名 BaseClass.h
版权信息 自由版权
模块描述 垃圾收的托管对象信息
最后修改日期 2007年11月14日
*/
#pragma once
//凡准备注册为垃圾收集的类必须继承于此
class CBaseClass
...{
public:
//引用计数默认初始化
CBaseClass();
//增引用计数
void Increment ();
//减引用计数
void Decrement ();
//是否能删除
bool CanRelease ();
virtual ~CBaseClass ();
private:
long m_ref; //引用计数变量
};

// BaseClass.cpp : 实现文件
//
#include "stdafx.h"
#include "GarbageCollection.h"
#include "BaseClass.h"

// CBaseClass
CBaseClass::CBaseClass() : m_ref(1)
...{
}
CBaseClass::~CBaseClass ()
...{
}
void CBaseClass::Increment ()
...{
InterlockedIncrement(&m_ref);
}
void CBaseClass::Decrement ()
...{
InterlockedDecrement(&m_ref);
}
bool CBaseClass::CanRelease ()
...{
return m_ref<=0;
}
// CBaseClass 成员函数

/**//*
文件名 GCPtr.h
版权信息 自由版权
模块描述 与垃圾收集配套使用的托管智能指针
最后修改日期 2007年11月14日
*/
#pragma once
#include "BaseClass.h"
//托管指针类
template <typename T>
class GCPtr
...{
public:
explicit GCPtr(CBaseClass* ptr=0);
GCPtr (const GCPtr<T>& other);
GCPtr<T>& operator= (const GCPtr<T>& other);
~GCPtr();
//与指针有关的操作
T* operator-> () const;
T& operator* () const;
bool IsNull () const;
template <typename U>
operator GCPtr<U> ();
private:
CBaseClass* m_ptr; //引用对象
};
template <typename T>
GCPtr<T>::GCPtr(CBaseClass* ptr) : m_ptr(ptr)
...{
}
template <typename T>
GCPtr<T>::GCPtr (const GCPtr<T>& other)
...{
if (m_ptr!=NULL)
...{
m_ptr->Decrement();
}
m_ptr=other.m_ptr;
if (m_ptr!=NULL)
...{
m_ptr->Increment();
}
}
template <typename T>
GCPtr<T>& GCPtr<T>::operator = (const GCPtr<T>& other)
...{
if (m_ptr==other.m_ptr)
...{
return *this;
}
if (m_ptr!=NULL)
...{
m_ptr->Decrement();
}
m_ptr=other.m_ptr;
if (m_ptr!=NULL)
...{
m_ptr->Increment();
}
return *this;
}
template <typename T>
GCPtr<T>::~GCPtr()
...{
m_ptr->Decrement();
}
template <typename T>
T* GCPtr<T>::operator-> () const
...{
return (T*)m_ptr;
}
template <typename T>
T& GCPtr<T>::operator* () const
...{
return *((T*)m_ptr);
}
template <typename T>
bool GCPtr<T>::IsNull () const
...{
return m_ptr==NULL;
}
template <typename T>
template <typename U>
GCPtr<T>::operator GCPtr<U> ()
...{
return GCPtr<U>(m_ptr);
}

/**//*
文件名 GC.h
版权信息 自由版权
模块描述 垃圾收集装置
最后修改日期 2007年11月14日
*/
#ifndef _GC_
#define _GC_
#include <afxmt.h>
#include <list>
#include "BaseClass.h"
#include "GCPtr.h"
#define GC_MAX 2
//垃圾收集装置
template <typename T>
class GC
...{
public:
//注册托管对象
static GCPtr<T> Register (CBaseClass* newTarget);
private:
//垃圾收集线程函数
static UINT StartGC (LPVOID);
//退出处理
static void EndGC ();
//托管对象信息
static std::list <CBaseClass*>* m_info;
//互斥对象
static CMutex* m_sny;
static bool m_first;
//收集标志
static bool m_sign;
//垃圾收集线程
static CWinThread* m_gcThread;
static int m_count;
};
template <typename T>
bool GC<T>::m_first=true;
template <typename T>
CWinThread* GC<T>::m_gcThread=NULL;
template <typename T>
bool GC<T>::m_sign=false;
template <typename T>
CMutex* GC<T>::m_sny=new CMutex;
template <typename T>
std::list <CBaseClass*>* GC<T>::m_info=new std::list <CBaseClass*>;
template <typename T>
int GC<T>::m_count=0;
template <typename T>
GCPtr<T> GC<T>::Register (CBaseClass* newTarget)
...{
//第一次执行
if (m_first)
...{
m_first=false;
//注册扫尾函数
atexit(EndGC);
//启动垃圾收集线程
m_gcThread=AfxBeginThread(StartGC, NULL, THREAD_PRIORITY_LOWEST);
}
CSingleLock Locker(m_sny);
Locker.Lock();
if (m_count>=GC_MAX)
...{
//允许垃圾收集
m_sign=true;
}
++m_count;
m_info->push_front(newTarget);
Locker.Unlock();
return GCPtr<T>(newTarget);
}
template <typename T>
UINT GC<T>::StartGC (LPVOID)
...{
for (;;)
...{
if (m_sign)
...{
//开始垃圾收集处理
CSingleLock Locker(m_sny);
Locker.Lock();
std::list<CBaseClass*>::iterator pos=m_info->begin();
for (; pos!=m_info->end(); ++pos)
...{
if ((*pos)->CanRelease())
...{
delete (*pos);
(*pos)=NULL;
}
}
m_count=0;
m_sign=false;
m_info->remove(NULL);
Locker.Unlock();
}
}
}
template <typename T>
void GC<T>::EndGC ()
...{
CSingleLock Locker(m_sny);
Locker.Lock();
m_sign=false;
std::list<CBaseClass*>::iterator pos=m_info->begin();
for (; pos!=m_info->end(); ++pos)
...{
delete (CBaseClass*)(*pos);
(*pos)=NULL;
}
Locker.Unlock();
//删掉关联资源
delete m_info; //链表
delete m_sny; //同步对象
}
#endif
//测试文件
// GarbageCollection.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "GarbageCollection.h"
#include <list> //?
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include "GC.h"
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
class Person : public CBaseClass
...{
public:
Person ()
...{
printf("Person on ");
}
virtual void print ()
...{
printf("Hello Garbage Collection ");
}
virtual ~Person ()
...{
printf("Person off ");
}
};
class Student : public Person
...{
public:
Student ()
...{
printf("Student on ");
}
virtual void print ()
...{
printf("Hello GC ");
}
virtual ~Student ()
...{
printf("Student off ");
}
};
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
...{
int nRetCode = 0;
GCPtr<Person> c;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
...{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("致命错误: MFC 初始化失败 "));
nRetCode = 1;
}
else
...{
GCPtr<Person> p=GC<Person>::Register (new Person);
GCPtr<Person> q=GC<Person>::Register (new Student);
GCPtr<Person> r=GC<Person>::Register (new Person);
c=r;
p->print();
q->print();
(*r).print();
}
system("pause");
return nRetCode;
}
发表于 @ 2007年11月17日 18:22:00 | 评论( loading... ) | 举报| 收藏