From eab3989878495b3e3a9b068f0021b5d89361b8b0 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Fri, 19 Jan 2024 20:59:29 +0100 Subject: [PATCH] Added more const-qualifiers in the simple memory functions --- core/exports/librr/memory.h | 8 ++++---- core/src-c/memory.c | 20 +++++++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/core/exports/librr/memory.h b/core/exports/librr/memory.h index b253a79..9564cc4 100644 --- a/core/exports/librr/memory.h +++ b/core/exports/librr/memory.h @@ -11,7 +11,7 @@ /// @param block2 The second block to check for overlapping. /// @param length2 The length of the second block. /// @return Whether the two blocks overlap. -bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2); +bool_t rr_mem_overlap(const void *block1, usz_t length1, const void *block2, usz_t length2); /// @brief Checks if two memory blocks contain the equal data. /// @param block1 Address of the data to be checked for equality with 'block2' @@ -19,7 +19,7 @@ bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2); /// this should be the correct data to be checked against. /// @param count How many bytes will be checked. /// @return Whether the two memory regions are equal. -bool_t rr_memequ(void *block1, void *block2, usz_t count); +bool_t rr_memequ(const void *block1, const void *block2, usz_t count); /// @brief Sets all bytes in a memory region all to one byte. /// @param destination Address to the memory region to fill with one byte. @@ -34,13 +34,13 @@ void rr_memset(void *destination, usz_t count, u8_t value); /// @param sequence Sequence of bytes which will be written to destination. /// @param len_sequence Length of 'sequence'. /// @return The number of attempted or partial repetitions which have been performed. -usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_sequence); +usz_t rr_memrep(void *destination, usz_t num_bytes, const void *sequence, usz_t len_sequence); /// @brief Copies a given number of bytes from a source memory region to a destination memory region. /// @attention The source and the destination cannot overlap! /// @param destination Memory region to which to write. /// @param source Memory region from which to read the bytes to write into the destination. /// @param num_bytes Number of bytes to copy from the source to the destination. -void rr_memcopy(void *destination, void *source, usz_t num_bytes); +void rr_memcopy(void *destination, const void *source, usz_t num_bytes); #endif // RR_MEMORY_H diff --git a/core/src-c/memory.c b/core/src-c/memory.c index aad9dd5..03a2717 100644 --- a/core/src-c/memory.c +++ b/core/src-c/memory.c @@ -1,13 +1,11 @@ #include -#include -#include -bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2) +bool_t rr_mem_overlap(const void *block1, usz_t length1, const void *block2, usz_t length2) { // Make block1 always be before block2 if(block1 > block2) { - void *block_backup = block2; + const void *block_backup = block2; usz_t length_backup = length2; block2 = block1; length2 = length1; @@ -27,10 +25,10 @@ bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2) return FALSE; } -bool_t rr_memequ(void *block1, void *block2, usz_t count) +bool_t rr_memequ(const void *block1, const void *block2, usz_t count) { - u8_t *block1_8 = block1; - u8_t *block2_8 = block2; + const u8_t *block1_8 = block1; + const u8_t *block2_8 = block2; for(usz_t index = 0; index < count; ++index) if(block1_8[index] != block2_8[index]) return FALSE; return TRUE; @@ -43,22 +41,22 @@ void rr_memset(void *destination, usz_t count, u8_t value) destination_8[index] = value; } -usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_sequence) +usz_t rr_memrep(void *destination, usz_t num_bytes, const void *sequence, usz_t len_sequence) { if(rr_mem_overlap(destination, num_bytes, sequence, len_sequence)) return 0; u8_t *destination_8 = destination; - u8_t *sequence_8 = sequence; + const u8_t *sequence_8 = sequence; for(usz_t index = 0; index < num_bytes; ++index) destination_8[index] = sequence_8[index % len_sequence]; return (num_bytes / len_sequence) // How many full repetitions are performed + ((num_bytes % len_sequence) != 0); // One more if a partial repetiton is done } -void rr_memcopy(void *destination, void *source, usz_t num_bytes) +void rr_memcopy(void *destination, const void *source, usz_t num_bytes) { if(rr_mem_overlap(destination, num_bytes, source, num_bytes)) return; u8_t *destination_8 = destination; - u8_t *source_8 = source; + const u8_t *source_8 = source; for(usz_t index = 0; index < num_bytes; ++index) destination_8[index] = source_8[index]; }