1/*
2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2003 Underbit Technologies, Inc.
4 *
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.
9 *
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.
14 *
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
18 *
19 * $Id: debug.c,v 1.7 2003/04/19 00:14:33 rob Exp $
20 */
21
22# ifdef HAVE_CONFIG_H
23#  include "config.h"
24# endif
25
26# include "global.h"
27
28# undef malloc
29# undef calloc
30# undef realloc
31# undef free
32
33# include <stdio.h>
34# include <stdlib.h>
35# include <string.h>
36
37# include "debug.h"
38
39# if defined(DEBUG)
40
41# define DEBUG_MAGIC  0xdeadbeefL
42
43struct debug {
44  char const *file;
45  unsigned int line;
46  size_t size;
47  struct debug *next;
48  struct debug *prev;
49  long int magic;
50};
51
52static struct debug *allocated;
53static int registered;
54
55static
56void check(void)
57{
58  struct debug *debug;
59
60  for (debug = allocated; debug; debug = debug->next) {
61    if (debug->magic != DEBUG_MAGIC) {
62      fprintf(stderr, "memory corruption\n");
63      break;
64    }
65
66    fprintf(stderr, "%s:%u: leaked %lu bytes\n",
67	    debug->file, debug->line, debug->size);
68  }
69}
70
71void *id3_debug_malloc(size_t size, char const *file, unsigned int line)
72{
73  struct debug *debug;
74
75  if (!registered) {
76    atexit(check);
77    registered = 1;
78  }
79
80  if (size == 0)
81    fprintf(stderr, "%s:%u: malloc(0)\n", file, line);
82
83  debug = malloc(sizeof(*debug) + size);
84  if (debug == 0) {
85    fprintf(stderr, "%s:%u: malloc(%lu) failed\n", file, line, size);
86    return 0;
87  }
88
89  debug->magic = DEBUG_MAGIC;
90
91  debug->file = file;
92  debug->line = line;
93  debug->size = size;
94
95  debug->next = allocated;
96  debug->prev = 0;
97
98  if (allocated)
99    allocated->prev = debug;
100
101  allocated = debug;
102
103  return ++debug;
104}
105
106void *id3_debug_calloc(size_t nmemb, size_t size,
107		       char const *file, unsigned int line)
108{
109  void *ptr;
110
111  ptr = id3_debug_malloc(nmemb * size, file, line);
112  if (ptr)
113    memset(ptr, 0, nmemb * size);
114
115  return ptr;
116}
117
118void *id3_debug_realloc(void *ptr, size_t size,
119			char const *file, unsigned int line)
120{
121  struct debug *debug, *new;
122
123  if (size == 0) {
124    id3_debug_free(ptr, file, line);
125    return 0;
126  }
127
128  if (ptr == 0)
129    return id3_debug_malloc(size, file, line);
130
131  debug = ptr;
132  --debug;
133
134  if (debug->magic != DEBUG_MAGIC) {
135    fprintf(stderr, "%s:%u: realloc(%p, %lu) memory not allocated\n",
136	    file, line, ptr, size);
137    return 0;
138  }
139
140  new = realloc(debug, sizeof(*debug) + size);
141  if (new == 0) {
142    fprintf(stderr, "%s:%u: realloc(%p, %lu) failed\n", file, line, ptr, size);
143    return 0;
144  }
145
146  if (allocated == debug)
147    allocated = new;
148
149  debug = new;
150
151  debug->file = file;
152  debug->line = line;
153  debug->size = size;
154
155  if (debug->next)
156    debug->next->prev = debug;
157  if (debug->prev)
158    debug->prev->next = debug;
159
160  return ++debug;
161}
162
163void id3_debug_free(void *ptr, char const *file, unsigned int line)
164{
165  struct debug *debug;
166
167  if (ptr == 0) {
168    fprintf(stderr, "%s:%u: free(0)\n", file, line);
169    return;
170  }
171
172  debug = ptr;
173  --debug;
174
175  if (debug->magic != DEBUG_MAGIC) {
176    fprintf(stderr, "%s:%u: free(%p) memory not allocated\n", file, line, ptr);
177    return;
178  }
179
180  debug->magic = 0;
181
182  if (debug->next)
183    debug->next->prev = debug->prev;
184  if (debug->prev)
185    debug->prev->next = debug->next;
186
187  if (allocated == debug)
188    allocated = debug->next;
189
190  free(debug);
191}
192
193void *id3_debug_release(void *ptr, char const *file, unsigned int line)
194{
195  struct debug *debug;
196
197  if (ptr == 0)
198    return 0;
199
200  debug = ptr;
201  --debug;
202
203  if (debug->magic != DEBUG_MAGIC) {
204    fprintf(stderr, "%s:%u: release(%p) memory not allocated\n",
205	    file, line, ptr);
206    return ptr;
207  }
208
209  if (debug->next)
210    debug->next->prev = debug->prev;
211  if (debug->prev)
212    debug->prev->next = debug->next;
213
214  if (allocated == debug)
215    allocated = debug->next;
216
217  memmove(debug, debug + 1, debug->size);
218
219  return debug;
220}
221
222# endif
223