945gc:GC



JAVA,C sharpsupporter经常拿语言特有垃圾回收器说事.C是万能!当然前提是使用它人必须先是全能.
下例是以我现在浅薄C掌握水平所实现基于标记-清除算法GC.如果类对象想实现自动回收其内存,其必须派生自Collectable类,并且实现CollectableGetReference虚,由于我是无法预知派生类指针或引用成员变量,所以采用template设计模式依赖倒置.
简单示意:
void Class1::GetReference
{
m_reftoClass2 ->doMark;//标记其引用对象
or ;//如果没有引用对象,同时递归终止
}

#ndef _GC
# _GC
#pragma _disibledevent=>
iSelf;
}

void Add2GC(Collectable *obj);
void GarbageCollection(Collectable *rootObj);
private:
GarbageCollector
{
}
void Mark(Collectable *obj);
void Sweep;

MEMPOOL iMemPool;

GarbageCollector *iSelf;//the _disibledevent=>virtual ~Collectable{}
void doMark(void)
{
(!iMarkBit)
{
iMarkBit = true;
GetReference;
GarbageCollector::Add2GC(this);
}
}
virtual void GetReference = 0;
protected:
bool iMarkBit;//Mark for GC
};
GarbageCollector *GarbageCollector::iSelf = NULL;
void GarbageCollector::Add2GC(Collectable *obj)
{
(iSelf->iMemPool).push_back(obj);
}
void GarbageCollector::Mark(Collectable *obj)
{
(obj)
obj->doMark;
}
void GarbageCollector::Sweep
{
for(MEMPOOL::iterator vIter = iMemPool.begin; vIter != iMemPool.end;)
{
Collectable *temp = *vIter;
//Here it's the data used by other data
(temp->iMarkBit)
{
temp->iMarkBit = false;
vIter;
}

{
//Discard the useless data block and adjust iterator to proper position
vIter = iMemPool.erase(vIter);
//free the memory or push the space o memory pool
delete temp;
}
}
}
void GarbageCollector::GarbageCollection(Collectable *rootObj)
{
(rootObj)
{
Mark(rootObj);
Sweep;
}
}
#end
本例其实存在隐患,即有可能stack overflow.doMark->GetReference->doMark... ...递归扫描,而且Collectable和GarbageCollector双向耦合,耦合度高.另外在清除标记为0(即不再被引用对象)时,只是简单释放给系统堆.如果内重载operator delete(void *)使的仅仅归还给Memory Pool其价值更高.其实在寸土寸金嵌入式设备中,内寸很容易耗尽,GC价值应该被集成到系统内存管理器中,哎,继续学习Symbian中,还没有看到它内存管理策略是什么.
Tags:  技嘉945gc intel945gc gcms 945gc

延伸阅读

最新评论

发表评论