Next:Marking Roots for the Garbage Collector, Previous:Support for inheritance, Up:Memory Management and Type Information [Contents][Index]
The garbage collector supports types for which no automatic markingcode is generated. For these types, the user is required to providethree functions: one to act as a marker for garbage collection, andtwo functions to act as marker and pointer walker for pre-compiledheaders.
Given a structurestruct GTY((user)) my_struct, the following functionsshould be defined to markmy_struct:
void gt_ggc_mx (my_struct *p){ /* This marks field 'fld'. */ gt_ggc_mx (p->fld);}void gt_pch_nx (my_struct *p){ /* This marks field 'fld'. */ gt_pch_nx (tp->fld);}void gt_pch_nx (my_struct *p, gt_pointer_operator op, void *cookie){ /* For every field 'fld', call the given pointer operator. */ op (&(tp->fld), NULL, cookie);}In general, each markerM should callM for everypointer field in the structure. Fields that are not allocated in GCor are not pointers must be ignored.
For embedded lists (e.g., structures with anext orprevpointer), the marker must follow the chain and mark every element init.
Note that the rules for the pointer walkergt_pch_nx (my_struct*, gt_pointer_operator, void *) are slightly different. In thiscase, the operationop must be applied to theaddress ofevery pointer field.
When a template typeTP is marked withGTY, allinstances of that type are considered user-provided types. This meansthat the individual instances ofTP do not need to be markedwithGTY. The user needs to provide template functions to markall the fields of the type.
The following code snippets represent all the functions that need tobe provided. Note that typeTP may reference to more than onetype. In these snippets, there is only one typeT, but therecould be more.
template<typename T>void gt_ggc_mx (TP<T> *tp){ extern void gt_ggc_mx (T&); /* This marks field 'fld' of type 'T'. */ gt_ggc_mx (tp->fld);}template<typename T>void gt_pch_nx (TP<T> *tp){ extern void gt_pch_nx (T&); /* This marks field 'fld' of type 'T'. */ gt_pch_nx (tp->fld);}template<typename T>void gt_pch_nx (TP<T *> *tp, gt_pointer_operator op, void *cookie){ /* For every field 'fld' of 'tp' with type 'T *', call the given pointer operator. */ op (&(tp->fld), NULL, cookie);}template<typename T>void gt_pch_nx (TP<T> *tp, gt_pointer_operator, void *cookie){ extern void gt_pch_nx (T *, gt_pointer_operator, void *); /* For every field 'fld' of 'tp' with type 'T', call the pointer walker for all the fields of T. */ gt_pch_nx (&(tp->fld), op, cookie);}Support for user-defined types is currently limited. The followingrestrictions apply:
TP and all the argument typesT must bemarked withGTY.TP can only have type names in its argument list.TP<T> andTP<T *>. In the case ofTP<T>, references toT must be handled by callinggt_pch_nx (whichwill, in turn, walk all the pointers inside fields ofT).In the case ofTP<T *>, references toT * must behandled by calling theop function on the address of thepointer (see the code snippets above).Next:Marking Roots for the Garbage Collector, Previous:Support for inheritance, Up:Memory Management and Type Information [Contents][Index]