diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-01-02 23:02:48 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-01-02 23:02:48 -0800 |
| commit | 1c54bc5674a54d84112da5191c28a745d448c816 (patch) | |
| tree | 18261f5dfc361ae0414c134b3fe84acbab0bb6ba | |
| parent | 29678ad27eff07ef0c4726b01a07794f5bff1bc7 (diff) | |
| download | nng-1c54bc5674a54d84112da5191c28a745d448c816.tar.gz nng-1c54bc5674a54d84112da5191c28a745d448c816.tar.bz2 nng-1c54bc5674a54d84112da5191c28a745d448c816.zip | |
Add NNI_GET32, NNI_PUT32 macros.
| -rw-r--r-- | src/core/defs.h | 14 | ||||
| -rw-r--r-- | src/protocol/reqrep/rep.c | 13 | ||||
| -rw-r--r-- | src/protocol/reqrep/req.c | 5 |
3 files changed, 17 insertions, 15 deletions
diff --git a/src/core/defs.h b/src/core/defs.h index e44be1ec..1114704d 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -44,4 +44,18 @@ typedef int64_t nni_duration; // Relative time (usec). #define NNI_ALLOC_STRUCT(s) nni_alloc(sizeof (*s)) #define NNI_FREE_STRUCT(s) nni_free((s), sizeof (*s)) +#define NNI_PUT32(ptr, u) \ + do { \ + ptr[0] = (uint8_t)(((uint32_t)u) >> 24); \ + ptr[1] = (uint8_t)(((uint32_t)u) >> 16); \ + ptr[2] = (uint8_t)(((uint32_t)u) >> 8); \ + ptr[3] = (uint8_t)((uint32_t)u); \ + } while (0) + +#define NNI_GET32(ptr, v) \ + v = (((uint32_t)((uint8_t)ptr[0])) << 24) + \ + (((uint32_t)((uint8_t)ptr[1])) << 16) + \ + (((uint32_t)((uint8_t)ptr[2])) << 8) + \ + (((uint32_t)(uint8_t)ptr[3])) + #endif // CORE_DEFS_H diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 96413523..ca59d799 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -185,13 +185,7 @@ nni_rep_topsender(void *arg) nni_msg_free(msg); continue; } - id = header[0]; - id <<= 8; - id += header[1]; - id <<= 8; - id += header[2]; - id <<= 8; - id += header[3]; + NNI_GET32(header, id); nni_msg_trim_header(msg, 4); nni_mtx_lock(&rep->mx); @@ -256,10 +250,7 @@ nni_rep_pipe_recv(void *arg) uint8_t idbuf[4]; uint32_t id = nni_pipe_id(pipe); - idbuf[0] = (uint8_t) (id >> 24); - idbuf[1] = (uint8_t) (id >> 16); - idbuf[2] = (uint8_t) (id >> 8); - idbuf[3] = (uint8_t) (id); + NNI_PUT32(idbuf, id); for (;;) { size_t len; diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index 0879755f..4ce66ab6 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -326,10 +326,7 @@ nni_req_sendfilter(void *arg, nni_msg *msg) id = (req->nextid++) | 0x80000000u; // Request ID is in big endian format. - req->reqid[0] = (uint8_t) (id >> 24); - req->reqid[1] = (uint8_t) (id >> 16); - req->reqid[2] = (uint8_t) (id >> 8); - req->reqid[3] = (uint8_t) (id); + NNI_PUT32(req->reqid, id); if (nni_msg_append_header(msg, req->reqid, 4) != 0) { // Should be ENOMEM. |
