2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
10 Support for user-provided GC marking routines
11 *********************************************
13 The garbage collector supports types for which no automatic marking
14 code is generated. For these types, the user is required to provide
15 three functions: one to act as a marker for garbage collection, and
16 two functions to act as marker and pointer walker for pre-compiled
19 Given a structure ``struct GTY((user)) my_struct``, the following functions
20 should be defined to mark ``my_struct`` :
24 void gt_ggc_mx (my_struct *p)
26 /* This marks field 'fld'. */
30 void gt_pch_nx (my_struct *p)
32 /* This marks field 'fld'. */
36 void gt_pch_nx (my_struct *p, gt_pointer_operator op, void *cookie)
38 /* For every field 'fld', call the given pointer operator. */
39 op (&(tp->fld), NULL, cookie);
42 In general, each marker ``M`` should call ``M`` for every
43 pointer field in the structure. Fields that are not allocated in GC
44 or are not pointers must be ignored.
46 For embedded lists (e.g., structures with a ``next`` or ``prev``
47 pointer), the marker must follow the chain and mark every element in
50 Note that the rules for the pointer walker ``gt_pch_nx (my_struct
51 *, gt_pointer_operator, void *)`` are slightly different. In this
52 case, the operation ``op`` must be applied to the *address* of
55 User-provided marking routines for template types
56 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58 When a template type ``TP`` is marked with ``GTY``, all
59 instances of that type are considered user-provided types. This means
60 that the individual instances of ``TP`` do not need to be marked
61 with ``GTY``. The user needs to provide template functions to mark
62 all the fields of the type.
64 The following code snippets represent all the functions that need to
65 be provided. Note that type ``TP`` may reference to more than one
66 type. In these snippets, there is only one type ``T``, but there
72 void gt_ggc_mx (TP<T> *tp)
74 extern void gt_ggc_mx (T&);
76 /* This marks field 'fld' of type 'T'. */
81 void gt_pch_nx (TP<T> *tp)
83 extern void gt_pch_nx (T&);
85 /* This marks field 'fld' of type 'T'. */
90 void gt_pch_nx (TP<T *> *tp, gt_pointer_operator op, void *cookie)
92 /* For every field 'fld' of 'tp' with type 'T *', call the given
94 op (&(tp->fld), NULL, cookie);
98 void gt_pch_nx (TP<T> *tp, gt_pointer_operator, void *cookie)
100 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
102 /* For every field 'fld' of 'tp' with type 'T', call the pointer
103 walker for all the fields of T. */
104 gt_pch_nx (&(tp->fld), op, cookie);
107 Support for user-defined types is currently limited. The following
110 * Type ``TP`` and all the argument types ``T`` must be
113 * Type ``TP`` can only have type names in its argument list.
115 * The pointer walker functions are different for ``TP<T>`` and
116 ``TP<T *>``. In the case of ``TP<T>``, references to
117 ``T`` must be handled by calling ``gt_pch_nx`` (which
118 will, in turn, walk all the pointers inside fields of ``T``).
119 In the case of ``TP<T *>``, references to ``T *`` must be
120 handled by calling the ``op`` function on the address of the
121 pointer (see the code snippets above).