+/***************************************************************************
+ * malloc_debug.c
+ *
+ * Sat Aug 30 17:00:48 2003
+ * Copyright 2003 Ricardo Markiewicz
+ * rmarkie@fi.uba.ar
+ ****************************************************************************/
+/*
+ * 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 Library 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.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+/* este debe ser el ultimo include, para evitar errores de compilacion */
+#include "malloc_debug.h"
+
+/* Tengo que sacar los macros dentro de malloc_debug para evitar problemas */
+#undef malloc
+#undef free
+
+typedef struct _t_malloc_ {
+ void *ptr;
+ int size;
+ char file[21];
+ int line;
+ time_t hora;
+ struct _t_malloc_ *next;
+} t_MDAlloc;
+
+static t_MDAlloc *lstMalloc = NULL;
+
+void *MD_malloc(int size, int line, char *file)
+{
+ void *ptr;
+ t_MDAlloc *nodo;
+
+ ptr = malloc(size);
+ if (ptr == NULL) {
+ /* No registro nada porque no hay memoria! */
+ return NULL;
+ }
+
+ nodo = (t_MDAlloc *)malloc(sizeof(t_MDAlloc));
+ if (nodo == NULL) {
+ free(ptr);
+ return NULL;
+ }
+ nodo->ptr = ptr;
+ nodo->size = size;
+ nodo->line = line;
+ nodo->hora = time(NULL);
+ strncpy(nodo->file, file, 20);
+
+ /* Agrego el nodo al principio */
+ nodo->next = lstMalloc;
+ lstMalloc = nodo;
+
+ return ptr;
+}
+
+void MD_free(void *ptr)
+{
+ /* Tengo que buscar el nodo alocado */
+ t_MDAlloc *node, *padre;
+ node = lstMalloc;
+ padre = NULL;
+ while (node != NULL) {
+ if (node->ptr == ptr) {
+ /* lo encontre! */
+ if (padre != NULL) {
+ padre->next = node->next;
+ } else {
+ lstMalloc = node->next;
+ }
+ free(node);
+ break;
+ }
+ padre = node;
+ node = node->next;
+ }
+ free(ptr);
+}
+
+void MD_Listar()
+{
+ struct tm *hora;
+ t_MDAlloc *nodo;
+ nodo = lstMalloc;
+ printf("+----------+--------+-%-20s-+---------+-------+----------+\n", "--------------------");
+ printf("|Direccion | TamaƱo | %-20s | Linea | Fecha | Hora |\n", "Archivo");
+ printf("+----------+--------+-%-20s-+---------+-------+----------+\n", "--------------------");
+ while (nodo != NULL) {
+ hora = localtime(&nodo->hora);
+ printf("|%p | % 6d | %-20s | % 7d | %02d/%02d | %02d:%02d:%02d |\n", \
+ nodo->ptr, nodo->size, nodo->file, nodo->line, hora->tm_mday, hora->tm_mon, \
+ hora->tm_hour, hora->tm_min, hora->tm_sec);
+ printf("+----------+--------+-%-20s-+---------+-------+----------+\n", "--------------------");
+ free(hora);
+ nodo = nodo->next;
+ }
+}