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 #ifdef USE_PTH_THREADS
39 #define N_(msgid) gettext(msgid)
41 static struct memlist_s
*memlist
;
42 #ifdef USE_PTH_THREADS
43 static pth_mutex_t mem_mutex
;
45 static pthread_mutex_t mem_mutex
;
48 static size_t allocations
, deallocations
;
56 #ifdef USE_PTH_THREADS
57 pth_mutex_init(&mem_mutex
);
59 pthread_mutex_init(&mem_mutex
, NULL
);
65 static int memlist_remove(void *ptr
, const char *func
)
67 struct memlist_s
*m
, *last
= NULL
, *p
;
69 #ifdef USE_PTH_THREADS
70 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
72 pthread_mutex_lock(&mem_mutex
);
75 for (m
= memlist
; m
; m
= m
->next
) {
78 fprintf(stderr
, "%s: %p %i\n", func
, ptr
, m
->size
);
80 memset(m
->ptr
, 0, m
->size
);
94 #ifdef USE_PTH_THREADS
95 pth_mutex_release(&mem_mutex
);
97 pthread_mutex_unlock(&mem_mutex
);
105 #ifdef USE_PTH_THREADS
106 pth_mutex_release(&mem_mutex
);
108 pthread_mutex_unlock(&mem_mutex
);
113 static void memlist_prepend(struct memlist_s
*new)
115 #ifdef USE_PTH_THREADS
116 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
118 pthread_mutex_lock(&mem_mutex
);
125 #ifdef USE_PTH_THREADS
126 pth_mutex_release(&mem_mutex
);
128 pthread_mutex_unlock(&mem_mutex
);
132 void xfree(void *ptr
)
137 if (!memlist_remove(ptr
, __FUNCTION__
)) {
138 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
143 void *xmalloc(size_t size
)
151 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
154 if ((p
= (void *)malloc(size
)) == NULL
) {
163 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
168 void *xcalloc(size_t nmemb
, size_t size
)
176 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
179 if ((p
= calloc(nmemb
, size
)) == NULL
) {
185 m
->size
= nmemb
* size
;
188 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
193 void *xrealloc(void *ptr
, size_t size
)
202 return xmalloc(size
);
204 #ifdef USE_PTH_THREADS
205 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
207 pthread_mutex_lock(&mem_mutex
);
210 for (m
= memlist
; m
; m
= m
->next
) {
212 if ((p
= (void *)malloc(size
)) == NULL
) {
213 #ifdef USE_PTH_THREADS
214 pth_mutex_release(&mem_mutex
);
216 pthread_mutex_unlock(&mem_mutex
);
221 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
222 memset(m
->ptr
, 0, m
->size
);
227 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
229 #ifdef USE_PTH_THREADS
230 pth_mutex_release(&mem_mutex
);
232 pthread_mutex_unlock(&mem_mutex
);
238 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
239 #ifdef USE_PTH_THREADS
240 pth_mutex_release(&mem_mutex
);
242 pthread_mutex_unlock(&mem_mutex
);
248 char *xstrdup(const char *str
)
257 len
= strlen(str
) + 1;
259 if ((t
= (char *)xmalloc(len
* sizeof(char))) == NULL
)
262 for (p
= str
, tp
= t
; *p
; p
++)
267 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
276 #ifdef USE_PTH_THREADS
277 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
279 pthread_mutex_lock(&mem_mutex
);
282 for (m
= memlist
; m
; m
= memlist
)
285 #ifdef USE_PTH_THREADS
286 pth_mutex_release(&mem_mutex
);
288 pthread_mutex_unlock(&mem_mutex
);
298 #ifdef USE_PTH_THREADS
299 pth_mutex_acquire(&mem_mutex
, FALSE
, NULL
);
301 pthread_mutex_lock(&mem_mutex
);
304 for (m
= memlist
; m
; m
= m
->next
) {
305 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, m
->ptr
, m
->size
);
309 fprintf(stderr
, "Total unfreed: %i bytes, allocations: %i, deallocations: %i\n", total
,
310 allocations
, deallocations
);
311 #ifdef USE_PTH_THREADS
312 pth_mutex_release(&mem_mutex
);
314 pthread_mutex_unlock(&mem_mutex
);