From f7bf24f429cbc488b861ab1b1e4cf1983af56295 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 1 Jan 2025 17:06:39 -0800 Subject: api: Remove the NNG_FLAG_ALLOC This flag failed to provide real zero copy that it was intended for, and it also involved extra allocations. Further, the API for it was brittle and error prone. Modern code should just work directly with nng_msg structures. --- src/nng.c | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) (limited to 'src/nng.c') diff --git a/src/nng.c b/src/nng.c index 55e23214..c4bad311 100644 --- a/src/nng.c +++ b/src/nng.c @@ -79,35 +79,12 @@ nng_recv(nng_socket s, void *buf, size_t *szp, int flags) // Note that while it would be nice to make this a zero copy operation, // its not normally possible if a size was specified. - if ((rv = nng_recvmsg(s, &msg, flags & ~(NNG_FLAG_ALLOC))) != 0) { + if ((rv = nng_recvmsg(s, &msg, flags)) != 0) { return (rv); } - if (!(flags & NNG_FLAG_ALLOC)) { - memcpy(buf, nng_msg_body(msg), - *szp > nng_msg_len(msg) ? nng_msg_len(msg) : *szp); - *szp = nng_msg_len(msg); - } else { - // We'd really like to avoid a separate data copy, but since - // we have allocated messages with headroom, we can't really - // make free() work on the base pointer. We'd have to have - // some other API for this. Folks that want zero copy had - // better use nng_recvmsg() instead. - void *nbuf; - - if (nng_msg_len(msg) != 0) { - if ((nbuf = nni_alloc(nng_msg_len(msg))) == NULL) { - nng_msg_free(msg); - return (NNG_ENOMEM); - } - - *(void **) buf = nbuf; - memcpy(nbuf, nni_msg_body(msg), nni_msg_len(msg)); - *szp = nng_msg_len(msg); - } else { - *(void **) buf = NULL; - *szp = 0; - } - } + memcpy(buf, nng_msg_body(msg), + *szp > nng_msg_len(msg) ? nng_msg_len(msg) : *szp); + *szp = nng_msg_len(msg); nni_msg_free(msg); return (0); } @@ -159,10 +136,6 @@ nng_send(nng_socket s, void *buf, size_t len, int flags) if ((rv = nng_sendmsg(s, msg, flags)) != 0) { // If nng_sendmsg() succeeded, then it took ownership. nng_msg_free(msg); - } else { - if (flags & NNG_FLAG_ALLOC) { - nni_free(buf, len); - } } return (rv); } -- cgit v1.2.3-70-g09d2