comparison gcc/doc/gty.texi @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 @c Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010 1 @c Copyright (C) 2002-2017 Free Software Foundation, Inc.
2 @c Free Software Foundation, Inc.
3 @c This is part of the GCC manual. 2 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi. 3 @c For copying conditions, see the file gcc.texi.
5 4
6 @node Type Information 5 @node Type Information
7 @chapter Memory Management and Type Information 6 @chapter Memory Management and Type Information
11 GCC uses some fairly sophisticated memory management techniques, which 10 GCC uses some fairly sophisticated memory management techniques, which
12 involve determining information about GCC's data structures from GCC's 11 involve determining information about GCC's data structures from GCC's
13 source code and using this information to perform garbage collection and 12 source code and using this information to perform garbage collection and
14 implement precompiled headers. 13 implement precompiled headers.
15 14
16 A full C parser would be too complicated for this task, so a limited 15 A full C++ parser would be too complicated for this task, so a limited
17 subset of C is interpreted and special markers are used to determine 16 subset of C++ is interpreted and special markers are used to determine
18 what parts of the source to look at. All @code{struct} and 17 what parts of the source to look at. All @code{struct}, @code{union}
19 @code{union} declarations that define data structures that are 18 and @code{template} structure declarations that define data structures
20 allocated under control of the garbage collector must be marked. All 19 that are allocated under control of the garbage collector must be
21 global variables that hold pointers to garbage-collected memory must 20 marked. All global variables that hold pointers to garbage-collected
22 also be marked. Finally, all global variables that need to be saved 21 memory must also be marked. Finally, all global variables that need
23 and restored by a precompiled header must be marked. (The precompiled 22 to be saved and restored by a precompiled header must be marked. (The
24 header mechanism can only save static variables if they're scalar. 23 precompiled header mechanism can only save static variables if they're
25 Complex data structures must be allocated in garbage-collected memory 24 scalar. Complex data structures must be allocated in garbage-collected
26 to be saved in a precompiled header.) 25 memory to be saved in a precompiled header.)
27 26
28 The full format of a marker is 27 The full format of a marker is
29 @smallexample 28 @smallexample
30 GTY (([@var{option}] [(@var{param})], [@var{option}] [(@var{param})] @dots{})) 29 GTY (([@var{option}] [(@var{param})], [@var{option}] [(@var{param})] @dots{}))
31 @end smallexample 30 @end smallexample
63 The parser understands simple typedefs such as 62 The parser understands simple typedefs such as
64 @code{typedef struct @var{tag} *@var{name};} and 63 @code{typedef struct @var{tag} *@var{name};} and
65 @code{typedef int @var{name};}. 64 @code{typedef int @var{name};}.
66 These don't need to be marked. 65 These don't need to be marked.
67 66
67 Since @code{gengtype}'s understanding of C++ is limited, there are
68 several constructs and declarations that are not supported inside
69 classes/structures marked for automatic GC code generation. The
70 following C++ constructs produce a @code{gengtype} error on
71 structures/classes marked for automatic GC code generation:
72
73 @itemize @bullet
74 @item
75 Type definitions inside classes/structures are not supported.
76 @item
77 Enumerations inside classes/structures are not supported.
78 @end itemize
79
80 If you have a class or structure using any of the above constructs,
81 you need to mark that class as @code{GTY ((user))} and provide your
82 own marking routines (see section @ref{User GC} for details).
83
84 It is always valid to include function definitions inside classes.
85 Those are always ignored by @code{gengtype}, as it only cares about
86 data members.
87
68 @menu 88 @menu
69 * GTY Options:: What goes inside a @code{GTY(())}. 89 * GTY Options:: What goes inside a @code{GTY(())}.
90 * Inheritance and GTY:: Adding GTY to a class hierarchy.
91 * User GC:: Adding user-provided GC marking routines.
70 * GGC Roots:: Making global variables GGC roots. 92 * GGC Roots:: Making global variables GGC roots.
71 * Files:: How the generated files work. 93 * Files:: How the generated files work.
72 * Invoking the garbage collector:: How to invoke the garbage collector. 94 * Invoking the garbage collector:: How to invoke the garbage collector.
73 * Troubleshooting:: When something does not work as expected. 95 * Troubleshooting:: When something does not work as expected.
74 @end menu 96 @end menu
132 @table @code 154 @table @code
133 @findex length 155 @findex length
134 @item length ("@var{expression}") 156 @item length ("@var{expression}")
135 157
136 There are two places the type machinery will need to be explicitly told 158 There are two places the type machinery will need to be explicitly told
137 the length of an array. The first case is when a structure ends in a 159 the length of an array of non-atomic objects. The first case is when a
138 variable-length array, like this: 160 structure ends in a variable-length array, like this:
139 @smallexample 161 @smallexample
140 struct GTY(()) rtvec_def @{ 162 struct GTY(()) rtvec_def @{
141 int num_elem; /* @r{number of elements} */ 163 int num_elem; /* @r{number of elements} */
142 rtx GTY ((length ("%h.num_elem"))) elem[1]; 164 rtx GTY ((length ("%h.num_elem"))) elem[1];
143 @}; 165 @};
161 This second use of @code{length} also works on global variables, like: 183 This second use of @code{length} also works on global variables, like:
162 @verbatim 184 @verbatim
163 static GTY((length("reg_known_value_size"))) rtx *reg_known_value; 185 static GTY((length("reg_known_value_size"))) rtx *reg_known_value;
164 @end verbatim 186 @end verbatim
165 187
188 Note that the @code{length} option is only meant for use with arrays of
189 non-atomic objects, that is, objects that contain pointers pointing to
190 other GTY-managed objects. For other GC-allocated arrays and strings
191 you should use @code{atomic}.
192
166 @findex skip 193 @findex skip
167 @item skip 194 @item skip
168 195
169 If @code{skip} is applied to a field, the type machinery will ignore it. 196 If @code{skip} is applied to a field, the type machinery will ignore it.
170 This is somewhat dangerous; the only safe use is in a union when one 197 This is somewhat dangerous; the only safe use is in a union when one
171 field really isn't ever used. 198 field really isn't ever used.
199
200 @findex for_user
201 @item for_user
202
203 Use this to mark types that need to be marked by user gc routines, but are not
204 refered to in a template argument. So if you have some user gc type T1 and a
205 non user gc type T2 you can give T2 the for_user option so that the marking
206 functions for T1 can call non mangled functions to mark T2.
172 207
173 @findex desc 208 @findex desc
174 @findex tag 209 @findex tag
175 @findex default 210 @findex default
176 @item desc ("@var{expression}") 211 @item desc ("@var{expression}")
206 In this example, the value of BINDING_HAS_LEVEL_P when applied to a 241 In this example, the value of BINDING_HAS_LEVEL_P when applied to a
207 @code{struct tree_binding *} is presumed to be 0 or 1. If 1, the type 242 @code{struct tree_binding *} is presumed to be 0 or 1. If 1, the type
208 mechanism will treat the field @code{level} as being present and if 0, 243 mechanism will treat the field @code{level} as being present and if 0,
209 will treat the field @code{scope} as being present. 244 will treat the field @code{scope} as being present.
210 245
211 @findex param_is 246 The @code{desc} and @code{tag} options can also be used for inheritance
212 @findex use_param 247 to denote which subclass an instance is. See @ref{Inheritance and GTY}
213 @item param_is (@var{type}) 248 for more information.
214 @itemx use_param 249
215 250 @findex cache
216 Sometimes it's convenient to define some data structure to work on 251 @item cache
217 generic pointers (that is, @code{PTR}) and then use it with a specific 252
218 type. @code{param_is} specifies the real type pointed to, and 253 When the @code{cache} option is applied to a global variable gt_clear_cache is
219 @code{use_param} says where in the generic data structure that type 254 called on that variable between the mark and sweep phases of garbage
220 should be put. 255 collection. The gt_clear_cache function is free to mark blocks as used, or to
221 256 clear pointers in the variable.
222 For instance, to have a @code{htab_t} that points to trees, one would
223 write the definition of @code{htab_t} like this:
224 @smallexample
225 typedef struct GTY(()) @{
226 @dots{}
227 void ** GTY ((use_param, @dots{})) entries;
228 @dots{}
229 @} htab_t;
230 @end smallexample
231 and then declare variables like this:
232 @smallexample
233 static htab_t GTY ((param_is (union tree_node))) ict;
234 @end smallexample
235
236 @findex param@var{n}_is
237 @findex use_param@var{n}
238 @item param@var{n}_is (@var{type})
239 @itemx use_param@var{n}
240
241 In more complicated cases, the data structure might need to work on
242 several different types, which might not necessarily all be pointers.
243 For this, @code{param1_is} through @code{param9_is} may be used to
244 specify the real type of a field identified by @code{use_param1} through
245 @code{use_param9}.
246
247 @findex use_params
248 @item use_params
249
250 When a structure contains another structure that is parameterized,
251 there's no need to do anything special, the inner structure inherits the
252 parameters of the outer one. When a structure contains a pointer to a
253 parameterized structure, the type machinery won't automatically detect
254 this (it could, it just doesn't yet), so it's necessary to tell it that
255 the pointed-to structure should use the same parameters as the outer
256 structure. This is done by marking the pointer with the
257 @code{use_params} option.
258 257
259 @findex deletable 258 @findex deletable
260 @item deletable 259 @item deletable
261 260
262 @code{deletable}, when applied to a global variable, indicates that when 261 @code{deletable}, when applied to a global variable, indicates that when
263 garbage collection runs, there's no need to mark anything pointed to 262 garbage collection runs, there's no need to mark anything pointed to
264 by this variable, it can just be set to @code{NULL} instead. This is used 263 by this variable, it can just be set to @code{NULL} instead. This is used
265 to keep a list of free structures around for re-use. 264 to keep a list of free structures around for re-use.
266
267 @findex if_marked
268 @item if_marked ("@var{expression}")
269
270 Suppose you want some kinds of object to be unique, and so you put them
271 in a hash table. If garbage collection marks the hash table, these
272 objects will never be freed, even if the last other reference to them
273 goes away. GGC has special handling to deal with this: if you use the
274 @code{if_marked} option on a global hash table, GGC will call the
275 routine whose name is the parameter to the option on each hash table
276 entry. If the routine returns nonzero, the hash table entry will
277 be marked as usual. If the routine returns zero, the hash table entry
278 will be deleted.
279
280 The routine @code{ggc_marked_p} can be used to determine if an element
281 has been marked already; in fact, the usual case is to use
282 @code{if_marked ("ggc_marked_p")}.
283
284 @findex mark_hook
285 @item mark_hook ("@var{hook-routine-name}")
286
287 If provided for a structure or union type, the given
288 @var{hook-routine-name} (between double-quotes) is the name of a
289 routine called when the garbage collector has just marked the data as
290 reachable. This routine should not change the data, or call any ggc
291 routine. Its only argument is a pointer to the just marked (const)
292 structure or union.
293 265
294 @findex maybe_undef 266 @findex maybe_undef
295 @item maybe_undef 267 @item maybe_undef
296 268
297 When applied to a field, @code{maybe_undef} indicates that it's OK if 269 When applied to a field, @code{maybe_undef} indicates that it's OK if
349 PCH cannot handle data structures that depend on the absolute values 321 PCH cannot handle data structures that depend on the absolute values
350 of pointers. @code{reorder} functions can be expensive. When 322 of pointers. @code{reorder} functions can be expensive. When
351 possible, it is better to depend on properties of the data, like an ID 323 possible, it is better to depend on properties of the data, like an ID
352 number or the hash of a string instead. 324 number or the hash of a string instead.
353 325
354 @findex variable_size 326 @findex atomic
355 @item variable_size 327 @item atomic
356 328
357 The type machinery expects the types to be of constant size. When this 329 The @code{atomic} option can only be used with pointers. It informs
358 is not true, for example, with structs that have array fields or unions, 330 the GC machinery that the memory that the pointer points to does not
359 the type machinery cannot tell how many bytes need to be allocated at 331 contain any pointers, and hence it should be treated by the GC and PCH
360 each allocation. The @code{variable_size} is used to mark such types. 332 machinery as an ``atomic'' block of memory that does not need to be
361 The type machinery then provides allocators that take a parameter 333 examined when scanning memory for pointers. In particular, the
362 indicating an exact size of object being allocated. Note that the size 334 machinery will not scan that memory for pointers to mark them as
363 must be provided in bytes whereas the @code{length} option works with 335 reachable (when marking pointers for GC) or to relocate them (when
364 array lengths in number of elements. 336 writing a PCH file).
365 337
366 For example, 338 The @code{atomic} option differs from the @code{skip} option.
367 @smallexample 339 @code{atomic} keeps the memory under Garbage Collection, but makes the
368 struct GTY((variable_size)) sorted_fields_type @{ 340 GC ignore the contents of the memory. @code{skip} is more drastic in
369 int len; 341 that it causes the pointer and the memory to be completely ignored by
370 tree GTY((length ("%h.len"))) elts[1]; 342 the Garbage Collector. So, memory marked as @code{atomic} is
371 @}; 343 automatically freed when no longer reachable, while memory marked as
372 @end smallexample 344 @code{skip} is not.
373 345
374 Then the objects of @code{struct sorted_fields_type} are allocated in GC 346 The @code{atomic} option must be used with great care, because all
375 memory as follows: 347 sorts of problem can occur if used incorrectly, that is, if the memory
376 @smallexample 348 the pointer points to does actually contain a pointer.
377 field_vec = ggc_alloc_sorted_fields_type (size); 349
378 @end smallexample 350 Here is an example of how to use it:
379 351 @smallexample
380 If @var{field_vec->elts} stores @var{n} elements, then @var{size} 352 struct GTY(()) my_struct @{
381 could be calculated as follows: 353 int number_of_elements;
382 @smallexample 354 unsigned int * GTY ((atomic)) elements;
383 size_t size = sizeof (struct sorted_fields_type) + n * sizeof (tree); 355 @};
384 @end smallexample 356 @end smallexample
357 In this case, @code{elements} is a pointer under GC, and the memory it
358 points to needs to be allocated using the Garbage Collector, and will
359 be freed automatically by the Garbage Collector when it is no longer
360 referenced. But the memory that the pointer points to is an array of
361 @code{unsigned int} elements, and the GC must not try to scan it to
362 find pointers to mark or relocate, which is why it is marked with the
363 @code{atomic} option.
364
365 Note that, currently, global variables can not be marked with
366 @code{atomic}; only fields of a struct can. This is a known
367 limitation. It would be useful to be able to mark global pointers
368 with @code{atomic} to make the PCH machinery aware of them so that
369 they are saved and restored correctly to PCH files.
385 370
386 @findex special 371 @findex special
387 @item special ("@var{name}") 372 @item special ("@var{name}")
388 373
389 The @code{special} option is used to mark types that have to be dealt 374 The @code{special} option is used to mark types that have to be dealt
390 with by special case machinery. The parameter is the name of the 375 with by special case machinery. The parameter is the name of the
391 special case. See @file{gengtype.c} for further details. Avoid 376 special case. See @file{gengtype.c} for further details. Avoid
392 adding new special cases unless there is no other alternative. 377 adding new special cases unless there is no other alternative.
378
379 @findex user
380 @item user
381
382 The @code{user} option indicates that the code to mark structure
383 fields is completely handled by user-provided routines. See section
384 @ref{User GC} for details on what functions need to be provided.
393 @end table 385 @end table
386
387 @node Inheritance and GTY
388 @section Support for inheritance
389 gengtype has some support for simple class hierarchies. You can use
390 this to have gengtype autogenerate marking routines, provided:
391
392 @itemize @bullet
393 @item
394 There must be a concrete base class, with a discriminator expression
395 that can be used to identify which subclass an instance is.
396 @item
397 Only single inheritance is used.
398 @item
399 None of the classes within the hierarchy are templates.
400 @end itemize
401
402 If your class hierarchy does not fit in this pattern, you must use
403 @ref{User GC} instead.
404
405 The base class and its discriminator must be identified using the ``desc''
406 option. Each concrete subclass must use the ``tag'' option to identify
407 which value of the discriminator it corresponds to.
408
409 Every class in the hierarchy must have a @code{GTY(())} marker, as
410 gengtype will only attempt to parse classes that have such a marker
411 @footnote{Classes lacking such a marker will not be identified as being
412 part of the hierarchy, and so the marking routines will not handle them,
413 leading to a assertion failure within the marking routines due to an
414 unknown tag value (assuming that assertions are enabled).}.
415
416 @smallexample
417 class GTY((desc("%h.kind"), tag("0"))) example_base
418 @{
419 public:
420 int kind;
421 tree a;
422 @};
423
424 class GTY((tag("1"))) some_subclass : public example_base
425 @{
426 public:
427 tree b;
428 @};
429
430 class GTY((tag("2"))) some_other_subclass : public example_base
431 @{
432 public:
433 tree c;
434 @};
435 @end smallexample
436
437 The generated marking routines for the above will contain a ``switch''
438 on ``kind'', visiting all appropriate fields. For example, if kind is
439 2, it will cast to ``some_other_subclass'' and visit fields a, b, and c.
440
441 @node User GC
442 @section Support for user-provided GC marking routines
443 @cindex user gc
444 The garbage collector supports types for which no automatic marking
445 code is generated. For these types, the user is required to provide
446 three functions: one to act as a marker for garbage collection, and
447 two functions to act as marker and pointer walker for pre-compiled
448 headers.
449
450 Given a structure @code{struct GTY((user)) my_struct}, the following functions
451 should be defined to mark @code{my_struct}:
452
453 @smallexample
454 void gt_ggc_mx (my_struct *p)
455 @{
456 /* This marks field 'fld'. */
457 gt_ggc_mx (p->fld);
458 @}
459
460 void gt_pch_nx (my_struct *p)
461 @{
462 /* This marks field 'fld'. */
463 gt_pch_nx (tp->fld);
464 @}
465
466 void gt_pch_nx (my_struct *p, gt_pointer_operator op, void *cookie)
467 @{
468 /* For every field 'fld', call the given pointer operator. */
469 op (&(tp->fld), cookie);
470 @}
471 @end smallexample
472
473 In general, each marker @code{M} should call @code{M} for every
474 pointer field in the structure. Fields that are not allocated in GC
475 or are not pointers must be ignored.
476
477 For embedded lists (e.g., structures with a @code{next} or @code{prev}
478 pointer), the marker must follow the chain and mark every element in
479 it.
480
481 Note that the rules for the pointer walker @code{gt_pch_nx (my_struct
482 *, gt_pointer_operator, void *)} are slightly different. In this
483 case, the operation @code{op} must be applied to the @emph{address} of
484 every pointer field.
485
486 @subsection User-provided marking routines for template types
487 When a template type @code{TP} is marked with @code{GTY}, all
488 instances of that type are considered user-provided types. This means
489 that the individual instances of @code{TP} do not need to be marked
490 with @code{GTY}. The user needs to provide template functions to mark
491 all the fields of the type.
492
493 The following code snippets represent all the functions that need to
494 be provided. Note that type @code{TP} may reference to more than one
495 type. In these snippets, there is only one type @code{T}, but there
496 could be more.
497
498 @smallexample
499 template<typename T>
500 void gt_ggc_mx (TP<T> *tp)
501 @{
502 extern void gt_ggc_mx (T&);
503
504 /* This marks field 'fld' of type 'T'. */
505 gt_ggc_mx (tp->fld);
506 @}
507
508 template<typename T>
509 void gt_pch_nx (TP<T> *tp)
510 @{
511 extern void gt_pch_nx (T&);
512
513 /* This marks field 'fld' of type 'T'. */
514 gt_pch_nx (tp->fld);
515 @}
516
517 template<typename T>
518 void gt_pch_nx (TP<T *> *tp, gt_pointer_operator op, void *cookie)
519 @{
520 /* For every field 'fld' of 'tp' with type 'T *', call the given
521 pointer operator. */
522 op (&(tp->fld), cookie);
523 @}
524
525 template<typename T>
526 void gt_pch_nx (TP<T> *tp, gt_pointer_operator, void *cookie)
527 @{
528 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
529
530 /* For every field 'fld' of 'tp' with type 'T', call the pointer
531 walker for all the fields of T. */
532 gt_pch_nx (&(tp->fld), op, cookie);
533 @}
534 @end smallexample
535
536 Support for user-defined types is currently limited. The following
537 restrictions apply:
538
539 @enumerate
540 @item Type @code{TP} and all the argument types @code{T} must be
541 marked with @code{GTY}.
542
543 @item Type @code{TP} can only have type names in its argument list.
544
545 @item The pointer walker functions are different for @code{TP<T>} and
546 @code{TP<T *>}. In the case of @code{TP<T>}, references to
547 @code{T} must be handled by calling @code{gt_pch_nx} (which
548 will, in turn, walk all the pointers inside fields of @code{T}).
549 In the case of @code{TP<T *>}, references to @code{T *} must be
550 handled by calling the @code{op} function on the address of the
551 pointer (see the code snippets above).
552 @end enumerate
394 553
395 @node GGC Roots 554 @node GGC Roots
396 @section Marking Roots for the Garbage Collector 555 @section Marking Roots for the Garbage Collector
397 @cindex roots, marking 556 @cindex roots, marking
398 @cindex marking roots 557 @cindex marking roots
444 @code{GTFILES} variable in @file{Makefile.in}. 603 @code{GTFILES} variable in @file{Makefile.in}.
445 604
446 @item 605 @item
447 For files that are part of one front end, add the filename to the 606 For files that are part of one front end, add the filename to the
448 @code{gtfiles} variable defined in the appropriate 607 @code{gtfiles} variable defined in the appropriate
449 @file{config-lang.in}. For C, the file is @file{c-config-lang.in}. 608 @file{config-lang.in}.
450 Headers should appear before non-headers in this list. 609 Headers should appear before non-headers in this list.
451 610
452 @item 611 @item
453 For files that are part of some but not all front ends, add the 612 For files that are part of some but not all front ends, add the
454 filename to the @code{gtfiles} variable of @emph{all} the front ends 613 filename to the @code{gtfiles} variable of @emph{all} the front ends
493 @findex ggc_collect 652 @findex ggc_collect
494 653
495 The GCC garbage collector GGC is only invoked explicitly. In contrast 654 The GCC garbage collector GGC is only invoked explicitly. In contrast
496 with many other garbage collectors, it is not implicitly invoked by 655 with many other garbage collectors, it is not implicitly invoked by
497 allocation routines when a lot of memory has been consumed. So the 656 allocation routines when a lot of memory has been consumed. So the
498 only way to have GGC reclaim storage it to call the @code{ggc_collect} 657 only way to have GGC reclaim storage is to call the @code{ggc_collect}
499 function explicitly. This call is an expensive operation, as it may 658 function explicitly. This call is an expensive operation, as it may
500 have to scan the entire heap. Beware that local variables (on the GCC 659 have to scan the entire heap. Beware that local variables (on the GCC
501 call stack) are not followed by such an invocation (as many other 660 call stack) are not followed by such an invocation (as many other
502 garbage collectors do): you should reference all your data from static 661 garbage collectors do): you should reference all your data from static
503 or external @code{GTY}-ed variables, and it is advised to call 662 or external @code{GTY}-ed variables, and it is advised to call