5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
11 typedef struct sMemBlockInfo
16 struct sMemBlockInfo * next;
17 struct sMemBlockInfo * prev;
20 sMemBlockInfo memoryInfo;
21 UINT32 memoryMaxAllocated;
22 UINT32 currentAllocatedMemory;
23 UINT32 maximumAllocatedMemory;
26 void memory_addMemInfo(void * ptr, UINT32 size, char * info)
28 sMemBlockInfo * alias;
33 alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
34 if (alias->next == NULL)
39 alias->next->prev = alias;
47 void memory_init(void)
49 memoryInfo.next = NULL;
50 memoryInfo.prev = NULL;
51 currentAllocatedMemory = 0;
52 maximumAllocatedMemory = 0;
55 void * memory_malloc(UINT32 size, char * info)
59 ptr = (void *)malloc(size);
63 memory_addMemInfo(ptr, size, info);
64 currentAllocatedMemory += size;
66 if (currentAllocatedMemory > maximumAllocatedMemory)
67 maximumAllocatedMemory = currentAllocatedMemory;
72 void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info)
76 WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL) ? "unknown" : info);
77 ptr = (void *)malloc(size);
80 WriteLog("Failed!\n");
84 memory_addMemInfo(ptr, size, info);
85 currentAllocatedMemory += size;
86 if (currentAllocatedMemory > maximumAllocatedMemory)
87 maximumAllocatedMemory = currentAllocatedMemory;
92 void memory_memoryUsage(FILE * fp)
94 sMemBlockInfo * alias;
97 fprintf(fp, "Memory usage:\n");
102 fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
103 total += alias->size;
106 fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
107 fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));
110 void memory_done(void)
114 void memory_free(void * ptr)
116 sMemBlockInfo * alias;
120 while (alias->ptr != ptr)
123 currentAllocatedMemory -= alias->size;
124 alias->prev->next = alias->next;
125 if (alias->next != NULL)
126 alias->next->prev = alias->prev;