From d35747a40c7dea12de95f78a4f283a45ef687597 Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Mon, 23 Feb 2004 23:41:20 +0000 Subject: import from SF CVS git-svn-id: https://svn.musicpd.org/mpd/trunk@1 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/libid3tag/debug.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 src/libid3tag/debug.c (limited to 'src/libid3tag/debug.c') diff --git a/src/libid3tag/debug.c b/src/libid3tag/debug.c new file mode 100644 index 000000000..78b11bba3 --- /dev/null +++ b/src/libid3tag/debug.c @@ -0,0 +1,222 @@ +/* + * libid3tag - ID3 tag manipulation library + * Copyright (C) 2000-2003 Underbit Technologies, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: debug.c,v 1.1 2003/08/14 03:57:13 shank Exp $ + */ + +# ifdef HAVE_CONFIG_H +# include "config.h" +# endif + +# include "global.h" + +# undef malloc +# undef calloc +# undef realloc +# undef free + +# include +# include +# include + +# include "debug.h" + +# if defined(DEBUG) + +# define DEBUG_MAGIC 0xdeadbeefL + +struct debug { + char const *file; + unsigned int line; + size_t size; + struct debug *next; + struct debug *prev; + long int magic; +}; + +static struct debug *allocated; +static int registered; + +static +void check(void) +{ + struct debug *debug; + + for (debug = allocated; debug; debug = debug->next) { + if (debug->magic != DEBUG_MAGIC) { + fprintf(stderr, "memory corruption\n"); + break; + } + + fprintf(stderr, "%s:%u: leaked %lu bytes\n", + debug->file, debug->line, debug->size); + } +} + +void *id3_debug_malloc(size_t size, char const *file, unsigned int line) +{ + struct debug *debug; + + if (!registered) { + atexit(check); + registered = 1; + } + + if (size == 0) + fprintf(stderr, "%s:%u: malloc(0)\n", file, line); + + debug = malloc(sizeof(*debug) + size); + if (debug == 0) { + fprintf(stderr, "%s:%u: malloc(%lu) failed\n", file, line, size); + return 0; + } + + debug->magic = DEBUG_MAGIC; + + debug->file = file; + debug->line = line; + debug->size = size; + + debug->next = allocated; + debug->prev = 0; + + if (allocated) + allocated->prev = debug; + + allocated = debug; + + return ++debug; +} + +void *id3_debug_calloc(size_t nmemb, size_t size, + char const *file, unsigned int line) +{ + void *ptr; + + ptr = id3_debug_malloc(nmemb * size, file, line); + if (ptr) + memset(ptr, 0, nmemb * size); + + return ptr; +} + +void *id3_debug_realloc(void *ptr, size_t size, + char const *file, unsigned int line) +{ + struct debug *debug, *new; + + if (size == 0) { + id3_debug_free(ptr, file, line); + return 0; + } + + if (ptr == 0) + return id3_debug_malloc(size, file, line); + + debug = ptr; + --debug; + + if (debug->magic != DEBUG_MAGIC) { + fprintf(stderr, "%s:%u: realloc(%p, %lu) memory not allocated\n", + file, line, ptr, size); + return 0; + } + + new = realloc(debug, sizeof(*debug) + size); + if (new == 0) { + fprintf(stderr, "%s:%u: realloc(%p, %lu) failed\n", file, line, ptr, size); + return 0; + } + + if (allocated == debug) + allocated = new; + + debug = new; + + debug->file = file; + debug->line = line; + debug->size = size; + + if (debug->next) + debug->next->prev = debug; + if (debug->prev) + debug->prev->next = debug; + + return ++debug; +} + +void id3_debug_free(void *ptr, char const *file, unsigned int line) +{ + struct debug *debug; + + if (ptr == 0) { + fprintf(stderr, "%s:%u: free(0)\n", file, line); + return; + } + + debug = ptr; + --debug; + + if (debug->magic != DEBUG_MAGIC) { + fprintf(stderr, "%s:%u: free(%p) memory not allocated\n", file, line, ptr); + return; + } + + debug->magic = 0; + + if (debug->next) + debug->next->prev = debug->prev; + if (debug->prev) + debug->prev->next = debug->next; + + if (allocated == debug) + allocated = debug->next; + + free(debug); +} + +void *id3_debug_release(void *ptr, char const *file, unsigned int line) +{ + struct debug *debug; + + if (ptr == 0) + return 0; + + debug = ptr; + --debug; + + if (debug->magic != DEBUG_MAGIC) { + fprintf(stderr, "%s:%u: release(%p) memory not allocated\n", + file, line, ptr); + return ptr; + } + + if (debug->next) + debug->next->prev = debug->prev; + if (debug->prev) + debug->prev->next = debug->next; + + if (allocated == debug) + allocated = debug->next; + + memmove(debug, debug + 1, debug->size); + + return debug; +} + +# endif -- cgit v1.2.3