]> Shamusworld >> Repos - virtualjaguar/blob - src/memory.cpp
Extensive changes to remove gcc 4.x warnings, general code cleanup
[virtualjaguar] / src / memory.cpp
1 //
2 // Memory handler
3 //
4 // by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
7 //
8
9 #include "memory.h"
10
11 #include <malloc.h>
12 #include <stdlib.h>
13 #include "log.h"
14
15 // Useful structs (for doubly linked list in this case)
16
17 typedef struct sMemBlockInfo
18 {
19         void * ptr;
20         const char * info;
21         uint32 size;
22         sMemBlockInfo * next;
23         sMemBlockInfo * prev;
24 } sMemBlockInfo;
25
26 // Private global variables
27
28 static sMemBlockInfo memoryInfo;
29 //static uint32 memoryMaxAllocated;
30 static uint32 currentAllocatedMemory;
31 static uint32 maximumAllocatedMemory;
32
33
34 void memory_addMemInfo(void * ptr, uint32 size, const char * info)
35 {
36         sMemBlockInfo * alias = &memoryInfo;
37
38         while (alias->next)
39                 alias = alias->next;
40
41         alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
42
43         if (alias->next == NULL)
44         {
45                 exit(0);
46                 return;
47         }
48
49         alias->next->prev = alias;
50         alias = alias->next;
51         alias->next = NULL;
52         alias->size = size;
53         alias->ptr = ptr;
54         alias->info = info;
55 }
56
57 void InitMemory(void)
58 {
59         memoryInfo.next = memoryInfo.prev = NULL;
60         currentAllocatedMemory = maximumAllocatedMemory = 0;
61 }
62
63 void MemoryDone(void)
64 {
65 }
66
67 void * memory_malloc(uint32 size, const char * info)
68 {
69         void * ptr = (void *)malloc(size);
70
71         if (ptr == NULL)
72                 return NULL;
73
74         memory_addMemInfo(ptr, size, info);
75         currentAllocatedMemory += size;
76
77         if (currentAllocatedMemory > maximumAllocatedMemory)
78                 maximumAllocatedMemory = currentAllocatedMemory;
79
80         return ptr;
81 }
82
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
86
87 void memory_malloc_secure(void ** new_ptr, uint32 size, const char * info)
88 {
89         WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
90
91         void * ptr = malloc(size);
92
93         if (ptr == NULL)
94         {
95                 WriteLog("Failed!\n");
96                 log_done();
97
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...!
100 // !!! FIX !!!
101
102                 exit(0);
103         }
104
105         memory_addMemInfo(ptr, size, info);
106         currentAllocatedMemory += size;
107
108         if (currentAllocatedMemory > maximumAllocatedMemory)
109                 maximumAllocatedMemory = currentAllocatedMemory;
110
111         *new_ptr = ptr;
112         WriteLog("OK\n");
113 }
114
115 /*
116 void * memory_malloc_secure2(uint32 size, const char * info)
117 {
118         WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
119
120         void * ptr = malloc(size);
121
122         if (ptr == NULL)
123         {
124                 WriteLog("Failed!\n");
125                 log_done();
126
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...!
129 // !!! FIX !!!
130
131                 exit(0);
132         }
133
134         memory_addMemInfo(ptr, size, info);
135         currentAllocatedMemory += size;
136
137         if (currentAllocatedMemory > maximumAllocatedMemory)
138                 maximumAllocatedMemory = currentAllocatedMemory;
139
140         new_ptr = ptr;
141         WriteLog("OK\n");
142 }
143 */
144
145 void memory_free(void * ptr)
146 {
147 //      sMemBlockInfo * alias= &memoryInfo;
148 //      alias = alias->next;
149         sMemBlockInfo * alias= memoryInfo.next;
150
151         while (alias->ptr != ptr)
152                 alias = alias->next;
153
154         WriteLog("Memory: Freeing %i bytes from <%s>...\n", (int)alias->size, alias->info);
155
156         free(ptr);
157         currentAllocatedMemory -= alias->size;
158         alias->prev->next = alias->next;
159
160         if (alias->next != NULL)
161                 alias->next->prev = alias->prev;
162
163         free(alias);
164 }
165
166 void memory_memoryUsage(FILE * fp)
167 {
168         uint32 total = 0;
169
170         fprintf(fp, "Memory usage:\n");
171
172 //      sMemBlockInfo * alias = &memoryInfo;
173 //      alias = alias->next;
174         sMemBlockInfo * alias= memoryInfo.next;
175
176         while (alias)
177         {
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;
181                 alias = alias->next;
182         }
183
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));
186 }