Added memory copy function.

This commit is contained in:
Eric-Paul Ickhorn 2023-12-02 17:11:33 +01:00
parent 602cc942b0
commit b13ec4abcd
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
2 changed files with 20 additions and 5 deletions

View File

@ -9,7 +9,7 @@
// If the address of this block is higher than that of block2, the blocks will be swapped internally. // If the address of this block is higher than that of block2, the blocks will be swapped internally.
/// @param length1 The length of the first block. /// @param length1 The length of the first block.
/// @param block2 The second block to check for overlapping. /// @param block2 The second block to check for overlapping.
/// @param length2 /// @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(void *block1, usz_t length1, void *block2, usz_t length2);
@ -18,11 +18,11 @@ bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2);
/// @param block2 Address of the other memory block. In cases where there is a clear correct version, /// @param block2 Address of the other memory block. In cases where there is a clear correct version,
/// 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(void *block1, 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.
/// @param count Number of bytes to set; length of 'destination' /// @param count Number of bytes to set; length of 'destination'
/// @param value Value to write to every bte of 'destination'. /// @param value Value to write to every bte of 'destination'.
void rr_memset(void *destination, usz_t count, u8_t value); void rr_memset(void *destination, usz_t count, u8_t value);
@ -32,9 +32,15 @@ void rr_memset(void *destination, usz_t count, u8_t value);
/// @param destination Address in memory in which the sequence will be repeated. /// @param destination Address in memory in which the sequence will be repeated.
/// @param num_bytes How many bytes of the destination should be filled. The last sequence can be partially finished. /// @param num_bytes How many bytes of the destination should be filled. The last sequence can be partially finished.
/// @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, 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);
#endif // RR_MEMORY_H #endif // RR_MEMORY_H

View File

@ -49,3 +49,12 @@ usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_se
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)
{
if(rr_mem_overlap(destination, num_bytes, source, num_bytes)) return;
u8_t *destination_8 = destination;
u8_t *source_8 = source;
for(usz_t index = 0; index < num_bytes; ++index)
destination_8[index] = source_8[index];
}