Testing textures and events
This commit is contained in:
97
src/dawn/util/memory.hpp
Normal file
97
src/dawn/util/memory.hpp
Normal file
@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.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) {
|
||||
return (void *)malloc(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free some previously allocated memory space.
|
||||
* @param pointer Pointer in memory to free.
|
||||
*/
|
||||
static inline void memoryFree(void *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
|
||||
) {
|
||||
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
|
||||
) {
|
||||
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
|
||||
) {
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user