1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/* Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
* http://www.digitalmars.com
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* https://github.com/D-Programming-Language/dmd/blob/master/src/root/rmem.c
*/
#include "dsystem.h"
#include "rmem.h"
/* This implementation of the storage allocator uses the standard C allocation package.
*/
Mem mem;
char *Mem::xstrdup(const char *s)
{
char *p;
if (s)
{
#ifdef IN_GCC
p = ::xstrdup(s);
#else
p = strdup(s);
#endif
if (p)
return p;
error();
}
return NULL;
}
void *Mem::xmalloc(size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
#ifdef IN_GCC
p = ::xmalloc(size);
#else
p = malloc(size);
#endif
if (!p)
error();
}
return p;
}
void *Mem::xcalloc(size_t size, size_t n)
{ void *p;
if (!size || !n)
p = NULL;
else
{
#ifdef IN_GCC
p = ::xcalloc(size, n);
#else
p = calloc(size, n);
#endif
if (!p)
error();
}
return p;
}
void *Mem::xrealloc(void *p, size_t size)
{
if (!size)
{ if (p)
{
free(p);
p = NULL;
}
}
else if (!p)
{
#ifdef IN_GCC
p = ::xmalloc(size);
#else
p = malloc(size);
#endif
if (!p)
error();
}
else
{
void *psave = p;
#ifdef IN_GCC
p = ::xrealloc(psave, size);
#else
p = realloc(psave, size);
#endif
if (!p)
{ xfree(psave);
error();
}
}
return p;
}
void Mem::xfree(void *p)
{
if (p)
free(p);
}
void *Mem::xmallocdup(void *o, size_t size)
{ void *p;
if (!size)
p = NULL;
else
{
#ifdef IN_GCC
p = ::xmalloc(size);
#else
p = malloc(size);
#endif
if (!p)
error();
else
memcpy(p,o,size);
}
return p;
}
void Mem::error()
{
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
}
/* =================================================== */
/* Allocate, but never release
*/
// Allocate a little less than 1Mb because the C runtime adds some overhead that
// causes the actual memory block to be larger than 1Mb otherwise.
#define CHUNK_SIZE (256 * 4096 - 64)
static size_t heapleft = 0;
static void *heapp;
extern "C" void *allocmemory(size_t m_size)
{
// 16 byte alignment is better (and sometimes needed) for doubles
m_size = (m_size + 15) & ~15;
// The layout of the code is selected so the most common case is straight through
if (m_size <= heapleft)
{
L1:
heapleft -= m_size;
void *p = heapp;
heapp = (void *)((char *)heapp + m_size);
return p;
}
if (m_size > CHUNK_SIZE)
{
#ifdef IN_GCC
void *p = xmalloc(m_size);
#else
void *p = malloc(m_size);
#endif
if (p)
return p;
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
return p;
}
heapleft = CHUNK_SIZE;
#ifdef IN_GCC
heapp = xmalloc(CHUNK_SIZE);
#else
heapp = malloc(CHUNK_SIZE);
#endif
if (!heapp)
{
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
}
goto L1;
}
|