From 06a883d538f220d0aa761a85d9050c93b9ecbeb7 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 28 Apr 2024 17:41:00 +0530 Subject: [PATCH] Made lib/base conversion functions const where possible --- lib/base.c | 4 ++-- lib/base.h | 19 ++++++++++--------- test/lib/test-base.h | 31 ++++++++++++++++--------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/lib/base.c b/lib/base.c index f34307b..5fbe097 100644 --- a/lib/base.c +++ b/lib/base.c @@ -70,7 +70,7 @@ word_t word_bctoh(word_t w) return y.h; } -hword_t convert_bytes_to_hword(byte_t *bytes) +hword_t convert_bytes_to_hword(const byte_t *bytes) { hword_t h = 0; for (size_t i = 0; i < HWORD_SIZE; ++i) @@ -78,7 +78,7 @@ hword_t convert_bytes_to_hword(byte_t *bytes) return h; } -word_t convert_bytes_to_word(byte_t *bytes) +word_t convert_bytes_to_word(const byte_t *bytes) { word_t h = 0; for (size_t i = 0; i < WORD_SIZE; ++i) diff --git a/lib/base.h b/lib/base.h index 66043f1..0acfac0 100644 --- a/lib/base.h +++ b/lib/base.h @@ -104,7 +104,8 @@ static const int __i = 1; #endif /** - @brief Safely subtract SUB from W, where both are words (64 bit integers). + @brief Safely subtract SUB from W, where both are words (64 bit + integers). @details In case of underflow (i.e. where W - SUB < 0) returns 0 instead of the underflowed result. @@ -133,7 +134,7 @@ static const int __i = 1; code format (little endian) and that they are at least HWORD_SIZE in size. */ -hword_t convert_bytes_to_hword(byte_t *buffer); +hword_t convert_bytes_to_hword(const byte_t *buffer); /** @brief Convert a half word into a VM byte code format bytes (big @@ -143,7 +144,7 @@ hword_t convert_bytes_to_hword(byte_t *buffer); @param buffer: Buffer to store into. We assume the buffer has at least HWORD_SIZE space. */ -void convert_hword_to_bytes(hword_t h, byte_t *buffer); +void convert_hword_to_bytes(const hword_t h, byte_t *buffer); /** @brief Convert a buffer of bytes to a word. @@ -152,7 +153,7 @@ void convert_hword_to_bytes(hword_t h, byte_t *buffer); code format (little endian) and that they are at least WORD_SIZE in size. */ -word_t convert_bytes_to_word(byte_t *); +word_t convert_bytes_to_word(const byte_t *); /** @brief Convert a word into a VM byte code format bytes (little @@ -163,27 +164,27 @@ word_t convert_bytes_to_word(byte_t *); @param buffer: Buffer to store into. We assume the buffer has at least WORD_SIZE space. */ -void convert_word_to_bytes(word_t w, byte_t *buffer); +void convert_word_to_bytes(const word_t w, byte_t *buffer); /** @brief Convert a half word into bytecode format (little endian) */ -hword_t hword_htobc(hword_t); +hword_t hword_htobc(const hword_t); /** @brief Convert a half word in bytecode format (little endian) to host format */ -hword_t hword_bctoh(hword_t); +hword_t hword_bctoh(const hword_t); /** @brief Convert a word into bytecode format (little endian) */ -word_t word_htobc(word_t); +word_t word_htobc(const word_t); /** @brief Convert a word in bytecode format (little endian) to host format */ -word_t word_bctoh(word_t); +word_t word_bctoh(const word_t); #endif diff --git a/test/lib/test-base.h b/test/lib/test-base.h index 08e8d3e..08571e7 100644 --- a/test/lib/test-base.h +++ b/test/lib/test-base.h @@ -18,17 +18,18 @@ void testing_lib_bytes_to_hword(void) { - byte_t tests[][4] = {{0, 0, 0, 0}, - {0xFF, 0xFF, 0xFF, 0xFF}, - {1}, - {0, 0, 0, 0b10000000}, - {0x89, 0xab, 0xcd, 0xef}}; + const byte_t tests[][4] = {{0, 0, 0, 0}, + {0xFF, 0xFF, 0xFF, 0xFF}, + {1}, + {0, 0, 0, 0b10000000}, + {0x89, 0xab, 0xcd, 0xef}}; - hword_t expected[ARR_SIZE(tests)] = {0, HWORD_MAX, 1, 1 << 31, 0xefcdab89}; + const hword_t expected[ARR_SIZE(tests)] = {0, HWORD_MAX, 1, 1 << 31, + 0xefcdab89}; for (size_t i = 0; i < ARR_SIZE(tests); ++i) { - hword_t got = convert_bytes_to_hword(tests[i]); + const hword_t got = convert_bytes_to_hword(tests[i]); if (expected[i] != got) { FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, expected[i], got); @@ -40,18 +41,18 @@ void testing_lib_bytes_to_hword(void) void testing_lib_bytes_to_word(void) { - byte_t tests[][8] = {{0, 0, 0, 0}, - {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, - {0x01, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0b10000000}, - {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}; + const byte_t tests[][8] = {{0, 0, 0, 0}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0x01, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0b10000000}, + {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}; - word_t expected[ARR_SIZE(tests)] = {0, WORD_MAX, 1, 1LU << 63, + const word_t expected[ARR_SIZE(tests)] = {0, WORD_MAX, 1, 1LU << 63, - 0xefcdab8967452301}; + 0xefcdab8967452301}; for (size_t i = 0; i < ARR_SIZE(tests); ++i) { - word_t got = convert_bytes_to_word(tests[i]); + const word_t got = convert_bytes_to_word(tests[i]); if (expected[i] != got) { FAIL(__func__, "[%lu] -> Expected 0x%lx got 0x%lx\n", i, expected[i],