SVF
safe_mem.c
Go to the documentation of this file.
1 /* LINTLIBRARY */
2 
3 #include <stdio.h>
4 #include "CUDD/util.h"
5 
6 /*
7  * These are interface routines to be placed between a program and the
8  * system memory allocator.
9  *
10  * It forces well-defined semantics for several 'borderline' cases:
11  *
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
18  *
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).
22  */
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 extern char *MMalloc(long);
29 extern void MMout_of_memory(long);
30 extern char *MMrealloc(char *, long);
31 
33 
34 #ifdef __cplusplus
35 }
36 #endif
37 
38 
39 /* MMout_of_memory -- out of memory for lazy people, flush and exit */
40 void
41 MMout_of_memory(long size)
42 {
43  (void) fflush(stdout);
44  (void) fprintf(stderr, "\nout of memory allocating %lu bytes\n",
45  (unsigned long) size);
46  exit(1);
47 }
48 
49 
50 char *
51 MMalloc(long size)
52 {
53  char *p;
54 
55 #ifdef IBMPC
56  if (size > 65000L) {
57  if (MMoutOfMemory != (void (*)(long)) 0 ) (*MMoutOfMemory)(size);
58  return NIL(char);
59  }
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);
65  }
66  return p;
67 }
68 
69 
70 char *
71 MMrealloc(char *obj, long size)
72 {
73  char *p;
74 
75 #ifdef IBMPC
76  if (size > 65000L) {
77  if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
78  return NIL(char);
79  }
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);
86  }
87  return p;
88 }
89 
90 
91 void
92 MMfree(char *obj)
93 {
94  if (obj != 0) {
95  free(obj);
96  }
97 }
VOID_OR_INT exit()
void(* MMoutOfMemory)(long)
Definition: safe_mem.c:32
void MMout_of_memory(long)
Definition: safe_mem.c:41
#define NIL(type)
Definition: util.h:44
char * MMrealloc(char *, long)
Definition: safe_mem.c:71
VOID_OR_INT free(void *)
void MMfree(char *obj)
Definition: safe_mem.c:92
VOID_OR_CHAR * realloc()
VOID_OR_CHAR * malloc()
char * MMalloc(long)
Definition: safe_mem.c:51