GCC Middle and Back End API Reference
ggc-common.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "timevar.h"
#include "diagnostic-core.h"
#include "ggc-internal.h"
#include "hosthooks.h"
#include "plugin.h"
#include "options.h"
Include dependency graph for ggc-common.cc:

Data Structures

struct  ptr_data
struct  saving_hasher
struct  traversal_state
struct  mmap_info
class  ggc_usage

Macros

#define INCLUDE_MALLOC_H
#define POINTER_HASH(x)

Typedefs

typedef const struct ggc_root_tabconst_ggc_root_tab_t

Functions

static int compare_ptr_data (const void *, const void *)
static void relocate_ptrs (void *, void *, void *)
static void write_pch_globals (const struct ggc_root_tab *const *tab, struct traversal_state *state)
void ggc_register_root_tab (const struct ggc_root_tab *rt)
static void ggc_mark_root_tab (const_ggc_root_tab_t rt)
static void ggc_zero_rtab_roots (const_ggc_root_tab_t rt)
void ggc_mark_roots (void)
void * ggc_internal_cleared_alloc (size_t size, void(*f)(void *), size_t s, size_t n MEM_STAT_DECL)
void * ggc_internal_cleared_alloc_no_dtor (size_t size, void(*f)(void *), size_t s, size_t n MEM_STAT_DECL)
void * ggc_realloc (void *x, size_t size MEM_STAT_DECL)
void * ggc_cleared_alloc_htab_ignore_args (size_t c, size_t n)
void * ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
void * ggc_splay_alloc (int sz, void *nl)
void ggc_splay_dont_free (void *x, void *nl)
void ggc_print_common_statistics (FILE *stream, ggc_statistics *stats)
int gt_pch_note_object (void *obj, void *note_ptr_cookie, gt_note_pointers note_ptr_fn, size_t length_override)
void gt_pch_note_callback (void *obj, void *base)
void gt_pch_note_reorder (void *obj, void *note_ptr_cookie, gt_handle_reorder reorder_fn)
int ggc_call_count (ptr_data **slot, traversal_state *state)
int ggc_call_alloc (ptr_data **slot, traversal_state *state)
static int compare_ptr (const void *p1_p, const void *p2_p)
static unsigned char * read_uleb128 (unsigned char *p, size_t *val)
static size_t write_uleb128 (unsigned char *p, size_t val)
void gt_pch_save (FILE *f)
void gt_pch_restore (FILE *f)
void * default_gt_pch_get_address (size_t size, int fd)
int default_gt_pch_use_address (void *&base, size_t size, int fd, size_t offset)
size_t default_gt_pch_alloc_granularity (void)
static double ggc_rlimit_bound (double limit)
static int ggc_min_expand_heuristic (void)
static int ggc_min_heapsize_heuristic (void)
void init_ggc_heuristics (void)
void dump_ggc_loc_statistics ()
void ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL)
void ggc_free_overhead (void *ptr)
void ggc_prune_overhead_list (void)
void report_heap_memory_use ()
void ggc_common_finalize ()

Variables

bool ggc_protect_identifiers = true
static ggc_statisticsggc_stats
static vec< const_ggc_root_tab_textra_root_vec
static hash_table< saving_hasher > * saving_htab
static vec< void * > callback_vec
static vec< void * > reloc_addrs_vec
static mem_alloc_description< ggc_usageggc_mem_desc

Macro Definition Documentation

◆ INCLUDE_MALLOC_H

#define INCLUDE_MALLOC_H
Simple garbage collection for the GNU compiler.
   Copyright (C) 1999-2026 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.   
Generic garbage collection (GC) functions and data, not specific to
any particular GC implementation.   

◆ POINTER_HASH

#define POINTER_HASH ( x)

Typedef Documentation

◆ const_ggc_root_tab_t

