At 1:19 PM -0700 4/25/02, Steve Fink wrote: >I can't find any clear description of how memory allocation is >supposed to work. I see bits and pieces in pdd09_gc.pod, resources.c, >etc., but I still haven't been able to wrap my head around it all. > >Here's a hypothesis. Could someone correct me where I'm wrong? > >There are two kinds of memory: untracked and tracked. Untracked memory >is yours to do with as you see fit, and must be explicitly returned to >the system. Tracked memory is handled by the garbage collector and >never needs to be explicitly freed.
Note that system allocated memory with the sysmem bit set in the flags will get automatically freed if the buffer its attached to is found to be dead. >All of those are clear except for mem_setup_allocator(), which should >only be called by pregnant pigeons with fewer than 7 hit points. That sets up the memory allocation system. It should be called exactly once per interpreter, at interpreter setup time. >Tracked memory >o) free_bigint() and free_bignum() are declarations that bigints and > bignums are now free; if you call these functions and are caught > shoplifting bigints or bignums, you cannot be prosecuted. They > obviously don't free any memory because they don't take any > arguments. The bigint and bignum stuff should be tossed entirely. They were ideas that didn't work out. >o) mem_allocate() should be called in preference to Parrot_allocate() > if your compiler restricts the number of syllables in function names. And, of course, if you're stuffing the memory straight into a Buffer... >o) buffer_lives() is so named to avoid copyright infringement with the > name of a Buffy the Vampire Slayer episode. [Seriously, why is this > not static?] Because the PMC DOD run function may call it. >The various sorts of tracked memory form a small class hierarchy >rooted at Buffer: > > Buffer > STRING > >Other subclasses of Buffer can be created copying the fields of the >Buffer struct into the very start of the subclass's struct. >Alternatively, the subclass may just include a Buffer struct as its >first field. [Are there any alignment considerations?] > >Oops. Just read some more code. The last paragraph is an evil >deception, because if you make your own subclass, there will be no way >to allocate it since new_buffer_header() only allocates enough space >for the Buffer itself. There is a separate new_string_header() for >allocating STRING structures. > >Or maybe that's exactly what new_tracked_header() is for? That's what new_tracked_header is for. If you want a header that's bigger than a plain struct you're supposed to call new_tracked_header to get it. Unfortunately this function's never been written. >PMCs are not subclasses of Buffer because PMC stands for "Parrot Meaty >Chunk" and m-w.com defines buffer as "something that serves as a >protective barrier", so clearly a PMC is something that lacked a >buffer at some unfortunate moment in its past. [Better hypothesis: >PMCs are fixed-size allocations, Buffers are variable-sized?] Even better, because PMCs were designed first. That and it was easier and made sense for there to be a level of separation between Buffers, which essentially represent a chunk of memory, and PMCs, which represent interpreter variables. >The memory is now available under buffer->bufstart. If this call >triggers a garbage collection, then your header will not be garbage >collected if and only if you have changed the oil on your car at least >once every 10,000 miles. Calling allocate won't trigger a DOD run, so your header's safe. >Any Buffer or Buffer subclass must immediately have various flags set >on it. The available flags are defined in string.h because if they >were in resources.h bad people might find them. Or because they started as things hanging off of Strings, before we made things a bit more generic. They ought to get moved to buffer.h. >PMCs also have a set of flags. They are defined in pdd09_gc.pod too. >As a simple rule of thumb, if your Buffer's bufstart points to a >complex data structure that contains a mixture of PMCs and Buffer >pointers and other stuff, then in order to prevent things from getting >garbage collected out from under you, you must perform the Blood Dance >of the Pale Chicken under full moonlight. Or set the PMC_custom_mark_FLAG, and provide a mark vtable method. (Well, that and have the DOD phase actually call it on PMCs, which it doesn't. I'll go write that code now) -- Dan --------------------------------------"it's like this"------------------- Dan Sugalski even samurai [EMAIL PROTECTED] have teddy bears and even teddy bears get drunk