This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: global objects in C


"Keisuke Nishida" <kei@psn.net> writes:

> I suppose I have to protect the variable global_stack from GC
> by pointer, like:
> 
>   scm_protect_pointer (&global_stack);
> 
> before using global_stack.  Is there any way to do this?

----- stack.c -----
/* a stack module */


/*
 * the c data type 
 */
void *scm_tc16_stack;
struct stack {
  SCM values /* a scheme list */
};


/*
 * wrap a smob around the c data type
 */

static SCM
mark_stack (SCM stack_smob)
{
  struct stack *stack = SCM_CDR(stack_smob);

  scm_gc_mark(stack->values

  return SCM_BOOL_F;
}

static scm_sizet
free_stack (SCM stack_smob)
{
  struct stack *stack = SCM_CDR(stack_smob);

  free(stack);

  return sizeof *stack;
}

static int
print_stack (SCM type, SCM port, scm_print_state *pstate)
{
  scm_puts ("#<stack ", port);
  scm_puts (SCM_CHARS(scm_number_to_string(scm_ulong2num((unsigned long)type), SCM_MAKINUM (16))), port);
  scm_puts (">", port);

  return 1;
}

static scm_smobfuns stack_funs = {
  mark_stack, free_stack, print_stack, 0
};

/* the constructor */
/* SCM_PROC(s_make_stack, "make-stack", 0, 0, 0, scm_make_stack); */
static SCM
scm_make_stack ();
{
  SCM stack_smob;
  struct stack *stack;

  stack = scm_must_malloc (sizeof (*stack), s_make_stack);
  stack->value = SCM_EOL;

  SCM_NEWCELL (stack_smob);
  SCM_SETCDR (stack_smob, stack);
  SCM_SETCAR (stack_smob, scm_tc16_stack);

  return stack_smob;
}

SCM global_stack;

SCM scm_init_stack (/*SCM env*/)
{
  scm_tc16_stack = scm_newsmob (&stack_funs);

  gh_new_procedure0_0 ("make-stack", scm_make_stack, /*env*/);

/* add a global variable `global-stack' */
  /* global_stack = scm_make_stack(); */
  /* sch_environment_intern(env, "global-stack", global_stack); */

  return SCM_BOOL_T;
}
--- end of stack.c ---


You can call this function either from your my_main() function
(when environments are in place you can pass scm_interaction_environment
or scm_root_environment as the env argument) or link it dynamically by
storing scm_init_stack into the module registry and then call it
from scheme level.


Jost


  

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]