libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libid3tag / debug.c
blobd91a2c56e0149c9386ed2aa8861ad1933897e718
1 /*
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 $
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # undef malloc
29 # undef calloc
30 # undef realloc
31 # undef free
33 # include <stdio.h>
34 # include <stdlib.h>
35 # include <string.h>
37 # include "debug.h"
39 # if defined(DEBUG)
41 # define DEBUG_MAGIC 0xdeadbeefL
43 struct 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;
52 static struct debug *allocated;
53 static int registered;
55 static
56 void check(void)
58 struct debug *debug;
60 for (debug = allocated; debug; debug = debug->next) {
61 if (debug->magic != DEBUG_MAGIC) {
62 fprintf(stderr, "memory corruption\n");
63 break;
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)
73 struct debug *debug;
75 if (!registered) {
76 atexit(check);
77 registered = 1;
80 if (size == 0)
81 fprintf(stderr, "%s:%u: malloc(0)\n", file, line);
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;
89 debug->magic = DEBUG_MAGIC;
91 debug->file = file;
92 debug->line = line;
93 debug->size = size;
95 debug->next = allocated;
96 debug->prev = 0;
98 if (allocated)
99 allocated->prev = debug;
101 allocated = debug;
103 return ++debug;
106 void *id3_debug_calloc(size_t nmemb, size_t size,
107 char const *file, unsigned int line)
109 void *ptr;
111 ptr = id3_debug_malloc(nmemb * size, file, line);
112 if (ptr)
113 memset(ptr, 0, nmemb * size);
115 return ptr;
118 void *id3_debug_realloc(void *ptr, size_t size,
119 char const *file, unsigned int line)
121 struct debug *debug, *new;
123 if (size == 0) {
124 id3_debug_free(ptr, file, line);
125 return 0;
128 if (ptr == 0)
129 return id3_debug_malloc(size, file, line);
131 debug = ptr;
132 --debug;
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;
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;
146 if (allocated == debug)
147 allocated = new;
149 debug = new;
151 debug->file = file;
152 debug->line = line;
153 debug->size = size;
155 if (debug->next)
156 debug->next->prev = debug;
157 if (debug->prev)
158 debug->prev->next = debug;
160 return ++debug;
163 void id3_debug_free(void *ptr, char const *file, unsigned int line)
165 struct debug *debug;
167 if (ptr == 0) {
168 fprintf(stderr, "%s:%u: free(0)\n", file, line);
169 return;
172 debug = ptr;
173 --debug;
175 if (debug->magic != DEBUG_MAGIC) {
176 fprintf(stderr, "%s:%u: free(%p) memory not allocated\n", file, line, ptr);
177 return;
180 debug->magic = 0;
182 if (debug->next)
183 debug->next->prev = debug->prev;
184 if (debug->prev)
185 debug->prev->next = debug->next;
187 if (allocated == debug)
188 allocated = debug->next;
190 free(debug);
193 void *id3_debug_release(void *ptr, char const *file, unsigned int line)
195 struct debug *debug;
197 if (ptr == 0)
198 return 0;
200 debug = ptr;
201 --debug;
203 if (debug->magic != DEBUG_MAGIC) {
204 fprintf(stderr, "%s:%u: release(%p) memory not allocated\n",
205 file, line, ptr);
206 return ptr;
209 if (debug->next)
210 debug->next->prev = debug->prev;
211 if (debug->prev)
212 debug->prev->next = debug->next;
214 if (allocated == debug)
215 allocated = debug->next;
217 memmove(debug, debug + 1, debug->size);
219 return debug;
222 # endif