emergency commit
[cl-cudd.git] / distr / util / safe_mem.c
blob597cc892b3dfe42584a5a6ac44e28806e059ec40
1 /* LINTLIBRARY */
3 #include <stdio.h>
4 #include "util.h"
6 /*
7 * These are interface routines to be placed between a program and the
8 * system memory allocator.
10 * It forces well-defined semantics for several 'borderline' cases:
12 * malloc() of a 0 size object is guaranteed to return something
13 * which is not 0, and can safely be freed (but not dereferenced)
14 * free() accepts (silently) an 0 pointer
15 * realloc of a 0 pointer is allowed, and is equiv. to malloc()
16 * For the IBM/PC it forces no object > 64K; note that the size argument
17 * to malloc/realloc is a 'long' to catch this condition
19 * The function pointer MMoutOfMemory() contains a vector to handle a
20 * 'out-of-memory' error (which, by default, points at a simple wrap-up
21 * and exit routine).
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
28 extern char *MMalloc(long);
29 extern void MMout_of_memory(long);
30 extern char *MMrealloc(char *, long);
32 void (*MMoutOfMemory)(long) = MMout_of_memory;
34 #ifdef __cplusplus
36 #endif
39 /* MMout_of_memory -- out of memory for lazy people, flush and exit */
40 void
41 MMout_of_memory(long size)
43 (void) fflush(stdout);
44 (void) fprintf(stderr, "\nout of memory allocating %lu bytes\n",
45 (unsigned long) size);
46 exit(1);
50 char *
51 MMalloc(long size)
53 char *p;
55 #ifdef IBMPC
56 if (size > 65000L) {
57 if (MMoutOfMemory != (void (*)(long)) 0 ) (*MMoutOfMemory)(size);
58 return NIL(char);
60 #endif
61 if (size == 0) size = sizeof(long);
62 if ((p = (char *) malloc((unsigned long) size)) == NIL(char)) {
63 if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
64 return NIL(char);
66 return p;
70 char *
71 MMrealloc(char *obj, long size)
73 char *p;
75 #ifdef IBMPC
76 if (size > 65000L) {
77 if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
78 return NIL(char);
80 #endif
81 if (obj == NIL(char)) return MMalloc(size);
82 if (size <= 0) size = sizeof(long);
83 if ((p = (char *) realloc(obj, (unsigned long) size)) == NIL(char)) {
84 if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
85 return NIL(char);
87 return p;
91 void
92 MMfree(char *obj)
94 if (obj != 0) {
95 free(obj);