From 5b8550980d50ab38bbfed127657f6e488c9dfe58 Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Wed, 16 Nov 2022 15:21:07 -0500 Subject: [PATCH] byte: Don't bother with aligned byte-access functions Signed-off-by: Cole Miller --- src/byte.h | 33 +++++++++++---------------------- src/configuration.c | 8 ++++---- src/uv_metadata.c | 16 ++++++++-------- test/unit/test_byte.c | 10 +++++----- test/unit/test_configuration.c | 10 +++++----- 5 files changed, 33 insertions(+), 44 deletions(-) diff --git a/src/byte.h b/src/byte.h index dd42600bc..510e56a14 100644 --- a/src/byte.h +++ b/src/byte.h @@ -83,19 +83,14 @@ BYTE__INLINE void bytePut8(void **cursor, uint8_t value) BYTE__INLINE void bytePut32(void **cursor, uint32_t value) { - uint32_t **p = (uint32_t **)cursor; - **p = byteFlip32(value); - *p += 1; + unsigned i; + uint32_t flipped = byteFlip32(value); + for (i = 0; i < sizeof(uint32_t); i++) { + bytePut8(cursor, ((uint8_t *)(&flipped))[i]); + } } BYTE__INLINE void bytePut64(void **cursor, uint64_t value) -{ - uint64_t **p = (uint64_t **)cursor; - **p = byteFlip64(value); - *p += 1; -} - -BYTE__INLINE void bytePut64Unaligned(void **cursor, uint64_t value) { unsigned i; uint64_t flipped = byteFlip64(value); @@ -121,21 +116,15 @@ BYTE__INLINE uint8_t byteGet8(const void **cursor) BYTE__INLINE uint32_t byteGet32(const void **cursor) { - const uint32_t **p = (const uint32_t **)cursor; - uint32_t value = byteFlip32(**p); - *p += 1; - return value; + uint32_t value = 0; + unsigned i; + for (i = 0; i < sizeof(uint32_t); i++) { + ((uint8_t *)(&value))[i] = byteGet8(cursor); + } + return byteFlip32(value); } BYTE__INLINE uint64_t byteGet64(const void **cursor) -{ - const uint64_t **p = (const uint64_t **)cursor; - uint64_t value = byteFlip64(**p); - *p += 1; - return value; -} - -BYTE__INLINE uint64_t byteGet64Unaligned(const void **cursor) { uint64_t value = 0; unsigned i; diff --git a/src/configuration.c b/src/configuration.c index 426002d20..7a6d23d47 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -243,12 +243,12 @@ void configurationEncodeToBuf(const struct raft_configuration *c, void *buf) bytePut8(&cursor, ENCODING_FORMAT); /* Number of servers. */ - bytePut64Unaligned(&cursor, c->n); /* cursor might not be 8-byte aligned */ + bytePut64(&cursor, c->n); for (i = 0; i < c->n; i++) { struct raft_server *server = &c->servers[i]; assert(server->address != NULL); - bytePut64Unaligned(&cursor, server->id); /* might not be aligned */ + bytePut64(&cursor, server->id); bytePutString(&cursor, server->address); assert(server->role < 255); bytePut8(&cursor, (uint8_t)server->role); @@ -300,7 +300,7 @@ int configurationDecode(const struct raft_buffer *buf, } /* Read the number of servers. */ - n = (size_t)byteGet64Unaligned(&cursor); + n = (size_t)byteGet64(&cursor); /* Decode the individual servers. */ for (i = 0; i < n; i++) { @@ -310,7 +310,7 @@ int configurationDecode(const struct raft_buffer *buf, int rv; /* Server ID. */ - id = byteGet64Unaligned(&cursor); + id = byteGet64(&cursor); /* Server Address. */ address = byteGetString( diff --git a/src/uv_metadata.c b/src/uv_metadata.c index 5034e06f3..f8363b302 100644 --- a/src/uv_metadata.c +++ b/src/uv_metadata.c @@ -14,10 +14,10 @@ static void uvMetadataEncode(const struct uvMetadata *metadata, void *buf) { void *cursor = buf; - bytePut64Unaligned(&cursor, UV__DISK_FORMAT); - bytePut64Unaligned(&cursor, metadata->version); - bytePut64Unaligned(&cursor, metadata->term); - bytePut64Unaligned(&cursor, metadata->voted_for); + bytePut64(&cursor, UV__DISK_FORMAT); + bytePut64(&cursor, metadata->version); + bytePut64(&cursor, metadata->term); + bytePut64(&cursor, metadata->voted_for); } /* Decode the content of a metadata file. */ @@ -27,14 +27,14 @@ static int uvMetadataDecode(const void *buf, { const void *cursor = buf; uint64_t format; - format = byteGet64Unaligned(&cursor); + format = byteGet64(&cursor); if (format != UV__DISK_FORMAT) { ErrMsgPrintf(errmsg, "bad format version %ju", format); return RAFT_MALFORMED; } - metadata->version = byteGet64Unaligned(&cursor); - metadata->term = byteGet64Unaligned(&cursor); - metadata->voted_for = byteGet64Unaligned(&cursor); + metadata->version = byteGet64(&cursor); + metadata->term = byteGet64(&cursor); + metadata->voted_for = byteGet64(&cursor); /* Coherence checks that values make sense */ if (metadata->version == 0) { diff --git a/test/unit/test_byte.c b/test/unit/test_byte.c index 6309d0be3..a70036b1c 100644 --- a/test/unit/test_byte.c +++ b/test/unit/test_byte.c @@ -98,19 +98,19 @@ TEST(byteGetString, malformed, NULL, NULL, 0, NULL) /****************************************************************************** * - * byteGet64Unaligned + * byteGet64 * *****************************************************************************/ -SUITE(byteGet64Unaligned) +SUITE(byteGet64) -TEST(byteGet64Unaligned, success, NULL, NULL, 0, NULL) +TEST(byteGet64, success, NULL, NULL, 0, NULL) { uint8_t *buf = munit_malloc(sizeof(uint64_t) * 2); void *cursor1 = buf + 1; const void *cursor2 = buf + 1; - bytePut64Unaligned(&cursor1, 1); - munit_assert_int(byteGet64Unaligned(&cursor2), ==, 1); + bytePut64(&cursor1, 1); + munit_assert_int(byteGet64(&cursor2), ==, 1); free(buf); return MUNIT_OK; } diff --git a/test/unit/test_configuration.c b/test/unit/test_configuration.c index f4ba2d066..71d16c72a 100644 --- a/test/unit/test_configuration.c +++ b/test/unit/test_configuration.c @@ -426,9 +426,9 @@ TEST(configurationEncode, one_server, setUp, tearDown, 0, NULL) cursor = buf.base; munit_assert_int(byteGet8(&cursor), ==, 1); - munit_assert_int(byteGet64Unaligned(&cursor), ==, 1); + munit_assert_int(byteGet64(&cursor), ==, 1); - munit_assert_int(byteGet64Unaligned(&cursor), ==, 1); + munit_assert_int(byteGet64(&cursor), ==, 1); munit_assert_string_equal(byteGetString(&cursor, strlen(address) + 1), address); munit_assert_int(byteGet8(&cursor), ==, RAFT_VOTER); @@ -462,14 +462,14 @@ TEST(configurationEncode, two_servers, setUp, tearDown, 0, NULL) cursor = buf.base; munit_assert_int(byteGet8(&cursor), ==, 1); - munit_assert_int(byteGet64Unaligned(&cursor), ==, 2); + munit_assert_int(byteGet64(&cursor), ==, 2); - munit_assert_int(byteGet64Unaligned(&cursor), ==, 1); + munit_assert_int(byteGet64(&cursor), ==, 1); munit_assert_string_equal(byteGetString(&cursor, strlen(address1) + 1), address1); munit_assert_int(byteGet8(&cursor), ==, RAFT_STANDBY); - munit_assert_int(byteGet64Unaligned(&cursor), ==, 2); + munit_assert_int(byteGet64(&cursor), ==, 2); munit_assert_string_equal(byteGetString(&cursor, strlen(address2) + 1), address2); munit_assert_int(byteGet8(&cursor), ==, RAFT_VOTER);