typedef const struct ggc_root_tab* const_ggc_root_tab_t
Maintain global roots that are preserved during GC.   
This extra vector of dynamically registered root_tab-s is used by
ggc_mark_roots and gives the ability to dynamically add new GGC root
tables, for instance from some plugins; this vector is on the heap
since it is used by GGC internally.   

Function Documentation

◆ compare_ptr()

int compare_ptr ( const void * p1_p,
const void * p2_p )
static

◆ compare_ptr_data()

int compare_ptr_data ( const void * p1_p,
const void * p2_p )
static
Callback for qsort.   

References ptr_data::new_addr.

Referenced by gt_pch_save().

◆ default_gt_pch_alloc_granularity()

size_t default_gt_pch_alloc_granularity ( void )
Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS.   Return the
alignment required for allocating virtual memory. Usually this is the
same as pagesize.   

◆ default_gt_pch_get_address()

void * default_gt_pch_get_address ( size_t size,
int fd )
Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
Select no address whatsoever, and let gt_pch_save choose what it will with
malloc, presumably.   

References NULL.

◆ default_gt_pch_use_address()

int default_gt_pch_use_address ( void *& base,
size_t size,
int fd,
size_t offset )
Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
Allocate SIZE bytes with malloc.  Return 0 if the address we got is the
same as base, indicating that the memory has been allocated but needs to
be read in from the file.  Return -1 if the address differs, to relocation
of the PCH file would be required.   

◆ dump_ggc_loc_statistics()

void dump_ggc_loc_statistics ( )
Dump per-site memory statistics.   

References GGC_COLLECT_FORCE, ggc_mem_desc, and GGC_ORIGIN.

Referenced by dump_memory_report().

◆ ggc_call_alloc()

int ggc_call_alloc ( ptr_data ** slot,
traversal_state * state )

◆ ggc_call_count()

int ggc_call_count ( ptr_data ** slot,
traversal_state * state )
Callbacks for htab_traverse.   

References ggc_pch_count_object(), ptr_data::obj, and ptr_data::size.

Referenced by gt_pch_save().

◆ ggc_cleared_alloc_htab_ignore_args()

void * ggc_cleared_alloc_htab_ignore_args ( size_t c,
size_t n )

References gcc_assert, and ggc_cleared_alloc().

◆ ggc_cleared_alloc_ptr_array_two_args()

void * ggc_cleared_alloc_ptr_array_two_args ( size_t c,
size_t n )
TODO: once we actually use type information in GGC, create a new tag
gt_gcc_ptr_array and use it for pointer arrays.   

References gcc_assert, and ggc_cleared_vec_alloc().

◆ ggc_common_finalize()

void ggc_common_finalize ( )

◆ ggc_free_overhead()

void ggc_free_overhead ( void * ptr)
Notice that the pointer has been freed.   

References ggc_mem_desc.

Referenced by ggc_free().

◆ ggc_internal_cleared_alloc()

void * ggc_internal_cleared_alloc ( size_t size,
void(* )(void *),
size_t s,
size_t n MEM_STAT_DECL )
Allocate a block of memory, then clear it.   

References ggc_internal_alloc(), MEM_STAT_DECL, and PASS_MEM_STAT.

Referenced by ggc_internal_cleared_alloc_no_dtor().

◆ ggc_internal_cleared_alloc_no_dtor()

void * ggc_internal_cleared_alloc_no_dtor ( size_t size,
void(* )(void *),
size_t s,
size_t n MEM_STAT_DECL )

◆ ggc_mark_root_tab()

void ggc_mark_root_tab ( const_ggc_root_tab_t rt)
static
Mark all the roots in the table RT.   

References ggc_root_tab::base, ggc_root_tab::cb, i, ggc_root_tab::nelt, NULL, and ggc_root_tab::stride.

Referenced by ggc_mark_roots().

◆ ggc_mark_roots()

void ggc_mark_roots ( void )
Iterate through all registered roots and mark each element.   
Garbage collection for the GNU compiler.  Internal definitions
   for ggc-*.c and stringpool.cc.

   Copyright (C) 2009-2026 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.   
