1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
31 #define N_(msgid) gettext(msgid)
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
);
69 static struct memlist_s
*memlist_append(struct memlist_s
*list
, struct memlist_s
*p
)
79 for (m
= list
; m
; m
= m
->next
) {
98 for (m
= memlist
; m
; m
= m
->next
) {
101 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, ptr
, m
->size
);
103 memlist
= memlist_remove(memlist
, m
);
108 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
112 void *xmalloc(size_t size
)
120 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
123 if ((p
= malloc(size
)) == NULL
) {
130 memlist
= memlist_append(memlist
, m
);
132 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
137 void *xcalloc(size_t nmemb
, size_t size
)
145 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
148 if ((p
= calloc(nmemb
, size
)) == NULL
) {
154 m
->size
= nmemb
* size
;
155 memlist
= memlist_append(memlist
, m
);
157 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
162 void *xrealloc(void *ptr
, size_t size
)
168 return xmalloc(size
);
173 for (m
= memlist
; m
; m
= m
->next
) {
175 if ((p
= malloc(size
)) == NULL
)
178 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
179 memset(m
->ptr
, 0, m
->size
);
184 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
190 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
195 char *xstrdup(const char *str
)
204 len
= strlen(str
) + 1;
206 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
209 for (p
= str
, tp
= t
; *p
; p
++)
214 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
223 for (m
= memlist
; m
; m
= memlist
)