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
32 #define N_(msgid) gettext(msgid)
36 static struct memlist_s
*memlist
;
37 static pthread_mutex_t mem_mutex
;
39 static size_t allocations
, deallocations
;
47 pthread_mutex_init(&mem_mutex
, NULL
);
54 pthread_mutex_destroy(&mem_mutex
);
57 static int memlist_remove(void *ptr
, const char *func
)
59 struct memlist_s
*m
, *last
= NULL
, *p
;
62 for (m
= memlist
; m
; m
= m
->next
) {
65 fprintf(stderr
, "%s: %p %i\n", func
, ptr
, m
->size
);
68 memset(m
->ptr
, 0, m
->size
);
97 static void memlist_prepend(struct memlist_s
*new)
106 void xfree(void *ptr
)
108 pthread_mutex_lock(&mem_mutex
);
111 pthread_mutex_unlock(&mem_mutex
);
115 if (!memlist_remove(ptr
, __FUNCTION__
)) {
116 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
117 pthread_mutex_unlock(&mem_mutex
);
121 pthread_mutex_unlock(&mem_mutex
);
124 void *xmalloc(size_t size
)
129 pthread_mutex_lock(&mem_mutex
);
132 pthread_mutex_unlock(&mem_mutex
);
136 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
) {
137 pthread_mutex_unlock(&mem_mutex
);
141 if ((p
= (void *)malloc(size
)) == NULL
) {
143 pthread_mutex_unlock(&mem_mutex
);
151 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
153 pthread_mutex_unlock(&mem_mutex
);
157 void *xcalloc(size_t nmemb
, size_t size
)
162 pthread_mutex_lock(&mem_mutex
);
165 pthread_mutex_unlock(&mem_mutex
);
169 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
) {
170 pthread_mutex_unlock(&mem_mutex
);
174 if ((p
= calloc(nmemb
, size
)) == NULL
) {
176 pthread_mutex_unlock(&mem_mutex
);
181 m
->size
= nmemb
* size
;
184 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
186 pthread_mutex_unlock(&mem_mutex
);
190 void *xrealloc(void *ptr
, size_t size
)
199 return xmalloc(size
);
201 pthread_mutex_lock(&mem_mutex
);
203 for (m
= memlist
; m
; m
= m
->next
) {
205 if ((p
= (void *)malloc(size
)) == NULL
) {
206 pthread_mutex_unlock(&mem_mutex
);
210 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
211 memset(m
->ptr
, 0, m
->size
);
216 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
218 pthread_mutex_unlock(&mem_mutex
);
223 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
224 pthread_mutex_unlock(&mem_mutex
);
229 char *xstrdup(const char *str
)
238 len
= strlen(str
) + 1;
240 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
243 for (p
= str
, tp
= t
; *p
; p
++)
248 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
257 pthread_mutex_lock(&mem_mutex
);
259 for (m
= memlist
; m
; m
= memlist
)
262 pthread_mutex_unlock(&mem_mutex
);
271 pthread_mutex_lock(&mem_mutex
);
273 for (m
= memlist
; m
; m
= m
->next
) {
274 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, m
->ptr
, m
->size
);
278 fprintf(stderr
, "Total unfreed: %i bytes, allocations: %i, deallocations: %i\n", total
,
279 allocations
, deallocations
);
280 pthread_mutex_unlock(&mem_mutex
);