[OT] Looking for garbage collection library

  • Thread starter Philipp Klaus Krause
  • Start date
P

Philipp Klaus Krause

I'm looking for a garbage collection library, but have not been
sucessfull so far.

Requirements:

- Written in C (and C only), I guess this requirement practically
implies that the GC has to get memory via malloc() or just use a global
array.

- Is small

- Works well with low amounts of memory, e.g. managing just half a KB or so.

Has anyone come across such a library?

Philipp
 
A

August Karlstrom

I'm looking for a garbage collection library, but have not been
sucessfull so far.

Requirements:

- Written in C (and C only), I guess this requirement practically
implies that the GC has to get memory via malloc() or just use a global
array.

- Is small

- Works well with low amounts of memory, e.g. managing just half a KB or so.

Has anyone come across such a library?

Does libgc meet your requirements?

http://www.hpl.hp.com/personal/Hans_Boehm/gc/


/August
 
M

Michael Schumacher

Philipp said:
I'm looking for a garbage collection library, but have not been
sucessfull so far.

Requirements:

- Written in C (and C only), I guess this requirement practically
implies that the GC has to get memory via malloc() or just use a global
array.

- Is small

- Works well with low amounts of memory, e.g. managing just half a KB or
so.

Has anyone come across such a library?

I'm a bit puzzled here... What exactly do you expect it to do?
If you simply want to have it "automagically" regain memory that
has been malloc()ed, but not freed, albeit not being used any
longer, and all this in already existing, pure C code, I'd say
the reason why you haven't found such a GC is simply that there's
no conceivable way to implement it.

If you need a malloc()-Debugger that helps finding memory leaks,
then that's a different story: electric fence, dmalloc, NJAMD, ...

If you're looking for a solution where dynamically allocated
memory will automatically be free()d upon leaving the function
that requested it, then alloca() will be your friend.

But a reliably working GC for plain C code? Uh-oh... ;)


mike
 
E

Eric Sosman

Am 16.10.2010 20:08, schrieb August Karlstrom:

No: "The collector inherently has to perform operations, [...], that are
not possible in portable C code. [...]

This will probably be true of any garbage-collection scheme
for full-fledged C. To determine whether a piece of memory is
garbage, the GC needs to discover whether any "live" pointers aim
at it or into it. Thus, it needs to examine all the places such
pointers might live -- and the discovery and examination of all
those places seems a highly non-portable task. Even if you assume
that all memory blocks are referenced via actual pointers (and not
via self-relative offsets, for example), I think you will find it
difficult to locate all those pointers. For example, suppose that
the only extant pointer to a block is an `auto' or `register'
variable in a function six frames higher up the call stack; the
task of examining that dormant function's local variables does not
lend itself to a portable solution.

There's reference counting, but you need a way to maintain the
counters as pointers are written, overwritten, and rewritten. Just
copying a pointer from one place to another, or passing one as a
function argument, or stuffing it into a struct and then copying the
entire struct -- all of these can create new references that you'd
have to track as they went into and out of existence. You could ask
the programmer to help by using explicit seize(p) and forget(p) calls,
but that seems extremely error-prone. (If you trust the programmer
to use seize/forget properly, you can probably live with something a
good deal less complicated than full-blown GC.)

It seems to me that you'll need to live with some amount of
non-portability, or else limit yourself to a narrow subset of C and
enforce strict coding conventions.

Can you tell us more about the larger problem you're facing?
 
P

Philipp Klaus Krause

Am 16.10.2010 21:18, schrieb Eric Sosman:
Can you tell us more about the larger problem you're facing?

I was wondering about Scheme to C translation. I had looked at some
Scheme to C translators, but found them unsuitable for what I want to
do. I wondered how much effort it would take to roll my own. Scheme has
garbage collection and passes everything by reference. I hoped there
would be some library I could use for the garbage collection.

Philipp
 
I

Ian Collins

Am 16.10.2010 21:18, schrieb Eric Sosman:

I was wondering about Scheme to C translation. I had looked at some
Scheme to C translators, but found them unsuitable for what I want to
do. I wondered how much effort it would take to roll my own. Scheme has
garbage collection and passes everything by reference. I hoped there
would be some library I could use for the garbage collection.

There is, libgc is a library you could use for the garbage collection.
It is probably the most widely used garbage collector for C and C++
applications.
 
J

Jon

Ian Collins said:
There is, libgc is a library you could use for the garbage collection.
It is probably the most widely used garbage collector for C and C++
applications.

I fail to see how garbage collection, a highly controversial topic and
yet to become more controversial, is relevant in a ng that spews "what's
on topic?!" threads.
 
J

Jon

Jon said:
I fail to see how garbage collection, a highly controversial topic and
yet to become more controversial, is relevant in a ng that spews
"what's on topic?!" threads.

I missed the [OT] lameness.
 
P

Philipp Klaus Krause

Am 16.10.2010 21:04, schrieb Michael Schumacher:
I'm a bit puzzled here... What exactly do you expect it to do?
If you simply want to have it "automagically" regain memory that
has been malloc()ed, but not freed, albeit not being used any
longer, and all this in already existing, pure C code, I'd say
the reason why you haven't found such a GC is simply that there's
no conceivable way to implement it.

I was wondering about Scheme to C translation. Scheme has garbage
collection, thus it has to be handled somehow. I suppose that by
restristing how and where the thing have have to be handled by garbage
collection are stored it can be done in pure C.

Philipp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top