Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #327 from cole-miller/byte-access
Browse files Browse the repository at this point in the history
byte: Don't bother with aligned byte-access functions
  • Loading branch information
Mathieu Borderé authored Nov 17, 2022
2 parents 7a2d087 + 5b85509 commit 281eea5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 44 deletions.
33 changes: 11 additions & 22 deletions src/byte.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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++) {
Expand All @@ -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(
Expand Down
16 changes: 8 additions & 8 deletions src/uv_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions test/unit/test_byte.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions test/unit/test_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 281eea5

Please sign in to comment.