/* showcase for the use of scm_gc_protect_object */ #include /* Global variables to hold symbols; as their names indicate, the * first one is to survive garbage collection, the second isn't. */ SCM exists; SCM doesnt_exist; /* initialise the above topology symols */ /* return #t if sym equals exists, #f otherwise */ static SCM equals_exists_q (SCM sym) { SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-exists?"); return sym==exists?SCM_BOOL_T:SCM_BOOL_F; }; /* return #t if sym equals doesnt_exist, #f otherwise */ static SCM equals_doesnt_exist_q (SCM sym) { SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-doesnt-exist?"); return sym==doesnt_exist?SCM_BOOL_T:SCM_BOOL_F; }; /* Here's the beef. The symbol exists is protected from the garbage * collector whereas the symbol doesnt_exist is not. If we access the * global variables exist and doesnt_exist from C code (invoked in * turn from Scheme code), they behave differently since doesnt_exist * has already been garbage collected away by the time the scheme * interpreter is invoked. */ static void init (void) { scm_gc_protect_object(exists=scm_str2symbol("exists")); doesnt_exist=scm_str2symbol("doesnt_exist"); scm_c_define_gsubr("equals-exists?", 1, 0, 0, equals_exists_q); scm_c_define_gsubr("equals-doesnt-exist?", 1, 0, 0, equals_doesnt_exist_q); }; /* The actual main function. main (below) does not much more than * invoke this function. */ static void inner_main (void *closure, int argc, char **argv) { init(); scm_shell(argc, argv); }; /* Just the standard main for a Guile extended program. The actual * work is done by inner_main above. */ int main (int argc, char *argv[]) { scm_boot_guile (argc, argv, inner_main, NULL); exit(0); /* never get here */ };