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?