Debian Codegraph @

deps/libneon/src/ne_alloc.c

raw
1 /*
2 Replacement memory allocation handling etc.
3 Copyright (C) 1999-2005, Joe Orton <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA
19
20 */
21
22 #include "config.h"
23
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31
32 #include <stdio.h>
33
34 #include "ne_alloc.h"
35
36 static ne_oom_callback_fn oom;
37
38 void ne_oom_callback(ne_oom_callback_fn callback)
39 {
40 oom = callback;
41 }
42
43 #ifndef NEON_MEMLEAK
44
45 #define DO_MALLOC(ptr, len) do { \
46 ptr = malloc((len)); \
47 if (!ptr) { \
48 if (oom != NULL) \
49 oom(); \
50 abort(); \
51 } \
52 } while(0);
53
54 void *ne_malloc(size_t len)
55 {
56 void *ptr;
57 DO_MALLOC(ptr, len);
58 return ptr;
59 }
60
61 void *ne_calloc(size_t len)
62 {
63 void *ptr;
64 DO_MALLOC(ptr, len);
65 return memset(ptr, 0, len);
66 }
67
68 void *ne_realloc(void *ptr, size_t len)
69 {
70 void *ret = realloc(ptr, len);
71 if (!ret) {
72 if (oom)
73 oom();
74 abort();
75 }
76 return ret;
77 }
78
79 #ifdef WIN32
80 /* Implemented only to ensure free is bound to the correct DLL. */
81 void ne_free(void *ptr)
82 {
83 free(ptr);
84 }
85 #endif
86
87 char *ne_strdup(const char *s)
88 {
89 char *ret;
90 DO_MALLOC(ret, strlen(s) + 1);
91 return strcpy(ret, s);
92 }
93
94 char *ne_strndup(const char *s, size_t n)
95 {
96 char *new;
97 DO_MALLOC(new, n+1);
98 new[n] = '\0';
99 memcpy(new, s, n);
100 return new;
101 }
102
103 #else /* NEON_MEMLEAK */
104
105 #include <assert.h>
106
107 /* Memory-leak detection implementation: ne_malloc and friends are
108 * #defined to ne_malloc_ml etc by memleak.h, which is conditionally
109 * included by config.h. */
110
111 /* memory allocated be ne_*alloc, but not freed. */
112 size_t ne_alloc_used = 0;
113
114 static struct block {
115 void *ptr;
116 size_t len;
117 const char *file;
118 int line;
119 struct block *next;
120 } *blocks = NULL;
121
122 void ne_alloc_dump(FILE *f)
123 {
124 struct block *b;
125
126 for (b = blocks; b != NULL; b = b->next)
127 fprintf(f, "%" NE_FMT_SIZE_T "b@%s:%d%s", b->len, b->file, b->line,
128 b->next?", ":"");
129 }
130
131 static void *tracking_malloc(size_t len, const char *file, int line)
132 {
133 void *ptr = malloc((len));
134 struct block *block;
135
136 if (!ptr) {
137 if (oom) oom();
138 abort();
139 }
140
141 block = malloc(sizeof *block);
142 if (block != NULL) {
143 block->ptr = ptr;
144 block->len = len;
145 block->file = file;
146 block->line = line;
147 block->next = blocks;
148 blocks = block;
149 ne_alloc_used += len;
150 }
151
152 return ptr;
153 }
154
155 void *ne_malloc_ml(size_t size, const char *file, int line)
156 {
157 return tracking_malloc(size, file, line);
158 }
159
160 void *ne_calloc_ml(size_t size, const char *file, int line)
161 {
162 return memset(tracking_malloc(size, file, line), 0, size);
163 }
164
165 void *ne_realloc_ml(void *ptr, size_t s, const char *file, int line)
166 {
167 void *ret;
168 struct block *b;
169
170 if (ptr == NULL)
171 return tracking_malloc(s, file, line);
172
173 ret = realloc(ptr, s);
174 if (!ret) {
175 if (oom) oom();
176 abort();
177 }
178
179 for (b = blocks; b != NULL; b = b->next) {
180 if (b->ptr == ptr) {
181 ne_alloc_used += s - b->len;
182 b->ptr = ret;
183 b->len = s;
184 break;
185 }
186 }
187 assert(b != NULL);
188
189 return ret;
190 }
191
192 char *ne_strdup_ml(const char *s, const char *file, int line)
193 {
194 return strcpy(tracking_malloc(strlen(s) + 1, file, line), s);
195 }
196
197 char *ne_strndup_ml(const char *s, size_t n, const char *file, int line)
198 {
199 char *ret = tracking_malloc(n + 1, file, line);
200 ret[n] = '\0';
201 return memcpy(ret, s, n);
202 }
203
204 void ne_free_ml(void *ptr)
205 {
206 struct block *b, *last = NULL;
207
208 for (b = blocks; b != NULL; last = b, b = b->next) {
209 if (b->ptr == ptr) {
210 ne_alloc_used -= b->len;
211 if (last)
212 last->next = b->next;
213 else
214 blocks = b->next;
215 free(b);
216 break;
217 }
218 }
219
220 free(ptr);
221 }
222
223 #endif /* NEON_MEMLEAK */