4 ms·
This article teaches you how to write a conservative GC, which is what you'd do if you wanted to garbage collect in C or some other system where you don't know
by munificent 12y ago
This article teaches you how to write a conservative GC, which is what you'd do if you wanted to garbage collect in C or some other system where you don't know the shape of objects at runtime.
If you do know that -- like you're writing a GC for a higher-level programming language -- you can write a precise GC instead and avoid all of the (very interesting) muckiness this article goes through.
I wrote an article on that a while back:
http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/ http://journal.stuffwithstuff.com/2013/12/08/babys-first-gar...
- Someone 12y agoAlso, that avoids the very muckiness this article skips. In particular: "Local variables can also be stored in registers, but we won't worry about this because registers and usually dedicated to local variables, and by the time our function is called they'll probably be saved on the stack anyway" Ignoring things that 'usually' or 'probably' do not happen is not a good idea when programming, doubly so when doing systems programming. It is one thing when your application crashes, another when the OS crashes or becomes exploitable due to a bug. If you want to implement a robust conservative garbage collector you will have to study the calling conventions of the language you write it in, will have to study the source of the compiler, and will have to study the release notes of every OS or compiler upgrade. For example: - rust moved from segmented stacks to conventional ones. That probably broke any code walking the stack. - on some systems, locals can get stored below the stack pointer (http://en.m.wikipedia.org/wiki/Red_zone_(computing) http://en.m.wikipedia.org/wiki/Red_zone_(computing)) (Yes, this example code appears to be broken on x64)
- tankenmate 12y agoChecking the registers is easy enough; just push them onto the stack and a quick check of several consecutive memory addresses and you are done. The other way to improve GC if you are using C is to make sure your local allocations are in fact local; use alloca() and friends (but only for sub page sizes, possibly less if you are doing lots of local allocations). This will reduce the amount of GC required. As far as Red Zone goes if you are in the GC then you can't be in another local function; unless you are running your GC pre-emptively (signals etc), in which case you need to be much much more careful.