User-GC.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: User GC</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: User GC">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: User GC">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Type-Information.html#Type-Information" rel="up" title="Type Information">
  30. <link href="GGC-Roots.html#GGC-Roots" rel="next" title="GGC Roots">
  31. <link href="Inheritance-and-GTY.html#Inheritance-and-GTY" rel="prev" title="Inheritance and GTY">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="User-GC"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="GGC-Roots.html#GGC-Roots" accesskey="n" rel="next">GGC Roots</a>, Previous: <a href="Inheritance-and-GTY.html#Inheritance-and-GTY" accesskey="p" rel="prev">Inheritance and GTY</a>, Up: <a href="Type-Information.html#Type-Information" accesskey="u" rel="up">Type Information</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Support-for-user_002dprovided-GC-marking-routines"></a>
  69. <h3 class="section">22.3 Support for user-provided GC marking routines</h3>
  70. <a name="index-user-gc"></a>
  71. <p>The garbage collector supports types for which no automatic marking
  72. code is generated. For these types, the user is required to provide
  73. three functions: one to act as a marker for garbage collection, and
  74. two functions to act as marker and pointer walker for pre-compiled
  75. headers.
  76. </p>
  77. <p>Given a structure <code>struct GTY((user)) my_struct</code>, the following functions
  78. should be defined to mark <code>my_struct</code>:
  79. </p>
  80. <div class="smallexample">
  81. <pre class="smallexample">void gt_ggc_mx (my_struct *p)
  82. {
  83. /* This marks field 'fld'. */
  84. gt_ggc_mx (p-&gt;fld);
  85. }
  86. void gt_pch_nx (my_struct *p)
  87. {
  88. /* This marks field 'fld'. */
  89. gt_pch_nx (tp-&gt;fld);
  90. }
  91. void gt_pch_nx (my_struct *p, gt_pointer_operator op, void *cookie)
  92. {
  93. /* For every field 'fld', call the given pointer operator. */
  94. op (&amp;(tp-&gt;fld), cookie);
  95. }
  96. </pre></div>
  97. <p>In general, each marker <code>M</code> should call <code>M</code> for every
  98. pointer field in the structure. Fields that are not allocated in GC
  99. or are not pointers must be ignored.
  100. </p>
  101. <p>For embedded lists (e.g., structures with a <code>next</code> or <code>prev</code>
  102. pointer), the marker must follow the chain and mark every element in
  103. it.
  104. </p>
  105. <p>Note that the rules for the pointer walker <code>gt_pch_nx (my_struct
  106. *, gt_pointer_operator, void *)</code> are slightly different. In this
  107. case, the operation <code>op</code> must be applied to the <em>address</em> of
  108. every pointer field.
  109. </p>
  110. <a name="User_002dprovided-marking-routines-for-template-types"></a>
  111. <h4 class="subsection">22.3.1 User-provided marking routines for template types</h4>
  112. <p>When a template type <code>TP</code> is marked with <code>GTY</code>, all
  113. instances of that type are considered user-provided types. This means
  114. that the individual instances of <code>TP</code> do not need to be marked
  115. with <code>GTY</code>. The user needs to provide template functions to mark
  116. all the fields of the type.
  117. </p>
  118. <p>The following code snippets represent all the functions that need to
  119. be provided. Note that type <code>TP</code> may reference to more than one
  120. type. In these snippets, there is only one type <code>T</code>, but there
  121. could be more.
  122. </p>
  123. <div class="smallexample">
  124. <pre class="smallexample">template&lt;typename T&gt;
  125. void gt_ggc_mx (TP&lt;T&gt; *tp)
  126. {
  127. extern void gt_ggc_mx (T&amp;);
  128. /* This marks field 'fld' of type 'T'. */
  129. gt_ggc_mx (tp-&gt;fld);
  130. }
  131. template&lt;typename T&gt;
  132. void gt_pch_nx (TP&lt;T&gt; *tp)
  133. {
  134. extern void gt_pch_nx (T&amp;);
  135. /* This marks field 'fld' of type 'T'. */
  136. gt_pch_nx (tp-&gt;fld);
  137. }
  138. template&lt;typename T&gt;
  139. void gt_pch_nx (TP&lt;T *&gt; *tp, gt_pointer_operator op, void *cookie)
  140. {
  141. /* For every field 'fld' of 'tp' with type 'T *', call the given
  142. pointer operator. */
  143. op (&amp;(tp-&gt;fld), cookie);
  144. }
  145. template&lt;typename T&gt;
  146. void gt_pch_nx (TP&lt;T&gt; *tp, gt_pointer_operator, void *cookie)
  147. {
  148. extern void gt_pch_nx (T *, gt_pointer_operator, void *);
  149. /* For every field 'fld' of 'tp' with type 'T', call the pointer
  150. walker for all the fields of T. */
  151. gt_pch_nx (&amp;(tp-&gt;fld), op, cookie);
  152. }
  153. </pre></div>
  154. <p>Support for user-defined types is currently limited. The following
  155. restrictions apply:
  156. </p>
  157. <ol>
  158. <li> Type <code>TP</code> and all the argument types <code>T</code> must be
  159. marked with <code>GTY</code>.
  160. </li><li> Type <code>TP</code> can only have type names in its argument list.
  161. </li><li> The pointer walker functions are different for <code>TP&lt;T&gt;</code> and
  162. <code>TP&lt;T *&gt;</code>. In the case of <code>TP&lt;T&gt;</code>, references to
  163. <code>T</code> must be handled by calling <code>gt_pch_nx</code> (which
  164. will, in turn, walk all the pointers inside fields of <code>T</code>).
  165. In the case of <code>TP&lt;T *&gt;</code>, references to <code>T *</code> must be
  166. handled by calling the <code>op</code> function on the address of the
  167. pointer (see the code snippets above).
  168. </li></ol>
  169. <hr>
  170. <div class="header">
  171. <p>
  172. Next: <a href="GGC-Roots.html#GGC-Roots" accesskey="n" rel="next">GGC Roots</a>, Previous: <a href="Inheritance-and-GTY.html#Inheritance-and-GTY" accesskey="p" rel="prev">Inheritance and GTY</a>, Up: <a href="Type-Information.html#Type-Information" accesskey="u" rel="up">Type Information</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  173. </div>
  174. </body>
  175. </html>