Make the filename argument to pwmc optional. The command may be a
[libpwmd.git] / mem.c
blobb4c9880520de8f2983a86848863b928b5601affe
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 "mem.h"
28 static struct memlist_s *memlist;
30 static struct memlist_s *memlist_remove(struct memlist_s *list, struct memlist_s *r)
32 struct memlist_s *m, *last = NULL, *p;
34 for (m = list; m; m = m->next) {
35 if (m->ptr == r->ptr) {
36 memset(m->ptr, 0, m->size);
37 free(m->ptr);
38 p = m->next;
39 free(m);
41 if (last) {
42 if (p)
43 last->next = p;
44 else
45 last->next = NULL;
47 else if (p)
48 list = p;
49 else
50 list = NULL;
52 break;
55 last = m;
58 return list;
61 static struct memlist_s *memlist_append(struct memlist_s *list, struct memlist_s *p)
63 struct memlist_s *m;
65 if (!list) {
66 list = p;
67 list->next = NULL;
68 return list;
71 for (m = list; m; m = m->next) {
72 if (!m->next) {
73 m->next = p;
74 m = m->next;
75 m->next = NULL;
76 break;
80 return list;
83 void xfree(void *ptr)
85 struct memlist_s *m;
87 if (!ptr)
88 return;
90 for (m = memlist; m; m = m->next) {
91 if (m->ptr == ptr) {
92 #ifdef DEBUG
93 fprintf(stderr, "xfree(): %p %i\n", ptr, m->size);
94 #endif
95 memlist = memlist_remove(memlist, m);
96 return;
100 warnx("xfree(): %p not found", ptr);
101 assert(0);
104 void *xmalloc(size_t size)
106 void *p;
107 struct memlist_s *m;
109 if (size <= 0)
110 return NULL;
112 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
113 return NULL;
115 if ((p = malloc(size)) == NULL) {
116 free(m);
117 return NULL;
120 m->ptr = p;
121 m->size = size;
122 memlist = memlist_append(memlist, m);
123 #ifdef DEBUG
124 fprintf(stderr, "xmalloc(): %p %i\n", p, size);
125 #endif
126 return m->ptr;
129 void *xcalloc(size_t nmemb, size_t size)
131 void *p;
132 struct memlist_s *m;
134 if (size <= 0)
135 return NULL;
137 if ((m = (struct memlist_s *)malloc(sizeof(struct memlist_s))) == NULL)
138 return NULL;
140 if ((p = calloc(nmemb, size)) == NULL) {
141 free(m);
142 return NULL;
145 m->ptr = p;
146 m->size = size;
147 memlist = memlist_append(memlist, m);
148 #ifdef DEBUG
149 fprintf(stderr, "xcalloc(): %p %i\n", p, size);
150 #endif
151 return m->ptr;
154 void *xrealloc(void *ptr, size_t size)
156 void *p;
157 struct memlist_s *m;
159 if (!ptr)
160 return xmalloc(size);
162 if (size <= 0)
163 return ptr;
165 for (m = memlist; m; m = m->next) {
166 if (m->ptr == ptr) {
167 if ((p = malloc(size)) == NULL)
168 return NULL;
170 memcpy(p, m->ptr, (size < m->size) ? size : m->size);
171 memset(m->ptr, 0, m->size);
172 free(m->ptr);
173 m->ptr = p;
174 m->size = size;
175 #ifdef DEBUG
176 fprintf(stderr, "xrealloc(): %p %i\n", p, size);
177 #endif
178 return m->ptr;
182 warnx("xrealloc(): %p not found", ptr);
183 assert(0);
184 return NULL;
187 char *xstrdup(const char *str)
189 char *t, *tp;
190 size_t len;
191 const char *p;
193 if (!str)
194 return NULL;
196 len = strlen(str) + 1;
198 if ((t = (char *)xmalloc(len * sizeof(char))) == NULL)
199 return NULL;
201 for (p = str, tp = t; *p; p++)
202 *tp++ = *p;
204 *tp = 0;
205 #ifdef DEBUG
206 fprintf(stderr, "xstrdup(): %p\n", t);
207 #endif
208 return t;