I have examined the memory management code pretty extensively trying to determine if it would be possible to make the existing memory manager thread safe.
From what I see in code I think I can explain how it works. Mark, maybe kind enough to correct my anaysis since for some of it you have to look at the assembly genrated by the BMAX compiler to understand, and I may be intepreting the assembly code incorrectly.
First off the difference between global and local variables.
Global Variables
Are allocated at assembly time in the Data Segment of the process by the Assembled code.
Local Variables
Are allocated on the Stack when declared and are allocated at run-time. Not the CPU Cache. as Dremora states.
The algorimth for BlitzMax is a Hybrid Mark/Sweep + reference Counted Garbage Collector. It is also considered a Consevative collector as it only will call GCCollect automatically when you allocate memory. ie. New Object. See
http://www.iecc.com/gclist/GC-faq.html for details about GC's.
So if you allocate a Global Var and assign it to a type the reference count of object stored in variable is increased. The memory is in the HEAP mangage by the GC. If you call GCCollect it scans the pointers allocated on the heap. If the reference count is > 0 it marks it, so it wont be released. When you set the variable to null it reduces the reference and if zero Free's the memory.
Local variables are a bit different in that they are on the Stack, if you allocate a Local Variable it doesnt use the reference count, unless you assign it to another variable. These get cleaned up by the GC by scanning the Stack and the registers of the CPU. The garbage collector scans the Regsiters and the stack for references to live objects in the HEAP. If the address is in the stack or register it Marks the Object as still alive. This is why when you set a local variable to null and call Collect it might not get collected as there may still be reference in a register.
Overall its a fairly effecient Garbage Collection schema. Based upon the way its structured currently with the reference counting done by compiler for globals there is no way to make it thread safe, with out changing the compiler.
There is more too it in the way Freelists are handled but that is not so much a GC function as a Memory Managment function but the basics are outlined above.
I think Mark goal has been balancing pretty well the convenice of a Garbage Collector vs speed of manual memory managment. Ie. New/Delete. Although I dont care for the limitation of the reference counting portion of it, vs a pure Mark/Sweep.
I have been considering playing around with this replacing the Blitzmax Collector with this one.
http://www.hpl.hp.com/personal/Hans_Boehm/gc/It is a Mark/Sweep that implements Generational or Incremental Collection. This one would stubb out the refrence counting and would allow for cyclical references.
Tests out pretty well in C/C++ but cant get a good performance test in BMAX without coding it in. It can also be made thread-safe. Although being thread-safe would reduce performance.
This is heavy Computer Science on the links I provided but I think they can give you a good idea on how it works.
Doug Stastny