From: Ricardo Markiewicz Date: Mon, 5 Apr 2004 15:45:11 +0000 (+0000) Subject: * Le agregue el malloc debugger al tipo3 y ahora no falla. Me hace sospechar que... X-Git-Tag: svn_import_r684~645 X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/commitdiff_plain/064dc9896dce3d936a5876b927cece0f44808912?ds=inline * Le agregue el malloc debugger al tipo3 y ahora no falla. Me hace sospechar que en algun lado falla algo importante que habra que buscar. --- diff --git a/tipo3/Makefile b/tipo3/Makefile index 6d93bd5..f04eaee 100644 --- a/tipo3/Makefile +++ b/tipo3/Makefile @@ -3,5 +3,5 @@ LDFLAGS= all: main -main: main.c param_cte.c emufs.c +main: main.c param_cte.c emufs.c malloc_debug.c diff --git a/tipo3/emufs.h b/tipo3/emufs.h index aeae572..cf16da9 100644 --- a/tipo3/emufs.h +++ b/tipo3/emufs.h @@ -3,6 +3,7 @@ #include #include +#include "malloc_debug.h" typedef enum {T1, T2, T3} EMUFS_TYPE; diff --git a/tipo3/main.c b/tipo3/main.c index 0e38cc5..09fb9d5 100644 --- a/tipo3/main.c +++ b/tipo3/main.c @@ -7,7 +7,7 @@ int main() EMUFS *fp; char a[] = "1234567890"; int n; - char b[101]; + char b[100]; fp = emufs_crear("articulos", T3, 512, 100); @@ -15,11 +15,13 @@ int main() printf("ID = %d\n", n); - fp->leer_registro(fp, n, b, 20); + fp->leer_registro(fp, n, b, 100); printf("Ok\n"); printf("Recuperado : %s\n", b); emufs_destruir(fp); + + MD_Listar(); return 0; } diff --git a/tipo3/malloc_debug.c b/tipo3/malloc_debug.c new file mode 100644 index 0000000..cd2a35a --- /dev/null +++ b/tipo3/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/tipo3/malloc_debug.h b/tipo3/malloc_debug.h new file mode 100644 index 0000000..00ba43f --- /dev/null +++ b/tipo3/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 + diff --git a/tipo3/param_cte.c b/tipo3/param_cte.c index 0ee3d7f..dd9ab11 100644 --- a/tipo3/param_cte.c +++ b/tipo3/param_cte.c @@ -22,25 +22,29 @@ int leer_registro(EMUFS *emu, int ID, void *ptr, unsigned long tam_reg) /*si existe, lo busco en el archivo de bloques*/ block = buscar_registro(emu,ID); /*me devuelve el nro de bloque al que pertenece el registro*/ - bloque = (char*)malloc(emu->tam_bloque); + bloque = (char*)malloc(emu->tam_bloque); + if (bloque == NULL) { + printf("No hay memoria.\n"); + return -1; + } if (leer_bloque(emu, block, bloque)==-1) { free(bloque); return -1; /*No se pudo leer el bloque*/ } - + while ( iterador < emu->tam_bloque ){ memcpy(&ID_aux, bloque+iterador, sizeof(int)); - iterador += 4; + iterador += sizeof(int); if ( ID_aux == ID ){ - memcpy(ptr,&bloque[iterador],tam_reg); + memcpy(ptr,bloque+iterador,tam_reg); break; } - iterador += tam_reg; - } - + iterador += tam_reg; + } + fclose(f_block_reg); /* TODO :Ver por que causa un SEGFAULT ACA!! */ - //free(bloque); + free(bloque); return 0; } diff --git a/tipo3/param_cte.h b/tipo3/param_cte.h index cfc1295..22715c1 100644 --- a/tipo3/param_cte.h +++ b/tipo3/param_cte.h @@ -4,7 +4,7 @@ #include #include #include "emufs.h" - +#include "malloc_debug.h" typedef struct block_free_t{ int block;