X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmemory.cpp;h=39376ca0fe81c81b78064e1be5dd6e79a8260622;hb=4fc7c64eac42b571ee64ec693e6da1d5a458344f;hp=8c4007f318716c26390d4625f3afdbcd53a34c6b;hpb=aaa37222bec76a065e74830b363e8c9dfa4709ae;p=virtualjaguar diff --git a/src/memory.cpp b/src/memory.cpp index 8c4007f..39376ca 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -1,184 +1,128 @@ -////////////////////////////////////////////////////////////////////////////// // -////////////////////////////////////////////////////////////////////////////// +// Memory handler // +// by cal2 +// GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS) +// Cleanups by James L. Hammons // -// -// -// -// -////////////////////////////////////////////////////////////////////////////// -#include "include/memory.h" +#include "memory.h" typedef struct sMemBlockInfo { - void *ptr; - char *info; - UINT32 size; - struct sMemBlockInfo *next; - struct sMemBlockInfo *prev; + void * ptr; + char * info; + UINT32 size; + struct sMemBlockInfo * next; + struct sMemBlockInfo * prev; } sMemBlockInfo; -sMemBlockInfo memoryInfo; -UINT32 memoryMaxAllocated; -UINT32 currentAllocatedMemory; -UINT32 maximumAllocatedMemory; +sMemBlockInfo memoryInfo; +UINT32 memoryMaxAllocated; +UINT32 currentAllocatedMemory; +UINT32 maximumAllocatedMemory; -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// -void memory_addMemInfo(void *ptr, UINT32 size, char *info) + +void memory_addMemInfo(void * ptr, UINT32 size, char * info) { - sMemBlockInfo *alias; + sMemBlockInfo * alias; - alias=&memoryInfo; - while (alias->next) alias=alias->next; - alias->next=(sMemBlockInfo*)malloc(sizeof(sMemBlockInfo)); - if (alias->next==NULL) + alias = &memoryInfo; + while (alias->next) + alias=alias->next; + alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo)); + if (alias->next == NULL) { exit(0); return; } - alias->next->prev=alias; - alias=alias->next; - alias->next=NULL; - alias->size=size; - alias->ptr=ptr; - alias->info=info; + alias->next->prev = alias; + alias = alias->next; + alias->next = NULL; + alias->size = size; + alias->ptr = ptr; + alias->info = info; } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// + void memory_init(void) { - memoryInfo.next=NULL; - memoryInfo.prev=NULL; - currentAllocatedMemory=0; - maximumAllocatedMemory=0; + memoryInfo.next = NULL; + memoryInfo.prev = NULL; + currentAllocatedMemory = 0; + maximumAllocatedMemory = 0; } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// -void *memory_malloc(UINT32 size, char *info) + +void * memory_malloc(UINT32 size, char * info) { - void *ptr; + void * ptr; - ptr=(void*)malloc(size); - if (ptr==NULL) - { - return(NULL); - } - memory_addMemInfo(ptr,size,info); - currentAllocatedMemory+=size; - if (currentAllocatedMemory>maximumAllocatedMemory) - maximumAllocatedMemory=currentAllocatedMemory; - return(ptr); + ptr = (void *)malloc(size); + if (ptr == NULL) + return NULL; + + memory_addMemInfo(ptr, size, info); + currentAllocatedMemory += size; + + if (currentAllocatedMemory > maximumAllocatedMemory) + maximumAllocatedMemory = currentAllocatedMemory; + + return ptr; } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -// -// -// -////////////////////////////////////////////////////////////////////////////// -void memory_malloc_secure(void **new_ptr, UINT32 size, char *info) + +void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info) { - void *ptr; + void * ptr; - fprintf(log_get(),"memory: allocating %i bytes of memory for <%s>...",size,(info==NULL)?"unknown":info); - ptr=(void*)malloc(size); - if (ptr==NULL) + fprintf(log_get(), "memory: allocating %i bytes of memory for <%s>...", size, (info == NULL) ? "unknown" : info); + ptr = (void *)malloc(size); + if (ptr == NULL) { - fprintf(log_get(),"failed\n"); + fprintf(log_get(), "failed\n"); log_done(); exit(0); } - memory_addMemInfo(ptr,size,info); - currentAllocatedMemory+=size; - if (currentAllocatedMemory>maximumAllocatedMemory) - maximumAllocatedMemory=currentAllocatedMemory; - *new_ptr=ptr; - fprintf(log_get(),"ok\n"); + memory_addMemInfo(ptr, size, info); + currentAllocatedMemory += size; + if (currentAllocatedMemory > maximumAllocatedMemory) + maximumAllocatedMemory = currentAllocatedMemory; + *new_ptr = ptr; + fprintf(log_get(), "ok\n"); } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -////////////////////////////////////////////////////////////////////////////// -void memory_memoryUsage(FILE *fp) + +void memory_memoryUsage(FILE * fp) { - sMemBlockInfo *alias; - UINT32 total=0; + sMemBlockInfo * alias; + UINT32 total = 0; - fprintf(fp,"Memory usage:\n"); - alias=&memoryInfo; - alias=alias->next; + fprintf(fp, "Memory usage:\n"); + alias = &memoryInfo; + alias = alias->next; while (alias) { - fprintf(fp,"\t%16i bytes : <%s> (@ 0x%.8x)\n",alias->size,alias->info,alias->ptr); - total+=alias->size; - alias=alias->next; + fprintf(fp, "\t%16i bytes : <%s> (@ 0x%.8x)\n", alias->size, alias->info, alias->ptr); + total += alias->size; + alias = alias->next; } - fprintf(fp,"\n\t%16i bytes total(%i Mb)\n",total,(total>>20)); - fprintf(fp,"\n\t%16i bytes memory peak(%i Mb)\n",maximumAllocatedMemory,maximumAllocatedMemory>>20); + fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", total, total >> 20); + fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", maximumAllocatedMemory, maximumAllocatedMemory >> 20); } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -////////////////////////////////////////////////////////////////////////////// void memory_done(void) { } -////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////////////////////////////////////////////////// -// -// -// -////////////////////////////////////////////////////////////////////////////// -void memory_free(void *ptr) + +void memory_free(void * ptr) { - sMemBlockInfo *alias; + sMemBlockInfo * alias; - alias=&memoryInfo; - alias=alias->next; - while (alias->ptr!=ptr) - alias=alias->next; + alias = &memoryInfo; + alias = alias->next; + while (alias->ptr != ptr) + alias = alias->next; free(ptr); - currentAllocatedMemory-=alias->size; - alias->prev->next=alias->next; - if (alias->next!=NULL) - alias->next->prev=alias->prev; + currentAllocatedMemory -= alias->size; + alias->prev->next = alias->next; + if (alias->next != NULL) + alias->next->prev = alias->prev; free(alias); }