]> Shamusworld >> Repos - virtualjaguar/commitdiff
Minor cleanups
authorShamus Hammons <jlhamm@acm.org>
Sat, 20 Mar 2004 19:21:12 +0000 (19:21 +0000)
committerShamus Hammons <jlhamm@acm.org>
Sat, 20 Mar 2004 19:21:12 +0000 (19:21 +0000)
src/memory.cpp

index f5bb2ff9ef997faa2e1f5c695eb9853414c3c09a..e934146b4bdf80ddaf57db791dd4aa99c1f1047d 100644 (file)
 //
 // Memory handler
 //
-// by cal2
+// by David Raingeard (Cal2)
 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
 // Cleanups by James L. Hammons
 //
 
+#include <malloc.h>
+#include <stdlib.h>
+#include "log.h"
 #include "memory.h"
 
+// Useful structs (for doubly linked list in this case)
+
 typedef struct sMemBlockInfo
 {
-    void * ptr;
-    char * info;
-    UINT32 size;
-    struct sMemBlockInfo * next;
-    struct sMemBlockInfo * prev;
+       void * ptr;
+       char * info;
+       UINT32 size;
+       sMemBlockInfo * next;
+       sMemBlockInfo * prev;
 } sMemBlockInfo;
 
-sMemBlockInfo memoryInfo;
-UINT32 memoryMaxAllocated;
-UINT32 currentAllocatedMemory;
-UINT32 maximumAllocatedMemory;
+// Private global variables
+
+static sMemBlockInfo memoryInfo;
+//static UINT32 memoryMaxAllocated;
+static UINT32 currentAllocatedMemory;
+static UINT32 maximumAllocatedMemory;
 
 
 void memory_addMemInfo(void * ptr, UINT32 size, char * info)
 {
-    sMemBlockInfo * alias;
-
-    alias = &memoryInfo;
-    while (alias->next)
-       alias=alias->next;
-    alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
-    if (alias->next == NULL)
-    {
+       sMemBlockInfo * 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;
+               return;
+       }
+
+       alias->next->prev = alias;
+       alias = alias->next;
+       alias->next = NULL;
+       alias->size = size;
+       alias->ptr = ptr;
+       alias->info = info;
 }
 
-void memory_init(void)
+void InitMemory(void)
+{
+       memoryInfo.next = memoryInfo.prev = NULL;
+       currentAllocatedMemory = maximumAllocatedMemory = 0;
+}
+
+void MemoryDone(void)
 {
-    memoryInfo.next = NULL;
-    memoryInfo.prev = NULL;
-    currentAllocatedMemory = 0;
-    maximumAllocatedMemory = 0;
 }
 
 void * memory_malloc(UINT32 size, char * info)
 {
-    void * ptr;
+       void * ptr = (void *)malloc(size);
 
-    ptr = (void *)malloc(size);
-    if (ptr == NULL)
-        return NULL;
+       if (ptr == NULL)
+               return NULL;
 
-    memory_addMemInfo(ptr, size, info);
-    currentAllocatedMemory += size;
+       memory_addMemInfo(ptr, size, info);
+       currentAllocatedMemory += size;
 
-    if (currentAllocatedMemory > maximumAllocatedMemory)
-        maximumAllocatedMemory = currentAllocatedMemory;
+       if (currentAllocatedMemory > maximumAllocatedMemory)
+               maximumAllocatedMemory = currentAllocatedMemory;
 
-    return ptr;
+       return ptr;
 }
 
 void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info)
 {
-    void * ptr;
+       WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
+
+       void * ptr = malloc(size);
 
-    WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL) ? "unknown" : info);
-    ptr = (void *)malloc(size);
-    if (ptr == NULL)
-    {
+       if (ptr == NULL)
+       {
                WriteLog("Failed!\n");
                log_done();
                exit(0);
-    }
-    memory_addMemInfo(ptr, size, info);
-    currentAllocatedMemory += size;
-    if (currentAllocatedMemory > maximumAllocatedMemory)
-        maximumAllocatedMemory = currentAllocatedMemory;
-    *new_ptr = ptr;
+       }
+
+       memory_addMemInfo(ptr, size, info);
+       currentAllocatedMemory += size;
+
+       if (currentAllocatedMemory > maximumAllocatedMemory)
+               maximumAllocatedMemory = currentAllocatedMemory;
+
+       *new_ptr = ptr;
        WriteLog("OK\n");
 }
 
-void memory_memoryUsage(FILE * fp)
+void memory_free(void * ptr)
 {
-    sMemBlockInfo * alias;
-    UINT32 total = 0;
-
-    fprintf(fp, "Memory usage:\n");
-    alias = &memoryInfo;
-    alias = alias->next;
-    while (alias)
-    {
-        fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
-        total += alias->size;
-        alias = alias->next;
-    }
-    fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
-    fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));
-}
+//     sMemBlockInfo * alias= &memoryInfo;
+//     alias = alias->next;
+       sMemBlockInfo * alias= memoryInfo.next;
 
-void memory_done(void)
-{
+       while (alias->ptr != ptr)
+               alias = alias->next;
+
+       WriteLog("Memory: Freeing %i bytes from <%s>...\n", (int)alias->size, alias->info);
+
+       free(ptr);
+       currentAllocatedMemory -= alias->size;
+       alias->prev->next = alias->next;
+
+       if (alias->next != NULL)
+               alias->next->prev = alias->prev;
+
+       free(alias);
 }
 
-void memory_free(void * ptr)
+void memory_memoryUsage(FILE * fp)
 {
-    sMemBlockInfo * alias;
-
-    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;
-    free(alias);
+       UINT32 total = 0;
+
+       fprintf(fp, "Memory usage:\n");
+
+//     sMemBlockInfo * alias = &memoryInfo;
+//     alias = alias->next;
+       sMemBlockInfo * alias= memoryInfo.next;
+
+       while (alias)
+       {
+               fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
+               total += alias->size;
+               alias = alias->next;
+       }
+
+       fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
+       fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));
 }