Amiga Computing - Commodore Is Awesome
Amiga Computing - Commodore Is Awesome
Amiga Computing - Commodore Is Awesome
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
•<br />
64<br />
A ILOCATION AND I<br />
DEALLOCATION<br />
From time to time in my assembler<br />
columns etc_ I've talked about<br />
my stack-based schemes used to<br />
handle allocation and deallocation<br />
operations. Now you are going to<br />
get the chance to see how I do it<br />
from C when several different<br />
code modules are in use. Stacks,<br />
of course, store things on a last in<br />
First out basis and what my<br />
scheme does is ensure that ANY<br />
routine which successfully allocates<br />
or opens some returnable or<br />
closeable system resource pushes<br />
the address of a corresponding<br />
closedown routine onto a stack.<br />
To allow for allocation failures<br />
as the program runs, I adopt the<br />
convention of having all allocation<br />
routines return error numbers that<br />
indicate whether they've succeeded<br />
or not Providing these conventions<br />
are followed, a program<br />
is able to perform its closedown<br />
operations by using a loop to pull<br />
those deallocation routine pointers<br />
from the stack and executing<br />
the corresponding routines. The<br />
beauty of this approach is that the<br />
stack automatically deallocates<br />
things in the reverse order to<br />
the original allocations - things<br />
which are allocated last get deallocated<br />
first (a good, safe, general<br />
method to adopt with all <strong>Amiga</strong><br />
programs).<br />
How do I set up the allocation/de-allocation<br />
code and get<br />
the right pieces of code executed<br />
at the appropriate times? The best<br />
idea is to look at some example<br />
fragments. Listing 1 shows two<br />
routines which open and close the<br />
Intuition library. Notice that the<br />
UWE AllocateResourtelLIEWE tount,WITE f*listC3)Evoid))<br />
UWE i, error_ntiober;<br />
for (i:0;1