Added more const-qualifiers in the simple memory functions
This commit is contained in:
parent
8b65c5ddb3
commit
eab3989878
|
@ -11,7 +11,7 @@
|
||||||
/// @param block2 The second block to check for overlapping.
|
/// @param block2 The second block to check for overlapping.
|
||||||
/// @param length2 The length of the second block.
|
/// @param length2 The length of the second block.
|
||||||
/// @return Whether the two blocks overlap.
|
/// @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.
|
/// @brief Checks if two memory blocks contain the equal data.
|
||||||
/// @param block1 Address of the data to be checked for equality with 'block2'
|
/// @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.
|
/// this should be the correct data to be checked against.
|
||||||
/// @param count How many bytes will be checked.
|
/// @param count How many bytes will be checked.
|
||||||
/// @return Whether the two memory regions are equal.
|
/// @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.
|
/// @brief Sets all bytes in a memory region all to one byte.
|
||||||
/// @param destination Address to the memory region to fill with 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 sequence Sequence of bytes which will be written to destination.
|
||||||
/// @param len_sequence Length of 'sequence'.
|
/// @param len_sequence Length of 'sequence'.
|
||||||
/// @return The number of attempted or partial repetitions which have been performed.
|
/// @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.
|
/// @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!
|
/// @attention The source and the destination cannot overlap!
|
||||||
/// @param destination Memory region to which to write.
|
/// @param destination Memory region to which to write.
|
||||||
/// @param source Memory region from which to read the bytes to write into the destination.
|
/// @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.
|
/// @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
|
#endif // RR_MEMORY_H
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
#include <librr/memory.h>
|
#include <librr/memory.h>
|
||||||
#include <librr/strutil.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
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
|
// Make block1 always be before block2
|
||||||
if(block1 > block2)
|
if(block1 > block2)
|
||||||
{
|
{
|
||||||
void *block_backup = block2;
|
const void *block_backup = block2;
|
||||||
usz_t length_backup = length2;
|
usz_t length_backup = length2;
|
||||||
block2 = block1;
|
block2 = block1;
|
||||||
length2 = length1;
|
length2 = length1;
|
||||||
|
@ -27,10 +25,10 @@ bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2)
|
||||||
return FALSE;
|
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;
|
const u8_t *block1_8 = block1;
|
||||||
u8_t *block2_8 = block2;
|
const u8_t *block2_8 = block2;
|
||||||
for(usz_t index = 0; index < count; ++index)
|
for(usz_t index = 0; index < count; ++index)
|
||||||
if(block1_8[index] != block2_8[index]) return FALSE;
|
if(block1_8[index] != block2_8[index]) return FALSE;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -43,22 +41,22 @@ void rr_memset(void *destination, usz_t count, u8_t value)
|
||||||
destination_8[index] = 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;
|
if(rr_mem_overlap(destination, num_bytes, sequence, len_sequence)) return 0;
|
||||||
u8_t *destination_8 = destination;
|
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)
|
for(usz_t index = 0; index < num_bytes; ++index)
|
||||||
destination_8[index] = sequence_8[index % len_sequence];
|
destination_8[index] = sequence_8[index % len_sequence];
|
||||||
return (num_bytes / len_sequence) // How many full repetitions are performed
|
return (num_bytes / len_sequence) // How many full repetitions are performed
|
||||||
+ ((num_bytes % len_sequence) != 0); // One more if a partial repetiton is done
|
+ ((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;
|
if(rr_mem_overlap(destination, num_bytes, source, num_bytes)) return;
|
||||||
u8_t *destination_8 = destination;
|
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)
|
for(usz_t index = 0; index < num_bytes; ++index)
|
||||||
destination_8[index] = source_8[index];
|
destination_8[index] = source_8[index];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue