GCdef.h
#ifndef __GCDEF_H
#define __GCDEF_H
#include <string>
#include <cstdio>
#include <vector>
#include <list>
using namespace std;
//所有GC对象来源于此
class GCObject
{
public:
GCObject(string strObjName)
{
m_strObjname = strObjName;
m_nMark = 0;
}
virtual ~GCObject()
{
printf("GCObject %s Deleted\n",m_strObjname.c_str());
}
void ClearMark()
{
m_nMark = 0;
}
void SetMark()
{
m_nMark = 1;
}
bool IsMark()
{
return m_nMark == 1;
}
protected:
string m_strObjname;
int m_nMark;
};
class GCString : public GCObject
{
public:
GCString(string strObjName):GCObject(strObjName)
{}
virtual ~GCString(){}
};
class GCValue : public GCObject
{
public:
GCValue(string strObjName):GCObject(strObjName)
{}
virtual ~GCValue(){}
};
class GCFunction : public GCObject
{
public:
GCFunction(string strObjName):GCObject(strObjName)
{
}
virtual ~GCFunction()
{
printf("GCFunction %s Deleted \n",m_strObjname.c_str());
}
void LinkGCObject(GCObject* pObj);
void SetObj();
protected:
std::list<GCObject*> m_gcList;
};
class LuaState;
class GlobalState
{
public:
GlobalState(){}
virtual ~GlobalState(){}
void LinkGCObject(GCObject* pObj);
void LinkGlobalObj(GCObject* pObj);
void ClearMark();
void SetGlobalObj();
void DelGCObjs();
LuaState* m_pMainState;
std::list<GCObject*> m_rootgc;
std::list<GCObject*> m_globalValue;
};
class LuaState
{
public:
LuaState(){}
~LuaState(){}
GlobalState* GetGState();
GCFunction* GetCurFunc();
void SetObj();
void EnterFunc(GCFunction* pFunc);
void ExitFunc(GCFunction* pFunc);
GlobalState* m_pGState;
std::vector<GCFunction*> m_FunctionVec;
};
void GCProcess(LuaState* pLState);
#endif
GCdef.cpp
#include <cstdio>
#include "GCDef.h"
void GCFunction::LinkGCObject(GCObject* pObj)
{
m_gcList.push_back(pObj);
}
void GlobalState::LinkGCObject(GCObject* pObj)
{
m_rootgc.push_back(pObj);
}
void GlobalState::LinkGlobalObj(GCObject* pObj)
{
m_globalValue.push_back(pObj);
}
GCFunction* LuaState::GetCurFunc()
{
if(m_FunctionVec.empty())
{
printf("Error: FunctionVec Is NULL");
return NULL;
}
return m_FunctionVec[m_FunctionVec.size() - 1];
}
GlobalState* LuaState::GetGState()
{
return m_pGState;
}
void GlobalState::ClearMark()
{
std::list<GCObject*>::iterator iter;
for(iter = m_rootgc.begin(); iter != m_rootgc.end(); ++iter)
{
(*iter)->ClearMark();
}
}
void GCFunction::SetObj()
{
std::list<GCObject*>::iterator iter;
for(iter = m_gcList.begin(); iter != m_gcList.end(); ++iter)
{
(*iter)->SetMark();
}
}
void LuaState::SetObj()
{
for(int i = 0; i < m_FunctionVec.size(); ++i)
{
m_FunctionVec[i]->SetMark();
m_FunctionVec[i]->SetObj();
}
}
void GlobalState::SetGlobalObj()
{
std::list<GCObject*>::iterator iter;
for(iter = m_globalValue.begin(); iter != m_globalValue.end(); ++iter)
{
(*iter)->SetMark();
}
}
void GlobalState::DelGCObjs()
{
std::list<GCObject*>::iterator iter = m_rootgc.begin();
while(iter != m_rootgc.end())
{
if(!(*iter)->IsMark())
{
delete (*iter);
iter = m_rootgc.erase(iter);
}
else
{
++iter;
}
}
}
//GC流程
void GCProcess(LuaState* pLState)
{
GlobalState* pGState = pLState->GetGState();
pGState->ClearMark();
pGState->SetGlobalObj();
pLState->SetObj();
pGState->DelGCObjs();
}
void LuaState::EnterFunc(GCFunction* pFunc)
{
m_FunctionVec.push_back(pFunc);
// m_FunctionVec[m_FunctionVec.size()] = pFunc;
}
void LuaState::ExitFunc(GCFunction* pFunc)
{
if(m_FunctionVec.empty())
return;
if(m_FunctionVec[m_FunctionVec.size() - 1] != pFunc)
printf("Error : GCFunction 未找到");
m_FunctionVec.pop_back();
}
newop.h
#ifndef __NEWOP_H
#define __NEWOP_H
#include "GCDef.h"
//一些新的new操作
GCObject* NewGCObject(LuaState* pState,string strObjName,bool bGlobal = false);
GCFunction* NewGCFunction(LuaState* pState,string strFuncName,bool bGlobal = false);
#endif
newop.cpp
#include "newop.h"
static int nNewCount;
GCObject* NewGCObject(LuaState* pState,string strObjName,bool bGlobal)
{
nNewCount++;
GCObject* pObj = new GCObject(strObjName);
GlobalState* pGState = pState->GetGState();
pGState->LinkGCObject(pObj);
if(bGlobal)
{
pGState->LinkGlobalObj(pObj);
}
GCFunction* pFunc = pState->GetCurFunc();
if(pFunc != NULL)
{
pFunc->LinkGCObject(pObj);
}
return pObj;
}
GCFunction* NewGCFunction(LuaState* pState,string strFuncName,bool bGlobal)
{
nNewCount++;
GCObject* pFunc = new GCFunction(strFuncName);
GlobalState* pGState = pState->GetGState();
pGState->LinkGCObject(pFunc);
return (GCFunction*)(pFunc);
}
main.cpp
#include "newop.h"
#include "GCDef.h"
GlobalState gState;
LuaState lState;
void Func2()
{
GCFunction* pFunc = NewGCFunction(&lState,"Func2");
lState.EnterFunc(pFunc);
GCObject* pObj1 = NewGCObject(&lState,"Func2 Obj1");
lState.ExitFunc(pFunc);
GCProcess(&lState);
}
void Func3()
{
GCFunction* pFunc = NewGCFunction(&lState,"Func3");
lState.EnterFunc(pFunc);
GCObject* pObj1 = NewGCObject(&lState,"Func3 Obj1");
lState.ExitFunc(pFunc);
GCProcess(&lState);
}
void Func1()
{
GCFunction* pFunc = NewGCFunction(&lState,"Func1");
lState.EnterFunc(pFunc);
GCObject* pObj1 = NewGCObject(&lState,"Func1 Obj1");
Func2();
Func3();
lState.ExitFunc(pFunc);
GCProcess(&lState);
}
int main()
{
lState.m_pGState = &gState;
gState.m_pMainState = &lState;
GCFunction* pFunc = NewGCFunction(&lState,"Main");
lState.EnterFunc(pFunc);
GCObject* pObj1 = NewGCObject(&lState,"Main Obj1");
GCObject* pObj2 = NewGCObject(&lState,"Main Obj2");
Func1();
lState.ExitFunc(pFunc);
GCProcess(&lState);
}
比较简单 只是进行了函数内的局部变量的内存回收

浙公网安备 33010602011771号