annotate src/libid3tag-0.15.1b/debug.c @ 83:ae30d91d2ffe

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