1 /* $Id: xalloc.c,v 1.6 2002/03/02 20:35:52 sverrehu Exp $ */
2 /*------------------------------------------------------------------------
3 | FILE xalloc.c
4 | MODULE OF xalloc - memory allocation with error checking
5 |
6 | DESCRIPTION Functions common to the entire library.
7 |
8 | WRITTEN BY Sverre H. Huseby <shh@thathost.com>
9 +----------------------------------------------------------------------*/
10
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include "xalloc.h"
15
16 /*-----------------------------------------------------------------------+
17 | PRIVATE DATA |
18 +-----------------------------------------------------------------------*/
19
20 /* Pointer to the error handler function. If the value is NULL, the
21 * default action is taken (program abortion). */
22 static void (*xaErrFunc)(void);
23
24 /*-----------------------------------------------------------------------+
25 | PUBLIC FUNCTIONS |
26 +-----------------------------------------------------------------------*/
27
28 /*------------------------------------------------------------------------
29 | NAME _xaOutOfMen
30 |
31 | FUNCTION Handle out of memory -problems.
32 |
33 | DESCRIPTION This is the function that is called when any of the
34 | memory functions fail. If the user has set up a handler
35 | function, this function is called. Else the program
36 | is aborted with an "out of memory"-message on stdout.
37 |
38 | NOTE This is considered an internal function. It should not
39 | be called by the user.
40 */
41 void
_xaOutOfMem(void)42 _xaOutOfMem(void)
43 {
44 if (!xaErrFunc)
45 fprintf(stderr, "out of memory\n");
46 else
47 xaErrFunc();
48 exit(99);
49 }
50
51 /*------------------------------------------------------------------------
52 | NAME xaSetErrFunc
53 |
54 | FUNCTION Specify a user supplied error handling function.
55 |
56 | SYNOPSIS #include "xalloc.h"
57 | void xaSetErrFunc(void (*f)(void));
58 |
59 | INPUT f pointer to function that should be called when
60 | any of the memory (re-)allocating functions
61 | fail. If the function supplied returns, the
62 | program is aborted. The function may very well
63 | use setjmp/longjmp to do some major recovery.
64 |
65 | Let f be NULL to restore the default action,
66 | which is to display "out of memory" and abort
67 | the program.
68 */
69 void
xaSetErrFunc(void (* f)(void))70 xaSetErrFunc(void (*f)(void))
71 {
72 xaErrFunc = f;
73 }
74