diff options
author | Tharre <tharre3@gmail.com> | 2014-04-16 22:13:05 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2014-04-16 22:13:05 +0200 |
commit | 9e5c74c5711557b120945b5a2c0d6723d3427c20 (patch) | |
tree | dc366a6c93ce71c30209b74cf7f4e4184f450790 /src/util.c | |
parent | 3fbeb8a37f74bda763c7de2da63ae358fc0b1b0e (diff) | |
download | redo-9e5c74c5711557b120945b5a2c0d6723d3427c20.tar.gz redo-9e5c74c5711557b120945b5a2c0d6723d3427c20.tar.xz redo-9e5c74c5711557b120945b5a2c0d6723d3427c20.zip |
Rewrote malloc() and other wrappers
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 25 |
1 files changed, 22 insertions, 3 deletions
@@ -11,14 +11,32 @@ #include "../config.h" /* A safe malloc wrapper. */ -void *ec_malloc(size_t size) { +void *safe_malloc_(size_t size, const char *file, unsigned int line) { void *ptr = malloc(size); + if (!ptr) - fatal("redo: cannot allocate %zu bytes", size); + fatal_(file, line, "redo: cannot allocate %zu bytes", size); return ptr; } +void *safe_realloc_(void *ptr, size_t size, const char *file, unsigned int line) { + void *ptr2 = realloc(ptr, size); + if (!ptr2) + fatal_(file, line, "redo: cannot reallocate %zu bytes", size); + + return ptr2; +} + +FILE *safe_fopen_(const char *path, const char *mode, const char *file, + unsigned int line) { + FILE *temp = fopen(path, mode); + if (!temp) + fatal_(file, line, "redo: failed to open %s", path); + + return temp; +} + /* For concating multiple strings into a single larger one. */ char *concat(size_t count, ...) { assert(count > 0); @@ -31,7 +49,7 @@ char *concat(size_t count, ...) { size += args_len[i]; } size++; - char *result = ec_malloc(size); + char *result = safe_malloc(size); /* debug("Allocated %zu bytes at %p\n", size, result); */ uintptr_t offset = 0; for (i = 0; i < count; ++i) { @@ -49,6 +67,7 @@ char *xbasename(const char *path) { return ptr? ptr+1 : (char*) path; } +/* TODO: REIMPLEMENT */ char *ec_strdup(const char* str) { assert(str); size_t len = strlen(str) + 1; |