Non Linux 2.6 systems need to specify --disable-locking to configure.
[pwmd.git] / src / mem.c
blobf054407f297b28d2f10a8f579f0f3da05e5ce028
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 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #include "gettext.h"
31 #define N_(msgid) gettext(msgid)
33 #include "mem.h"
35 static struct memlist_s *memlist;
37 static struct memlist_s *memlist_remove(struct memlist_s *list, struct memlist_s *r)
39 struct memlist_s *m, *last = NULL, *p;
41 for (m = list; m; m = m->next) {
42 if (m->ptr == r->ptr) {
43 memset(m->ptr, 0, m->size);
44 free(m->ptr);
46 p = m->next;
47 free(m);
49 if (last) {
50 if (p)
51 last->next = p;
52 else
53 last->next = NULL;
55 else if (p)
56 list = p;
57 else
58 list = NULL;
60 break;
63 last = m;
66 return list;
69 static struct memlist_s *memlist_append(struct memlist_s *list, struct memlist_s *p)
71 struct memlist_s *m;
73 if (!list) {
74 list = p;
75 list->next = NULL;
76 return list;
79 for (m = list; m; m = m->next) {
80 if (!m->next) {
81 m->next = p;
82 m = m->next;
83 m->next = NULL;
84 break;
88 return list;
91 void xfree(void *ptr)
93 struct memlist_s *m;
95 if (!ptr)
96 return;
98 for (m = memlist; m; m = m->next) {
99 if (m->ptr == ptr) {
100 #ifdef DEBUG
101 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, ptr, m->size);
102 #endif
103 memlist = memlist_remove(memlist, m);
104 return;
108 warnx(N_("%s: %p not found"), __FUNCTION__, ptr);
109 assert(0);
112 void *xmalloc(size_t size)
114 void *p;
115 struct memlist_s *m;
117 if (size <= 0)
118 return NULL;
120 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
121 return NULL;
123 if ((p = malloc(size)) == NULL) {
124 free(m);
125 return NULL;
128 m->ptr = p;
129 m->size = size;
130 memlist = memlist_append(memlist, m);
131 #ifdef DEBUG
132 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, size);
133 #endif
134 return m->ptr;
137 void *xcalloc(size_t nmemb, size_t size)
139 void *p;
140 struct memlist_s *m;
142 if (size <= 0)
143 return NULL;
145 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
146 return NULL;
148 if ((p = calloc(nmemb, size)) == NULL) {
149 free(m);
150 return NULL;
153 m->ptr = p;
154 m->size = nmemb * size;
155 memlist = memlist_append(memlist, m);
156 #ifdef DEBUG
157 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, nmemb * size);
158 #endif
159 return m->ptr;
162 void *xrealloc(void *ptr, size_t size)
164 void *p;
165 struct memlist_s *m;
167 if (!ptr)
168 return xmalloc(size);
170 if (size <= 0)
171 return ptr;
173 for (m = memlist; m; m = m->next) {
174 if (m->ptr == ptr) {
175 if ((p = malloc(size)) == NULL)
176 return NULL;
178 memcpy(p, m->ptr, size < m->size ? size : m->size);
179 memset(m->ptr, 0, m->size);
180 free(m->ptr);
181 m->ptr = p;
182 m->size = size;
183 #ifdef DEBUG
184 fprintf(stderr, "%s: %p %i\n", __FUNCTION__, p, size);
185 #endif
186 return m->ptr;
190 warnx(N_("%s: %p not found"), __FUNCTION__, ptr);
191 assert(0);
192 return NULL;
195 char *xstrdup(const char *str)
197 char *t, *tp;
198 size_t len;
199 const char *p;
201 if (!str)
202 return NULL;
204 len = strlen(str) + 1;
206 if ((t = (char *)xmalloc(len * sizeof(char))) == NULL)
207 return NULL;
209 for (p = str, tp = t; *p; p++)
210 *tp++ = *p;
212 *tp = 0;
213 #ifdef DEBUG
214 fprintf(stderr, "%s: %p\n", __FUNCTION__, t);
215 #endif
216 return t;
219 void xpanic(void)
221 struct memlist_s *m;
223 for (m = memlist; m; m = memlist)
224 xfree(m->ptr);