Răsfoiți Sursa

Replaced malloc with operator new and throwError with printf in the heap allocator.

David Piuva 11 luni în urmă
părinte
comite
b20bdeb376
1 a modificat fișierele cu 9 adăugiri și 13 ștergeri
  1. 9 13
      Source/DFPSR/base/heap.cpp

+ 9 - 13
Source/DFPSR/base/heap.cpp

@@ -24,9 +24,8 @@
 #include "heap.h"
 #include "heap.h"
 #include <mutex>
 #include <mutex>
 #include <thread>
 #include <thread>
-
-// TODO: Avoid dynamic memory allocation in error messages from failed memory allocation.
-#include "../api/stringAPI.h"
+#include <stdio.h>
+#include <new>
 
 
 // Get settings from here.
 // Get settings from here.
 #include "../settings.h"
 #include "../settings.h"
@@ -93,13 +92,13 @@ namespace dsr {
 		uint8_t *allocationPointer = nullptr; // The allocation pointer that moves from bottom to top when filling the arena.
 		uint8_t *allocationPointer = nullptr; // The allocation pointer that moves from bottom to top when filling the arena.
 		uint8_t *bottom = nullptr; // The end of the arena, where the allocation pointer is when empty.
 		uint8_t *bottom = nullptr; // The end of the arena, where the allocation pointer is when empty.
 		HeapMemory(uintptr_t size) {
 		HeapMemory(uintptr_t size) {
-			this->top = (uint8_t*)malloc(size);
+			this->top = (uint8_t*)(operator new (size));
 			this->bottom = this->top + size;
 			this->bottom = this->top + size;
 			this->allocationPointer = this->bottom;
 			this->allocationPointer = this->bottom;
 		}
 		}
 		~HeapMemory() {
 		~HeapMemory() {
 			if (this->top != nullptr) {
 			if (this->top != nullptr) {
-				free(this->top);
+				delete this->top;
 				this->top = nullptr;
 				this->top = nullptr;
 			}
 			}
 			this->allocationPointer = nullptr;
 			this->allocationPointer = nullptr;
@@ -121,7 +120,7 @@ namespace dsr {
 				while (nextHeap != nullptr) {
 				while (nextHeap != nullptr) {
 					HeapMemory *currentHeap = nextHeap;
 					HeapMemory *currentHeap = nextHeap;
 					nextHeap = currentHeap->prevHeap;
 					nextHeap = currentHeap->prevHeap;
-					delete currentHeap;
+					operator delete(currentHeap);
 				}
 				}
 			this->poolLock.unlock();
 			this->poolLock.unlock();
 		}
 		}
@@ -171,7 +170,7 @@ namespace dsr {
 		UnsafeAllocation result(nullptr, nullptr);
 		UnsafeAllocation result(nullptr, nullptr);
 		if (binIndex == -1) {
 		if (binIndex == -1) {
 			// If the requested allocation is so big that there is no power of two that can contain it without overflowing the address space, then it can not be allocated.
 			// If the requested allocation is so big that there is no power of two that can contain it without overflowing the address space, then it can not be allocated.
-			throwError(U"Exceeded the maximum size when trying to allocate ", minimumSize, U" bytes in heap_allocate!.\n");
+			printf("Heap error: Exceeded the maximum size when trying to allocate!\n");
 		} else {
 		} else {
 			uintptr_t paddedSize = ((uintptr_t)1 << binIndex) * heapAlignment;
 			uintptr_t paddedSize = ((uintptr_t)1 << binIndex) * heapAlignment;
 			defaultHeap.poolLock.lock();
 			defaultHeap.poolLock.lock();
@@ -188,7 +187,7 @@ namespace dsr {
 					// Look for a heap with enough space for a new allocation.
 					// Look for a heap with enough space for a new allocation.
 					result = tryToAllocate(defaultHeap, paddedSize, heapAlignmentAndMask, binIndex);
 					result = tryToAllocate(defaultHeap, paddedSize, heapAlignmentAndMask, binIndex);
 					if (result.data == nullptr) {
 					if (result.data == nullptr) {
-						throwError(U"Failed to allocate ", minimumSize, U" bytes of data with heap_allocate!\n");
+						printf("Heap error: Failed to allocate more memory!\n");
 					}
 					}
 				}
 				}
 			}
 			}
@@ -204,14 +203,11 @@ namespace dsr {
 			// Get the recycled allocation's header and its bin index.
 			// Get the recycled allocation's header and its bin index.
 			HeapHeader *newHeader = headerFromAllocation(allocation);
 			HeapHeader *newHeader = headerFromAllocation(allocation);
 			if (newHeader->flags & heapFlag_recycled) {
 			if (newHeader->flags & heapFlag_recycled) {
-				throwError(U"A heap allocation was freed twice!\n");
-				//#ifdef SAFE_POINTER_CHECKS
-				// TODO: Print more information when possible.
-				//#endif
+				printf("Heap error: A heap allocation was freed twice!\n");
 			} else {
 			} else {
 				int binIndex = header->binIndex;
 				int binIndex = header->binIndex;
 				if (binIndex >= MAX_BIN_COUNT) {
 				if (binIndex >= MAX_BIN_COUNT) {
-					throwError(U"Out of bound recycling bin index in corrupted head of freed allocation!\n");
+					printf("Heap error: Out of bound recycling bin index in corrupted head of freed allocation!\n");
 				} else {
 				} else {
 					// Make any previous head from the bin into the new tail.
 					// Make any previous head from the bin into the new tail.
 					HeapHeader *oldHeader = defaultHeap.recyclingBin[binIndex];
 					HeapHeader *oldHeader = defaultHeap.recyclingBin[binIndex];