From 49c8c264057c4c7b0f9ff45f352168cb1deeb1a1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 31 Aug 2018 11:25:50 -0700 Subject: fixes #691 Desire 16 and 64 bit message manipulators We use macros to generate message bodies for each of the various variants, reducing source code size (but not compiled size). The documentation is updated to indicate each of these variants. --- src/nng.c | 104 +++++++++++++++++++++++++------------------------------------- 1 file changed, 41 insertions(+), 63 deletions(-) (limited to 'src/nng.c') diff --git a/src/nng.c b/src/nng.c index 37ee3e5f..64968cae 100644 --- a/src/nng.c +++ b/src/nng.c @@ -1329,69 +1329,47 @@ nng_msg_header_append(nng_msg *msg, const void *data, size_t sz) return (nni_msg_header_append(msg, data, sz)); } -int -nng_msg_header_append_u32(nng_msg *msg, uint32_t val) -{ - return (nni_msg_header_append_u32(msg, val)); -} - -int -nng_msg_header_insert_u32(nng_msg *msg, uint32_t val) -{ - return (nni_msg_header_insert_u32(msg, val)); -} - -int -nng_msg_header_chop_u32(nng_msg *msg, uint32_t *valp) -{ - if (nni_msg_header_len(msg) < sizeof(uint32_t)) { - return (NNG_EINVAL); - } - *valp = nni_msg_header_chop_u32(msg); - return (0); -} - -int -nng_msg_header_trim_u32(nng_msg *msg, uint32_t *valp) -{ - if (nni_msg_header_len(msg) < sizeof(uint32_t)) { - return (NNG_EINVAL); - } - *valp = nni_msg_header_trim_u32(msg); - return (0); -} - -int -nng_msg_append_u32(nng_msg *msg, uint32_t val) -{ - return (nni_msg_append_u32(msg, val)); -} - -int -nng_msg_insert_u32(nng_msg *msg, uint32_t val) -{ - return (nni_msg_insert_u32(msg, val)); -} - -int -nng_msg_chop_u32(nng_msg *msg, uint32_t *valp) -{ - if (nni_msg_len(msg) < sizeof(uint32_t)) { - return (NNG_EINVAL); - } - *valp = nni_msg_chop_u32(msg); - return (0); -} - -int -nng_msg_trim_u32(nng_msg *msg, uint32_t *valp) -{ - if (nni_msg_len(msg) < sizeof(uint32_t)) { - return (NNG_EINVAL); - } - *valp = nni_msg_trim_u32(msg); - return (0); -} +#define DEF_MSG_ADD_N(op, n) \ + int nng_msg_header_##op##_u##n(nng_msg *m, uint##n##_t v) \ + { \ + return (nni_msg_header_##op##_u##n(m, v)); \ + } \ + int nng_msg_##op##_u##n(nng_msg *m, uint##n##_t v) \ + { \ + return (nni_msg_##op##_u##n(m, v)); \ + } +#define DEF_MSG_REM_N(op, n) \ + int nng_msg_header_##op##_u##n(nng_msg *m, uint##n##_t *vp) \ + { \ + if (nni_msg_header_len(m) < sizeof(*vp)) { \ + return (NNG_EINVAL); \ + } \ + *vp = nni_msg_header_##op##_u##n(m); \ + return (0); \ + } \ + int nng_msg_##op##_u##n(nng_msg *m, uint##n##_t *vp) \ + { \ + if (nni_msg_len(m) < sizeof(*vp)) { \ + return (NNG_EINVAL); \ + } \ + *vp = nni_msg_##op##_u##n(m); \ + return (0); \ + } + +#define DEF_MSG_ADD(op) \ + DEF_MSG_ADD_N(op, 16) DEF_MSG_ADD_N(op, 32) DEF_MSG_ADD_N(op, 64) +#define DEF_MSG_REM(op) \ + DEF_MSG_REM_N(op, 16) DEF_MSG_REM_N(op, 32) DEF_MSG_REM_N(op, 64) + +DEF_MSG_ADD(append) +DEF_MSG_ADD(insert) +DEF_MSG_REM(chop) +DEF_MSG_REM(trim) + +#undef DEF_MSG_ADD_N +#undef DEF_MSG_REM_N +#undef DEF_MSG_ADD +#undef DEF_MSG_REM int nng_msg_header_insert(nng_msg *msg, const void *data, size_t sz) -- cgit v1.2.3-70-g09d2