/** * Copyright (c) 2022 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include "assert/assert.hpp" /** * Allocate some space in memory to use for your needs. Memory allocation may * change how it functions later on to keep things nice and efficient. For now * this is just an API forward for malloc. * * @param size Size of the array you wish to buffer. * @return Pointer to the space in memory to use. */ static inline void * memoryAllocate(const size_t size) { assertTrue(size > 0); auto x = (void *)malloc(size); assertNotNull(x); return x; } /** * Allocate space in memory, where all values are set to 0 (in binary space). * * @param size Size of the array. * @return Pointer to the space in memory to use. */ static inline void * memoryFillWithZero(const size_t size) { assertTrue(size > 0); auto x =(void *)calloc(1, size); assertNotNull(x); return x; } /** * Free some previously allocated memory space. * @param pointer Pointer in memory to free. */ static inline void memoryFree(void *pointer) { assertNotNull(pointer); free(pointer); } /** * Copies data from one buffer to another. Typically used for array operations. * * @param source Source pointer. * @param destination Destination buffer. * @param size Size in bytes of data to copy. */ static inline void memoryCopy( void *source, void *destination, size_t size ) { assertNotNull(source); assertNotNull(destination); assertTrue(destination != source); assertTrue(size > 0); memcpy(destination, source, size); } /** * Compares the data within two memory banks. Shorthand for memcpy. * * @param left Left item to compare. * @param right Right item to compare. * @param size Count of bytes to compare. * @return 0 for equal, <0 for left being greater, >0 for right being greater. */ static inline int32_t memoryCompare( const void *left, const void *right, const size_t size ) { assertTrue(left != right); assertTrue(size > 0); return memcmp(left, right, size); } /** * Fill destination with a repeating set of bytes. * * @param dest Destination pointer in memory. * @param data Data byte to write. * @param length How many times to write that byte. */ static inline void memorySet( void *dest, uint8_t data, size_t length ) { assertNotNull(dest); assertTrue(length > 0); memset(dest, data, length); } /** * Reallocate a part of memory. Reallocation simply creates a new buffer that * will take all of the existing contents and then free's the original buffer. * * @param pointer Pointer to pointer in memory that you wish to re-allocate. * @param currentSize Current size of the buffer that the pointer points to. * @param newSize The new size of the buffer. * @return The new size param you provided. */ static inline size_t memoryReallocate( void **pointer, size_t currentSize, size_t newSize ) { // Create the new buffer void *newBuffer = memoryAllocate(newSize); memoryCopy(*pointer, newBuffer, currentSize); memoryFree(*pointer); *pointer = newBuffer; return newSize; }