This is the mail archive of the guile@sourceware.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: Making GC aware of overall allocation?


Lars Arvestad <arve@nada.kth.se> writes:

> [ summary: how to make the Guile GC aware of (some of) user's mallocs ]

the primitive mechanism in Guile is the function `scm_done_malloc'.
it takes a number, which is supposed to be the malloced size.  it
can be negative, so that you can say to the GC how much memory you
just freed.

it's probably a good idea to provide some sort of malloc/free wrappers
with this bookkeeping, if you can put up with wasted space, something
like this (written on the spot, untested)

/* maximum alignment on your system, the below is good for x86 */
#define SCM_MAX_ALIGN 4

void *
scm_sys_malloc (scm_sizet size, char const *what)
{
  void *res = malloc (size + SCM_MAX_ALIGN);
  SCM_ASSERT (res, SCM_MAKINUM (size), what);

  *((long *)res) = (long)size;
  scm_done_malloc ((long)size);

  return res + SCM_MAX_ALIGN;
}

void
scm_sys_free (void *ptr_)
{
  void *ptr = ptr_ - SCM_MAX_ALIGN;
  scm_done_malloc (0 - *((long *)ptr));
  free (ptr);
}

> 	Best regards,
> 	Lars

hth,
--mike

-- 
Don't trust these UNIX people.  They are all demons.  They kill their
parents and fork children.  I don't know how they could do this with
their balls cut off but they manage.                        -- anonymous

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