1
|
1
|
|
2 /* Copyright (c) 2000 Digital Mars */
|
|
3 /* All Rights Reserved */
|
|
4
|
|
5 #include <stdio.h>
|
|
6 #include <stdlib.h>
|
|
7 #include <string.h>
|
|
8
|
|
9 #include "gc.h"
|
|
10
|
|
11 #include "mem.h"
|
|
12
|
|
13 /* This implementation of the storage allocator uses the standard C allocation package.
|
|
14 */
|
|
15
|
|
16 Mem mem;
|
|
17
|
|
18 void Mem::init()
|
|
19 {
|
|
20 GC_init();
|
|
21 }
|
|
22
|
|
23 char *Mem::strdup(const char *s)
|
|
24 {
|
|
25 char *p;
|
|
26
|
|
27 if (s)
|
|
28 {
|
|
29 p = GC_strdup(s);
|
|
30 if (p)
|
|
31 return p;
|
|
32 error();
|
|
33 }
|
|
34 return NULL;
|
|
35 }
|
|
36
|
|
37 void *Mem::malloc(size_t size)
|
|
38 { void *p;
|
|
39
|
|
40 if (!size)
|
|
41 p = NULL;
|
|
42 else
|
|
43 {
|
|
44 p = GC_malloc(size);
|
|
45 if (!p)
|
|
46 error();
|
|
47 }
|
|
48 return p;
|
|
49 }
|
|
50
|
|
51 void *Mem::calloc(size_t size, size_t n)
|
|
52 { void *p;
|
|
53
|
|
54 if (!size || !n)
|
|
55 p = NULL;
|
|
56 else
|
|
57 {
|
|
58 p = GC_malloc(size * n);
|
|
59 if (!p)
|
|
60 error();
|
|
61 memset(p, 0, size * n);
|
|
62 }
|
|
63 return p;
|
|
64 }
|
|
65
|
|
66 void *Mem::realloc(void *p, size_t size)
|
|
67 {
|
|
68 if (!size)
|
|
69 { if (p)
|
|
70 { GC_free(p);
|
|
71 p = NULL;
|
|
72 }
|
|
73 }
|
|
74 else if (!p)
|
|
75 {
|
|
76 p = GC_malloc(size);
|
|
77 if (!p)
|
|
78 error();
|
|
79 }
|
|
80 else
|
|
81 {
|
|
82 p = GC_realloc(p, size);
|
|
83 if (!p)
|
|
84 error();
|
|
85 }
|
|
86 return p;
|
|
87 }
|
|
88
|
|
89 void Mem::free(void *p)
|
|
90 {
|
|
91 if (p)
|
|
92 GC_free(p);
|
|
93 }
|
|
94
|
|
95 void *Mem::mallocdup(void *o, size_t size)
|
|
96 { void *p;
|
|
97
|
|
98 if (!size)
|
|
99 p = NULL;
|
|
100 else
|
|
101 {
|
|
102 p = GC_malloc(size);
|
|
103 if (!p)
|
|
104 error();
|
|
105 else
|
|
106 memcpy(p,o,size);
|
|
107 }
|
|
108 return p;
|
|
109 }
|
|
110
|
|
111 void Mem::error()
|
|
112 {
|
|
113 printf("Error: out of memory\n");
|
|
114 exit(EXIT_FAILURE);
|
|
115 }
|
|
116
|
|
117 void Mem::fullcollect()
|
|
118 {
|
|
119 GC_gcollect();
|
|
120 }
|
|
121
|
|
122 void Mem::mark(void *pointer)
|
|
123 {
|
|
124 (void) pointer; // necessary for VC /W4
|
|
125 }
|
|
126
|
|
127 /* =================================================== */
|
|
128
|
|
129 void * operator new(size_t m_size)
|
|
130 {
|
|
131 void *p = GC_malloc(m_size);
|
|
132 if (p)
|
|
133 return p;
|
|
134 printf("Error: out of memory\n");
|
|
135 exit(EXIT_FAILURE);
|
|
136 return p;
|
|
137 }
|
|
138
|
|
139 void operator delete(void *p)
|
|
140 {
|
|
141 GC_free(p);
|
|
142 }
|
|
143
|
|
144
|