gc.h
헤더 파일에 제공된 것보다 더 많은 참조가 필요하면 더 이상 진행하기 전에 가비지 컬렉터를 읽어야합니다.
이
typedef void (*GC_finalization_proc)
GC_PROTO((GC_PTR obj, GC_PTR client_data));
GC_API void GC_register_finalizer
GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
GC_finalization_proc *ofn, GC_PTR *ocd));
GC_API void GC_debug_register_finalizer
GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
GC_finalization_proc *ofn, GC_PTR *ocd));
/* When obj is no longer accessible, invoke */
/* (*fn)(obj, cd). If a and b are inaccessible, and */
/* a points to b (after disappearing links have been */
/* made to disappear), then only a will be */
/* finalized. (If this does not create any new */
/* pointers to b, then b will be finalized after the */
/* next collection.) Any finalizable object that */
/* is reachable from itself by following one or more */
/* pointers will not be finalized (or collected). */
/* Thus cycles involving finalizable objects should */
/* be avoided, or broken by disappearing links. */
/* All but the last finalizer registered for an object */
/* is ignored. */
/* Finalization may be removed by passing 0 as fn. */
/* Finalizers are implicitly unregistered just before */
/* they are invoked. */
/* The old finalizer and client data are stored in */
/* *ofn and *ocd. */
/* Fn is never invoked on an accessible object, */
/* provided hidden pointers are converted to real */
/* pointers only if the allocation lock is held, and */
/* such conversions are not performed by finalization */
/* routines. */
/* If GC_register_finalizer is aborted as a result of */
/* a signal, the object may be left with no */
/* finalization, even if neither the old nor new */
/* finalizer were NULL. */
/* Obj should be the nonNULL starting address of an */
/* object allocated by GC_malloc or friends. */
/* Note that any garbage collectable object referenced */
/* by cd will be considered accessible until the */
/* finalizer is invoked. */
그래서 당신은 콜백을 정의 : 당신이 질문을 다시
에서, gc.h
헤더는 당신이 필요로하는 것을 가지고
typedef <any type at all you want passed to the callback
as data for its own use> MY_ENVIRONMENT;
void my_callback(GC_PTR void_obj, GC_PTR void_environment) {
MY_ENVIRONMENT *env = (MY_ENVIRONMENT)void_environment;
MY_OBJECT *obj = (MY_OBJECT*)void_obj;
// Do finalization here.
}
환경 만들기 (있는 경우는 true, 그렇지 않은 경우는 단지 NULL을 전달) :
MY_ENVIRONMENT *my_env = new MY_ENVIRONMENT;
// Initialize if necessary.
다음 새로 할당 된 개체에 등록 :
이 특정 객체가 수집 될 때
MY_
MY_ENVIRONMENT old_env;
GC_finalization_proc old_proc;
GC_register_finalizer(new_obj, my_callback, my_env, &old_env, &old_proc);
은 이제 my_callback
는 환경 레코드와 호출됩니다.
질문 2와 관련하여 요점을 놓치고 있습니다. Boehm GC 은 malloc/new를 대체하고 무료 메모리 공간을 관리합니다. 일반적으로 컬렉션을 수행 할 때 자체를 결정합니다. 일반적으로 대부분의 경기장이 모두 소모 된 경우입니다. 가비지 콜렉션은 비어있는 블록을 식별하므로 재 할당 할 수 있습니다. API notes에서 분명히 말하듯이 컬렉션과 강제없는 오브젝트를 강제로 실행할 수 있지만 일반적으로 이러한 오브젝트는 필요하지 않습니다.