Send "OPTION PINENTRY=N" when PWMD_OPTION_PINENTRY changes to enable
[libpwmd.git] / mem.c
blob84a4b2dc7e221db8d7cbcfccd4ca663899e37492
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);
45 p = m->next;
46 free(m);
48 if (last) {
49 if (p)
50 last->next = p;
51 else
52 last->next = NULL;
54 else if (p)
55 list = p;
56 else
57 list = NULL;
59 break;
62 last = m;
65 return list;
68 static struct memlist_s *memlist_append(struct memlist_s *list, struct memlist_s *p)
70 struct memlist_s *m;
72 if (!list) {
73 list = p;
74 list->next = NULL;
75 return list;
78 for (m = list; m; m = m->next) {
79 if (!m->next) {
80 m->next = p;
81 m = m->next;
82 m->next = NULL;
83 break;
87 return list;
90 void xfree(void *ptr)
92 struct memlist_s *m;
94 if (!ptr)
95 return;
97 for (m = memlist; m; m = m->next) {
98 if (m->ptr == ptr) {
99 #ifdef DEBUG
100 fprintf(stderr, "xfree(): %p %i\n", ptr, m->size);
101 #endif
102 memlist = memlist_remove(memlist, m);
103 return;
107 warnx("xfree(): %p %s:", ptr, N_("not found"));
108 assert(0);
111 void *xmalloc(size_t size)
113 void *p;
114 struct memlist_s *m;
116 if (size <= 0)
117 return NULL;
119 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
120 return NULL;
122 if ((p = malloc(size)) == NULL) {
123 free(m);
124 return NULL;
127 m->ptr = p;
128 m->size = size;
129 memlist = memlist_append(memlist, m);
130 #ifdef DEBUG
131 fprintf(stderr, "xmalloc(): %p %i\n", p, size);
132 #endif
133 return m->ptr;
136 void *xcalloc(size_t nmemb, size_t size)
138 void *p;
139 struct memlist_s *m;
141 if (size <= 0)
142 return NULL;
144 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
145 return NULL;
147 if ((p = calloc(nmemb, size)) == NULL) {
148 free(m);
149 return NULL;
152 m->ptr = p;
153 m->size = nmemb * size;
154 memlist = memlist_append(memlist, m);
155 #ifdef DEBUG
156 fprintf(stderr, "xcalloc(): %p %i\n", p, nmemb * size);
157 #endif
158 return m->ptr;
161 void *xrealloc(void *ptr, size_t size)
163 void *p;
164 struct memlist_s *m;
166 if (!ptr)
167 return xmalloc(size);
169 if (size <= 0)
170 return ptr;
172 for (m = memlist; m; m = m->next) {
173 if (m->ptr == ptr) {
174 if ((p = malloc(size)) == NULL)
175 return NULL;
177 memcpy(p, m->ptr, size < m->size ? size : m->size);
178 memset(m->ptr, 0, m->size);
179 free(m->ptr);
180 m->ptr = p;
181 m->size = size;
182 #ifdef DEBUG
183 fprintf(stderr, "xrealloc(): %p %i\n", p, size);
184 #endif
185 return m->ptr;
189 warnx("xrealloc(): %p %s", ptr, N_("not found"));
190 assert(0);
191 return NULL;
194 char *xstrdup(const char *str)
196 char *t, *tp;
197 size_t len;
198 const char *p;
200 if (!str)
201 return NULL;
203 len = strlen(str) + 1;
205 if ((t = (char *)xmalloc(len * sizeof(char))) == NULL)
206 return NULL;
208 for (p = str, tp = t; *p; p++)
209 *tp++ = *p;
211 *tp = 0;
212 #ifdef DEBUG
213 fprintf(stderr, "xstrdup(): %p\n", t);
214 #endif
215 return t;
218 void xpanic(void)
220 struct memlist_s *m;
222 for (m = memlist; m; m = memlist)
223 xfree(m->ptr);