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
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
);
61 static struct memlist_s
*memlist_append(struct memlist_s
*list
, struct memlist_s
*p
)
71 for (m
= list
; m
; m
= m
->next
) {
90 for (m
= memlist
; m
; m
= m
->next
) {
93 fprintf(stderr
, "xfree(): %p %i\n", ptr
, m
->size
);
95 memlist
= memlist_remove(memlist
, m
);
100 warnx("xfree(): %p not found", ptr
);
104 void *xmalloc(size_t size
)
112 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
115 if ((p
= malloc(size
)) == NULL
) {
122 memlist
= memlist_append(memlist
, m
);
124 fprintf(stderr
, "xmalloc(): %p %i\n", p
, size
);
129 void *xcalloc(size_t nmemb
, size_t size
)
137 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
140 if ((p
= calloc(nmemb
, size
)) == NULL
) {
147 memlist
= memlist_append(memlist
, m
);
149 fprintf(stderr
, "xcalloc(): %p %i\n", p
, size
);
154 void *xrealloc(void *ptr
, size_t size
)
160 return xmalloc(size
);
165 for (m
= memlist
; m
; m
= m
->next
) {
167 if ((p
= malloc(size
)) == NULL
)
170 memcpy(p
, m
->ptr
, (size
< m
->size
) ? size
: m
->size
);
171 memset(m
->ptr
, 0, m
->size
);
176 fprintf(stderr
, "xrealloc(): %p %i\n", p
, size
);
182 warnx("xrealloc(): %p not found", ptr
);
187 char *xstrdup(const char *str
)
196 len
= strlen(str
) + 1;
198 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
201 for (p
= str
, tp
= t
; *p
; p
++)
206 fprintf(stderr
, "xstrdup(): %p\n", t
);