cannam@85
|
1 /*
|
cannam@85
|
2 * libid3tag - ID3 tag manipulation library
|
cannam@85
|
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
|
cannam@85
|
4 *
|
cannam@85
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@85
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@85
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@85
|
8 * (at your option) any later version.
|
cannam@85
|
9 *
|
cannam@85
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@85
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@85
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@85
|
13 * GNU General Public License for more details.
|
cannam@85
|
14 *
|
cannam@85
|
15 * You should have received a copy of the GNU General Public License
|
cannam@85
|
16 * along with this program; if not, write to the Free Software
|
cannam@85
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
cannam@85
|
18 *
|
cannam@85
|
19 * $Id: debug.c,v 1.8 2004/01/23 09:41:32 rob Exp $
|
cannam@85
|
20 */
|
cannam@85
|
21
|
cannam@85
|
22 # ifdef HAVE_CONFIG_H
|
cannam@85
|
23 # include "config.h"
|
cannam@85
|
24 # endif
|
cannam@85
|
25
|
cannam@85
|
26 # include "global.h"
|
cannam@85
|
27
|
cannam@85
|
28 # undef malloc
|
cannam@85
|
29 # undef calloc
|
cannam@85
|
30 # undef realloc
|
cannam@85
|
31 # undef free
|
cannam@85
|
32
|
cannam@85
|
33 # include <stdio.h>
|
cannam@85
|
34 # include <stdlib.h>
|
cannam@85
|
35 # include <string.h>
|
cannam@85
|
36
|
cannam@85
|
37 # include "debug.h"
|
cannam@85
|
38
|
cannam@85
|
39 # if defined(DEBUG)
|
cannam@85
|
40
|
cannam@85
|
41 # define DEBUG_MAGIC 0xdeadbeefL
|
cannam@85
|
42
|
cannam@85
|
43 struct debug {
|
cannam@85
|
44 char const *file;
|
cannam@85
|
45 unsigned int line;
|
cannam@85
|
46 size_t size;
|
cannam@85
|
47 struct debug *next;
|
cannam@85
|
48 struct debug *prev;
|
cannam@85
|
49 long int magic;
|
cannam@85
|
50 };
|
cannam@85
|
51
|
cannam@85
|
52 static struct debug *allocated;
|
cannam@85
|
53 static int registered;
|
cannam@85
|
54
|
cannam@85
|
55 static
|
cannam@85
|
56 void check(void)
|
cannam@85
|
57 {
|
cannam@85
|
58 struct debug *debug;
|
cannam@85
|
59
|
cannam@85
|
60 for (debug = allocated; debug; debug = debug->next) {
|
cannam@85
|
61 if (debug->magic != DEBUG_MAGIC) {
|
cannam@85
|
62 fprintf(stderr, "memory corruption\n");
|
cannam@85
|
63 break;
|
cannam@85
|
64 }
|
cannam@85
|
65
|
cannam@85
|
66 fprintf(stderr, "%s:%u: leaked %lu bytes\n",
|
cannam@85
|
67 debug->file, debug->line, debug->size);
|
cannam@85
|
68 }
|
cannam@85
|
69 }
|
cannam@85
|
70
|
cannam@85
|
71 void *id3_debug_malloc(size_t size, char const *file, unsigned int line)
|
cannam@85
|
72 {
|
cannam@85
|
73 struct debug *debug;
|
cannam@85
|
74
|
cannam@85
|
75 if (!registered) {
|
cannam@85
|
76 atexit(check);
|
cannam@85
|
77 registered = 1;
|
cannam@85
|
78 }
|
cannam@85
|
79
|
cannam@85
|
80 if (size == 0)
|
cannam@85
|
81 fprintf(stderr, "%s:%u: malloc(0)\n", file, line);
|
cannam@85
|
82
|
cannam@85
|
83 debug = malloc(sizeof(*debug) + size);
|
cannam@85
|
84 if (debug == 0) {
|
cannam@85
|
85 fprintf(stderr, "%s:%u: malloc(%lu) failed\n", file, line, size);
|
cannam@85
|
86 return 0;
|
cannam@85
|
87 }
|
cannam@85
|
88
|
cannam@85
|
89 debug->magic = DEBUG_MAGIC;
|
cannam@85
|
90
|
cannam@85
|
91 debug->file = file;
|
cannam@85
|
92 debug->line = line;
|
cannam@85
|
93 debug->size = size;
|
cannam@85
|
94
|
cannam@85
|
95 debug->next = allocated;
|
cannam@85
|
96 debug->prev = 0;
|
cannam@85
|
97
|
cannam@85
|
98 if (allocated)
|
cannam@85
|
99 allocated->prev = debug;
|
cannam@85
|
100
|
cannam@85
|
101 allocated = debug;
|
cannam@85
|
102
|
cannam@85
|
103 return ++debug;
|
cannam@85
|
104 }
|
cannam@85
|
105
|
cannam@85
|
106 void *id3_debug_calloc(size_t nmemb, size_t size,
|
cannam@85
|
107 char const *file, unsigned int line)
|
cannam@85
|
108 {
|
cannam@85
|
109 void *ptr;
|
cannam@85
|
110
|
cannam@85
|
111 ptr = id3_debug_malloc(nmemb * size, file, line);
|
cannam@85
|
112 if (ptr)
|
cannam@85
|
113 memset(ptr, 0, nmemb * size);
|
cannam@85
|
114
|
cannam@85
|
115 return ptr;
|
cannam@85
|
116 }
|
cannam@85
|
117
|
cannam@85
|
118 void *id3_debug_realloc(void *ptr, size_t size,
|
cannam@85
|
119 char const *file, unsigned int line)
|
cannam@85
|
120 {
|
cannam@85
|
121 struct debug *debug, *new;
|
cannam@85
|
122
|
cannam@85
|
123 if (size == 0) {
|
cannam@85
|
124 id3_debug_free(ptr, file, line);
|
cannam@85
|
125 return 0;
|
cannam@85
|
126 }
|
cannam@85
|
127
|
cannam@85
|
128 if (ptr == 0)
|
cannam@85
|
129 return id3_debug_malloc(size, file, line);
|
cannam@85
|
130
|
cannam@85
|
131 debug = ptr;
|
cannam@85
|
132 --debug;
|
cannam@85
|
133
|
cannam@85
|
134 if (debug->magic != DEBUG_MAGIC) {
|
cannam@85
|
135 fprintf(stderr, "%s:%u: realloc(%p, %lu) memory not allocated\n",
|
cannam@85
|
136 file, line, ptr, size);
|
cannam@85
|
137 return 0;
|
cannam@85
|
138 }
|
cannam@85
|
139
|
cannam@85
|
140 new = realloc(debug, sizeof(*debug) + size);
|
cannam@85
|
141 if (new == 0) {
|
cannam@85
|
142 fprintf(stderr, "%s:%u: realloc(%p, %lu) failed\n", file, line, ptr, size);
|
cannam@85
|
143 return 0;
|
cannam@85
|
144 }
|
cannam@85
|
145
|
cannam@85
|
146 if (allocated == debug)
|
cannam@85
|
147 allocated = new;
|
cannam@85
|
148
|
cannam@85
|
149 debug = new;
|
cannam@85
|
150
|
cannam@85
|
151 debug->file = file;
|
cannam@85
|
152 debug->line = line;
|
cannam@85
|
153 debug->size = size;
|
cannam@85
|
154
|
cannam@85
|
155 if (debug->next)
|
cannam@85
|
156 debug->next->prev = debug;
|
cannam@85
|
157 if (debug->prev)
|
cannam@85
|
158 debug->prev->next = debug;
|
cannam@85
|
159
|
cannam@85
|
160 return ++debug;
|
cannam@85
|
161 }
|
cannam@85
|
162
|
cannam@85
|
163 void id3_debug_free(void *ptr, char const *file, unsigned int line)
|
cannam@85
|
164 {
|
cannam@85
|
165 struct debug *debug;
|
cannam@85
|
166
|
cannam@85
|
167 if (ptr == 0) {
|
cannam@85
|
168 fprintf(stderr, "%s:%u: free(0)\n", file, line);
|
cannam@85
|
169 return;
|
cannam@85
|
170 }
|
cannam@85
|
171
|
cannam@85
|
172 debug = ptr;
|
cannam@85
|
173 --debug;
|
cannam@85
|
174
|
cannam@85
|
175 if (debug->magic != DEBUG_MAGIC) {
|
cannam@85
|
176 fprintf(stderr, "%s:%u: free(%p) memory not allocated\n", file, line, ptr);
|
cannam@85
|
177 return;
|
cannam@85
|
178 }
|
cannam@85
|
179
|
cannam@85
|
180 debug->magic = 0;
|
cannam@85
|
181
|
cannam@85
|
182 if (debug->next)
|
cannam@85
|
183 debug->next->prev = debug->prev;
|
cannam@85
|
184 if (debug->prev)
|
cannam@85
|
185 debug->prev->next = debug->next;
|
cannam@85
|
186
|
cannam@85
|
187 if (allocated == debug)
|
cannam@85
|
188 allocated = debug->next;
|
cannam@85
|
189
|
cannam@85
|
190 free(debug);
|
cannam@85
|
191 }
|
cannam@85
|
192
|
cannam@85
|
193 void *id3_debug_release(void *ptr, char const *file, unsigned int line)
|
cannam@85
|
194 {
|
cannam@85
|
195 struct debug *debug;
|
cannam@85
|
196
|
cannam@85
|
197 if (ptr == 0)
|
cannam@85
|
198 return 0;
|
cannam@85
|
199
|
cannam@85
|
200 debug = ptr;
|
cannam@85
|
201 --debug;
|
cannam@85
|
202
|
cannam@85
|
203 if (debug->magic != DEBUG_MAGIC) {
|
cannam@85
|
204 fprintf(stderr, "%s:%u: release(%p) memory not allocated\n",
|
cannam@85
|
205 file, line, ptr);
|
cannam@85
|
206 return ptr;
|
cannam@85
|
207 }
|
cannam@85
|
208
|
cannam@85
|
209 if (debug->next)
|
cannam@85
|
210 debug->next->prev = debug->prev;
|
cannam@85
|
211 if (debug->prev)
|
cannam@85
|
212 debug->prev->next = debug->next;
|
cannam@85
|
213
|
cannam@85
|
214 if (allocated == debug)
|
cannam@85
|
215 allocated = debug->next;
|
cannam@85
|
216
|
cannam@85
|
217 memmove(debug, debug + 1, debug->size);
|
cannam@85
|
218
|
cannam@85
|
219 return debug;
|
cannam@85
|
220 }
|
cannam@85
|
221
|
cannam@85
|
222 # endif
|