-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmalloc.c
276 lines (216 loc) · 5.33 KB
/
nmalloc.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
TRURLib
Copyright (C) 1999 Pawel A. Gajda ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nassert.h"
#include "nmalloc.h"
typedef void (*memh) (void);
static memh mem_fail_fn = NULL;
void (*setxmallocs_handler(void (*handler) (void))) (void) {
memh tmp = mem_fail_fn;
mem_fail_fn = handler;
return tmp;
}
void (*n_malloc_set_failhook(void (*fn) (void)))
{
memh tmp = mem_fail_fn;
mem_fail_fn = fn;
return tmp;
}
static void nomem(void)
{
if (mem_fail_fn != NULL)
mem_fail_fn();
else {
printf("Memory exhausted\n");
exit(1);
}
}
void *n_malloc(size_t size)
{
register void *v;
if ((v = malloc(size)) == NULL)
nomem();
return v;
}
void *n_calloc(size_t nmemb, size_t size)
{
register void *v = calloc(nmemb, size);
if (v == 0)
nomem();
return v;
}
void *n_realloc(void *ptr, size_t size)
{
n_assert(size > 0);
if ((ptr = realloc(ptr, size)) == NULL)
nomem();
return ptr;
}
char *n_strdupl(const char *s, size_t length)
{
register char *new;
if ((new = n_malloc(length + 1)) == NULL) {
nomem();
return NULL;
}
memcpy(new, s, length);
new[length] = '\0';
return new;
}
char *n_strdup(const char *s)
{
register size_t len = strlen(s) + 1;
register char *new;
if ((new = n_malloc(len)) == NULL) {
nomem();
return NULL;
}
return memcpy(new, s, len);
}
void *n_memdup(const void *ptr, size_t size)
{
register void *new;
if ((new = n_malloc(size)) == NULL) {
nomem();
return NULL;
}
return memcpy(new, ptr, size);
}
void n_free(void *ptr)
{
free(ptr);
}
void n_cfree(void *ptr)
{
void **pptr = (void**)ptr;
if (*pptr) {
free(*pptr);
*pptr = NULL;
}
}
static void *malloc_malloc(tn_alloc *na, size_t size)
{
na = na;
return n_malloc(size);
}
static
void *malloc_calloc(tn_alloc *na, size_t size)
{
na = na;
return n_calloc(size, 1);
}
static
void *malloc_realloc(tn_alloc *na, void *ptr, size_t size, size_t newsize)
{
na = na; size = size;
return n_realloc(ptr, newsize);
}
static
void malloc_free(tn_alloc *na, void *ptr)
{
na = na;
return n_free(ptr);
}
#define obstack_chunk_alloc n_malloc
#define obstack_chunk_free free
#if HAVE_OBSTACK
# include <obstack.h>
#else
# include "lib/obstack.h"
#endif
static
void *aobstack_malloc(tn_alloc *na, size_t size)
{
return obstack_alloc((struct obstack*)na->_privdata, size);
}
static
void *aobstack_calloc(tn_alloc *na, size_t size)
{
void *ptr = obstack_alloc((struct obstack*)na->_privdata, size);
memset(ptr, 0, size);
return ptr;
}
static
void aobstack_free(tn_alloc *na, void *ptr)
{
na = na;
ptr = ptr;
}
void *aobstack_realloc(tn_alloc *na, void *ptr, size_t size, size_t newsize)
{
void *new;
new = obstack_alloc((struct obstack*)na->_privdata, newsize);
memcpy(new, ptr, size);
return new;
}
void n_alloc_debug_dump(tn_alloc *na)
{
fprintf(stderr, "%p chunk_size %ld\n", na,
((struct obstack*)na->_privdata)->chunk_size);
}
tn_alloc *n_alloc_new(size_t chunkkb, unsigned int flags)
{
tn_alloc *na;
na = n_calloc(1, sizeof(*na));
na->_refcnt = 0;
na->_flags = flags;
if (flags & TN_ALLOC_MALLOC) {
na->_privdata = NULL;
na->na_malloc = malloc_malloc;
na->na_calloc = malloc_calloc;
na->na_free = malloc_free;
na->na_realloc = malloc_realloc;
} else if (flags & TN_ALLOC_OBSTACK) {
struct obstack *ob = n_malloc(sizeof(*ob));
obstack_init(ob);
if (chunkkb < 2)
chunkkb = 2;
if (chunkkb > 4096)
fprintf(stderr, "n_alloc_new: do you really request obstack's "
"chunk size greater than 4M?\n");
obstack_chunk_size(ob) = 1024 * chunkkb;
#if HAVE_CPU_UNALIGNED_ACCESS
obstack_alignment_mask(ob) = 0; /* TODO: configurable */
#endif
na->_privdata = ob;
na->na_malloc = aobstack_malloc;
na->na_calloc = aobstack_calloc;
na->na_free = aobstack_free;
na->na_realloc = aobstack_realloc;
} else {
n_assert(0);
}
return na;
}
void n_alloc_free(tn_alloc *na)
{
if (na->_refcnt > 0) {
na->_refcnt--;
return;
}
if (na->_flags & TN_ALLOC_OBSTACK) {
obstack_free(na->_privdata, NULL);
n_free(na->_privdata);
}
n_free(na);
}