From 9f5a4b155f0d5599b1e7f04418333995d484d820 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Tue, 30 Mar 2004 05:51:41 +0000 Subject: [PATCH] * Agrego el malloc debugger de taller para controlar la memoria durante esta fase del proyecto GUI --- gui/Makefile | 4 +- gui/form.h | 2 + gui/gui.c | 2 + gui/malloc_debug.c | 114 +++++++++++++++++++++++++++++++++++++++++++++ gui/malloc_debug.h | 40 ++++++++++++++++ 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 gui/malloc_debug.c create mode 100644 gui/malloc_debug.h diff --git a/gui/Makefile b/gui/Makefile index cab2129..62d6c2f 100644 --- a/gui/Makefile +++ b/gui/Makefile @@ -1,7 +1,7 @@ -CFLAGS=-Wall +CFLAGS=-Wall -g LDFLAGS=-lncurses all: gui -gui: form.c gui.c +gui: form.c gui.c malloc_debug.c diff --git a/gui/form.h b/gui/form.h index 9ac2a3c..ecec54d 100644 --- a/gui/form.h +++ b/gui/form.h @@ -7,6 +7,8 @@ #include #include +#include "malloc_debug.h" + typedef enum {INPUT, RADIO} t_Campo; typedef struct _elem_ { diff --git a/gui/gui.c b/gui/gui.c index 898fde9..f1a8a21 100644 --- a/gui/gui.c +++ b/gui/gui.c @@ -69,6 +69,8 @@ int main(int argc, char *argv[]) /* Libero el formulario */ form_destruir(form); + + MD_Listar(); return 0; } diff --git a/gui/malloc_debug.c b/gui/malloc_debug.c new file mode 100644 index 0000000..cd2a35a --- /dev/null +++ b/gui/malloc_debug.c @@ -0,0 +1,114 @@ +/*************************************************************************** + * 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 +#include +#include +/* 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; + } +} diff --git a/gui/malloc_debug.h b/gui/malloc_debug.h new file mode 100644 index 0000000..00ba43f --- /dev/null +++ b/gui/malloc_debug.h @@ -0,0 +1,40 @@ +/*************************************************************************** + * malloc_debug.h + * + * Sat Aug 30 16:55:51 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. + */ + +/* Si ya esta definido malloc y/o free, lo dehabilito */ + +#include + +/* Macro para emular la funcion malloc y free */ +#define malloc(x) MD_malloc(x, __LINE__, __FILE__) +#define free(x) MD_free(x) + +#ifndef _MALLOC_DEBUG_H +#define _MALLOC_DEBUG_H 1 + +void *MD_malloc(int size, int line, char *file); +void MD_free(void *); +void MD_Listar(); + +#endif + -- 2.43.0