これが私のガベージコレクターコードです:(そのオブジェクトがもう到達できないことがわかった場合)
HEADER* ptr;
static int gc_checked_count;
static void** gc_checked_array;//=malloc(10000);
//This method called when the the pointer in this class is changing.
inline void Destruct() {
//If the pointer is null or it is alright pointed from the stack then
if ((!ptr?true:ptr->getOnStack()))
//exit.
return;
//GC_THROW_USED uses this variable. - need to zero it.
gc_checked_count=0;
try {
GC_THROW_USED(ptr);
//If this function didn't threw a bool ,then run it's finalize method
//getType is temporary as function pointer.
((void(*)(void*))ptr->getType())(ptr);
//, free his information
free(ptr->getArray());
//, free itself
free(ptr);
//and zero `ptr` because it isn't valid anymore.
ptr=0;
}
catch (bool x) {
//If reachable then don't do anything to this object.
//Keep yourself alive because life is good :).
}
}
inline void GC_THROW_USED(HEADER* p) {
//Check if this pointer didn't checked by this method
for (uint i=0;i<gc_checked_count;++i)
//, if yes then
if (gc_checked_array[i]==p)
//exit.
return;
//Append this pointer to the checked list
gc_checked_array[gc_checked_count++]=p;
//If this pointer is pointed on the stack then
if (p->getOnStack())
//throw. (it says that `ptr` is reachable.)
throw false;
uint count=p->getCount();
HEADER** pArray=(HEADER**)p->getArray();
for (uint i=0;i<count;++i)
//Run this method on it's containers too. (Until exception or there is no object to run on)
GC_THROW_USED(pArray[i]);
}
ステートメントの前に、それを説明するコメントがあります。ご覧のとおり、このGCは、コンテナーがスタック上にあることが検出されるまで、ポインターのコンテナー(このポインターを知っている人)で実行されます。その後、このオブジェクトが到達可能であることを意味します。そうでない場合は、このオブジェクトを完成させて解放します。
本当の質問:パフォーマンスを向上させるためにこのプロセスを最適化する方法はありますか?パーツ(コードの//Run this method on it's containers too. (Until exception or there is no object to run on)
最後の数行)はカットでき、バグなしで実行できますか?
このGCメソッドは私の脳に現れたばかりで、バグなしで機能するため、少し混乱しています。