diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-07-20 13:42:13 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-07-24 07:57:39 -0700 |
| commit | ccc24a8e508131a2226474642a038baaa2cbcc8c (patch) | |
| tree | 7029f7668fe3e1a9899da57bf6c1e60e0394bacb /src/core | |
| parent | 9b9526e4a643d36d9c66f2254f00df7298e5562f (diff) | |
| download | nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.tar.gz nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.tar.bz2 nng-ccc24a8e508131a2226474642a038baaa2cbcc8c.zip | |
fixes #605 NNI_ALLOC_STRUCT/NNI_ALLOC_STRUCTS should zero memory
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/defs.h | 4 | ||||
| -rw-r--r-- | src/core/message.c | 10 | ||||
| -rw-r--r-- | src/core/msgqueue.c | 4 | ||||
| -rw-r--r-- | src/core/platform.h | 13 |
4 files changed, 18 insertions, 13 deletions
diff --git a/src/core/defs.h b/src/core/defs.h index a0cca368..3a3f23ff 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -82,9 +82,9 @@ typedef struct { #define NNI_SECOND (1000) // Structure allocation conveniences. -#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof(*s)) +#define NNI_ALLOC_STRUCT(s) nni_zalloc(sizeof(*s)) #define NNI_FREE_STRUCT(s) nni_free((s), sizeof(*s)) -#define NNI_ALLOC_STRUCTS(s, n) nni_alloc(sizeof(*s) * n) +#define NNI_ALLOC_STRUCTS(s, n) nni_zalloc(sizeof(*s) * n) #define NNI_FREE_STRUCTS(s, n) nni_free(s, sizeof(*s) * n) #define NNI_PUT16(ptr, u) \ diff --git a/src/core/message.c b/src/core/message.c index ba3c0e84..f17240a4 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -136,7 +136,7 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) newsz = ch->ch_cap - headroom; } - if ((newbuf = nni_alloc(newsz + headwanted)) == NULL) { + if ((newbuf = nni_zalloc(newsz + headwanted)) == NULL) { return (NNG_ENOMEM); } // Copy all the data, but not header or trailer. @@ -152,7 +152,7 @@ nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted) // the backing store. In this case, we just check against the // allocated capacity and grow, or don't grow. if ((newsz + headwanted) >= ch->ch_cap) { - if ((newbuf = nni_alloc(newsz + headwanted)) == NULL) { + if ((newbuf = nni_zalloc(newsz + headwanted)) == NULL) { return (NNG_ENOMEM); } nni_free(ch->ch_buf, ch->ch_cap); @@ -215,7 +215,7 @@ nni_chunk_trim(nni_chunk *ch, size_t len) static int nni_chunk_dup(nni_chunk *dst, const nni_chunk *src) { - if ((dst->ch_buf = nni_alloc(src->ch_cap)) == NULL) { + if ((dst->ch_buf = nni_zalloc(src->ch_cap)) == NULL) { return (NNG_ENOMEM); } dst->ch_cap = src->ch_cap; @@ -387,7 +387,7 @@ nni_msg_dup(nni_msg **dup, const nni_msg *src) } NNI_LIST_FOREACH (&src->m_options, mo) { - newmo = nni_alloc(sizeof(*newmo) + mo->mo_sz); + newmo = nni_zalloc(sizeof(*newmo) + mo->mo_sz); if (newmo == NULL) { nni_msg_free(m); return (NNG_ENOMEM); @@ -436,7 +436,7 @@ nni_msg_setopt(nni_msg *m, int opt, const void *val, size_t sz) break; } } - if ((newmo = nni_alloc(sizeof(*newmo) + sz)) == NULL) { + if ((newmo = nni_zalloc(sizeof(*newmo) + sz)) == NULL) { return (NNG_ENOMEM); } newmo->mo_val = ((char *) newmo + sizeof(*newmo)); diff --git a/src/core/msgqueue.c b/src/core/msgqueue.c index 62f57553..e58fe6f5 100644 --- a/src/core/msgqueue.c +++ b/src/core/msgqueue.c @@ -57,7 +57,7 @@ nni_msgq_init(nni_msgq **mqp, unsigned cap) if ((mq = NNI_ALLOC_STRUCT(mq)) == NULL) { return (NNG_ENOMEM); } - if ((mq->mq_msgs = nni_alloc(sizeof(nng_msg *) * alloc)) == NULL) { + if ((mq->mq_msgs = nni_zalloc(sizeof(nng_msg *) * alloc)) == NULL) { NNI_FREE_STRUCT(mq); return (NNG_ENOMEM); } @@ -496,7 +496,7 @@ nni_msgq_resize(nni_msgq *mq, int cap) alloc = cap + 2; if (alloc > mq->mq_alloc) { - newq = nni_alloc(sizeof(nni_msg *) * alloc); + newq = nni_zalloc(sizeof(nni_msg *) * alloc); if (newq == NULL) { return (NNG_ENOMEM); } diff --git a/src/core/platform.h b/src/core/platform.h index 2ae0fd0b..70f4e9ef 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -69,10 +69,15 @@ extern const char *nni_plat_strerror(int); // to return NULL if memory cannot be allocated. extern void *nni_alloc(size_t); -// nni_free frees memory allocated with nni_alloc. It takes a size because -// some allocators do not track size, or can operate more efficiently if -// the size is provided with the free call. Examples of this are slab -// allocators like this found in Solaris/illumos (see libumem or kmem). +// nni_zalloc is just like nni_alloc, but ensures that memory is +// initialized to zero. It is a separate function because some platforms +// can use a more efficient zero-based allocation. +extern void *nni_zalloc(size_t); + +// nni_free frees memory allocated with nni_alloc or nni_zalloc. It takes +// a size because some allocators do not track size, or can operate more +// efficiently if the size is provided with the free call. Examples of this +// are slab allocators like this found in Solaris/illumos (see libumem). // This routine does nothing if supplied with a NULL pointer and zero size. // Most implementations can just call free() here. extern void nni_free(void *, size_t); |
