From: TJ Date: Wed, 30 Mar 2016 16:13:28 +0000 (+0100) Subject: use heap_and_reap memory manager X-Git-Tag: v1.3~4 X-Git-Url: https://iam.tj/gitweb/gitweb.cgi?p=firmware_header_edit.git;a=commitdiff_plain;h=1b0a3180ee46f9e5b2805ca8c9bb6466144be24b use heap_and_reap memory manager Signed-off-by: TJ --- diff --git a/Makefile b/Makefile index 5c2a249..40f9374 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,17 @@ -PROG=fwheaditor -SRC=firmware_header_editor.c -CFLAGS=-std=c11 -g +PROG = fwheaditor +OBJS = heap_reap.o firmware_header_editor.o +CFLAGS = -std=c11 -g export PROG -$(PROG): $(SRC) - $(CC) $(CFLAGS) -o $@ $< +$(PROG): $(OBJS) + $(CC) $(CFLAGS) -o $@ $(OBJS) + +%.c: + $(CC) $(CFLAGS) -c $@ clean: - -rm -f $(PROG) + -rm -f $(PROG) $(OBJ) tests: $(MAKE) -C testsuite diff --git a/firmware_header_editor.c b/firmware_header_editor.c index 78e4177..ea9bfff 100644 --- a/firmware_header_editor.c +++ b/firmware_header_editor.c @@ -53,6 +53,8 @@ static const char *help = \ #include #include #include +#include +#include "heap_reap.h" static unsigned int MESSAGE_SIZE = 1024; static unsigned int TAG_VER_LEN = 4; @@ -137,6 +139,7 @@ pr_error_exit(unsigned int usage, const char *error, ...) if (usage) pr_usage(usage); + heap_and_reap(NULL-1, 0, 0); exit(EXIT_FAILURE); } @@ -175,7 +178,7 @@ main(int argc, char **argv) fprintf(stderr, "%s\nVersion: %0.2f\n%s\n", title, VERSION, copyright); - if ((model_id = calloc(MODEL_ID_LEN, 1)) == NULL) { + if ((model_id = heap_and_reap(NULL, MODEL_ID_LEN, 1)) == NULL) { pr_error_exit(0, "Unable to allocate memory (%d bytes)\n", MODEL_ID_LEN); } memcpy(model_id, match_model_id, MODEL_ID_LEN); @@ -214,7 +217,7 @@ main(int argc, char **argv) break; case 'h': // help pr_usage(1); - exit(0); + goto end; } } else { pr_error_exit(0, "cannot read data from stdin; provide a filename"); @@ -266,7 +269,7 @@ main(int argc, char **argv) if (filename) { if ((fd = open(filename, fd_mode)) > 0) { - if ( (buffer = calloc(header_len + sizeof(crc), 1)) != NULL) { + if ( (buffer = heap_and_reap(NULL, header_len + sizeof(crc), 1)) != NULL) { ssize_t qty; if ( (qty = read(fd, buffer, header_len + sizeof(crc))) < header_len) { if (!opt_quiet) @@ -348,7 +351,7 @@ main(int argc, char **argv) ); } - free(buffer); + heap_and_reap(buffer, 0, 0); close(fd); } else { fprintf(stderr, "Unable to open for %s (%s)\n", opt_write ? "writing" : "reading" , filename ); @@ -357,6 +360,8 @@ main(int argc, char **argv) pr_usage(0); } +end: + heap_and_reap(NULL-1, 0, 0); return 0; }