Call ggc_set_mark on all the roots.   

References ggc_root_tab::base, extra_root_vec, FOR_EACH_VEC_ELT, ggc_mark_root_tab(), ggc_mark_stringpool(), ggc_protect_identifiers, ggc_purge_stringpool(), gt_clear_caches(), gt_ggc_deletable_rtab, gt_ggc_rtab, i, invoke_plugin_callbacks(), ggc_root_tab::nelt, NULL, and ggc_root_tab::stride.

Referenced by ggc_collect().

◆ ggc_min_expand_heuristic()

int ggc_min_expand_heuristic ( void )
static
Heuristic to set a default for GGC_MIN_EXPAND.   

References ggc_rlimit_bound(), MIN, and ONE_G.

Referenced by ggc_min_heapsize_heuristic(), and init_ggc_heuristics().

◆ ggc_min_heapsize_heuristic()

int ggc_min_heapsize_heuristic ( void )
static
Heuristic to set a default for GGC_MIN_HEAPSIZE.   

References ggc_min_expand_heuristic(), ggc_rlimit_bound(), MAX, MIN, and ONE_K.

Referenced by init_ggc_heuristics().

◆ ggc_print_common_statistics()

void ggc_print_common_statistics ( FILE * stream,
ggc_statistics * stats )
Used by the various collectors to gather and print statistics that
do not depend on the collector in use.   

References ggc_stats, and NULL.

Referenced by ggc_print_statistics().

◆ ggc_prune_overhead_list()

void ggc_prune_overhead_list ( void )
After live values has been marked, walk all recorded pointers and see if
they are still live.   

References ggc_marked_p(), and ggc_mem_desc.

Referenced by ggc_collect().

◆ ggc_realloc()

void * ggc_realloc ( void * x,
size_t size MEM_STAT_DECL )
Resize a block of memory, possibly re-allocating it.   

References ggc_free(), ggc_get_size(), ggc_internal_alloc(), MEM_STAT_DECL, NULL, PASS_MEM_STAT, r, and VALGRIND_DISCARD.

Referenced by grow_tree_vec(), and realloc_for_line_map().

◆ ggc_record_overhead()

void ggc_record_overhead ( size_t allocated,
size_t overhead,
void *ptr MEM_STAT_DECL )
Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION).   

References FINAL_PASS_MEM_STAT, ggc_mem_desc, GGC_ORIGIN, MEM_STAT_DECL, and usage().

Referenced by ggc_internal_alloc().

◆ ggc_register_root_tab()

void ggc_register_root_tab ( const struct ggc_root_tab * rt)
Dynamically register a new GGC root table RT. This is useful for
plugins.  
Register an additional root table.  This can be useful for some
plugins.  Does nothing if the passed pointer is NULL.  

References extra_root_vec.

Referenced by register_callback().

◆ ggc_rlimit_bound()

double ggc_rlimit_bound ( double limit)
static
Modify the bound based on rlimits.   

References ONE_M.

Referenced by ggc_min_expand_heuristic(), and ggc_min_heapsize_heuristic().

◆ ggc_splay_alloc()

void * ggc_splay_alloc ( int sz,
void * nl )
These are for splay_tree_new_ggc.   

References gcc_assert, and ggc_internal_alloc().

◆ ggc_splay_dont_free()

void ggc_splay_dont_free ( void * x,
void * nl )

References gcc_assert.

◆ ggc_zero_rtab_roots()

void ggc_zero_rtab_roots ( const_ggc_root_tab_t rt)
static
Zero out all the roots in the table RT.   

References ggc_root_tab::base, i, ggc_root_tab::nelt, NULL, and ggc_root_tab::stride.

Referenced by ggc_common_finalize().

◆ gt_pch_note_callback()

void gt_pch_note_callback ( void * obj,
void * base )
Register address of a callback pointer.   
Used by the gt_pch_p_* routines.  Register address of a callback
pointer.   

