[comp.sys.mac.programmer] Memory allocation in a shared heap...

martin@cs.uchicago.edu (02/15/91)

The problem is handling memory allocation in a shared heap..

The 'sysz' resource specifies the amount to which the system heap
needs to be increased---but it's up to your init/drvr/whatever to
actually grab that space for your own, correct?  (The "use it or lose
it" theory of memory allocation.)

Suppose your init/drvr may need to allocate space later on.  Is the
usual technique to allocate all your space on startup and then reduce
it later as needed?  Thus, at initialization, you do:

    unused_siz := AllMySpace;
    unused_hdl := NewHandle (unused_siz);

when you need a new handle, you do:

    unused_siz := unused_siz - needed_siz;
    SetHandleSize (unused_hdl, unused_siz);
    needed_hdl := NewHandle (needed_size);

and when you're done with it:

    DisposHandle (needed_hdl);
    unused_siz := unused_siz + needed_siz;
    SetHandleSize (unused_hdl, unused_siz);

Is this a standard technique for handling memory allocation in inits
and drivers that may be sharing their heap with other, not-so-friendly
pieces of code?  (One problem, of course, is that someone *else* may
have fragmented your heap so that you can't restore the freed space to
your unused block.)

Charles Martin // martin@cs.uchicago.edu