Boehm-Demers-Weiser conservative garbage collector

AdaCL features a binding to the Boehm-Demers-Weiser conservative garbage collector. A garbage collector will automatically reclaim unused memory so that there is no need for Ada.Unchecked_Deallocation. Mind you, AdaCL itself frees all memory allocated.

There is much discussion about garbage collectors and whether they are really needed in Ada. Personally I think that some the original designer actually wanted a garbage collector. Read the following chapters of the reference manual:

Sadly almost no Ada implementors ever made it to actually implement a garbage collector. For comparison: most other modern languages (java, modula-3, eiffel, c#, managed c++, d, objective-pascal) now have one.

Luckily the GCC Team designed a language independent garbage collector usable for all GCC languages - and that includes GNAT.

Unchecked De-allocation

Often it is suggested to use a no operation for a garbage collecting storage pool. But after looking at the relevant chapter of the reference manual I think this is not what the Ada designers wanted:

If you call unchecked de-allocation then memory is freed unconditionally and access types still pointing to that memory will be left dangling.

If you make an unchecked de-allocation on an access to a class wide type then all possible types should be garbage collected, or not - otherwise garbage collected memory will be freed with a normal free or vice versa. As it says: it is unchecked de-allocation and there are a lot of ways to shoot yourself in the foot.

Finalisation

Relying on a garbage collector gives finalisation a new interesting twist. That is, you don't know when finalisation is called. And sometimes it is not called at all. First Problem of course is that the reference manual is a little vague on how to implement finalisation:

The Boehm-Demers-Weiser conservative garbage collector is used for Java as well so on that end everything needed is in place. On the Ada end one need to rely on GNAT internals to implement finalisation. See System.Finalization_Root and System.Finalization_Implementation for details.

What if I don't want a garbage collector?

Then open the Scripts\*.gnatprep for your operating system and change GC_Active to false.

How to use

The Garbage Collector can be use like any other Storage Pool. Since it make no difference how often the Storage Pool types are instantiated a convenience instance is provided within the packages. There are several different Storage Pools for different purposes provided within the AdaCL.GC hierarchy.

gc and Linux

As one might expect the Linux implementation works “out of the box”.

gc and NT

The Windows implementation doesn't work, so it has been disabled in Scripts\NT.gnatprep. The problem is that Windows GNAT is based on MinGW and not cygwin but the Windows implementation of the garbage collector needs cygwin.

hints

Copy gc.a to libgc.a if you want to try it anyway. For example if you have a newer version of MinGW.

gc and OS/2

Apart from a strange Warning, the GC for OS/2 works fine. However the OS2_makefile of the for the Boehm-Demers-Weiser conservative garbage collector is outdated. Write a Support Request on the AdaCL Project Page if you need help.

hints

A copy of gc.a to libgc.a is not needed.


Ada programming, © 2005,2006 the Authors, Content is available under GNU Free Documentation License.