guile/libguile/gc.c

828 lines
22 KiB
C
Raw Normal View History

/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/* #define DEBUGINFO */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
* gc.c: (scm_gc_mark_dependencies): new function. like `scm_gc_mark', but doesn't mark the argument itself. defined using an arrangement similar to that in eval.c: `scm_gc_mark' and `scm_gc_mark_dependencies' are derived from the same "template" by ugly preprocessor magic. * gc.h: added prototype for `scm_gc_mark_dependencies'. * init.c (scm_init_guile_1): call the renamed `scm_init_guardians'. * guardians.h: changed prototypes for `scm_make_guardian' and `scm_init_guardians'. * guardians.c (guardian_t): added new fields `greedy_p' and `listed_p'. (GUARDIAN_P): predicate that says whether its argument is a guardian. (GUARDIAN_GREEDY_P, GUARDIAN_LISTED_P): new predicates. (greedy_guardians, sharing_guardians): new variables. hold the greedy and sharing live guardian lists, respectively. (first_live_guardian, current_link_field): removed. (greedily_guarded_prop): new variable. holds the "is greedily guarded" object property. (self_centered_zombies): new variable. stores guarded objects that are parts of cycles. (add_to_live_list): new function, introduced to decouple marking a guardian and adding it to the live list. (guardian_mark): call `add_to_live_list'. (guardian_print): print whether the guardian is greedy or not. also change "live" and "zombie" to "reachable" and "unreachable" respectively, to be less confusing. (scm_guard): if the guardian is greedy, test whether the object is already greedily marked. throw an error if so. (scm_get_one_zombie): if the guardian is greedy, remove the "greedily guarded" property from the object. (scm_make_guardian): add a new optional boolean argument which says whether the guardian is greedy or sharing. (guardian_gc_init): init the new live lists. (mark_dependencies): new function. (mark_and_zombify): new function. (guardian_zombify): reworked to support the new guardian semantics. move some logic to `mark_dependencies' and `mark_and_zombify'. (whine_about_self_centered_zombies): new function. installed in the `after-gc-hook' to complain about guarded objects which are parts of cycles. (scm_init_guardians): init the new stuff. renamed from `scm_init_guardian'.
2000-12-23 23:00:23 +00:00
#include "libguile/gen-scmconfig.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef __ia64__
#include <ucontext.h>
extern unsigned long * __libc_ia64_register_backing_store_base;
#endif
#include "libguile/_scm.h"
#include "libguile/eval.h"
#include "libguile/stime.h"
#include "libguile/stackchk.h"
#include "libguile/struct.h"
#include "libguile/smob.h"
#include "libguile/arrays.h"
#include "libguile/async.h"
#include "libguile/ports.h"
#include "libguile/root.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/weaks.h"
#include "libguile/hashtab.h"
#include "libguile/tags.h"
2002-08-04 00:17:18 +00:00
#include "libguile/private-gc.h"
#include "libguile/validate.h"
* validate.h (SCM_NUM2{SIZE,PTRDIFF,SHORT,USHORT,BITS,UBITS,INT,UINT}[_DEF]): new macros. * unif.h: type renaming: scm_array -> scm_array_t scm_array_dim -> scm_array_dim_t the old names are deprecated, all in-Guile uses changed. * tags.h (scm_ubits_t): new typedef, representing unsigned scm_bits_t. * stacks.h: type renaming: scm_info_frame -> scm_info_frame_t scm_stack -> scm_stack_t the old names are deprecated, all in-Guile uses changed. * srcprop.h: type renaming: scm_srcprops -> scm_srcprops_t scm_srcprops_chunk -> scm_srcprops_chunk_t the old names are deprecated, all in-Guile uses changed. * gsubr.c, procs.c, print.c, ports.c, read.c, rdelim.c, ramap.c, rw.c, smob.c, sort.c, srcprop.c, stacks.c, strings.c, strop.c, strorder.c, strports.c, struct.c, symbols.c, unif.c, values.c, vectors.c, vports.c, weaks.c: various int/size_t -> size_t/scm_bits_t changes. * random.h: type renaming: scm_rstate -> scm_rstate_t scm_rng -> scm_rng_t scm_i_rstate -> scm_i_rstate_t the old names are deprecated, all in-Guile uses changed. * procs.h: type renaming: scm_subr_entry -> scm_subr_entry_t the old name is deprecated, all in-Guile uses changed. * options.h (scm_option_t.val): unsigned long -> scm_bits_t. type renaming: scm_option -> scm_option_t the old name is deprecated, all in-Guile uses changed. * objects.c: various long -> scm_bits_t changes. (scm_i_make_class_object): flags: unsigned long -> scm_ubits_t * numbers.h (SCM_FIXNUM_BIT): deprecated, renamed to SCM_I_FIXNUM_BIT. * num2integral.i.c: new file, multiply included by numbers.c, used to "templatize" the various integral <-> num conversion routines. * numbers.c (scm_mkbig, scm_big2num, scm_adjbig, scm_normbig, scm_copybig, scm_2ulong2big, scm_dbl2big, scm_big2dbl): deprecated. (scm_i_mkbig, scm_i_big2inum, scm_i_adjbig, scm_i_normbig, scm_i_copybig, scm_i_short2big, scm_i_ushort2big, scm_i_int2big, scm_i_uint2big, scm_i_long2big, scm_i_ulong2big, scm_i_bits2big, scm_i_ubits2big, scm_i_size2big, scm_i_ptrdiff2big, scm_i_long_long2big, scm_i_ulong_long2big, scm_i_dbl2big, scm_i_big2dbl, scm_short2num, scm_ushort2num, scm_int2num, scm_uint2num, scm_bits2num, scm_ubits2num, scm_size2num, scm_ptrdiff2num, scm_num2short, scm_num2ushort, scm_num2int, scm_num2uint, scm_num2bits, scm_num2ubits, scm_num2ptrdiff, scm_num2size): new functions. * modules.c (scm_module_reverse_lookup): i, n: int -> scm_bits_t.x * load.c: change int -> size_t in various places (where the variable is used to store a string length). (search-path): call scm_done_free, not scm_done_malloc. * list.c (scm_ilength): return a scm_bits_t, not long. some other {int,long} -> scm_bits_t changes. * hashtab.c: various [u]int -> scm_bits_t changes. scm_ihashx_closure -> scm_ihashx_closure_t (and made a typedef). (scm_ihashx): n: uint -> scm_bits_t use scm_bits2num instead of scm_ulong2num. * gsubr.c: various int -> scm_bits_t changes. * gh_data.c (gh_scm2double): no loss of precision any more. * gh.h (gh_str2scm): len: int -> size_t (gh_{get,set}_substr): start: int -> scm_bits_t, len: int -> size_t (gh_<num>2scm): n: int -> scm_bits_t (gh_*vector_length): return scm_[u]size_t, not unsigned long. (gh_length): return scm_bits_t, not unsigned long. * fports.h: type renaming: scm_fport -> scm_fport_t the old name is deprecated, all in-Guile uses changed. * fports.c (fport_fill_input): count: int -> scm_bits_t (fport_flush): init_size, remaining, count: int -> scm_bits_t * debug.h (scm_lookup_cstr, scm_lookup_soft, scm_evstr): removed those prototypes, as the functions they prototype don't exist. * fports.c (default_buffer_size): int -> size_t (scm_fport_buffer_add): read_size, write_size: int -> scm_bits_t default_size: int -> size_t (scm_setvbuf): csize: int -> scm_bits_t * fluids.c (n_fluids): int -> scm_bits_t (grow_fluids): old_length, i: int -> scm_bits_t (next_fluid_num, scm_fluid_ref, scm_fluid_set_x): n: int -> scm_bits_t (scm_c_with_fluids): flen, vlen: int -> scm_bits_t * filesys.c (s_scm_open_fdes): changed calls to SCM_NUM2LONG to the new and shiny SCM_NUM2INT. * extensions.c: extension -> extension_t (and made a typedef). * eval.h (SCM_IFRAME): cast to scm_bits_t, not int. just so there are no nasty surprises if/when the various deeply magic tag bits move somewhere else. * eval.c: changed the locals used to store results of SCM_IFRAME, scm_ilength and such to be of type scm_bits_t (and not int/long). (iqq): depth, edepth: int -> scm_bits_t (scm_eval_stack): int -> scm_bits_t (SCM_CEVAL): various vars are not scm_bits_t instead of int. (check_map_args, scm_map, scm_for_each): len: long -> scm_bits_t i: int -> scm_bits_t * environments.c: changed the many calls to scm_ulong2num to scm_ubits2num. (import_environment_fold): proc_as_ul: ulong -> scm_ubits_t * dynwind.c (scm_dowinds): delta: long -> scm_bits_t * debug.h: type renaming: scm_debug_info -> scm_debug_info_t scm_debug_frame -> scm_debug_frame_t the old names are deprecated, all in-Guile uses changed. (scm_debug_eframe_size): int -> scm_bits_t * debug.c (scm_init_debug): use scm_c_define instead of the deprecated scm_define. * continuations.h: type renaming: scm_contregs -> scm_contregs_t the old name is deprecated, all in-Guile uses changed. (scm_contregs_t.num_stack_items): size_t -> scm_bits_t (scm_contregs_t.num_stack_items): ulong -> scm_ubits_t * continuations.c (scm_make_continuation): change the type of stack_size form long to scm_bits_t. * ports.h: type renaming: scm_port_rw_active -> scm_port_rw_active_t (and made a typedef) scm_port -> scm_port_t scm_ptob_descriptor -> scm_ptob_descriptor_t the old names are deprecated, all in-Guile uses changed. (scm_port_t.entry): int -> scm_bits_t. (scm_port_t.line_number): int -> long. (scm_port_t.putback_buf_size): int -> size_t. * __scm.h (long_long, ulong_long): deprecated (they pollute the global namespace and have little value besides that). (SCM_BITS_LENGTH): new, is the bit size of scm_bits_t (i.e. of an SCM handle). (ifdef spaghetti): include sys/types.h and sys/stdtypes.h, if they exist (for size_t & ptrdiff_t) (scm_sizet): deprecated. * Makefile.am (noinst_HEADERS): add num2integral.i.c
2001-05-24 00:50:51 +00:00
#include "libguile/deprecation.h"
#include "libguile/gc.h"
2005-03-02 20:42:01 +00:00
#include "libguile/dynwind.h"
#include "libguile/bdw-gc.h"
/* For GC_set_start_callback. */
#include <gc/gc_mark.h>
#ifdef GUILE_DEBUG_MALLOC
#include "libguile/debug-malloc.h"
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Set this to != 0 if every cell that is accessed shall be checked:
*/
int scm_debug_cell_accesses_p = 0;
int scm_expensive_debug_cell_accesses_p = 0;
/* Set this to 0 if no additional gc's shall be performed, otherwise set it to
* the number of cell accesses after which a gc shall be called.
*/
int scm_debug_cells_gc_interval = 0;
/* Hash table that keeps a reference to objects the user wants to protect from
garbage collection. It could arguably be private but applications have come
to rely on it (e.g., Lilypond 2.13.9). */
SCM scm_protects;
decruftify scm_sys_protects * libguile/root.h * libguile/root.c (scm_sys_protects): It used to be that for some reason we'd define a special array of "protected" values. This was a little silly, always, but with the BDW GC it's completely unnecessary. Also many of these variables were unused, and none of them were good API. So remove this array, and either eliminate, make static, or make internal the various values. * libguile/snarf.h: No need to generate calls to scm_permanent_object. * guile-readline/readline.c (scm_init_readline): No need to call scm_permanent_object. * libguile/array-map.c (ramap, rafe): Remove the dubious nullvect optimizations. * libguile/async.c (scm_init_async): No need to init scm_asyncs, it is no more. * libguile/eval.c (scm_init_eval): No need to init scm_listofnull, it is no more. * libguile/gc.c: Make scm_protects a static var. (scm_storage_prehistory): Change the sanity check to use the address of protects. (scm_init_gc_protect_object): No need to clear the scm_sys_protects, as it is no more. * libguile/keywords.c: Make the keyword obarray a static var. * libguile/numbers.c: Make flo0 a static var. * libguile/objprop.c: Make object_whash a static var. * libguile/properties.c: Make properties_whash a static var. * libguile/srcprop.h: * libguile/srcprop.c: Make scm_source_whash a global with internal linkage. * libguile/strings.h: * libguile/strings.c: Make scm_nullstr a global with internal linkage. * libguile/vectors.c (scm_init_vectors): No need to init scm_nullvect, it's unused.
2009-12-05 12:38:32 +01:00
#if (SCM_DEBUG_CELL_ACCESSES == 1)
/*
Assert that the given object is a valid reference to a valid cell. This
test involves to determine whether the object is a cell pointer, whether
this pointer actually points into a heap segment and whether the cell
pointed to is not a free cell. Further, additional garbage collections may
get executed after a user defined number of cell accesses. This helps to
find places in the C code where references are dropped for extremely short
periods.
*/
void
scm_i_expensive_validation_check (SCM cell)
{
/* If desired, perform additional garbage collections after a user
* defined number of cell accesses.
*/
if (scm_debug_cells_gc_interval)
{
static unsigned int counter = 0;
if (counter != 0)
{
--counter;
}
else
{
counter = scm_debug_cells_gc_interval;
scm_gc ();
}
}
}
/* Whether cell validation is already running. */
static int scm_i_cell_validation_already_running = 0;
void
scm_assert_cell_valid (SCM cell)
{
if (!scm_i_cell_validation_already_running && scm_debug_cell_accesses_p)
{
scm_i_cell_validation_already_running = 1; /* set to avoid recursion */
2002-08-04 00:17:18 +00:00
/*
During GC, no user-code should be run, and the guile core
should use non-protected accessors.
*/
2002-08-04 00:17:18 +00:00
if (scm_gc_running_p)
return;
2002-08-04 00:17:18 +00:00
/*
Only scm_in_heap_p and rescanning the heap is wildly
expensive.
*/
if (scm_expensive_debug_cell_accesses_p)
scm_i_expensive_validation_check (cell);
scm_i_cell_validation_already_running = 0; /* re-enable */
}
}
SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
(SCM flag),
"If @var{flag} is @code{#f}, cell access checking is disabled.\n"
"If @var{flag} is @code{#t}, cheap cell access checking is enabled,\n"
"but no additional calls to garbage collection are issued.\n"
"If @var{flag} is a number, strict cell access checking is enabled,\n"
"with an additional garbage collection after the given\n"
"number of cell accesses.\n"
"This procedure only exists when the compile-time flag\n"
"@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
#define FUNC_NAME s_scm_set_debug_cell_accesses_x
{
if (scm_is_false (flag))
{
scm_debug_cell_accesses_p = 0;
}
else if (scm_is_eq (flag, SCM_BOOL_T))
{
scm_debug_cells_gc_interval = 0;
scm_debug_cell_accesses_p = 1;
scm_expensive_debug_cell_accesses_p = 0;
}
else
{
scm_debug_cells_gc_interval = scm_to_signed_integer (flag, 0, INT_MAX);
scm_debug_cell_accesses_p = 1;
scm_expensive_debug_cell_accesses_p = 1;
}
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
2002-08-04 00:17:18 +00:00
#endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
/* Hooks. */
scm_t_c_hook scm_before_gc_c_hook;
scm_t_c_hook scm_before_mark_c_hook;
scm_t_c_hook scm_before_sweep_c_hook;
scm_t_c_hook scm_after_sweep_c_hook;
scm_t_c_hook scm_after_gc_c_hook;
/* GC Statistics Keeping
*/
unsigned long scm_gc_ports_collected = 0;
static unsigned long protected_obj_count = 0;
2002-08-04 14:09:14 +00:00
SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
SCM_SYMBOL (sym_heap_size, "heap-size");
SCM_SYMBOL (sym_heap_free_size, "heap-free-size");
SCM_SYMBOL (sym_heap_total_allocated, "heap-total-allocated");
SCM_SYMBOL (sym_heap_allocated_since_gc, "heap-allocated-since-gc");
SCM_SYMBOL (sym_protected_objects, "protected-objects");
SCM_SYMBOL (sym_times, "gc-times");
/* Number of calls to SCM_NEWCELL since startup. */
2002-08-04 00:17:18 +00:00
unsigned scm_newcell_count;
unsigned scm_newcell2_count;
/* {Scheme Interface to GC}
*/
static SCM
tag_table_to_type_alist (void *closure, SCM key, SCM val, SCM acc)
{
if (scm_is_integer (key))
{
int c_tag = scm_to_int (key);
char const * name = scm_i_tag_name (c_tag);
if (name != NULL)
{
key = scm_from_locale_string (name);
}
else
{
char s[100];
sprintf (s, "tag %d", c_tag);
key = scm_from_locale_string (s);
}
}
return scm_cons (scm_cons (key, val), acc);
}
SCM_DEFINE (scm_gc_live_object_stats, "gc-live-object-stats", 0, 0, 0,
(),
"Return an alist of statistics of the current live objects. ")
#define FUNC_NAME s_scm_gc_live_object_stats
{
SCM tab = scm_make_hash_table (scm_from_int (57));
SCM alist;
alist
= scm_internal_hash_fold (&tag_table_to_type_alist, NULL, SCM_EOL, tab);
return alist;
}
#undef FUNC_NAME
2002-08-04 14:09:14 +00:00
extern int scm_gc_malloc_yield_percentage;
SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
(),
"Return an association list of statistics about Guile's current\n"
2002-08-04 00:17:18 +00:00
"use of storage.\n")
#define FUNC_NAME s_scm_gc_stats
{
SCM answer;
size_t heap_size, free_bytes, bytes_since_gc, total_bytes;
size_t gc_times;
heap_size = GC_get_heap_size ();
free_bytes = GC_get_free_bytes ();
bytes_since_gc = GC_get_bytes_since_gc ();
total_bytes = GC_get_total_bytes ();
gc_times = GC_gc_no;
answer =
scm_list_n (scm_cons (sym_gc_time_taken, SCM_INUM0),
scm_cons (sym_heap_size, scm_from_size_t (heap_size)),
scm_cons (sym_heap_free_size, scm_from_size_t (free_bytes)),
scm_cons (sym_heap_total_allocated,
scm_from_size_t (total_bytes)),
scm_cons (sym_heap_allocated_since_gc,
scm_from_size_t (bytes_since_gc)),
scm_cons (sym_protected_objects,
scm_from_ulong (protected_obj_count)),
scm_cons (sym_times, scm_from_size_t (gc_times)),
SCM_UNDEFINED);
2002-08-04 00:17:18 +00:00
return answer;
}
2002-08-04 00:17:18 +00:00
#undef FUNC_NAME
SCM_DEFINE (scm_gc_dump, "gc-dump", 0, 0, 0,
(void),
"Dump information about the garbage collector's internal data "
"structures and memory usage to the standard output.")
#define FUNC_NAME s_scm_gc_dump
{
GC_dump ();
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
2002-08-04 00:17:18 +00:00
SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
(SCM obj),
"Return an integer that for the lifetime of @var{obj} is uniquely\n"
"returned by this function for @var{obj}")
#define FUNC_NAME s_scm_object_address
{
return scm_from_ulong (SCM_UNPACK (obj));
}
2002-08-04 00:17:18 +00:00
#undef FUNC_NAME
* validate.h (SCM_NUM2{SIZE,PTRDIFF,SHORT,USHORT,BITS,UBITS,INT,UINT}[_DEF]): new macros. * unif.h: type renaming: scm_array -> scm_array_t scm_array_dim -> scm_array_dim_t the old names are deprecated, all in-Guile uses changed. * tags.h (scm_ubits_t): new typedef, representing unsigned scm_bits_t. * stacks.h: type renaming: scm_info_frame -> scm_info_frame_t scm_stack -> scm_stack_t the old names are deprecated, all in-Guile uses changed. * srcprop.h: type renaming: scm_srcprops -> scm_srcprops_t scm_srcprops_chunk -> scm_srcprops_chunk_t the old names are deprecated, all in-Guile uses changed. * gsubr.c, procs.c, print.c, ports.c, read.c, rdelim.c, ramap.c, rw.c, smob.c, sort.c, srcprop.c, stacks.c, strings.c, strop.c, strorder.c, strports.c, struct.c, symbols.c, unif.c, values.c, vectors.c, vports.c, weaks.c: various int/size_t -> size_t/scm_bits_t changes. * random.h: type renaming: scm_rstate -> scm_rstate_t scm_rng -> scm_rng_t scm_i_rstate -> scm_i_rstate_t the old names are deprecated, all in-Guile uses changed. * procs.h: type renaming: scm_subr_entry -> scm_subr_entry_t the old name is deprecated, all in-Guile uses changed. * options.h (scm_option_t.val): unsigned long -> scm_bits_t. type renaming: scm_option -> scm_option_t the old name is deprecated, all in-Guile uses changed. * objects.c: various long -> scm_bits_t changes. (scm_i_make_class_object): flags: unsigned long -> scm_ubits_t * numbers.h (SCM_FIXNUM_BIT): deprecated, renamed to SCM_I_FIXNUM_BIT. * num2integral.i.c: new file, multiply included by numbers.c, used to "templatize" the various integral <-> num conversion routines. * numbers.c (scm_mkbig, scm_big2num, scm_adjbig, scm_normbig, scm_copybig, scm_2ulong2big, scm_dbl2big, scm_big2dbl): deprecated. (scm_i_mkbig, scm_i_big2inum, scm_i_adjbig, scm_i_normbig, scm_i_copybig, scm_i_short2big, scm_i_ushort2big, scm_i_int2big, scm_i_uint2big, scm_i_long2big, scm_i_ulong2big, scm_i_bits2big, scm_i_ubits2big, scm_i_size2big, scm_i_ptrdiff2big, scm_i_long_long2big, scm_i_ulong_long2big, scm_i_dbl2big, scm_i_big2dbl, scm_short2num, scm_ushort2num, scm_int2num, scm_uint2num, scm_bits2num, scm_ubits2num, scm_size2num, scm_ptrdiff2num, scm_num2short, scm_num2ushort, scm_num2int, scm_num2uint, scm_num2bits, scm_num2ubits, scm_num2ptrdiff, scm_num2size): new functions. * modules.c (scm_module_reverse_lookup): i, n: int -> scm_bits_t.x * load.c: change int -> size_t in various places (where the variable is used to store a string length). (search-path): call scm_done_free, not scm_done_malloc. * list.c (scm_ilength): return a scm_bits_t, not long. some other {int,long} -> scm_bits_t changes. * hashtab.c: various [u]int -> scm_bits_t changes. scm_ihashx_closure -> scm_ihashx_closure_t (and made a typedef). (scm_ihashx): n: uint -> scm_bits_t use scm_bits2num instead of scm_ulong2num. * gsubr.c: various int -> scm_bits_t changes. * gh_data.c (gh_scm2double): no loss of precision any more. * gh.h (gh_str2scm): len: int -> size_t (gh_{get,set}_substr): start: int -> scm_bits_t, len: int -> size_t (gh_<num>2scm): n: int -> scm_bits_t (gh_*vector_length): return scm_[u]size_t, not unsigned long. (gh_length): return scm_bits_t, not unsigned long. * fports.h: type renaming: scm_fport -> scm_fport_t the old name is deprecated, all in-Guile uses changed. * fports.c (fport_fill_input): count: int -> scm_bits_t (fport_flush): init_size, remaining, count: int -> scm_bits_t * debug.h (scm_lookup_cstr, scm_lookup_soft, scm_evstr): removed those prototypes, as the functions they prototype don't exist. * fports.c (default_buffer_size): int -> size_t (scm_fport_buffer_add): read_size, write_size: int -> scm_bits_t default_size: int -> size_t (scm_setvbuf): csize: int -> scm_bits_t * fluids.c (n_fluids): int -> scm_bits_t (grow_fluids): old_length, i: int -> scm_bits_t (next_fluid_num, scm_fluid_ref, scm_fluid_set_x): n: int -> scm_bits_t (scm_c_with_fluids): flen, vlen: int -> scm_bits_t * filesys.c (s_scm_open_fdes): changed calls to SCM_NUM2LONG to the new and shiny SCM_NUM2INT. * extensions.c: extension -> extension_t (and made a typedef). * eval.h (SCM_IFRAME): cast to scm_bits_t, not int. just so there are no nasty surprises if/when the various deeply magic tag bits move somewhere else. * eval.c: changed the locals used to store results of SCM_IFRAME, scm_ilength and such to be of type scm_bits_t (and not int/long). (iqq): depth, edepth: int -> scm_bits_t (scm_eval_stack): int -> scm_bits_t (SCM_CEVAL): various vars are not scm_bits_t instead of int. (check_map_args, scm_map, scm_for_each): len: long -> scm_bits_t i: int -> scm_bits_t * environments.c: changed the many calls to scm_ulong2num to scm_ubits2num. (import_environment_fold): proc_as_ul: ulong -> scm_ubits_t * dynwind.c (scm_dowinds): delta: long -> scm_bits_t * debug.h: type renaming: scm_debug_info -> scm_debug_info_t scm_debug_frame -> scm_debug_frame_t the old names are deprecated, all in-Guile uses changed. (scm_debug_eframe_size): int -> scm_bits_t * debug.c (scm_init_debug): use scm_c_define instead of the deprecated scm_define. * continuations.h: type renaming: scm_contregs -> scm_contregs_t the old name is deprecated, all in-Guile uses changed. (scm_contregs_t.num_stack_items): size_t -> scm_bits_t (scm_contregs_t.num_stack_items): ulong -> scm_ubits_t * continuations.c (scm_make_continuation): change the type of stack_size form long to scm_bits_t. * ports.h: type renaming: scm_port_rw_active -> scm_port_rw_active_t (and made a typedef) scm_port -> scm_port_t scm_ptob_descriptor -> scm_ptob_descriptor_t the old names are deprecated, all in-Guile uses changed. (scm_port_t.entry): int -> scm_bits_t. (scm_port_t.line_number): int -> long. (scm_port_t.putback_buf_size): int -> size_t. * __scm.h (long_long, ulong_long): deprecated (they pollute the global namespace and have little value besides that). (SCM_BITS_LENGTH): new, is the bit size of scm_bits_t (i.e. of an SCM handle). (ifdef spaghetti): include sys/types.h and sys/stdtypes.h, if they exist (for size_t & ptrdiff_t) (scm_sizet): deprecated. * Makefile.am (noinst_HEADERS): add num2integral.i.c
2001-05-24 00:50:51 +00:00
SCM_DEFINE (scm_gc_disable, "gc-disable", 0, 0, 0,
(),
"Disables the garbage collector. Nested calls are permitted. "
"GC is re-enabled once @code{gc-enable} has been called the "
"same number of times @code{gc-disable} was called.")
#define FUNC_NAME s_scm_gc_disable
{
GC_disable ();
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_gc_enable, "gc-enable", 0, 0, 0,
(),
"Enables the garbage collector.")
#define FUNC_NAME s_scm_gc_enable
{
GC_enable ();
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
2002-08-04 00:17:18 +00:00
SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
(),
"Scans all of SCM objects and reclaims for further use those that are\n"
"no longer accessible.")
#define FUNC_NAME s_scm_gc
{
scm_i_gc ("call");
2002-08-04 00:17:18 +00:00
return SCM_UNSPECIFIED;
}
2002-08-04 00:17:18 +00:00
#undef FUNC_NAME
2002-08-04 00:17:18 +00:00
void
scm_i_gc (const char *what)
2002-08-04 00:17:18 +00:00
{
#ifndef HAVE_GC_SET_START_CALLBACK
run_before_gc_c_hook ();
#endif
GC_gcollect ();
}
/* {GC Protection Helper Functions}
*/
/*
* If within a function you need to protect one or more scheme objects from
* garbage collection, pass them as parameters to one of the
* scm_remember_upto_here* functions below. These functions don't do
* anything, but since the compiler does not know that they are actually
* no-ops, it will generate code that calls these functions with the given
* parameters. Therefore, you can be sure that the compiler will keep those
* scheme values alive (on the stack or in a register) up to the point where
* scm_remember_upto_here* is called. In other words, place the call to
* scm_remember_upto_here* _behind_ the last code in your function, that
* depends on the scheme object to exist.
*
2001-08-31 14:42:31 +00:00
* Example: We want to make sure that the string object str does not get
* garbage collected during the execution of 'some_function' in the code
* below, because otherwise the characters belonging to str would be freed and
* 'some_function' might access freed memory. To make sure that the compiler
* keeps str alive on the stack or in a register such that it is visible to
* the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
* call to 'some_function'. Note that this would not be necessary if str was
* used anyway after the call to 'some_function'.
* char *chars = scm_i_string_chars (str);
* some_function (chars);
* scm_remember_upto_here_1 (str); // str will be alive up to this point.
*/
/* Remove any macro versions of these while defining the functions.
Functions are always included in the library, for upward binary
compatibility and in case combinations of GCC and non-GCC are used. */
#undef scm_remember_upto_here_1
#undef scm_remember_upto_here_2
void
scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
{
/* Empty. Protects a single object from garbage collection. */
}
void
scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
{
/* Empty. Protects two objects from garbage collection. */
}
void
scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
{
/* Empty. Protects any number of objects from garbage collection. */
}
/*
These crazy functions prevent garbage collection
of arguments after the first argument by
ensuring they remain live throughout the
function because they are used in the last
line of the code block.
It'd be better to have a nice compiler hint to
aid the conservative stack-scanning GC. --03/09/00 gjb */
SCM
scm_return_first (SCM elt, ...)
{
return elt;
}
int
scm_return_first_int (int i, ...)
{
return i;
}
SCM
scm_permanent_object (SCM obj)
{
return (scm_gc_protect_object (obj));
}
/* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
other references are dropped, until the object is unprotected by calling
scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
i. e. it is possible to protect the same object several times, but it is
necessary to unprotect the object the same number of times to actually get
the object unprotected. It is an error to unprotect an object more often
than it has been protected before. The function scm_protect_object returns
OBJ.
*/
/* Implementation note: For every object X, there is a counter which
scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
*/
SCM
scm_gc_protect_object (SCM obj)
{
SCM handle;
/* This critical section barrier will be replaced by a mutex. */
2005-03-30 22:11:07 +00:00
/* njrev: Indeed; if my comment above is correct, there is the same
critsec/mutex inconsistency here. */
2005-03-02 20:42:01 +00:00
SCM_CRITICAL_SECTION_START;
handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
protected_obj_count ++;
2005-03-02 20:42:01 +00:00
SCM_CRITICAL_SECTION_END;
return obj;
}
/* Remove any protection for OBJ established by a prior call to
scm_protect_object. This function returns OBJ.
See scm_protect_object for more information. */
SCM
scm_gc_unprotect_object (SCM obj)
{
SCM handle;
/* This critical section barrier will be replaced by a mutex. */
2005-03-30 22:11:07 +00:00
/* njrev: and again. */
2005-03-02 20:42:01 +00:00
SCM_CRITICAL_SECTION_START;
if (scm_gc_running_p)
{
fprintf (stderr, "scm_unprotect_object called during GC.\n");
abort ();
}
handle = scm_hashq_get_handle (scm_protects, obj);
if (scm_is_false (handle))
{
fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
abort ();
}
else
{
SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
if (scm_is_eq (count, scm_from_int (0)))
scm_hashq_remove_x (scm_protects, obj);
else
* validate.h (SCM_NUM2{SIZE,PTRDIFF,SHORT,USHORT,BITS,UBITS,INT,UINT}[_DEF]): new macros. * unif.h: type renaming: scm_array -> scm_array_t scm_array_dim -> scm_array_dim_t the old names are deprecated, all in-Guile uses changed. * tags.h (scm_ubits_t): new typedef, representing unsigned scm_bits_t. * stacks.h: type renaming: scm_info_frame -> scm_info_frame_t scm_stack -> scm_stack_t the old names are deprecated, all in-Guile uses changed. * srcprop.h: type renaming: scm_srcprops -> scm_srcprops_t scm_srcprops_chunk -> scm_srcprops_chunk_t the old names are deprecated, all in-Guile uses changed. * gsubr.c, procs.c, print.c, ports.c, read.c, rdelim.c, ramap.c, rw.c, smob.c, sort.c, srcprop.c, stacks.c, strings.c, strop.c, strorder.c, strports.c, struct.c, symbols.c, unif.c, values.c, vectors.c, vports.c, weaks.c: various int/size_t -> size_t/scm_bits_t changes. * random.h: type renaming: scm_rstate -> scm_rstate_t scm_rng -> scm_rng_t scm_i_rstate -> scm_i_rstate_t the old names are deprecated, all in-Guile uses changed. * procs.h: type renaming: scm_subr_entry -> scm_subr_entry_t the old name is deprecated, all in-Guile uses changed. * options.h (scm_option_t.val): unsigned long -> scm_bits_t. type renaming: scm_option -> scm_option_t the old name is deprecated, all in-Guile uses changed. * objects.c: various long -> scm_bits_t changes. (scm_i_make_class_object): flags: unsigned long -> scm_ubits_t * numbers.h (SCM_FIXNUM_BIT): deprecated, renamed to SCM_I_FIXNUM_BIT. * num2integral.i.c: new file, multiply included by numbers.c, used to "templatize" the various integral <-> num conversion routines. * numbers.c (scm_mkbig, scm_big2num, scm_adjbig, scm_normbig, scm_copybig, scm_2ulong2big, scm_dbl2big, scm_big2dbl): deprecated. (scm_i_mkbig, scm_i_big2inum, scm_i_adjbig, scm_i_normbig, scm_i_copybig, scm_i_short2big, scm_i_ushort2big, scm_i_int2big, scm_i_uint2big, scm_i_long2big, scm_i_ulong2big, scm_i_bits2big, scm_i_ubits2big, scm_i_size2big, scm_i_ptrdiff2big, scm_i_long_long2big, scm_i_ulong_long2big, scm_i_dbl2big, scm_i_big2dbl, scm_short2num, scm_ushort2num, scm_int2num, scm_uint2num, scm_bits2num, scm_ubits2num, scm_size2num, scm_ptrdiff2num, scm_num2short, scm_num2ushort, scm_num2int, scm_num2uint, scm_num2bits, scm_num2ubits, scm_num2ptrdiff, scm_num2size): new functions. * modules.c (scm_module_reverse_lookup): i, n: int -> scm_bits_t.x * load.c: change int -> size_t in various places (where the variable is used to store a string length). (search-path): call scm_done_free, not scm_done_malloc. * list.c (scm_ilength): return a scm_bits_t, not long. some other {int,long} -> scm_bits_t changes. * hashtab.c: various [u]int -> scm_bits_t changes. scm_ihashx_closure -> scm_ihashx_closure_t (and made a typedef). (scm_ihashx): n: uint -> scm_bits_t use scm_bits2num instead of scm_ulong2num. * gsubr.c: various int -> scm_bits_t changes. * gh_data.c (gh_scm2double): no loss of precision any more. * gh.h (gh_str2scm): len: int -> size_t (gh_{get,set}_substr): start: int -> scm_bits_t, len: int -> size_t (gh_<num>2scm): n: int -> scm_bits_t (gh_*vector_length): return scm_[u]size_t, not unsigned long. (gh_length): return scm_bits_t, not unsigned long. * fports.h: type renaming: scm_fport -> scm_fport_t the old name is deprecated, all in-Guile uses changed. * fports.c (fport_fill_input): count: int -> scm_bits_t (fport_flush): init_size, remaining, count: int -> scm_bits_t * debug.h (scm_lookup_cstr, scm_lookup_soft, scm_evstr): removed those prototypes, as the functions they prototype don't exist. * fports.c (default_buffer_size): int -> size_t (scm_fport_buffer_add): read_size, write_size: int -> scm_bits_t default_size: int -> size_t (scm_setvbuf): csize: int -> scm_bits_t * fluids.c (n_fluids): int -> scm_bits_t (grow_fluids): old_length, i: int -> scm_bits_t (next_fluid_num, scm_fluid_ref, scm_fluid_set_x): n: int -> scm_bits_t (scm_c_with_fluids): flen, vlen: int -> scm_bits_t * filesys.c (s_scm_open_fdes): changed calls to SCM_NUM2LONG to the new and shiny SCM_NUM2INT. * extensions.c: extension -> extension_t (and made a typedef). * eval.h (SCM_IFRAME): cast to scm_bits_t, not int. just so there are no nasty surprises if/when the various deeply magic tag bits move somewhere else. * eval.c: changed the locals used to store results of SCM_IFRAME, scm_ilength and such to be of type scm_bits_t (and not int/long). (iqq): depth, edepth: int -> scm_bits_t (scm_eval_stack): int -> scm_bits_t (SCM_CEVAL): various vars are not scm_bits_t instead of int. (check_map_args, scm_map, scm_for_each): len: long -> scm_bits_t i: int -> scm_bits_t * environments.c: changed the many calls to scm_ulong2num to scm_ubits2num. (import_environment_fold): proc_as_ul: ulong -> scm_ubits_t * dynwind.c (scm_dowinds): delta: long -> scm_bits_t * debug.h: type renaming: scm_debug_info -> scm_debug_info_t scm_debug_frame -> scm_debug_frame_t the old names are deprecated, all in-Guile uses changed. (scm_debug_eframe_size): int -> scm_bits_t * debug.c (scm_init_debug): use scm_c_define instead of the deprecated scm_define. * continuations.h: type renaming: scm_contregs -> scm_contregs_t the old name is deprecated, all in-Guile uses changed. (scm_contregs_t.num_stack_items): size_t -> scm_bits_t (scm_contregs_t.num_stack_items): ulong -> scm_ubits_t * continuations.c (scm_make_continuation): change the type of stack_size form long to scm_bits_t. * ports.h: type renaming: scm_port_rw_active -> scm_port_rw_active_t (and made a typedef) scm_port -> scm_port_t scm_ptob_descriptor -> scm_ptob_descriptor_t the old names are deprecated, all in-Guile uses changed. (scm_port_t.entry): int -> scm_bits_t. (scm_port_t.line_number): int -> long. (scm_port_t.putback_buf_size): int -> size_t. * __scm.h (long_long, ulong_long): deprecated (they pollute the global namespace and have little value besides that). (SCM_BITS_LENGTH): new, is the bit size of scm_bits_t (i.e. of an SCM handle). (ifdef spaghetti): include sys/types.h and sys/stdtypes.h, if they exist (for size_t & ptrdiff_t) (scm_sizet): deprecated. * Makefile.am (noinst_HEADERS): add num2integral.i.c
2001-05-24 00:50:51 +00:00
SCM_SETCDR (handle, count);
}
protected_obj_count --;
2005-03-02 20:42:01 +00:00
SCM_CRITICAL_SECTION_END;
return obj;
}
void
scm_gc_register_root (SCM *p)
{
/* Nothing. */
}
void
scm_gc_unregister_root (SCM *p)
{
/* Nothing. */
}
void
scm_gc_register_roots (SCM *b, unsigned long n)
{
SCM *p = b;
for (; p < b + n; ++p)
scm_gc_register_root (p);
}
void
scm_gc_unregister_roots (SCM *b, unsigned long n)
{
SCM *p = b;
for (; p < b + n; ++p)
scm_gc_unregister_root (p);
}
static void
run_before_gc_c_hook (void)
{
scm_c_hook_run (&scm_before_gc_c_hook, NULL);
}
2002-08-04 00:17:18 +00:00
/*
MOVE THIS FUNCTION. IT DOES NOT HAVE ANYTHING TODO WITH GC.
*/
/* Get an integer from an environment variable. */
2002-08-04 00:17:18 +00:00
int
scm_getenv_int (const char *var, int def)
{
2002-08-04 00:17:18 +00:00
char *end = 0;
char *val = getenv (var);
long res = def;
if (!val)
return def;
res = strtol (val, &end, 10);
if (end == val)
return def;
return res;
}
* hooks.c (scm_c_hook_add): Fixed bug in append mode. * environments.c (obarray_enter, obarray_retrieve, obarray_remove, leaf_environment_fold, obarray_remove_all): Use hashtable accessors. * gc.c (scm_init_storage): Moved hook initialization to scm_storage_prehistory. (scm_storage_prehistory): New function. (scm_igc): Added commentary about placement of scm_after_sweep_c_hook. * gc-mark.c (scm_mark_all): Use hashtable accessors. (scm_gc_mark_dependencies): Use SCM_WVECT_WEAK_KEY_P and SCM_WVECT_WEAK_VALUE_P. * hashtab.c, hashtab.h (scm_hash_for_each, scm_hash_map): New functions. (scm_vector_to_hash_table, scm_c_make_resizing_hash_table): Removed. (scm_make_weak_key_hash_table, scm_make_weak_value_hash_table, scm_make_doubly_weak_hash_table): Moved here from weaks.c. * init.c (scm_init_guile_1): Removed call to scm_init_weaks; Added calls to scm_storage_prehistory and scm_hashtab_prehistory. * modules.c (module-reverse-lookup): Use hashtable accessors. * symbols.c, symbols.h (scm_i_hash_symbol): New function. * weaks.c, weaks.h (scm_make_weak_key_alist_vector, scm_make_weak_value_alist_vector, scm_make_doubly_weak_alist_vector): New functions. * weaks.c (scm_init_weaks_builtins): New function. * weaks.h (SCM_WVECTF_WEAK_KEY, SCM_WVECTF_WEAK_VALUE, SCM_WVECTF_NOSCAN, SCM_WVECT_WEAK_KEY_P, SCM_WVECT_WEAK_VALUE_P, SCM_WVECT_NOSCAN_P): New macros. * weaks.c (scm_scan_weak_vectors): Use SCM_WVECT_WEAK_KEY_P and SCM_WVECT_WEAK_VALUE_P. * weaks.c, weaks.h (scm_i_allocate_weak_vector): Renamed from allocate_weak_vector and exported. * Makefile.am (ice9_sources): Added weak-vector.scm. * weak-vector.scm: New file. * boot-9.scm (module-clear!): Use hash-clear!. (module-for-each): Use hash-for-each. (module-map): Use hash-map.
2003-02-19 15:04:51 +00:00
void
scm_storage_prehistory ()
{
GC_all_interior_pointers = 0;
GC_set_free_space_divisor (scm_getenv_int ("GC_FREE_SPACE_DIVISOR", 3));
GC_INIT ();
#if (! ((defined GC_VERSION_MAJOR) && (GC_VERSION_MAJOR >= 7))) \
&& (defined SCM_I_GSC_USE_PTHREAD_THREADS)
/* When using GC 6.8, this call is required to initialize thread-local
freelists (shouldn't be necessary with GC 7.0). */
GC_init ();
#endif
GC_expand_hp (SCM_DEFAULT_INIT_HEAP_SIZE_2);
/* We only need to register a displacement for those types for which the
higher bits of the type tag are used to store a pointer (that is, a
pointer to an 8-octet aligned region). For `scm_tc3_struct', this is
handled in `scm_alloc_struct ()'. */
GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
eval.c closures are now applicable smobs, not tc3s * libguile/debug.c (scm_procedure_name): Remove a SCM_CLOSUREP case and some dead code. (scm_procedure_module): Remove. This was introduced a few months ago for the hygienic expander, but now it is no longer needed, as the expander keeps track of this information itself. * libguile/debug.h: Remove scm_procedure_module. * libguile/eval.c: Instead of using tc3 closures, define a "boot closure" applicable smob type, and represent closures with that. The advantage is that after eval.scm is compiled, boot closures take up no address space (besides a smob number) in the runtime, and require no special cases in procedure dispatch. * libguile/eval.h: Remove the internal functions scm_i_call_closure_0 and scm_closure_apply, and the public function scm_closure. * libguile/gc.c (scm_storage_prehistory): No tc3_closure displacement registration. (scm_i_tag_name): Remove closure case, and a dead cclo case. * libguile/vm.c (apply_foreign): * libguile/print.c (iprin1): * libguile/procs.c (scm_procedure_p, scm_procedure_documentation); * libguile/evalext.c (scm_self_evaluating_p): * libguile/goops.c (scm_class_of): Remove tc3_closure/tcs_closure cases. * libguile/hash.c (scm_hasher): * libguile/hooks.c (scm_add_hook_x): Use new scm_i_procedure_arity. * libguile/macros.c (macro_print): Print all macros using the same code. (scm_macro_transformer): Return any procedure, not just programs. * libguile/procprop.h: * libguile/procprop.c (scm_i_procedure_arity): Instead of returning a list that the caller has to parse, have the same prototype as scm_i_program_arity. An incompatible change, but it's an internal function anyway. (scm_procedure_properties, scm_set_procedure_properties) (scm_procedure_property, scm_set_procedure_property): Remove closure cases, and use scm_i_program_arity for arity. * libguile/procs.h (SCM_CLOSUREP, SCM_CLOSCAR, SCM_CODE) (SCM_CLOSURE_NUM_REQUIRED_ARGS, SCM_CLOSURE_HAS_REST_ARGS) (SCM_CLOSURE_BODY, SCM_PROCPROPS, SCM_SETPROCPROPS, SCM_ENV) (SCM_TOP_LEVEL): Remove these macros that pertain to boot closures only. Only eval.c should know abut boot closures. * libguile/procs.c (scm_closure_p): Remove this function. There is a simple stub in deprecated.scm now. (scm_thunk_p): Use scm_i_program_arity. * libguile/tags.h (scm_tc3_closure): Remove. Yay, another tc3 to play with! (scm_tcs_closures): Remove. * libguile/validate.h (SCM_VALIDATE_CLOSURE): Remove. * module/ice-9/deprecated.scm (closure?): Add stub. * module/ice-9/documentation.scm (object-documentation) * module/ice-9/session.scm (help-doc, arity) * module/oop/goops.scm (compute-getters-n-setters) * module/oop/goops/describe.scm (describe) * module/system/repl/describe.scm (display-object, display-type): Remove calls to closure?.
2009-12-04 19:20:11 +01:00
/* GC_REGISTER_DISPLACEMENT (scm_tc3_unused); */
/* Sanity check. */
if (!GC_is_visible (&scm_protects))
abort ();
* hooks.c (scm_c_hook_add): Fixed bug in append mode. * environments.c (obarray_enter, obarray_retrieve, obarray_remove, leaf_environment_fold, obarray_remove_all): Use hashtable accessors. * gc.c (scm_init_storage): Moved hook initialization to scm_storage_prehistory. (scm_storage_prehistory): New function. (scm_igc): Added commentary about placement of scm_after_sweep_c_hook. * gc-mark.c (scm_mark_all): Use hashtable accessors. (scm_gc_mark_dependencies): Use SCM_WVECT_WEAK_KEY_P and SCM_WVECT_WEAK_VALUE_P. * hashtab.c, hashtab.h (scm_hash_for_each, scm_hash_map): New functions. (scm_vector_to_hash_table, scm_c_make_resizing_hash_table): Removed. (scm_make_weak_key_hash_table, scm_make_weak_value_hash_table, scm_make_doubly_weak_hash_table): Moved here from weaks.c. * init.c (scm_init_guile_1): Removed call to scm_init_weaks; Added calls to scm_storage_prehistory and scm_hashtab_prehistory. * modules.c (module-reverse-lookup): Use hashtable accessors. * symbols.c, symbols.h (scm_i_hash_symbol): New function. * weaks.c, weaks.h (scm_make_weak_key_alist_vector, scm_make_weak_value_alist_vector, scm_make_doubly_weak_alist_vector): New functions. * weaks.c (scm_init_weaks_builtins): New function. * weaks.h (SCM_WVECTF_WEAK_KEY, SCM_WVECTF_WEAK_VALUE, SCM_WVECTF_NOSCAN, SCM_WVECT_WEAK_KEY_P, SCM_WVECT_WEAK_VALUE_P, SCM_WVECT_NOSCAN_P): New macros. * weaks.c (scm_scan_weak_vectors): Use SCM_WVECT_WEAK_KEY_P and SCM_WVECT_WEAK_VALUE_P. * weaks.c, weaks.h (scm_i_allocate_weak_vector): Renamed from allocate_weak_vector and exported. * Makefile.am (ice9_sources): Added weak-vector.scm. * weak-vector.scm: New file. * boot-9.scm (module-clear!): Use hash-clear!. (module-for-each): Use hash-for-each. (module-map): Use hash-map.
2003-02-19 15:04:51 +00:00
scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
}
2005-03-02 20:42:01 +00:00
scm_i_pthread_mutex_t scm_i_gc_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
void
scm_init_gc_protect_object ()
{
scm_protects = scm_c_make_hash_table (31);
2005-03-02 20:42:01 +00:00
#if 0
/* We can't have a cleanup handler since we have no thread to run it
in. */
#ifdef HAVE_ATEXIT
atexit (cleanup);
#else
#ifdef HAVE_ON_EXIT
on_exit (cleanup, 0);
#endif
2005-03-02 20:42:01 +00:00
#endif
#endif
}
SCM scm_after_gc_hook;
static SCM after_gc_async_cell;
/* The function after_gc_async_thunk causes the execution of the
* after-gc-hook. It is run after the gc, as soon as the asynchronous
* events are handled by the evaluator.
*/
static SCM
after_gc_async_thunk (void)
{
/* Fun, no? Hook-run *and* run-hook? */
scm_c_hook_run (&scm_after_gc_c_hook, NULL);
scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
return SCM_UNSPECIFIED;
}
/* The function queue_after_gc_hook is run by the scm_before_gc_c_hook
* at the end of the garbage collection. The only purpose of this
* function is to mark the after_gc_async (which will eventually lead to
* the execution of the after_gc_async_thunk).
*/
static void *
queue_after_gc_hook (void * hook_data SCM_UNUSED,
void *fn_data SCM_UNUSED,
void *data SCM_UNUSED)
{
/* If cell access debugging is enabled, the user may choose to perform
* additional garbage collections after an arbitrary number of cell
* accesses. We don't want the scheme level after-gc-hook to be performed
* for each of these garbage collections for the following reason: The
* execution of the after-gc-hook causes cell accesses itself. Thus, if the
* after-gc-hook was performed with every gc, and if the gc was performed
* after a very small number of cell accesses, then the number of cell
* accesses during the execution of the after-gc-hook will suffice to cause
* the execution of the next gc. Then, guile would keep executing the
* after-gc-hook over and over again, and would never come to do other
* things.
*
* To overcome this problem, if cell access debugging with additional
* garbage collections is enabled, the after-gc-hook is never run by the
* garbage collecter. When running guile with cell access debugging and the
* execution of the after-gc-hook is desired, then it is necessary to run
* the hook explicitly from the user code. This has the effect, that from
* the scheme level point of view it seems that garbage collection is
* performed with a much lower frequency than it actually is. Obviously,
* this will not work for code that depends on a fixed one to one
* relationship between the execution counts of the C level garbage
* collection hooks and the execution count of the scheme level
* after-gc-hook.
*/
2005-03-02 20:42:01 +00:00
#if (SCM_DEBUG_CELL_ACCESSES == 1)
if (scm_debug_cells_gc_interval == 0)
#endif
{
scm_i_thread *t = SCM_I_CURRENT_THREAD;
if (scm_is_false (SCM_CDR (after_gc_async_cell)))
{
SCM_SETCDR (after_gc_async_cell, t->active_asyncs);
t->active_asyncs = after_gc_async_cell;
t->pending_asyncs = 1;
}
}
return NULL;
}
char const *
scm_i_tag_name (scm_t_bits tag)
{
if (tag >= 255)
{
int k = 0xff & (tag >> 8);
return (scm_smobs[k].name);
}
switch (tag) /* 7 bits */
{
case scm_tcs_struct:
return "struct";
case scm_tcs_cons_imcar:
return "cons (immediate car)";
case scm_tcs_cons_nimcar:
return "cons (non-immediate car)";
case scm_tc7_pointer:
return "foreign";
case scm_tc7_hashtable:
return "hashtable";
case scm_tc7_fluid:
return "fluid";
case scm_tc7_dynamic_state:
return "dynamic state";
case scm_tc7_frame:
return "frame";
case scm_tc7_objcode:
return "objcode";
case scm_tc7_vm:
return "vm";
case scm_tc7_vm_cont:
return "vm continuation";
case scm_tc7_wvect:
return "weak vector";
case scm_tc7_vector:
return "vector";
case scm_tc7_number:
switch (tag)
{
case scm_tc16_real:
return "real";
break;
case scm_tc16_big:
return "bignum";
break;
case scm_tc16_complex:
return "complex number";
break;
case scm_tc16_fraction:
return "fraction";
break;
}
break;
case scm_tc7_string:
return "string";
break;
case scm_tc7_stringbuf:
return "string buffer";
break;
case scm_tc7_symbol:
return "symbol";
break;
case scm_tc7_variable:
return "variable";
break;
case scm_tc7_port:
return "port";
break;
case scm_tc7_smob:
return "smob"; /* should not occur. */
break;
}
return NULL;
}
void
scm_init_gc ()
{
/* `GC_INIT ()' was invoked in `scm_storage_prehistory ()'. */
scm_after_gc_hook = scm_make_hook (SCM_INUM0);
scm_c_define ("after-gc-hook", scm_after_gc_hook);
/* When the async is to run, the cdr of the gc_async pair gets set to
the asyncs queue of the current thread. */
after_gc_async_cell = scm_cons (scm_c_make_gsubr ("%after-gc-thunk", 0, 0, 0,
after_gc_async_thunk),
SCM_BOOL_F);
scm_c_hook_add (&scm_before_gc_c_hook, queue_after_gc_hook, NULL, 0);
#ifdef HAVE_GC_SET_START_CALLBACK
GC_set_start_callback (run_before_gc_c_hook);
#endif
#include "libguile/gc.x"
}
2002-08-04 00:17:18 +00:00
void
scm_gc_sweep (void)
#define FUNC_NAME "scm_gc_sweep"
{
/* FIXME */
fprintf (stderr, "%s: doing nothing\n", FUNC_NAME);
2002-08-04 00:17:18 +00:00
}
#undef FUNC_NAME
/*
Local Variables:
c-file-style: "gnu"
End:
*/