guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is my procedure getting GCed?


From: Michael Livshin
Subject: Re: Is my procedure getting GCed?
Date: 16 May 2001 22:31:14 +0300
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Copyleft)

Neil Jerram <address@hidden> writes:

> >>>>> "Michael" == Michael Livshin <address@hidden> writes:
> 
>     Michael> Neil Jerram <address@hidden> writes:
>     >> What's the difference between roots and the objects protected
>     >> by scm_protect?
> 
>     Michael> static SCM my_global_thingie; ...  my_global_thingie =
>     Michael> scm_cons (a, b);
> 
>     Michael> after the assignment, the object would be the cons
>     Michael> (i.e. `my_global_thingie') and the root would be the
>     Michael> pointer _itself_ (i.e. `&my_global_thingie').
> 
> Thanks, but I'm afraid I still don't get it.  What then is the big
> advantage of roots over scm_protect?  (Sorry if I'm being totally
> thick here.)

I'm not sure about *big*, but...

consider two possible styles of writing the same piece of functionality:

SCM
do_some_work ()
{
  static SCM persistent_state = SCM_BOOL_F;
  if (SCM_FALSEP (persistent_state))
    /* init the state */
    persistent_state = scm_protect_object (scm_cons (...));
  }
  ...
  if (some_interesting_situation_p)
    {
      /* need to update the state */
      scm_unprotect_object (persistent_state);
      persistent_state = scm_protect_object (some_other_thing (...));
      ...
    }
  ...
}

<- versus ->

SCM
do_some_work ()
{
  static SCM persistent_state = SCM_BOOL_F;
  if (SCM_FALSEP (persistent_state))
    {
      /* init the state */
      scm_gc_add_root (&persistent_state);
      persistent_state = scm_cons (...);
    }
  ...
  if (some_interesting_situation_p)
    {
      /* need to update the state */
      persistent_state = some_other_thing (...);
      ...
    }
  ...
}

seems less error-prone and more straightforward to me.

-- 
All ITS machines now have hardware for a new machine instruction --
BAH
Branch And Hang.
Please update your programs.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]