1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of libpwmd.
8 Libpwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Libpwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
34 #define N_(msgid) gettext(msgid)
38 static struct memlist_s
*memlist
;
39 static pthread_mutex_t mem_mutex
;
41 static size_t allocations
, deallocations
;
49 pthread_mutex_init(&mem_mutex
, NULL
);
54 static int memlist_remove(void *ptr
, const char *func
)
56 struct memlist_s
*m
, *last
= NULL
, *p
;
58 pthread_mutex_lock(&mem_mutex
);
60 for (m
= memlist
; m
; m
= m
->next
) {
63 fprintf(stderr
, "%s: %p %i\n", func
, ptr
, m
->size
);
65 memset(m
->ptr
, 0, m
->size
);
79 pthread_mutex_unlock(&mem_mutex
);
86 pthread_mutex_unlock(&mem_mutex
);
90 static void memlist_prepend(struct memlist_s
*new)
92 pthread_mutex_lock(&mem_mutex
);
98 pthread_mutex_unlock(&mem_mutex
);
101 void _xfree(void *ptr
)
106 if (!memlist_remove(ptr
, __FUNCTION__
)) {
107 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
112 void *_xmalloc(size_t size
)
120 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
123 if ((p
= (void *)malloc(size
)) == NULL
) {
132 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
137 void *_xcalloc(size_t nmemb
, size_t size
)
145 if ((m
= (struct memlist_s
*)malloc(sizeof(struct memlist_s
))) == NULL
)
148 if ((p
= calloc(nmemb
, size
)) == NULL
) {
154 m
->size
= nmemb
* size
;
157 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, nmemb
* size
);
162 void *_xrealloc(void *ptr
, size_t size
)
171 return _xmalloc(size
);
173 pthread_mutex_lock(&mem_mutex
);
175 for (m
= memlist
; m
; m
= m
->next
) {
177 if ((p
= (void *)malloc(size
)) == NULL
) {
178 pthread_mutex_unlock(&mem_mutex
);
182 memcpy(p
, m
->ptr
, size
< m
->size
? size
: m
->size
);
183 memset(m
->ptr
, 0, m
->size
);
188 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, p
, size
);
190 pthread_mutex_unlock(&mem_mutex
);
195 warnx(N_("%s: %p not found"), __FUNCTION__
, ptr
);
196 pthread_mutex_unlock(&mem_mutex
);
201 char *_xstrdup(const char *str
)
210 len
= strlen(str
) + 1;
212 if ((t
= (char *)_xmalloc(len
* sizeof(char))) == NULL
)
215 for (p
= str
, tp
= t
; *p
; p
++)
220 fprintf(stderr
, "%s: %p\n", __FUNCTION__
, t
);
229 pthread_mutex_lock(&mem_mutex
);
231 for (m
= memlist
; m
; m
= memlist
)
234 pthread_mutex_unlock(&mem_mutex
);
243 pthread_mutex_lock(&mem_mutex
);
245 for (m
= memlist
; m
; m
= m
->next
) {
246 fprintf(stderr
, "%s: %p %i\n", __FUNCTION__
, m
->ptr
, m
->size
);
250 fprintf(stderr
, "Total unfreed: %i bytes, allocations: %i, deallocations: %i\n", total
,
251 allocations
, deallocations
);
252 pthread_mutex_unlock(&mem_mutex
);