|
@@ -24,6 +24,10 @@
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include "lz78.h"
|
|
|
+#include "../inc/x3mem.h"
|
|
|
+
|
|
|
+UX3_EXT QLIST QM_LZ [ONE]; // Dynamic-memory QLIST
|
|
|
+
|
|
|
|
|
|
/* Code used to represent an EOF */
|
|
|
#define DICT_CODE_EOF 256
|
|
@@ -168,14 +172,13 @@ uint8_t bitlen(uint32_t i) {
|
|
|
}
|
|
|
|
|
|
ht_dictionary* ht_dictionary_new(uint32_t d_size) {
|
|
|
- ht_dictionary* dict = malloc(sizeof(ht_dictionary));
|
|
|
+ ht_dictionary* dict = qmalloc(QM_LZ, sizeof(ht_dictionary));
|
|
|
if (dict == NULL)
|
|
|
return NULL;
|
|
|
|
|
|
d_size = DICT_LIMIT(d_size);
|
|
|
- dict->root = calloc(1, sizeof(ht_entry) * d_size);
|
|
|
+ dict->root = qcalloc(QM_LZ, 1, sizeof(ht_entry) * d_size);
|
|
|
if (dict->root == NULL) {
|
|
|
- free(dict);
|
|
|
return NULL;
|
|
|
} else {
|
|
|
dict->d_size = d_size;
|
|
@@ -243,22 +246,19 @@ void ht_dictionary_reset(ht_dictionary* d) {
|
|
|
void ht_dictionary_destroy(ht_dictionary* d) {
|
|
|
if (d != NULL)
|
|
|
{
|
|
|
- //free(d);
|
|
|
- printf("I SHOULD BE FREEING MEMORY RIGHT NOW!\n");
|
|
|
- // This is obviously going to be fixed in due course.
|
|
|
+ //qfree(d);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
dictionary* dictionary_new(uint32_t d_size) {
|
|
|
uint16_t i;
|
|
|
- dictionary* dict = malloc(sizeof(dictionary) + d_size);
|
|
|
+ dictionary* dict = qmalloc(QM_LZ, sizeof(dictionary) + d_size);
|
|
|
if (dict == NULL)
|
|
|
return NULL;
|
|
|
|
|
|
d_size = DICT_LIMIT(d_size);
|
|
|
- dict->root = malloc(sizeof(entry) * d_size);
|
|
|
+ dict->root = qmalloc(QM_LZ, sizeof(entry) * d_size);
|
|
|
if (dict->root == NULL) {
|
|
|
- free(dict);
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
@@ -310,8 +310,7 @@ void dictionary_reset(dictionary* d) {
|
|
|
|
|
|
void dictionary_destroy(dictionary* d) {
|
|
|
if (d != NULL) {
|
|
|
- free(d->root);
|
|
|
- free(d);
|
|
|
+ //qfree(d);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -441,7 +440,7 @@ lz78_instance* lz78_new(uint8_t cmode, uint32_t dsize) {
|
|
|
lz78_d* d;
|
|
|
|
|
|
int max_dim = (sizeof(lz78_c) > sizeof(lz78_d)) ? sizeof(lz78_c) : sizeof(lz78_d);
|
|
|
- i = malloc(sizeof(lz78_instance) + max_dim);
|
|
|
+ i = qmalloc(QM_LZ, (sizeof(lz78_instance) + max_dim));
|
|
|
if (i == NULL)
|
|
|
return NULL;
|
|
|
i->mode = cmode;
|
|
@@ -454,13 +453,13 @@ lz78_instance* lz78_new(uint8_t cmode, uint32_t dsize) {
|
|
|
c->completed = 0;
|
|
|
c->main = ht_dictionary_new(c->d_size);
|
|
|
if (c->main == NULL) {
|
|
|
- free(i);
|
|
|
+ //qfree(i);
|
|
|
return NULL;
|
|
|
}
|
|
|
c->secondary = ht_dictionary_new(c->d_size);
|
|
|
if (c->secondary == NULL) {
|
|
|
ht_dictionary_destroy(c->main);
|
|
|
- free(i);
|
|
|
+ //qfree(i);
|
|
|
return NULL;
|
|
|
}
|
|
|
c->bitbuf = DICT_CODE_START;
|
|
@@ -473,7 +472,7 @@ lz78_instance* lz78_new(uint8_t cmode, uint32_t dsize) {
|
|
|
d->completed = 0;
|
|
|
d->main = dictionary_new(DICT_SIZE_MIN);
|
|
|
if (d->main == NULL) {
|
|
|
- free(i);
|
|
|
+ //qfree(i);
|
|
|
return NULL;
|
|
|
}
|
|
|
return i;
|
|
@@ -630,13 +629,13 @@ void lz78_destroy(lz78_instance *lz78) {
|
|
|
|
|
|
case LZ78_MODE_DECOMPRESS:
|
|
|
d = (lz78_d*)&lz78->state;
|
|
|
- if (d != NULL) {
|
|
|
- //dictionary_destroy(d->main);
|
|
|
+ if (d != NULL && d->main != NULL) {
|
|
|
+ dictionary_destroy(d->main);
|
|
|
ht_dictionary_destroy(d->secondary);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- free(lz78);
|
|
|
+ qflush(QM_LZ);
|
|
|
}
|
|
|
}
|