References callback_vec, gcc_assert, NULL, ptr_data::obj, POINTER_HASH, and saving_htab.

◆ gt_pch_note_object()

int gt_pch_note_object ( void * obj,
void * note_ptr_cookie,
gt_note_pointers note_ptr_fn,
size_t length_override = (size_t) -1 )
Register an object in the hash table.   
Used by the gt_pch_n_* routines.  Register an object in the hash table.   

References gcc_assert, ggc_get_size(), gt_pch_p_S(), ptr_data::note_ptr_cookie, ptr_data::note_ptr_fn, NULL, ptr_data::obj, POINTER_HASH, and saving_htab.

Referenced by gt_pch_n_S(), gt_pch_n_S2(), and gt_pch_nx().

◆ gt_pch_note_reorder()

void gt_pch_note_reorder ( void * obj,
void * note_ptr_cookie,
gt_handle_reorder reorder_fn )
Register an object in the hash table.   
Used by the gt_pch_n_* routines.  Register that an object has a reorder
function.   

References gcc_assert, gcc_checking_assert, gt_pch_p_S(), ptr_data::note_ptr_cookie, NULL, ptr_data::obj, POINTER_HASH, ptr_data::reorder_fn, and saving_htab.

◆ gt_pch_restore()

◆ gt_pch_save()

◆ init_ggc_heuristics()

void init_ggc_heuristics ( void )
Heuristics.   

References ggc_min_expand_heuristic(), and ggc_min_heapsize_heuristic().

Referenced by toplev::main().

◆ read_uleb128()

unsigned char * read_uleb128 ( unsigned char * p,
size_t * val )
static
Decode one uleb128 from P, return first byte after it, store
decoded value into *VAL.   

References shift.

Referenced by gt_pch_restore().

◆ relocate_ptrs()

void relocate_ptrs ( void * ptr_p,
void * real_ptr_p,
void * state_p )
static
Callbacks for note_ptr_fn.   

References gcc_assert, ptr_data::new_addr, NULL, POINTER_HASH, reloc_addrs_vec, and saving_htab.

Referenced by gt_pch_save().

◆ report_heap_memory_use()

void report_heap_memory_use ( void )
Print memory used by heap if this info is available.   
Report current heap memory use to stderr.   

References PRsa, and SIZE_AMOUNT.

Referenced by execute_one_pass(), ipa_read_optimization_summaries_1(), and ipa_read_summaries_1().

◆ write_pch_globals()

void write_pch_globals ( const struct ggc_root_tab *const * tab,
struct traversal_state * state )
static
Write out, after relocation, the pointers in TAB.   

References ggc_root_tab::base, fatal_error(), i, input_location, ggc_root_tab::nelt, ptr_data::new_addr, NULL, POINTER_HASH, saving_htab, and ggc_root_tab::stride.

Referenced by gt_pch_save().

◆ write_uleb128()

size_t write_uleb128 ( unsigned char * p,
size_t val )
static
Store VAL as uleb128 at P, return length in bytes.   

Referenced by gt_pch_save().

Variable Documentation

◆ callback_vec

vec<void *> callback_vec
static

◆ extra_root_vec

vec<const_ggc_root_tab_t> extra_root_vec
static

◆ ggc_mem_desc

◆ ggc_protect_identifiers

bool ggc_protect_identifiers = true
When true, protect the contents of the identifier hash table.   
When true, identifier nodes are considered as GC roots.  When
false, identifier nodes are treated like any other GC-allocated
object, and the identifier hash table is treated as a weak
hash.   

Referenced by compile_file(), do_compile(), and ggc_mark_roots().

◆ ggc_stats

ggc_statistics* ggc_stats
static
Statistics about the allocation.   

Referenced by ggc_print_common_statistics().

◆ reloc_addrs_vec

vec<void *> reloc_addrs_vec
static

Referenced by gt_pch_save(), and relocate_ptrs().

◆ saving_htab