4 // by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
15 // Useful structs (for doubly linked list in this case)
17 typedef struct sMemBlockInfo
26 // Private global variables
28 static sMemBlockInfo memoryInfo;
29 //static uint32 memoryMaxAllocated;
30 static uint32 currentAllocatedMemory;
31 static uint32 maximumAllocatedMemory;
34 void memory_addMemInfo(void * ptr, uint32 size, const char * info)
36 sMemBlockInfo * alias = &memoryInfo;
41 alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
43 if (alias->next == NULL)
49 alias->next->prev = alias;
59 memoryInfo.next = memoryInfo.prev = NULL;
60 currentAllocatedMemory = maximumAllocatedMemory = 0;
67 void * memory_malloc(uint32 size, const char * info)
69 void * ptr = (void *)malloc(size);
74 memory_addMemInfo(ptr, size, info);
75 currentAllocatedMemory += size;
77 if (currentAllocatedMemory > maximumAllocatedMemory)
78 maximumAllocatedMemory = currentAllocatedMemory;
83 // OK, this sux, causes the compiler to complain about type punned pointers.
84 // The only difference between this and the previous is that this one ABORTS
85 // if it can't allocate the memory. BAD BAD BAD
87 void memory_malloc_secure(void ** new_ptr, uint32 size, const char * info)
89 WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
91 void * ptr = malloc(size);
95 WriteLog("Failed!\n");
98 //BAD, BAD, BAD! Need to do better than this!!!
99 //And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...!
105 memory_addMemInfo(ptr, size, info);
106 currentAllocatedMemory += size;
108 if (currentAllocatedMemory > maximumAllocatedMemory)
109 maximumAllocatedMemory = currentAllocatedMemory;
116 void * memory_malloc_secure2(uint32 size, const char * info)
118 WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
120 void * ptr = malloc(size);
124 WriteLog("Failed!\n");
127 //BAD, BAD, BAD! Need to do better than this!!!
128 //And since we ARE keeping track of all memory allocations, we should unwind the stack here as well...!
134 memory_addMemInfo(ptr, size, info);
135 currentAllocatedMemory += size;
137 if (currentAllocatedMemory > maximumAllocatedMemory)
138 maximumAllocatedMemory = currentAllocatedMemory;
145 void memory_free(void * ptr)
147 // sMemBlockInfo * alias= &memoryInfo;
148 // alias = alias->next;
149 sMemBlockInfo * alias= memoryInfo.next;
151 while (alias->ptr != ptr)
154 WriteLog("Memory: Freeing %i bytes from <%s>...\n", (int)alias->size, alias->info);
157 currentAllocatedMemory -= alias->size;
158 alias->prev->next = alias->next;
160 if (alias->next != NULL)
161 alias->next->prev = alias->prev;
166 void memory_memoryUsage(FILE * fp)
170 fprintf(fp, "Memory usage:\n");
172 // sMemBlockInfo * alias = &memoryInfo;
173 // alias = alias->next;
174 sMemBlockInfo * alias= memoryInfo.next;
178 // fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
179 fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, alias->ptr);
180 total += alias->size;
184 fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
185 fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));