iuno
This commit is contained in:
60
src/util/memory.c
Normal file
60
src/util/memory.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "memory.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void * memoryAllocate(const size_t size) {
|
||||
void *ptr = malloc(size);
|
||||
assertNotNull(ptr, "Failed to allocate memory.");
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void memoryFree(void *ptr) {
|
||||
assertNotNull(ptr, "Cannot free NULL memory.");
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void memoryCopy(void *dest, const void *src, const size_t size) {
|
||||
assertNotNull(dest, "Cannot copy to NULL memory.");
|
||||
assertNotNull(src, "Cannot copy from NULL memory.");
|
||||
assertTrue(size > 0, "Cannot copy 0 bytes of memory.");
|
||||
assertTrue(dest != src, "Cannot copy memory to itself.");
|
||||
memcpy(dest, src, size);
|
||||
}
|
||||
|
||||
void memorySet(void *dest, const uint8_t value, const size_t size) {
|
||||
assertNotNull(dest, "Cannot set NULL memory.");
|
||||
assertTrue(size > 0, "Cannot set 0 bytes of memory.");
|
||||
memset(dest, value, size);
|
||||
}
|
||||
|
||||
void memoryZero(void *dest, const size_t size) {
|
||||
memorySet(dest, 0, size);
|
||||
}
|
||||
|
||||
void memoryCopyRangeSafe(
|
||||
void *dest,
|
||||
const void *start,
|
||||
const void *end,
|
||||
const size_t sizeMax
|
||||
) {
|
||||
assertFalse(start == end, "Start and end pointers are the same.");
|
||||
assertTrue(end > start, "End pointer is not after start pointer.");
|
||||
|
||||
size_t copy = (size_t)end - (size_t)start;
|
||||
assertTrue(copy <= sizeMax, "Size of memory to copy is too large.");
|
||||
memoryCopy(dest, start, copy);
|
||||
}
|
||||
|
||||
void memoryMove(void *dest, const void *src, const size_t size) {
|
||||
assertNotNull(dest, "Cannot move to NULL memory.");
|
||||
assertNotNull(src, "Cannot move from NULL memory.");
|
||||
assertTrue(size > 0, "Cannot move 0 bytes of memory.");
|
||||
assertTrue(dest != src, "Cannot move memory to itself.");
|
||||
memmove(dest, src, size);
|
||||
}
|
||||
Reference in New Issue
Block a user