diff options
Diffstat (limited to 'huffman.c')
-rw-r--r-- | huffman.c | 69 |
1 files changed, 39 insertions, 30 deletions
@@ -1,9 +1,13 @@ -#include <string.h> +#include <stdio.h> +#include <stdlib.h> #include <stdint.h> +#include <string.h> +#include <math.h> +#include <limits.h> + #include "nmglobal.h" #include "nbench1.h" - /************************ ** HUFFMAN COMPRESSION ** ************************/ @@ -112,13 +116,13 @@ char *wordcatarray[WORDCATSIZE] = { /* FIXME: const? */ void DoHuffman(void) { HuffStruct *lochuffstruct; /* Loc pointer to global data */ -char *errorcontext; +char *context; int systemerror; unsigned long accumtime; double iterations; -char *comparray; -char *decomparray; -char *plaintext; +char *comparray = NULL; +char *decomparray = NULL; +char *plaintext = NULL; /* ** Link to global data @@ -128,7 +132,7 @@ lochuffstruct=&global_huffstruct; /* ** Set error context. */ -errorcontext="CPU:Huffman"; +context="CPU:Huffman"; /* ** Allocate memory for the plaintext and the compressed text. @@ -141,29 +145,34 @@ errorcontext="CPU:Huffman"; ** than 512 bytes. This is actually a super-conservative ** estimate...but, who cares?) */ -plaintext=(char *)AllocateMemory(lochuffstruct->arraysize,&systemerror); -if(systemerror) -{ ReportError(errorcontext,systemerror); +plaintext = malloc(lochuffstruct->arraysize); +if (!plaintext) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + exit(1); } -comparray=(char *)AllocateMemory(lochuffstruct->arraysize,&systemerror); -if(systemerror) -{ ReportError(errorcontext,systemerror); - FreeMemory(plaintext,&systemerror); + +comparray = malloc(lochuffstruct->arraysize); +if (!comparray) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + free(plaintext); + exit(1); /* FIXME: do I need exits here? */ } -decomparray=(char *)AllocateMemory(lochuffstruct->arraysize,&systemerror); -if(systemerror) -{ ReportError(errorcontext,systemerror); - FreeMemory(plaintext,&systemerror); - FreeMemory(comparray,&systemerror); + +decomparray = malloc(lochuffstruct->arraysize); +if (!decomparray) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + free(plaintext); + free(comparray); + exit(1); } -hufftree=(huff_node *)AllocateMemory(sizeof(huff_node) * 512, - &systemerror); -if(systemerror) -{ ReportError(errorcontext,systemerror); - FreeMemory(plaintext,&systemerror); - FreeMemory(comparray,&systemerror); - FreeMemory(decomparray,&systemerror); +hufftree = malloc(sizeof(huff_node) * 512); +if (!hufftree) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + free(plaintext); + free(comparray); + free(decomparray); + exit(1); } /* @@ -221,10 +230,10 @@ do { ** Clean up, calculate results, and go home. Be sure to ** show that we don't have to rerun adjustment code. */ -FreeMemory((void *)plaintext,&systemerror); -FreeMemory((void *)comparray,&systemerror); -FreeMemory((void *)decomparray,&systemerror); -FreeMemory((void *)hufftree,&systemerror); +free(plaintext); +free(comparray); +free(decomparray); +free(hufftree); lochuffstruct->iterspersec=iterations / TicksToFracSecs(accumtime); if(lochuffstruct->adjust==0) |