diff options
| author | Garrett D'Amore <garrett@damore.org> | 2016-12-28 22:37:03 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2016-12-28 22:37:03 -0800 |
| commit | db7e8d5e05b4d266d87d059f207a20792b224983 (patch) | |
| tree | d9b5fad6d859087d4b55ed54fc5bbe06f1e76efb /src | |
| parent | bdd36ae2d624bebab7fb6f071738ca7a154fa916 (diff) | |
| download | nng-db7e8d5e05b4d266d87d059f207a20792b224983.tar.gz nng-db7e8d5e05b4d266d87d059f207a20792b224983.tar.bz2 nng-db7e8d5e05b4d266d87d059f207a20792b224983.zip | |
Add new nni_msg_dup() API. (Needed for REQ protocol.)
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/message.c | 40 | ||||
| -rw-r--r-- | src/core/message.h | 1 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/core/message.c b/src/core/message.c index b07d107f..6fafc64c 100644 --- a/src/core/message.c +++ b/src/core/message.c @@ -148,6 +148,23 @@ nni_chunk_trim(nni_chunk *ch, size_t len) } +// nni_chunk_dup allocates storage for a new chunk, and copies +// the contents of the source to the destination. The new chunk will +// have the same size, headroom, and capacity as the original. +static int +nni_chunk_dup(nni_chunk *dst, const nni_chunk *src) +{ + if ((dst->ch_buf = nni_alloc(src->ch_cap)) != 0) { + return (NNG_ENOMEM); + } + dst->ch_cap = src->ch_cap; + dst->ch_len = src->ch_len; + dst->ch_ptr = dst->ch_buf + (src->ch_ptr - src->ch_buf); + memcpy(dst->ch_ptr, src->ch_ptr, dst->ch_len); + return (0); +} + + // nni_chunk_append appends the data to the chunk, growing as necessary. // If the data pointer is NULL, then the chunk data region is allocated, // but uninitialized. @@ -252,6 +269,29 @@ nni_msg_alloc(nni_msg **mp, size_t sz) } +int +nni_msg_dup(nni_msg **dup, const nni_msg *src) +{ + nni_msg *m; + int rv; + + if ((m = nni_alloc(sizeof (*m))) == NULL) { + return (NNG_ENOMEM); + } + if ((rv = nni_chunk_dup(&m->m_header, &src->m_header)) != 0) { + nni_free(m, sizeof (*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)); + return (rv); + } + *dup = m; + return (0); +} + + void nni_msg_free(nni_msg *m) { diff --git a/src/core/message.h b/src/core/message.h index 2982eeb7..4ce3c7fd 100644 --- a/src/core/message.h +++ b/src/core/message.h @@ -15,6 +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 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); |
