[comp.sys.amiga] How to make software more reliable. Commercial developers take note!

bryce@hoser.berkeley.edu (Bryce Nesbitt) (10/15/87)

	[ If you can't read this, you have a line eater bug. ]

--- ---
--- ---

All your programs all handle out_of_memory correctly, right?  It is possible
that other programs that you multitask with will not... and your program
could get the blame.  Or you might need to call DOS in an emergency
situation (like needing to SAVE a file) and it needs to allocate a lock.
The tiny function below keeps the system reliable even in such adverse
conditions:

------- cut here ------
#include "exec/memory.h"

/* Safe AllocMem.  Will not let your run the system down to zero bytes.
 * Crafted for all to use by Bryce Nesbitt
 *
 * You may wish to fiddle with PANIC_FACTOR.
 */
#define PANIC_FACTOR_CHIP 8192L

APTR  SafeAllocMem(size,flags)
ULONG size;
ULONG flags;
{
register APTR p;

  if ( p=(APTR)AllocMem(size, flags) )
    if ( AvailMem(MEMF_CHIP) < PANIC_FACTOR_CHIP )
      { FreeMem(p,size); return(0); } /* System is low... no go! */
return(p);
}
------- cut here ------

Simply replace any and all AllocMem() calls with SafeAllocMem().  While you
are at it check that a zero return from this function is handled with
grace.	Even if you can't handle it with complete grace, at least don't
corrupt memory by using a zero pointer!!

This does not slow things down by any significant ammount.  (Though it
could be made faster with a few quick hacks to exec :-)

If you need to tell the user that the system is out of memory, but there is
not enough memory to post a requester or Alert, simply set the title of
your Window or Screen to "** NO MEMORY **" (possibly with a red pen color).
This operation does not require any allocations.


Bill Volk, of Ageis (to take an example off the top of my head) has used this
with great sucess in programs that were reputed to be bulletproof even under
Kickstart V1.1! (V1.1 had lots of memory problems itself.  No help from the
application program needed then)

|\ /|  . Ack! (NAK, ENQ, SYN)
{o O} . bryce@hoser.berkeley.EDU -or- ucbvax!hoser!bryce
 (") 
  U	"It [the Amiga] is the machine the home market has been waiting
	for.  It's a computer without flaws." - David Seuss, President
	Spinnaker Software