lado falla algo importante que habra que buscar.
all: main
-main: main.c param_cte.c emufs.c
+main: main.c param_cte.c emufs.c malloc_debug.c
#include <stdlib.h>
#include <stdio.h>
+#include "malloc_debug.h"
typedef enum {T1, T2, T3} EMUFS_TYPE;
EMUFS *fp;
char a[] = "1234567890";
int n;
- char b[101];
+ char b[100];
fp = emufs_crear("articulos", T3, 512, 100);
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;
}
--- /dev/null
+/***************************************************************************
+ * 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;
+ }
+}
--- /dev/null
+/***************************************************************************
+ * 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 <stdio.h>
+
+/* 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
+
/*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;
}
#include <stdlib.h>
#include <string.h>
#include "emufs.h"
-
+#include "malloc_debug.h"
typedef struct block_free_t{
int block;