4 // by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
15 #warning This module needs some serious cleanup. !!! FIX !!!
17 // Useful structs (for doubly linked list in this case)
19 typedef struct sMemBlockInfo
28 // Private global variables
30 static sMemBlockInfo memoryInfo;
31 //static uint32 memoryMaxAllocated;
32 static uint32 currentAllocatedMemory;
33 static uint32 maximumAllocatedMemory;
36 void memory_addMemInfo(void * ptr, uint32 size, const char * info)
38 sMemBlockInfo * alias = &memoryInfo;
43 alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
45 if (alias->next == NULL)
51 alias->next->prev = alias;
61 memoryInfo.next = memoryInfo.prev = NULL;
62 currentAllocatedMemory = maximumAllocatedMemory = 0;
69 void * memory_malloc(uint32 size, const char * info)
71 void * ptr = (void *)malloc(size);
76 memory_addMemInfo(ptr, size, info);
77 currentAllocatedMemory += size;
79 if (currentAllocatedMemory > maximumAllocatedMemory)
80 maximumAllocatedMemory = currentAllocatedMemory;
85 // OK, this sux, causes the compiler to complain about type punned pointers.
86 // The only difference between this and the previous is that this one ABORTS
87 // if it can't allocate the memory. BAD BAD BAD
89 void memory_malloc_secure(void ** new_ptr, uint32 size, const char * info)
91 WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
93 void * ptr = malloc(size);
97 WriteLog("Failed!\n");
100 #warning BAD, BAD, BAD! Need to do better than this!!!
101 #warning And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...!
107 memory_addMemInfo(ptr, size, info);
108 currentAllocatedMemory += size;
110 if (currentAllocatedMemory > maximumAllocatedMemory)
111 maximumAllocatedMemory = currentAllocatedMemory;
118 void * memory_malloc_secure2(uint32 size, const char * info)
120 WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
122 void * ptr = malloc(size);
126 WriteLog("Failed!\n");
129 //BAD, BAD, BAD! Need to do better than this!!!
130 //And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...!
136 memory_addMemInfo(ptr, size, info);
137 currentAllocatedMemory += size;
139 if (currentAllocatedMemory > maximumAllocatedMemory)
140 maximumAllocatedMemory = currentAllocatedMemory;
147 void memory_free(void * ptr)
149 // sMemBlockInfo * alias= &memoryInfo;
150 // alias = alias->next;
151 sMemBlockInfo * alias= memoryInfo.next;
153 while (alias->ptr != ptr)
156 WriteLog("Memory: Freeing %i bytes from <%s>...\n", (int)alias->size, alias->info);
159 currentAllocatedMemory -= alias->size;
160 alias->prev->next = alias->next;
162 if (alias->next != NULL)
163 alias->next->prev = alias->prev;
168 void memory_memoryUsage(FILE * fp)
172 fprintf(fp, "Memory usage:\n");
174 // sMemBlockInfo * alias = &memoryInfo;
175 // alias = alias->next;
176 sMemBlockInfo * alias= memoryInfo.next;
180 // fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
181 fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, alias->ptr);
182 total += alias->size;
186 fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
187 fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));