From 22942d33dcdd7cc185b9ad198fc1c35159f154ea Mon Sep 17 00:00:00 2001 From: Alan Kennedy Date: Wed, 23 Jun 2004 20:02:31 +0000 Subject: [PATCH] Mejor manejo de memoria, still debe haber sus buenos holes en un rato lo reviso con valgrind --- src/statichuff/main_bychunk.c | 2 +- src/statichuff/statichuff.c | 27 ++++++++++++++------------- src/statichuff/statichuff.h | 7 ++++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/statichuff/main_bychunk.c b/src/statichuff/main_bychunk.c index 43426e8..12f8028 100644 --- a/src/statichuff/main_bychunk.c +++ b/src/statichuff/main_bychunk.c @@ -72,7 +72,7 @@ int main(int argc, char* argv[]) if (dflag == 1) { /* Init decoder */ shuff = shuff_init_decoder(argv[optind],NULL); - fp = fopen("chunkdump.txt","w"); + fp = fopen(argv[optind+1],"w"); /* Gimme chunks till last one */ while (shuff_decode_chunk(shuff,chunk,4,&decoded)) diff --git a/src/statichuff/statichuff.c b/src/statichuff/statichuff.c index 55346bd..2e2764a 100644 --- a/src/statichuff/statichuff.c +++ b/src/statichuff/statichuff.c @@ -393,10 +393,11 @@ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile) strcpy(shuff->targetfile,outputfile); } - /* Levanto cuantos bytes decodeo y la freq table */ + /* Levanto cuantos bytes debo decodificar y la freqtable */ if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return NULL; vfread(&(shuff->bytesleft),sizeof(unsigned long int),1,shuff->decoderfp); vfread(shuff->freqtable,sizeof(unsigned long int),256,shuff->decoderfp); + /* Armo el arbol de huffman que uso para decodificar */ shuff->codetree = shuff_buildtree(shuff->freqtable); return shuff; @@ -416,11 +417,11 @@ HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long vo strcpy(fshuff->sourcefile,inputfile); strcpy(fshuff->targetfile,outputfile); fshuff->volsize = volsize; + fshuff->bychunk = 0; fshuff->preloadfreq = 0; fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256); for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0; fshuff->sumfreq = 0; - fshuff->bytesleft = 0; fshuff->codetree = NULL; return fshuff; @@ -432,7 +433,7 @@ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize) HUFF_STATE *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE)); int i; - /* Inicializo la estructura para trabajar con Huff Static by Chunks */ + /* Inicializo la estructura para trabajar con Huff Static by Chunks */ cshuff->decoderfp = NULL; cshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(outputfile)+2)); cshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1)); @@ -440,12 +441,12 @@ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize) strcpy(cshuff->sourcefile,outputfile); strcat(cshuff->sourcefile,"~"); cshuff->volsize = volsize; + cshuff->bychunk = 1; cshuff->preloadfreq = 1; cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256); for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0; - cshuff->sumfreq = 0; - cshuff->bytesleft = 0; - cshuff->codetree = NULL; + cshuff->sumfreq = 0; + cshuff->codetree = NULL; /* Abrimos un archivo temporal para ir tirando los chunks */ if ((cshuff->coderfp = fopen(cshuff->sourcefile,"w")) == NULL) return NULL; @@ -456,11 +457,11 @@ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize) void shuff_deinit_encoder(HUFF_STATE *shuff) { /* Libero mallocs y cierro archivos */ - free(shuff->freqtable); - if (shuff->coderfp != NULL) fclose(shuff->coderfp); - if (shuff->preloadfreq == 1) unlink(shuff->sourcefile); - if (shuff->targetfile != NULL) free(shuff->targetfile); - free(shuff->sourcefile); + if (shuff->freqtable) free(shuff->freqtable); + if (shuff->coderfp) fclose(shuff->coderfp); + if (shuff->bychunk) unlink(shuff->sourcefile); + if (shuff->sourcefile) free(shuff->sourcefile); + if (shuff->targetfile) free(shuff->targetfile); /* Destruyo recursivamente el arbol de codigos */ } @@ -468,8 +469,8 @@ void shuff_deinit_encoder(HUFF_STATE *shuff) void shuff_deinit_decoder(HUFF_STATE *shuff) { /* Libero mallocs y cierro archivos */ - free(shuff->freqtable); - free(shuff->sourcefile); + if (shuff->freqtable) free(shuff->freqtable); + if (shuff->sourcefile != NULL) free(shuff->sourcefile); if (shuff->targetfile != NULL) free(shuff->targetfile); if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp); diff --git a/src/statichuff/statichuff.h b/src/statichuff/statichuff.h index 3d80401..2ae1a60 100644 --- a/src/statichuff/statichuff.h +++ b/src/statichuff/statichuff.h @@ -21,12 +21,13 @@ typedef struct t_code { } SHUFFCODE; typedef struct t_huff { - FILE *coderfp; /* filepointer usado en el coder de bychunk */ - VFILE *decoderfp; /* filepointer usado en ambos decoders */ + FILE *coderfp; /* fpointer usado en el coder de bychunk para el temp */ + VFILE *decoderfp; /* fpointer al archivo a descomrimir */ char *sourcefile; /* Nombre del archivo a comprimir */ char *targetfile; /* Nombre del archivo comprimido */ long volsize; /* Tamanio de volumen para multivol */ - char preloadfreq; /* 1 Freqtable preloaded (bychunk | canonico) - 0 byfile */ + char bychunk; /* 0 works byfile, 1 works bychunk */ + char preloadfreq; /* 1 Freqtable has been preloaded (bychunk | canonic) */ t_freq *freqtable; /* Tabla de frecuencias */ t_freq sumfreq; /* Frecuencia total acumulada */ SHUFFNODE *codetree; /* Puntero al arbol de codigos prefijos */ -- 2.43.0