Using libgc with GNAT?

271 Views Asked by At

I've heard several mentions that Ada supports garbage collection --- and looking at the language design, it's obviously been designed with that in mind.

I have a non-real-time application for which garbage collection would be really, really useful. However I haven't seen any mention of there being a garbage collector available for my compiler, GNAT. This surprises me; even C supports garbage collection, by simply linking against libgc.

If I simply add libgc to my linker line, will it work or will horrible things happen?

2

There are 2 best solutions below

1
David Given On BEST ANSWER

I have found a libgc binding as part of the AdaCL library here: http://adacl.sourceforge.net/

However, the libgc documentation also says that libgc can't see pointers stored in blocks allocated via system malloc(). This means that an access which is only stored in an object allocated from the default memory pool won't be considered a root and may be invalidated at any point. This disqualifies the use of standard Ada containers from storing accesses to collectable objects! This might also apply to the secondary stack --- but I don't know what the secondary stack is used for.

However, my investigations do show that garbage collection does naively work:

while true loop
  p := new big_thing_t;
end loop;

...runs forever. So it's probably theoretical possible to do, but right now I don't think it's safe.

0
Shark8 On

However I haven't seen any mention of there being a garbage collector available for my compiler, GNAT. This surprises me; even C supports garbage collection, by simply linking against libgc.

You could use GNAT targeting the JVM.

I think that the reason simply linking against libgc won't give you garbage collection [at least to my knowledge] is because GNAT doesn't have any notion of how to use it. However, given that GNAT uses the GCC backend I don't see why it shouldn't "just work" if it does w/ C.