1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 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
33 #define BACKTRACE(fn) { \
37 nptrs = backtrace(buffer, 20); \
38 strings = backtrace_symbols(buffer, nptrs); \
39 for (n = 0; n < nptrs; n++) \
40 fprintf(stderr, "BACKTRACE (%s) %i: %s\n", fn, n, strings[n]); \
41 fprintf(stderr, "\n"); \
50 #ifdef USE_PTH_THREADS
57 #define N_(msgid) gettext(msgid)
59 static struct memlist_s
*memlist
;
60 #ifdef USE_PTH_THREADS
61 static pth_mutex_t mem_mutex
;
63 static pthread_mutex_t mem_mutex
;
66 static size_t allocations
, deallocations
;
74 #ifdef USE_PTH_THREADS
75 pth_mutex_init(&mem_mutex
);
77 pthread_mutex_init(&mem_mutex
, NULL
);
83 static int memlist_remove(void *ptr
, const char *func
)
85 struct memlist_s
*m
, *last
= NULL
, *p
;
87 #ifdef USE_PTH_THREADS
88 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
90 pthread_mutex_lock(&mem_mutex
);
93 for (m
= memlist
; m
; m
= m
->next
) {
96 fprintf(stderr
, "%s: %p %i\n", func
, ptr
, m
->size
);
98 memset(m
->ptr
, 0, m
->size
);
112 #ifdef USE_PTH_THREADS
113 pth_mutex_release(&mem_mutex
);
115 pthread_mutex_unlock(&mem_mutex
);
123 #ifdef USE_PTH_THREADS
124 pth_mutex_release(&mem_mutex
);
126 pthread_mutex_unlock(&mem_mutex
);
131 static void memlist_prepend(struct memlist_s
*new)
133 #ifdef USE_PTH_THREADS
134 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
136 pthread_mutex_lock(&mem_mutex
);
143 #ifdef USE_PTH_THREADS
144 pth_mutex_release(&mem_mutex
);
146 pthread_mutex_unlock(&mem_mutex
);
150 void xfree(void *ptr
)
155 if (!memlist_remove(ptr
, __FUNCTION__
)) {
156 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
161 void *xmalloc(size_t size
)
166 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
169 if ((p
= (void *)malloc(size
)) == NULL
) {
178 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
179 BACKTRACE(__FUNCTION__
);
184 void *xcalloc(size_t nmemb
, size_t size
)
189 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
192 if ((p
= calloc(nmemb
, size
)) == NULL
) {
198 m
->size
= nmemb
* size
;
201 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
202 BACKTRACE(__FUNCTION__
);
207 void *xrealloc(void *ptr
, size_t size
)
218 return xmalloc(size
);
220 #ifdef USE_PTH_THREADS
221 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
223 pthread_mutex_lock(&mem_mutex
);
226 for (m
= memlist
; m
; m
= m
->next
) {
228 if ((p
= (void *)malloc(size
)) == NULL
) {
229 #ifdef USE_PTH_THREADS
230 pth_mutex_release(&mem_mutex
);
232 pthread_mutex_unlock(&mem_mutex
);
237 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
238 memset(m
->ptr
, 0, m
->size
);
243 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
244 BACKTRACE(__FUNCTION__
);
246 #ifdef USE_PTH_THREADS
247 pth_mutex_release(&mem_mutex
);
249 pthread_mutex_unlock(&mem_mutex
);
255 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
256 #ifdef USE_PTH_THREADS
257 pth_mutex_release(&mem_mutex
);
259 pthread_mutex_unlock(&mem_mutex
);
265 char *xstrdup(const char *str
)
271 len
= strlen(str
) + 1;
273 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
276 for (p
= str
, tp
= t
; *p
; p
++)
281 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
282 BACKTRACE(__FUNCTION__
);
291 #ifdef USE_PTH_THREADS
292 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
294 pthread_mutex_lock(&mem_mutex
);
297 for (m
= memlist
; m
; m
= memlist
)
300 #ifdef USE_PTH_THREADS
301 pth_mutex_release(&mem_mutex
);
303 pthread_mutex_unlock(&mem_mutex
);
313 #ifdef USE_PTH_THREADS
314 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
316 pthread_mutex_lock(&mem_mutex
);
319 for (m
= memlist
; m
; m
= m
->next
) {
320 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, m
->ptr
, m
->size
);
324 fprintf(stderr
, "Total unfreed: %i bytes, allocations: %i, deallocations: %i\n", total
,
325 allocations
, deallocations
);
326 #ifdef USE_PTH_THREADS
327 pth_mutex_release(&mem_mutex
);
329 pthread_mutex_unlock(&mem_mutex
);