1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 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 02110-1301 USA
31 #define N_(msgid) gettext(msgid)
35 static struct memlist_s
*memlist
;
37 static size_t allocations
, deallocations
;
40 static int memlist_remove(void *ptr
, const char *func
)
42 struct memlist_s
*m
, *last
= NULL
, *p
;
45 for (m
= memlist
; m
; m
= m
->next
) {
48 fprintf(stderr
, "%s: %p %i\n", func
, ptr
, m
->size
);
51 memset(m
->ptr
, 0, m
->size
);
80 static void memlist_prepend(struct memlist_s
*new)
94 if (!memlist_remove(ptr
, __FUNCTION__
)) {
95 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
100 void *xmalloc(size_t size
)
108 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
111 if ((p
= (void *)malloc(size
)) == NULL
) {
120 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
125 void *xcalloc(size_t nmemb
, size_t size
)
133 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
136 if ((p
= calloc(nmemb
, size
)) == NULL
) {
142 m
->size
= nmemb
* size
;
145 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
150 void *xrealloc(void *ptr
, size_t size
)
156 return xmalloc(size
);
161 for (m
= memlist
; m
; m
= m
->next
) {
163 if ((p
= (void *)malloc(size
)) == NULL
)
166 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
167 memset(m
->ptr
, 0, m
->size
);
172 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
178 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
183 char *xstrdup(const char *str
)
192 len
= strlen(str
) + 1;
194 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
197 for (p
= str
, tp
= t
; *p
; p
++)
202 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
211 for (m
= memlist
; m
; m
= memlist
)
221 for (m
= memlist
; m
; m
= m
->next
) {
222 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, m
->ptr
, m
->size
);
226 fprintf(stderr
, "Total unfreed: %i bytes, allocations: %i, deallocations: %i\n", total
,
227 allocations
, deallocations
);