summaryrefslogtreecommitdiff
path: root/awk.h
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2017-06-21 09:57:24 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2017-06-21 09:57:24 -0400
commitb7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7 (patch)
tree8fa6a43eb6bb9597c8fe5ee9a67d3e318cb5f1ad /awk.h
parent4264c894681d11d4a5ce694aa8040223726fad1e (diff)
downloadgawk-b7d8b6ebcd5dd714bc21acf7637d9a651e2f7ea7.tar.gz
Add ezalloc macro in gawk and API to allocate zero-filled memory.
Diffstat (limited to 'awk.h')
-rw-r--r--awk.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/awk.h b/awk.h
index ab84c58b..d5964f88 100644
--- a/awk.h
+++ b/awk.h
@@ -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 *