From cfdf646e9a818067887cdc9c01ade5b79f699190 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 25 Sep 2007 14:27:34 -0700 Subject: [PATCH] Add nasm_zalloc() to nasmlib.c Add nasm_zalloc(), a wrapper around calloc(), to allocate zero-initialized memory. For large allocations, this is often far more efficient than allocating and zeroing, since the operating system tends to keep a pool of zero pages around. --- hashtbl.c | 4 +--- nasmlib.c | 31 +++++++++++++++++++++++-------- nasmlib.h | 3 +++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/hashtbl.c b/hashtbl.c index 6ebba3e..cefd37f 100644 --- a/hashtbl.c +++ b/hashtbl.c @@ -15,9 +15,7 @@ static struct hash_tbl_node *alloc_table(size_t newsize) { size_t bytes = newsize*sizeof(struct hash_tbl_node); - struct hash_tbl_node *newtbl = nasm_malloc(bytes); - - memset(newtbl, 0, bytes); + struct hash_tbl_node *newtbl = nasm_zalloc(bytes); return newtbl; } diff --git a/nasmlib.c b/nasmlib.c index 59971c9..0feb3ed 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -45,7 +45,24 @@ void *nasm_malloc(size_t size) #ifdef LOGALLOC else fprintf(logfp, "%s %d malloc(%ld) returns %p\n", - file, line, (int32_t)size, p); + file, line, (long)size, p); +#endif + return p; +} + +#ifdef LOGALLOC +void *nasm_zalloc_log(char *file, int line, size_t size) +#else +void *nasm_zalloc(size_t size) +#endif +{ + void *p = calloc(size, 1); + if (!p) + nasm_malloc_error(ERR_FATAL | ERR_NOFILE, "out of memory"); +#ifdef LOGALLOC + else + fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n", + file, line, (long)size, p); #endif return p; } @@ -62,10 +79,10 @@ void *nasm_realloc(void *q, size_t size) #ifdef LOGALLOC else if (q) fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n", - file, line, q, (int32_t)size, p); + file, line, q, (long)size, p); else fprintf(logfp, "%s %d malloc(%ld) returns %p\n", - file, line, (int32_t)size, p); + file, line, (long)size, p); #endif return p; } @@ -99,7 +116,7 @@ char *nasm_strdup(const char *s) #ifdef LOGALLOC else fprintf(logfp, "%s %d strdup(%ld) returns %p\n", - file, line, (int32_t)size, p); + file, line, (long)size, p); #endif strcpy(p, s); return p; @@ -120,7 +137,7 @@ char *nasm_strndup(char *s, size_t len) #ifdef LOGALLOC else fprintf(logfp, "%s %d strndup(%ld) returns %p\n", - file, line, (int32_t)size, p); + file, line, (long)size, p); #endif strncpy(p, s, len); p[len] = '\0'; @@ -377,9 +394,7 @@ static struct RAA *real_raa_init(int layers) int i; if (layers == 0) { - r = nasm_malloc(LEAFSIZ); - r->layers = 0; - memset(r->u.l.data, 0, sizeof(r->u.l.data)); + r = nasm_zalloc(LEAFSIZ); r->stepsize = 1L; } else { r = nasm_malloc(BRANCHSIZ); diff --git a/nasmlib.h b/nasmlib.h index 4334209..a2544fc 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -74,17 +74,20 @@ typedef void (*efunc) (int severity, const char *fmt, ...); void nasm_set_malloc_error(efunc); #ifndef LOGALLOC void *nasm_malloc(size_t); +void *nasm_zalloc(size_t); void *nasm_realloc(void *, size_t); void nasm_free(void *); char *nasm_strdup(const char *); char *nasm_strndup(char *, size_t); #else void *nasm_malloc_log(char *, int, size_t); +void *nasm_zalloc_log(char *, int, size_t); void *nasm_realloc_log(char *, int, void *, size_t); void nasm_free_log(char *, int, void *); char *nasm_strdup_log(char *, int, const char *); char *nasm_strndup_log(char *, int, char *, size_t); #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x) +#define nasm_zalloc(x) nasm_malloc_log(__FILE__,__LINE__,x) #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y) #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x) #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x) -- 2.7.4