Allow implicit cast from null to maybe types ("Type?")
[delight/core.git] / dmd2 / mem.c
blob24dd49171e2031d7b2260b7b73fae5ccceb788bb
2 /* Copyright (c) 2000 Digital Mars */
3 /* All Rights Reserved */
5 /* NOTE: This file has been patched from the original DMD distribution to
6 work with the GDC compiler.
8 Modified by David Friedman, May 2005
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #if IN_GCC
16 #include "mem.h"
17 #elif linux
18 #include "../root/mem.h"
19 #else
20 #include "mem.h"
21 #endif
23 /* This implementation of the storage allocator uses the standard C allocation package.
26 Mem mem;
28 void Mem::init()
32 char *Mem::strdup(const char *s)
34 char *p;
36 if (s)
38 p = ::strdup(s);
39 if (p)
40 return p;
41 error();
43 return NULL;
46 void *Mem::malloc(size_t size)
47 { void *p;
49 if (!size)
50 p = NULL;
51 else
53 p = ::malloc(size);
54 if (!p)
55 error();
57 return p;
60 void *Mem::calloc(size_t size, size_t n)
61 { void *p;
63 if (!size || !n)
64 p = NULL;
65 else
67 p = ::calloc(size, n);
68 if (!p)
69 error();
71 return p;
74 void *Mem::realloc(void *p, size_t size)
76 if (!size)
77 { if (p)
78 { ::free(p);
79 p = NULL;
82 else if (!p)
84 p = ::malloc(size);
85 if (!p)
86 error();
88 else
90 p = ::realloc(p, size);
91 if (!p)
92 error();
94 return p;
97 void Mem::free(void *p)
99 if (p)
100 ::free(p);
103 void *Mem::mallocdup(void *o, size_t size)
104 { void *p;
106 if (!size)
107 p = NULL;
108 else
110 p = ::malloc(size);
111 if (!p)
112 error();
113 else
114 memcpy(p,o,size);
116 return p;
119 void Mem::error()
121 fprintf(stderr, "Error: out of memory\n");
122 exit(EXIT_FAILURE);
125 void Mem::fullcollect()
129 void Mem::mark(void *pointer)
131 (void) pointer; // necessary for VC /W4
134 /* =================================================== */
136 void * operator new(size_t m_size)
138 void *p = malloc(m_size);
139 if (p)
140 return p;
141 fprintf(stderr, "Error: out of memory\n");
142 exit(EXIT_FAILURE);
143 return p;
146 void operator delete(void *p)
148 free(p);