2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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 02111-1307 USA
19 * $Id: debug.c,v 1.8 2004/01/23 09:41:32 rob Exp $
41 # define DEBUG_MAGIC 0xdeadbeefL
52 static struct debug
*allocated
;
53 static int registered
;
60 for (debug
= allocated
; debug
; debug
= debug
->next
) {
61 if (debug
->magic
!= DEBUG_MAGIC
) {
62 fprintf(stderr
, "memory corruption\n");
66 fprintf(stderr
, "%s:%u: leaked %lu bytes\n",
67 debug
->file
, debug
->line
, debug
->size
);
71 void *id3_debug_malloc(size_t size
, char const *file
, unsigned int line
)
81 fprintf(stderr
, "%s:%u: malloc(0)\n", file
, line
);
83 debug
= malloc(sizeof(*debug
) + size
);
85 fprintf(stderr
, "%s:%u: malloc(%lu) failed\n", file
, line
, size
);
89 debug
->magic
= DEBUG_MAGIC
;
95 debug
->next
= allocated
;
99 allocated
->prev
= debug
;
106 void *id3_debug_calloc(size_t nmemb
, size_t size
,
107 char const *file
, unsigned int line
)
111 ptr
= id3_debug_malloc(nmemb
* size
, file
, line
);
113 memset(ptr
, 0, nmemb
* size
);
118 void *id3_debug_realloc(void *ptr
, size_t size
,
119 char const *file
, unsigned int line
)
121 struct debug
*debug
, *new;
124 id3_debug_free(ptr
, file
, line
);
129 return id3_debug_malloc(size
, file
, line
);
134 if (debug
->magic
!= DEBUG_MAGIC
) {
135 fprintf(stderr
, "%s:%u: realloc(%p, %lu) memory not allocated\n",
136 file
, line
, ptr
, size
);
140 new = realloc(debug
, sizeof(*debug
) + size
);
142 fprintf(stderr
, "%s:%u: realloc(%p, %lu) failed\n", file
, line
, ptr
, size
);
146 if (allocated
== debug
)
156 debug
->next
->prev
= debug
;
158 debug
->prev
->next
= debug
;
163 void id3_debug_free(void *ptr
, char const *file
, unsigned int line
)
168 fprintf(stderr
, "%s:%u: free(0)\n", file
, line
);
175 if (debug
->magic
!= DEBUG_MAGIC
) {
176 fprintf(stderr
, "%s:%u: free(%p) memory not allocated\n", file
, line
, ptr
);
183 debug
->next
->prev
= debug
->prev
;
185 debug
->prev
->next
= debug
->next
;
187 if (allocated
== debug
)
188 allocated
= debug
->next
;
193 void *id3_debug_release(void *ptr
, char const *file
, unsigned int line
)
203 if (debug
->magic
!= DEBUG_MAGIC
) {
204 fprintf(stderr
, "%s:%u: release(%p) memory not allocated\n",
210 debug
->next
->prev
= debug
->prev
;
212 debug
->prev
->next
= debug
->next
;
214 if (allocated
== debug
)
215 allocated
= debug
->next
;
217 memmove(debug
, debug
+ 1, debug
->size
);