X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmemory.cpp;h=9cff768f35c6436c403fb92a9343ee39062c5b12;hb=d239de704f276a75d927900e3d413a44cc87116c;hp=e934146b4bdf80ddaf57db791dd4aa99c1f1047d;hpb=f15fce96037da18521fdce80197cde37f56ea40c;p=virtualjaguar diff --git a/src/memory.cpp b/src/memory.cpp index e934146..9cff768 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -6,18 +6,21 @@ // Cleanups by James L. Hammons // +#include "memory.h" + #include #include #include "log.h" -#include "memory.h" + +#warning This module needs some serious cleanup. !!! FIX !!! // Useful structs (for doubly linked list in this case) typedef struct sMemBlockInfo { void * ptr; - char * info; - UINT32 size; + const char * info; + uint32 size; sMemBlockInfo * next; sMemBlockInfo * prev; } sMemBlockInfo; @@ -25,12 +28,12 @@ typedef struct sMemBlockInfo // Private global variables static sMemBlockInfo memoryInfo; -//static UINT32 memoryMaxAllocated; -static UINT32 currentAllocatedMemory; -static UINT32 maximumAllocatedMemory; +//static uint32 memoryMaxAllocated; +static uint32 currentAllocatedMemory; +static uint32 maximumAllocatedMemory; -void memory_addMemInfo(void * ptr, UINT32 size, char * info) +void memory_addMemInfo(void * ptr, uint32 size, const char * info) { sMemBlockInfo * alias = &memoryInfo; @@ -53,7 +56,7 @@ void memory_addMemInfo(void * ptr, UINT32 size, char * info) alias->info = info; } -void InitMemory(void) +void MemoryInit(void) { memoryInfo.next = memoryInfo.prev = NULL; currentAllocatedMemory = maximumAllocatedMemory = 0; @@ -63,7 +66,7 @@ void MemoryDone(void) { } -void * memory_malloc(UINT32 size, char * info) +void * memory_malloc(uint32 size, const char * info) { void * ptr = (void *)malloc(size); @@ -79,7 +82,11 @@ void * memory_malloc(UINT32 size, char * info) return ptr; } -void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info) +// OK, this sux, causes the compiler to complain about type punned pointers. +// The only difference between this and the previous is that this one ABORTS +// if it can't allocate the memory. BAD BAD BAD + +void memory_malloc_secure(void ** new_ptr, uint32 size, const char * info) { WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info)); @@ -88,7 +95,12 @@ void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info) if (ptr == NULL) { WriteLog("Failed!\n"); - log_done(); + LogDone(); + +#warning BAD, BAD, BAD! Need to do better than this!!! +#warning And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...! +#warning !!! FIX !!! + exit(0); } @@ -102,6 +114,36 @@ void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info) WriteLog("OK\n"); } +/* +void * memory_malloc_secure2(uint32 size, const char * info) +{ + WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info)); + + void * ptr = malloc(size); + + if (ptr == NULL) + { + WriteLog("Failed!\n"); + log_done(); + +//BAD, BAD, BAD! Need to do better than this!!! +//And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...! +// !!! FIX !!! + + exit(0); + } + + memory_addMemInfo(ptr, size, info); + currentAllocatedMemory += size; + + if (currentAllocatedMemory > maximumAllocatedMemory) + maximumAllocatedMemory = currentAllocatedMemory; + + new_ptr = ptr; + WriteLog("OK\n"); +} +*/ + void memory_free(void * ptr) { // sMemBlockInfo * alias= &memoryInfo; @@ -125,7 +167,7 @@ void memory_free(void * ptr) void memory_memoryUsage(FILE * fp) { - UINT32 total = 0; + uint32 total = 0; fprintf(fp, "Memory usage:\n"); @@ -135,7 +177,8 @@ void memory_memoryUsage(FILE * fp) while (alias) { - fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr); +// fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr); + fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, alias->ptr); total += alias->size; alias = alias->next; }