aboutsummaryrefslogtreecommitdiff
path: root/src/core/message.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2016-12-22 17:38:46 -0800
committerGarrett D'Amore <garrett@damore.org>2016-12-22 17:38:46 -0800
commit89c847f1f52969ee2ae6ed35018eef40366ca061 (patch)
treeba3f3c2da64e4a74c69d315b2198df59bcd4441b /src/core/message.c
parent934c1316ae47754a2e368c65228c3cbfe552680f (diff)
downloadnng-89c847f1f52969ee2ae6ed35018eef40366ca061.tar.gz
nng-89c847f1f52969ee2ae6ed35018eef40366ca061.tar.bz2
nng-89c847f1f52969ee2ae6ed35018eef40366ca061.zip
Work on endpoints. More C99 & type cleanups.
Diffstat (limited to 'src/core/message.c')
-rw-r--r--src/core/message.c186
1 files changed, 87 insertions, 99 deletions
diff --git a/src/core/message.c b/src/core/message.c
index 4ba30ed6..6ec47cb9 100644
--- a/src/core/message.c
+++ b/src/core/message.c
@@ -1,51 +1,47 @@
-/*
- * Copyright 2016 Garrett D'Amore <garrett@damore.org>
- *
- * This software is supplied under the terms of the MIT License, a
- * copy of which should be located in the distribution where this
- * file was obtained (LICENSE.txt). A copy of the license may also be
- * found online at https://opensource.org/licenses/MIT.
- */
+//
+// Copyright 2016 Garrett D'Amore <garrett@damore.org>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
#include <stdlib.h>
#include <string.h>
#include "core/nng_impl.h"
-/*
- * Message API.
- */
+// Message API.
-/* Message chunk, internal to the message implementation. */
+// Message chunk, internal to the message implementation.
typedef struct {
- size_t ch_cap; /* allocated size */
- size_t ch_len; /* length in use */
- uint8_t * ch_buf; /* underlying buffer */
- uint8_t * ch_ptr; /* pointer to actual data */
-} chunk_t;
+ size_t ch_cap; // allocated size
+ size_t ch_len; // length in use
+ uint8_t * ch_buf; // underlying buffer
+ uint8_t * ch_ptr; // pointer to actual data
+} nni_chunk;
-/* Underlying message chunk. */
+// Underlying message structure.
struct nng_msg {
- chunk_t m_header;
- chunk_t m_body;
- int64_t m_expire; /* Unix usec */
- nni_pipe_t m_pipe; /* Pipe message was received on */
+ nni_chunk m_header;
+ nni_chunk m_body;
+ nni_time m_expire; // usec
+ nni_pipe * m_pipe; // Pipe message was received on
};
-/*
- * chunk_grow increases the underlying space for a chunk. It ensures
- * that the desired amount of trailing space (including the length)
- * and headroom (excluding the length) are available. It also copies
- * any extant referenced data. Note that the capacity will increase,
- * but not the length. To increase the length of the referenced data,
- * use either chunk_append or chunk_prepend.
- *
- * Note that having some headroom is useful when data must be prepended
- * to a message - it avoids having to perform extra data copies, so we
- * encourage initial allocations to start with sufficient room.
- */
+// nni_chunk_grow increases the underlying space for a chunk. It ensures
+// that the desired amount of trailing space (including the length)
+// and headroom (excluding the length) are available. It also copies
+// any extant referenced data. Note that the capacity will increase,
+// but not the length. To increase the length of the referenced data,
+// use either chunk_append or chunk_prepend.
+//
+// Note that having some headroom is useful when data must be prepended
+// to a message - it avoids having to perform extra data copies, so we
+// encourage initial allocations to start with sufficient room.
static int
-chunk_grow(chunk_t *ch, size_t newsz, size_t headwanted)
+nni_chunk_grow(nni_chunk *ch, size_t newsz, size_t headwanted)
{
size_t headroom = 0;
uint8_t *newbuf;
@@ -111,7 +107,7 @@ chunk_grow(chunk_t *ch, size_t newsz, size_t headwanted)
static void
-chunk_free(chunk_t *ch)
+nni_chunk_free(nni_chunk *ch)
{
if ((ch->ch_cap != 0) && (ch->ch_buf != NULL)) {
nni_free(ch->ch_buf, ch->ch_cap);
@@ -123,9 +119,9 @@ chunk_free(chunk_t *ch)
}
-/* chunk_trunc truncates the number of bytes from the end of the chunk. */
+// nni_chunk_trunc truncates bytes from the end of the chunk.
static int
-chunk_trunc(chunk_t *ch, size_t len)
+nni_chunk_trunc(nni_chunk *ch, size_t len)
{
if (ch->ch_len < len) {
return (NNG_EINVAL);
@@ -135,9 +131,9 @@ chunk_trunc(chunk_t *ch, size_t len)
}
-/* chunk_trim removes the number of bytes from the beginning of the chunk. */
+// nni_chunk_trim removes bytes from the beginning of the chunk.
static int
-chunk_trim(chunk_t *ch, size_t len)
+nni_chunk_trim(nni_chunk *ch, size_t len)
{
if (ch->ch_len < len) {
return (NNG_EINVAL);
@@ -148,20 +144,18 @@ chunk_trim(chunk_t *ch, size_t len)
}
-/*
- * chunk_append appends the data to the chunk, growing the size as necessary.
- * If the data pointer is NULL, then the chunk data region is allocated, but
- * uninitialized.
- */
+// 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.
static int
-chunk_append(chunk_t *ch, const void *data, size_t len)
+nni_chunk_append(nni_chunk *ch, const void *data, size_t len)
{
int rv;
if (len == 0) {
return (0);
}
- if ((rv = chunk_grow(ch, len + ch->ch_len, 0)) != 0) {
+ if ((rv = nni_chunk_grow(ch, len + ch->ch_len, 0)) != 0) {
return (rv);
}
if (ch->ch_ptr == NULL) {
@@ -175,13 +169,11 @@ chunk_append(chunk_t *ch, const void *data, size_t len)
}
-/*
- * chunk_prepend prepends data to the chunk, as efficiently as possible.
- * If the data pointer is NULL, then no data is actually copied, but the
- * data region will have "grown" in the beginning, with uninitialized data.
- */
+// nni_chunk_prepend prepends data to the chunk, as efficiently as possible.
+// If the data pointer is NULL, then no data is actually copied, but the
+// data region will have "grown" in the beginning, with uninitialized data.
static int
-chunk_prepend(chunk_t *ch, const void *data, size_t len)
+nni_chunk_prepend(nni_chunk *ch, const void *data, size_t len)
{
int rv;
@@ -197,7 +189,7 @@ chunk_prepend(chunk_t *ch, const void *data, size_t len)
} else if ((ch->ch_len + len) <= ch->ch_cap) {
/* We had enough capacity, just shuffle data down. */
memmove(ch->ch_ptr + len, ch->ch_ptr, ch->ch_len);
- } else if ((rv = chunk_grow(ch, 0, len)) == 0) {
+ } else if ((rv = nni_chunk_grow(ch, 0, len)) == 0) {
/* We grew the chunk, so adjust. */
ch->ch_ptr -= len;
} else {
@@ -215,42 +207,38 @@ chunk_prepend(chunk_t *ch, const void *data, size_t len)
int
-nni_msg_alloc(nni_msg_t *mp, size_t sz)
+nni_msg_alloc(nni_msg **mp, size_t sz)
{
- nni_msg_t m;
+ nni_msg *m;
int rv;
if ((m = nni_alloc(sizeof (*m))) == NULL) {
return (NNG_ENOMEM);
}
- /*
- * 64-bytes of header, including room for 32 bytes
- * of headroom and 32 bytes of trailer.
- */
- if ((rv = chunk_grow(&m->m_header, 32, 32)) != 0) {
+ // 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));
return (rv);
}
- /*
- * If the message is less than 1024 bytes, or is not power
- * of two aligned, then we insert a 32 bytes of headroom
- * to allow for inlining backtraces, etc. We also allow the
- * amount of space at the end for the same reason. Large aligned
- * allocations are unmolested to avoid excessive overallocation.
- */
+ // If the message is less than 1024 bytes, or is not power
+ // of two aligned, then we insert a 32 bytes of headroom
+ // to allow for inlining backtraces, etc. We also allow the
+ // amount of space at the end for the same reason. Large aligned
+ // allocations are unmolested to avoid excessive overallocation.
if ((sz < 1024) || ((sz & (sz-1)) != 0)) {
- rv = chunk_grow(&m->m_body, sz + 32, 32);
+ rv = nni_chunk_grow(&m->m_body, sz + 32, 32);
} else {
- rv = chunk_grow(&m->m_body, sz, 0);
+ rv = nni_chunk_grow(&m->m_body, sz, 0);
}
if (rv != 0) {
- chunk_free(&m->m_header);
+ nni_chunk_free(&m->m_header);
nni_free(m, sizeof (*m));
}
- if ((rv = chunk_append(&m->m_body, NULL, sz)) != 0) {
- /* Should not happen since we just grew it to fit. */
+ if ((rv = nni_chunk_append(&m->m_body, NULL, sz)) != 0) {
+ // Should not happen since we just grew it to fit.
nni_panic("chunk_append failed");
}
@@ -260,34 +248,34 @@ nni_msg_alloc(nni_msg_t *mp, size_t sz)
void
-nni_msg_free(nni_msg_t m)
+nni_msg_free(nni_msg *m)
{
- chunk_free(&m->m_header);
- chunk_free(&m->m_body);
+ nni_chunk_free(&m->m_header);
+ nni_chunk_free(&m->m_body);
nni_free(m, sizeof (*m));
}
int
-nni_msg_realloc(nni_msg_t m, size_t sz)
+nni_msg_realloc(nni_msg *m, size_t sz)
{
int rv = 0;
if (m->m_body.ch_len < sz) {
- rv = chunk_append(&m->m_body, NULL, sz - m->m_body.ch_len);
+ rv = nni_chunk_append(&m->m_body, NULL, sz - m->m_body.ch_len);
if (rv != 0) {
return (rv);
}
} else {
- /* "Shrinking", just mark bytes at end usable again. */
- chunk_trunc(&m->m_body, m->m_body.ch_len - sz);
+ // "Shrinking", just mark bytes at end usable again.
+ nni_chunk_trunc(&m->m_body, m->m_body.ch_len - sz);
}
return (0);
}
void *
-nni_msg_header(nni_msg_t m, size_t *szp)
+nni_msg_header(nni_msg *m, size_t *szp)
{
if (szp != NULL) {
*szp = m->m_header.ch_len;
@@ -297,7 +285,7 @@ nni_msg_header(nni_msg_t m, size_t *szp)
void *
-nni_msg_body(nni_msg_t m, size_t *szp)
+nni_msg_body(nni_msg *m, size_t *szp)
{
if (szp != NULL) {
*szp = m->m_body.ch_len;
@@ -307,63 +295,63 @@ nni_msg_body(nni_msg_t m, size_t *szp)
int
-nni_msg_append(nni_msg_t m, const void *data, size_t len)
+nni_msg_append(nni_msg *m, const void *data, size_t len)
{
- return (chunk_append(&m->m_body, data, len));
+ return (nni_chunk_append(&m->m_body, data, len));
}
int
-nni_msg_prepend(nni_msg_t m, const void *data, size_t len)
+nni_msg_prepend(nni_msg *m, const void *data, size_t len)
{
- return (chunk_prepend(&m->m_body, data, len));
+ return (nni_chunk_prepend(&m->m_body, data, len));
}
int
-nni_msg_trim(nni_msg_t m, size_t len)
+nni_msg_trim(nni_msg *m, size_t len)
{
- return (chunk_trim(&m->m_body, len));
+ return (nni_chunk_trim(&m->m_body, len));
}
int
-nni_msg_trunc(nni_msg_t m, size_t len)
+nni_msg_trunc(nni_msg *m, size_t len)
{
- return (chunk_trunc(&m->m_body, len));
+ return (nni_chunk_trunc(&m->m_body, len));
}
int
-nni_msg_append_header(nni_msg_t m, const void *data, size_t len)
+nni_msg_append_header(nni_msg *m, const void *data, size_t len)
{
- return (chunk_append(&m->m_header, data, len));
+ return (nni_chunk_append(&m->m_header, data, len));
}
int
-nni_msg_prepend_header(nni_msg_t m, const void *data, size_t len)
+nni_msg_prepend_header(nni_msg *m, const void *data, size_t len)
{
- return (chunk_prepend(&m->m_header, data, len));
+ return (nni_chunk_prepend(&m->m_header, data, len));
}
int
-nni_msg_trim_header(nni_msg_t m, size_t len)
+nni_msg_trim_header(nni_msg *m, size_t len)
{
- return (chunk_trim(&m->m_header, len));
+ return (nni_chunk_trim(&m->m_header, len));
}
int
-nni_msg_trunc_header(nni_msg_t m, size_t len)
+nni_msg_trunc_header(nni_msg *m, size_t len)
{
- return (chunk_trunc(&m->m_header, len));
+ return (nni_chunk_trunc(&m->m_header, len));
}
int
-nni_msg_pipe(nni_msg_t m, nni_pipe_t *pp)
+nni_msg_pipe(nni_msg *m, nni_pipe **pp)
{
*pp = m->m_pipe;
return (0);