From c3d8e75f2bb76b5bfafc589f860036cf42cfbaa0 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 2 Jan 2017 11:26:43 -0800 Subject: Use new NNI_ALLOC_STRUCT macro. nni_msg_dup copies options too. --- src/core/defs.h | 4 ++++ src/core/message.c | 32 ++++++++++++++++++++++++-------- src/core/message.h | 2 +- src/core/msgqueue.c | 4 ++-- src/core/pipe.c | 10 +++++----- src/core/socket.c | 16 ++++++++-------- src/protocol/pair/pair.c | 6 +++--- src/protocol/reqrep/rep.c | 10 +++++----- src/protocol/reqrep/req.c | 10 +++++----- src/transport/inproc/inproc.c | 12 ++++++------ 10 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/core/defs.h b/src/core/defs.h index 9687215d..0fe894b1 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -39,4 +39,8 @@ typedef int64_t nni_duration; // Relative time (usec). #define NNI_TIME_ZERO ((nni_time) 0) #define NNI_SECOND (1000000) +// Structure allocation conveniences. +#define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof (*(s))) +#define NNI_FREE_STRUCT(s) nni_free((s), sizeof (*(s))) + #endif // CORE_DEFS_H diff --git a/src/core/message.c b/src/core/message.c index 6fafc64c..3135cc95 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -233,14 +233,14 @@ nni_msg_alloc(nni_msg **mp, size_t sz) nni_msg *m; int rv; - if ((m = nni_alloc(sizeof (*m))) == NULL) { + if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); } // 64-bytes of header, including room for 32 bytes // of headroom and 32 bytes of trailer. if ((rv = nni_chunk_grow(&m->m_header, 32, 32)) != 0) { - nni_free(m, sizeof (*m)); + NNI_FREE_STRUCT(m); return (rv); } @@ -256,7 +256,7 @@ nni_msg_alloc(nni_msg **mp, size_t sz) } if (rv != 0) { nni_chunk_free(&m->m_header); - nni_free(m, sizeof (*m)); + NNI_FREE_STRUCT(m); } if ((rv = nni_chunk_append(&m->m_body, NULL, sz)) != 0) { // Should not happen since we just grew it to fit. @@ -270,23 +270,39 @@ nni_msg_alloc(nni_msg **mp, size_t sz) int -nni_msg_dup(nni_msg **dup, const nni_msg *src) +nni_msg_dup(nni_msg **dup, nni_msg *src) { nni_msg *m; + nni_msgopt *mo; + nni_msgopt *newmo; int rv; - if ((m = nni_alloc(sizeof (*m))) == NULL) { + if ((m = NNI_ALLOC_STRUCT(m)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_chunk_dup(&m->m_header, &src->m_header)) != 0) { - nni_free(m, sizeof (*m)); + NNI_FREE_STRUCT(m); return (rv); } if ((rv = nni_chunk_dup(&m->m_body, &src->m_body)) != 0) { nni_chunk_free(&m->m_header); - nni_free(m, sizeof (*m)); + NNI_FREE_STRUCT(m); return (rv); } + + NNI_LIST_FOREACH (&src->m_options, mo) { + newmo = nni_alloc(sizeof (*newmo) + mo->mo_sz); + if (newmo == NULL) { + nni_msg_free(m); + return (NNG_ENOMEM); + } + newmo->mo_val = ((char *) newmo + sizeof (*newmo)); + newmo->mo_sz = mo->mo_sz; + newmo->mo_num = mo->mo_num; + memcpy(newmo->mo_val, mo->mo_val, mo->mo_sz); + nni_list_append(&m->m_options, newmo); + } + *dup = m; return (0); } @@ -303,7 +319,7 @@ nni_msg_free(nni_msg *m) nni_list_remove(&m->m_options, mo); nni_free(mo, sizeof (*mo) + mo->mo_sz); } - nni_free(m, sizeof (*m)); + NNI_FREE_STRUCT(m); } diff --git a/src/core/message.h b/src/core/message.h index 4ce3c7fd..dff71646 100644 --- a/src/core/message.h +++ b/src/core/message.h @@ -15,7 +15,7 @@ extern int nni_msg_alloc(nni_msg **, size_t); extern void nni_msg_free(nni_msg *); extern int nni_msg_realloc(nni_msg *, size_t); -extern int nni_msg_dup(nni_msg **, const nni_msg *); +extern int nni_msg_dup(nni_msg **, nni_msg *); extern void *nni_msg_header(nni_msg *, size_t *); extern void *nni_msg_body(nni_msg *, size_t *); extern int nni_msg_append(nni_msg *, const void *, size_t); diff --git a/src/core/msgqueue.c b/src/core/msgqueue.c index ba6aa6bb..4807fe0f 100644 --- a/src/core/msgqueue.c +++ b/src/core/msgqueue.c @@ -48,7 +48,7 @@ nni_msgqueue_create(nni_msgqueue **mqp, int cap) // a message back at the end to do a retry. alloc = cap + 2; - if ((mq = nni_alloc(sizeof (*mq))) == NULL) { + if ((mq = NNI_ALLOC_STRUCT(mq)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_mtx_init(&mq->mq_lock)) != 0) { @@ -113,7 +113,7 @@ nni_msgqueue_destroy(nni_msgqueue *mq) } nni_free(mq->mq_msgs, mq->mq_alloc * sizeof (nng_msg *)); - nni_free(mq, sizeof (*mq)); + NNI_FREE_STRUCT(mq); } diff --git a/src/core/pipe.c b/src/core/pipe.c index 34db782a..177216bd 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -79,7 +79,7 @@ nni_pipe_destroy(nni_pipe *p) if (p->p_pdata != NULL) { nni_free(p->p_pdata, p->p_psize); } - nni_free(p, sizeof (*p)); + NNI_FREE_STRUCT(p); } @@ -91,7 +91,7 @@ nni_pipe_create(nni_pipe **pp, nni_endpt *ep) nni_protocol *proto = &sock->s_ops; int rv; - if ((p = nni_alloc(sizeof (*p))) == NULL) { + if ((p = NNI_ALLOC_STRUCT(p)) == NULL) { return (NNG_ENOMEM); } p->p_sock = sock; @@ -102,20 +102,20 @@ nni_pipe_create(nni_pipe **pp, nni_endpt *ep) NNI_LIST_NODE_INIT(&p->p_node); if ((p->p_pdata = nni_alloc(p->p_psize)) == NULL) { - nni_free(p, sizeof (*p)); + NNI_FREE_STRUCT(p); return (NNG_ENOMEM); } rv = nni_thr_init(&p->p_recv_thr, proto->proto_pipe_recv, p->p_pdata); if (rv != 0) { nni_free(p->p_pdata, p->p_psize); - nni_free(p, sizeof (*p)); + NNI_FREE_STRUCT(p); return (rv); } rv = nni_thr_init(&p->p_send_thr, proto->proto_pipe_send, p->p_pdata); if (rv != 0) { nni_thr_fini(&p->p_recv_thr); nni_free(p->p_pdata, p->p_psize); - nni_free(p, sizeof (*p)); + NNI_FREE_STRUCT(p); return (rv); } p->p_psize = sock->s_ops.proto_pipe_size; diff --git a/src/core/socket.c b/src/core/socket.c index 48cd25bc..a8904500 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -95,7 +95,7 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) if ((ops = nni_protocol_find(proto)) == NULL) { return (NNG_ENOTSUP); } - if ((sock = nni_alloc(sizeof (*sock))) == NULL) { + if ((sock = NNI_ALLOC_STRUCT(sock)) == NULL) { return (NNG_ENOMEM); } sock->s_ops = *ops; @@ -110,19 +110,19 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) NNI_LIST_INIT(&sock->s_eps, nni_endpt, ep_node); if ((rv = nni_mtx_init(&sock->s_mx)) != 0) { - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } if ((rv = nni_cv_init(&sock->s_cv, &sock->s_mx)) != 0) { nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } if ((rv = nni_thr_init(&sock->s_reaper, nni_reaper, sock)) != 0) { nni_cv_fini(&sock->s_cv); nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } @@ -130,7 +130,7 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) nni_thr_fini(&sock->s_reaper); nni_cv_fini(&sock->s_cv); nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } if ((rv = nni_msgqueue_create(&sock->s_urq, 0)) != 0) { @@ -138,7 +138,7 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) nni_thr_fini(&sock->s_reaper); nni_cv_fini(&sock->s_cv); nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } @@ -148,7 +148,7 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) nni_thr_fini(&sock->s_reaper); nni_cv_fini(&sock->s_cv); nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (rv); } nni_thr_run(&sock->s_reaper); @@ -235,7 +235,7 @@ nni_socket_close(nni_socket *sock) nni_msgqueue_destroy(sock->s_uwq); nni_cv_fini(&sock->s_cv); nni_mtx_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); + NNI_FREE_STRUCT(sock); return (0); } diff --git a/src/protocol/pair/pair.c b/src/protocol/pair/pair.c index 9336af8c..6ca891f0 100644 --- a/src/protocol/pair/pair.c +++ b/src/protocol/pair/pair.c @@ -47,11 +47,11 @@ nni_pair_create(void **pairp, nni_socket *sock) nni_pair_sock *pair; int rv; - if ((pair = nni_alloc(sizeof (*pair))) == NULL) { + if ((pair = NNI_ALLOC_STRUCT(pair)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_mtx_init(&pair->mx)) != 0) { - nni_free(pair, sizeof (*pair)); + NNI_FREE_STRUCT(pair); return (rv); } pair->sock = sock; @@ -73,7 +73,7 @@ nni_pair_destroy(void *arg) // the socket already shut us down, and we don't have any other // threads that run. nni_mtx_fini(&pair->mx); - nni_free(pair, sizeof (*pair)); + NNI_FREE_STRUCT(pair); } diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 75cdaa2e..53007e12 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -51,11 +51,11 @@ nni_rep_create(void **repp, nni_socket *sock) nni_rep_sock *rep; int rv; - if ((rep = nni_alloc(sizeof (*rep))) == NULL) { + if ((rep = NNI_ALLOC_STRUCT(rep)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_mtx_init(&rep->mx)) != 0) { - nni_free(rep, sizeof (*rep)); + NNI_FREE_STRUCT(rep); return (rv); } rep->ttl = 8; // Per RFC @@ -65,7 +65,7 @@ nni_rep_create(void **repp, nni_socket *sock) rep->btrace_len = 0; if ((rv = nni_idhash_create(&rep->pipes)) != 0) { nni_mtx_fini(&rep->mx); - nni_free(rep, sizeof (*rep)); + NNI_FREE_STRUCT(rep); return (rv); } @@ -76,7 +76,7 @@ nni_rep_create(void **repp, nni_socket *sock) if (rv != 0) { nni_idhash_destroy(rep->pipes); nni_mtx_fini(&rep->mx); - nni_free(rep, sizeof (*rep)); + NNI_FREE_STRUCT(rep); return (rv); } *repp = rep; @@ -97,7 +97,7 @@ nni_rep_destroy(void *arg) if (rep->btrace != NULL) { nni_free(rep->btrace, rep->btrace_len); } - nni_free(rep, sizeof (*rep)); + NNI_FREE_STRUCT(rep); } diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index 611baadb..6c8f8543 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -55,16 +55,16 @@ nni_req_create(void **reqp, nni_socket *sock) nni_req_sock *req; int rv; - if ((req = nni_alloc(sizeof (*req))) == NULL) { + if ((req = NNI_ALLOC_STRUCT(req)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_mtx_init(&req->mx)) != 0) { - nni_free(req, sizeof (*req)); + NNI_FREE_STRUCT(req); return (rv); } if ((rv = nni_cv_init(&req->cv, &req->mx)) != 0) { nni_mtx_fini(&req->mx); - nni_free(req, sizeof (*req)); + NNI_FREE_STRUCT(req); return (rv); } // this is "semi random" start for request IDs. @@ -84,7 +84,7 @@ nni_req_create(void **reqp, nni_socket *sock) if (rv != 0) { nni_cv_fini(&req->cv); nni_mtx_fini(&req->mx); - nni_free(req, sizeof (*req)); + NNI_FREE_STRUCT(req); return (rv); } nni_thr_run(&req->resender); @@ -107,7 +107,7 @@ nni_req_destroy(void *arg) nni_thr_fini(&req->resender); nni_cv_fini(&req->cv); nni_mtx_fini(&req->mx); - nni_free(req, sizeof (*req)); + NNI_FREE_STRUCT(req); } diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c index 88cf96b9..27cf47cb 100644 --- a/src/transport/inproc/inproc.c +++ b/src/transport/inproc/inproc.c @@ -108,7 +108,7 @@ nni_inproc_pair_destroy(nni_inproc_pair *pair) nni_msgqueue_destroy(pair->q[1]); } nni_mtx_fini(&pair->mx); - nni_free(pair, sizeof (*pair)); + NNI_FREE_STRUCT(pair); } @@ -190,11 +190,11 @@ nni_inproc_ep_create(void **epp, const char *url, uint16_t proto) if (strlen(url) > NNG_MAXADDRLEN-1) { return (NNG_EINVAL); } - if ((ep = nni_alloc(sizeof (*ep))) == NULL) { + if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_cv_init(&ep->cv, &nni_inproc.mx)) != 0) { - nni_free(ep, sizeof (*ep)); + NNI_FREE_STRUCT(ep); return (rv); } @@ -219,7 +219,7 @@ nni_inproc_ep_destroy(void *arg) nni_panic("inproc_ep_destroy while not closed!"); } nni_cv_fini(&ep->cv); - nni_free(ep, sizeof (*free)); + NNI_FREE_STRUCT(ep); } @@ -347,11 +347,11 @@ nni_inproc_ep_accept(void *arg, void **pipep) } // Preallocate the pair, so we don't do it while holding a lock - if ((pair = nni_alloc(sizeof (*pair))) == NULL) { + if ((pair = NNI_ALLOC_STRUCT(pair)) == NULL) { return (NNG_ENOMEM); } if ((rv = nni_mtx_init(&pair->mx)) != 0) { - nni_free(pair, sizeof (*pair)); + NNI_FREE_STRUCT(pair); return (rv); } if (((rv = nni_msgqueue_create(&pair->q[0], 4)) != 0) || -- cgit v1.2.3-70-g09d2