source: icGREP/icgrep-devel/cudd-2.5.1/util/safe_mem.c @ 5820

Last change on this file since 5820 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 2.1 KB
Line 
1/* LINTLIBRARY */
2
3#include <stdio.h>
4#include "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
25extern "C" {
26#endif
27
28extern char *MMalloc(long);
29extern void MMout_of_memory(long);
30extern char *MMrealloc(char *, long);
31
32void (*MMoutOfMemory)(long) = MMout_of_memory;
33
34#ifdef __cplusplus
35}
36#endif
37
38
39/* MMout_of_memory -- out of memory for lazy people, flush and exit */
40void 
41MMout_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
50char *
51MMalloc(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
70char *
71MMrealloc(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
91void
92MMfree(char *obj)
93{
94    if (obj != 0) {
95        free(obj);
96    }
97}
Note: See TracBrowser for help on using the repository browser.