home *** CD-ROM | disk | FTP | other *** search
-
-
- README file for the Hackercorp Amiga cleanup routines
- -----------------------------------------------------
-
- The cleanup routines are placed in the public domain by
- Hackercorp in the hope that they will be of use to other
- Amiga developers.
-
- They may be included in a commercial product without
- license from or royalty to Hackercorp. Credit need
- not be given to Hackercorp in documentation, title
- screen, etc, although it is always appreciated.
-
- If you pass along the source, we ask that you retain
- our names as the developers of the tool.
-
- All correspondence is welcome. If you fix a bug, make
- an improvement, etc, try to pass it back to us.
-
-
- The Cleanup Routines
- --------------------
-
- Everyone who has ever programmed the Amiga in C has had to deal with the
- fact that the Amiga doesn't do resource tracking. Consequently, when
- your program exits, it must free all its locks, close all its libraries,
- close all its screens, free all its memory, etc, etc, etc.
-
- This job can be quite daunting, especially when a failure to allocate
- something can occur at any point in the program's startup or execution.
-
- We at Hackercorp have evolved some techniques and written some code
- to help ease this burden and we are sharing them now, to wit:
-
- o add_cleanup() routine that lets you queue up functions to be
- executed at exit time
-
- o cleanup() which is called at exit time to execute your cleanup
- routines
-
-
- o Initialize your pointers to NULL and only deallocate in your
- cleanup routines if the pointer is non-null.
-
-
-
- Example
- -------
-
- Consider a large program. You may find it reasonable to open and close
- all of your libraries in a file called libraries.c. There is an init
- routine to be called from main() or somewhere else in your startup.
- That init routine, when executed, calls add_cleanup to queue the cleanup
- routine to be executed at exit time.
-
- An example from one of my programs:
-
- --- libraries.c ------------------------
-
- #include <functions.h>
- #include <exec/types.h>
- #include <exec/devices.h>
- #include <intuition/intuition.h>
- #include <ctype.h>
- #include <midi/midi.h>
-
- struct IntuitionBase *IntuitionBase = NULL;
- struct MidiBase *MidiBase = NULL;
-
- trash_libs()
- {
- if (IntuitionBase)
- CloseLibrary(IntuitionBase);
-
- if (MidiBase)
- CloseLibrary(MidiBase);
- }
-
- init_libs()
- {
- add_cleanup(trash_libs);
-
- if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L)))
- panic("Can't open intuition.library");
-
- MidiBase = (struct MidiBase *)OpenLibrary(MIDINAME,MIDIVERSION);
-
- if (MidiBase == NULL)
- panic("Can't open midi.library");
- }
-
- --------------------------------------
-
-
- You then call init_libs() from your main prior to using the libraries.
- When you exit, be sure and call cleanup(). If you do so, cleanup will
- execute your cleanup routines in the reverse order they were queued,
- which is the reasonable way to do it. This way, you don't have to
- have icky all-encompassing cleanup routines.
-
- The Manx I/O routines abort the program if they detect a control-C when
- they're called. There's a routine called _abort() that gets called by
- the control-C code, so we need to replace it with our own that calls
- cleanup. For a CLI program, this could be:
-
- _abort()
- {
- cleanup();
- printf("^C abort\n");
- exit(1);
- }
-
- Workbench may want to do fancier things.
-
- I like to have a routine called panic() that takes a string for the
- panic reason and just lets you get the heck out with the minimum
- effort. Note the calls to panic in the init_libs routine above.
- It's a bit drastic, but appropriate when appropriate.
-
- A panic routine for a CLI program might be:
-
- panic(s)
- char *s;
- {
- fprintf(stderr,"panic: %s\n",s);
- fflush(stdout);
- fflush(stderr);
- cleanup();
- exit(1);
- }
-
- The panic function provide in the cleanup.c file is a little slicker,
- it can detect a "double panic." This is a condition that could occur
- if one of your cleanup routines ended up doing a panic() during a
- panic().
-
- There's a file called displayrequest.c included herein that takes a
- string and puts up a requester. Workbench-launchable programs, rather
- than printf'ing the error, might consider doing a:
-
- DisplayRequest(s,"PANIC","PANIC");
-
- Note that intuition.library must be open for this to work.
-
-
-
- Regards,
-
- Karl Lehenbauer
- Peter da Silva
-
- Hackercorp
- 3918 Panorama
- Missouri City, TX 77459
- 713-438-4964 voice
- 713-438-5018 data
-
- Internet/BITNET: karl@sugar.hackercorp.com, peter@sugar.hackercorp.com
- USENET: uunet!sugar!karl, uunet!sugar!peter
-