Added configuration parameter "recursion_depth". This specifies the
[pwmd.git] / src / mem.c
blobffeb724f5d9b5877b931797ac9c56e7cfa65ad85
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2007 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <err.h>
26 #include "gettext.h"
27 #define N_(msgid) gettext(msgid)
29 #include "mem.h"
31 static struct memlist_s *memlist;
33 static struct memlist_s *memlist_remove(struct memlist_s *list, struct memlist_s *r)
35 struct memlist_s *m, *last = NULL, *p;
37 for (m = list; m; m = m->next) {
38 if (m->ptr == r->ptr) {
39 memset(m->ptr, 0, m->size);
40 free(m->ptr);
42 if (RUNNING_ON_VALGRIND)
43 VALGRIND_FREELIKE_BLOCK(m->ptr, 0);
45 p = m->next;
46 free(m);
48 if (RUNNING_ON_VALGRIND)
49 VALGRIND_FREELIKE_BLOCK(m, 0);
51 if (last) {
52 if (p)
53 last->next = p;
54 else
55 last->next = NULL;
57 else if (p)
58 list = p;
59 else
60 list = NULL;
62 break;
65 last = m;
68 return list;
71 static struct memlist_s *memlist_append(struct memlist_s *list, struct memlist_s *p)
73 struct memlist_s *m;
75 if (!list) {
76 list = p;
77 list->next = NULL;
78 return list;
81 for (m = list; m; m = m->next) {
82 if (!m->next) {
83 m->next = p;
84 m = m->next;
85 m->next = NULL;
86 break;
90 return list;
93 void xfree(void *ptr)
95 struct memlist_s *m;
97 if (!ptr)
98 return;
100 for (m = memlist; m; m = m->next) {
101 if (m->ptr == ptr) {
102 #ifdef DEBUG
103 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, ptr, m->size);
104 #endif
105 memlist = memlist_remove(memlist, m);
106 return;
110 warnx(N_("%s: %p not found"), __FUNCTION__, ptr);
111 assert(0);
114 void *xmalloc(size_t size)
116 void *p;
117 struct memlist_s *m;
119 if (size <= 0)
120 return NULL;
122 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
123 return NULL;
125 if (RUNNING_ON_VALGRIND)
126 VALGRIND_MALLOCLIKE_BLOCK(m, sizeof(struct memlist_s), 0, 0);
128 if ((p = malloc(size)) == NULL) {
129 free(m);
131 if (RUNNING_ON_VALGRIND)
132 VALGRIND_FREELIKE_BLOCK(m, 0);
134 return NULL;
137 if (RUNNING_ON_VALGRIND)
138 VALGRIND_MALLOCLIKE_BLOCK(p, size, 0, 0);
140 m->ptr = p;
141 m->size = size;
142 memlist = memlist_append(memlist, m);
143 #ifdef DEBUG
144 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, size);
145 #endif
146 return m->ptr;
149 void *xcalloc(size_t nmemb, size_t size)
151 void *p;
152 struct memlist_s *m;
154 if (size <= 0)
155 return NULL;
157 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
158 return NULL;
160 if (RUNNING_ON_VALGRIND)
161 VALGRIND_MALLOCLIKE_BLOCK(m, sizeof(struct memlist_s), 0, 0);
163 if ((p = calloc(nmemb, size)) == NULL) {
164 free(m);
166 if (RUNNING_ON_VALGRIND)
167 VALGRIND_FREELIKE_BLOCK(m, 0);
169 return NULL;
172 if (RUNNING_ON_VALGRIND)
173 VALGRIND_MALLOCLIKE_BLOCK(p, nmemb * size, 0, 1);
175 m->ptr = p;
176 m->size = nmemb * size;
177 memlist = memlist_append(memlist, m);
178 #ifdef DEBUG
179 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, nmemb * size);
180 #endif
181 return m->ptr;
184 void *xrealloc(void *ptr, size_t size)
186 void *p;
187 struct memlist_s *m;
189 if (!ptr)
190 return xmalloc(size);
192 if (size <= 0)
193 return ptr;
195 for (m = memlist; m; m = m->next) {
196 if (m->ptr == ptr) {
197 if ((p = malloc(size)) == NULL)
198 return NULL;
200 if (RUNNING_ON_VALGRIND)
201 VALGRIND_MALLOCLIKE_BLOCK(p, size, 0, 0);
203 memcpy(p, m->ptr, size < m->size ? size : m->size);
204 memset(m->ptr, 0, m->size);
205 free(m->ptr);
207 if (RUNNING_ON_VALGRIND)
208 VALGRIND_FREELIKE_BLOCK(m->ptr, 0);
210 m->ptr = p;
211 m->size = size;
212 #ifdef DEBUG
213 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, size);
214 #endif
215 return m->ptr;
219 warnx(N_("%s: %p not found"), __FUNCTION__, ptr);
220 assert(0);
221 return NULL;
224 char *xstrdup(const char *str)
226 char *t, *tp;
227 size_t len;
228 const char *p;
230 if (!str)
231 return NULL;
233 len = strlen(str) + 1;
235 if ((t = (char *)xmalloc(len * sizeof(char))) == NULL)
236 return NULL;
238 for (p = str, tp = t; *p; p++)
239 *tp++ = *p;
241 *tp = 0;
242 #ifdef DEBUG
243 fprintf(stderr, "%s: %p\n", __FUNCTION__, t);
244 #endif
245 return t;
248 void xpanic(void)
250 struct memlist_s *m;
252 for (m = memlist; m; m = memlist)
253 xfree(m->ptr);