This commit is contained in:
2023-10-18 23:32:04 -05:00
parent 9ad9995663
commit b9493840f4
11 changed files with 134 additions and 96 deletions

View File

@ -281,7 +281,8 @@ Xml * Xml::getFirstChildOfType(std::string type) {
Xml::~Xml() {
auto it = this->children.begin();
while(it != this->children.end()) {
delete *it;
Xml *child = *it;
delete child;
++it;
}
}

View File

@ -5,6 +5,7 @@
#pragma once
#include "dawnsharedlibs.hpp"
#include "util/memory.hpp"
#include "assert/assert.hpp"
#include "util/array.hpp"

View File

@ -6,60 +6,40 @@
#include "memory.hpp"
#include "assert/assert.hpp"
uint64_t dawnAllocatedItemCount = -1;
void memoryInit() {
dawnAllocatedItemCount = 0;
void * operator new(size_t size) noexcept {
return memoryAllocate(size);
}
void memoryDispose() {
assertTrue(dawnAllocatedItemCount <= 0, "memoryDispose: !!!Leaked memory detected (not enough free)!!!");
assertTrue(dawnAllocatedItemCount >= 0, "memoryDispose: !!!Leaked memory detected (too much free)!!!");
dawnAllocatedItemCount = -1;
void operator delete (void *p) noexcept {
return memoryFree(p);
}
void * memoryAllocate(const size_t size) {
assertTrue(dawnAllocatedItemCount >= 0, "memoryAllocate: Either a memory leak or memoryInit was not called.");
assertTrue(size >= 0, "memoryAllocate: size must be greater than 0 or equal to.");
if(size == 0) {
// Technically this is an implementation specific case, I think it should
// basically be undefined behaviour tbh.
return memoryCallMalloc(size);
}
dawnAllocatedItemCount++;
auto x = (void *)memoryCallMalloc(size);
void *x = (void *)memoryCallMalloc(size);
assertNotNull(x, "memoryAllocate: Failed to allocate memory");
return x;
}
void * memoryAllocateEmpty(const size_t count, const size_t size) {
assertTrue(dawnAllocatedItemCount >= 0, "memoryAllocateEmpty: Either a memory leak or memoryInit was not called.");
assertTrue(count >= 0, "memoryAllocateEmpty: count must be greater than or equal to 0");
assertTrue(size >= 0, "memoryAllocateEmpty: size must be greater than or equal to 0");
if(count == 0 || size == 0) {
// Technically this is an implementation specific case, I think it should
// basically be undefined behaviour tbh.
return memoryCallCalloc(count, size);
}
dawnAllocatedItemCount++;
auto x = (void*)memoryCallCalloc(count, size);
void *x = (void *)memoryCallCalloc(count, size);
assertNotNull(x, "memoryAllocateEmpty: Failed to allocate memory");
return x;
}
void memoryFree(void *pointer) {
assertTrue(dawnAllocatedItemCount > 0, "memoryFree: Either a memory leak or memoryInit was not called.");
if(pointer == nullptr || pointer == NULL) {
// Technically this is an implementation specific case, I think it should
// basically be undefined behaviour tbh.
return memoryCallFree(pointer);
}
dawnAllocatedItemCount--;
memoryCallFree(pointer);
}
void * memoryReallocate(void *ptr, size_t newSize) {
assertTrue(newSize >= 0, "memoryReallocate: newSize must be greater than or equal to 0");
return memoryCallRealloc(ptr, newSize);
}
void memoryCopy(void *source, void *destination, size_t size) {
assertTrue(dawnAllocatedItemCount >= 0, "memoryAllocate: Either a memory leak or memoryInit was not called.");
assertNotNull(source, "memoryCopy: source must not be null");
assertNotNull(destination, "memoryCopy: destination must not be null");
assertTrue(destination != source, "memoryCopy: destination must not be source");
@ -68,32 +48,21 @@ void memoryCopy(void *source, void *destination, size_t size) {
}
int32_t memoryCompare(const void *left, const void *right, const size_t size) {
assertTrue(dawnAllocatedItemCount >= 0, "memoryAllocate: Either a memory leak or memoryInit was not called.");
assertTrue(left != right, "memoryCompare: left must not be right");
assertTrue(size > 0, "memoryCompare: size must be greater than 0");
return memcmp(left, right, size);
}
void memorySet(void *dest, uint8_t data, size_t length) {
assertTrue(dawnAllocatedItemCount >= 0, "memoryAllocate: Either a memory leak or memoryInit was not called.");
assertNotNull(dest, "memorySet: dest must not be null");
assertTrue(length > 0, "memorySet: length must be greater than 0");
memset(dest, data, length);
}
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;
void memoryInit() {
}
void * operator new(size_t size) {
return memoryAllocate(size);
}
void operator delete(void *pointer) {
memoryFree(pointer);
void memoryDispose() {
}

View File

@ -8,7 +8,7 @@
#pragma once
#include "dawnsharedlibs.hpp"
extern uint64_t dawnAllocatedItemCount;
#define DAWN_MEMORY_TRACKING 1
static void * memoryCallMalloc(const size_t size) {
return malloc(size);
@ -22,15 +22,9 @@ static void memoryCallFree(void *p) {
free(p);
}
/**
* Initializes the memory management system.
*/
void memoryInit();
/**
* Disposes of the memory management system.
*/
void memoryDispose();
static void * memoryCallRealloc(void *p, size_t newSize) {
return realloc(p, newSize);
}
/**
* Allocate some space in memory to use for your needs. Memory allocation may
@ -57,6 +51,16 @@ void * memoryAllocateEmpty(const size_t count, const size_t size);
*/
void memoryFree(void *pointer);
/**
* 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 newSize The new size of the buffer.
* @return Pointer to the new buffer.
*/
void * memoryReallocate(void *pointer, size_t newSize);
/**
* Copies data from one buffer to another. Typically used for array operations.
*
@ -86,32 +90,11 @@ int32_t memoryCompare(const void *left, const void *right, const size_t size);
void memorySet(void *dest, uint8_t data, size_t 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.
* Initializes the memory management system.
*/
size_t memoryReallocate(void **pointer, size_t currentSize, size_t newSize);
void memoryInit();
/**
* Overloads the new operator to use our memory allocation.
*
* @param size Size of the memory to allocate.
* @return Pointer to the memory.
* Disposes of the memory management system.
*/
void * operator new(size_t size);
/**
* Overloads the delete operator to use our memory allocation.
*
* @param p Pointer to the memory to free.
*/
void operator delete(void * p);
// Override default memory functions
#define malloc(n) memoryAllocate(n)
#define free(n) memoryFree(n)
#define calloc(n, s) memoryAllocateEmpty(n, s)
void memoryDispose();