diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2017-06-21 09:57:24 -0400 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2017-06-21 09:57:24 -0400 |
commit | b7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7 (patch) | |
tree | 8fa6a43eb6bb9597c8fe5ee9a67d3e318cb5f1ad /awk.h | |
parent | 4264c894681d11d4a5ce694aa8040223726fad1e (diff) | |
download | egawk-b7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7.tar.gz egawk-b7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7.tar.bz2 egawk-b7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7.zip |
Add ezalloc macro in gawk and API to allocate zero-filled memory.
Diffstat (limited to 'awk.h')
-rw-r--r-- | awk.h | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -1331,6 +1331,7 @@ DEREF(NODE *r) __LINE__, __FILE__) #define emalloc(var,ty,x,str) (void) (var = (ty) emalloc_real((size_t)(x), str, #var, __FILE__, __LINE__)) +#define ezalloc(var,ty,x,str) (void) (var = (ty) ezalloc_real((size_t)(x), str, #var, __FILE__, __LINE__)) #define erealloc(var,ty,x,str) (void) (var = (ty) erealloc_real((void *) var, (size_t)(x), str, #var, __FILE__, __LINE__)) #define efree(p) free(p) @@ -1950,6 +1951,24 @@ emalloc_real(size_t count, const char *where, const char *var, const char *file, return ret; } +/* ezalloc_real --- malloc zero-filled bytes with error checking */ + +static inline void * +ezalloc_real(size_t count, const char *where, const char *var, const char *file, int line) +{ + void *ret; + + if (count == 0) + fatal("%s:%d: ezalloc called with zero bytes", file, line); + + ret = (void *) calloc(1, count); + if (ret == NULL) + fatal(_("%s:%d:%s: %s: can't allocate %ld bytes of memory (%s)"), + file, line, where, var, (long) count, strerror(errno)); + + return ret; +} + /* erealloc_real --- realloc with error checking */ static inline void * |