]> Shamusworld >> Repos - virtualjaguar/blob - src/memory.cpp
Minor cleanups
[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 <malloc.h>
10 #include <stdlib.h>
11 #include "log.h"
12 #include "memory.h"
13
14 // Useful structs (for doubly linked list in this case)
15
16 typedef struct sMemBlockInfo
17 {
18         void * ptr;
19         char * info;
20         UINT32 size;
21         sMemBlockInfo * next;
22         sMemBlockInfo * prev;
23 } sMemBlockInfo;
24
25 // Private global variables
26
27 static sMemBlockInfo memoryInfo;
28 //static UINT32 memoryMaxAllocated;
29 static UINT32 currentAllocatedMemory;
30 static UINT32 maximumAllocatedMemory;
31
32
33 void memory_addMemInfo(void * ptr, UINT32 size, char * info)
34 {
35         sMemBlockInfo * alias = &memoryInfo;
36
37         while (alias->next)
38                 alias = alias->next;
39
40         alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
41
42         if (alias->next == NULL)
43         {
44                 exit(0);
45                 return;
46         }
47
48         alias->next->prev = alias;
49         alias = alias->next;
50         alias->next = NULL;
51         alias->size = size;
52         alias->ptr = ptr;
53         alias->info = info;
54 }
55
56 void InitMemory(void)
57 {
58         memoryInfo.next = memoryInfo.prev = NULL;
59         currentAllocatedMemory = maximumAllocatedMemory = 0;
60 }
61
62 void MemoryDone(void)
63 {
64 }
65
66 void * memory_malloc(UINT32 size, char * info)
67 {
68         void * ptr = (void *)malloc(size);
69
70         if (ptr == NULL)
71                 return NULL;
72
73         memory_addMemInfo(ptr, size, info);
74         currentAllocatedMemory += size;
75
76         if (currentAllocatedMemory > maximumAllocatedMemory)
77                 maximumAllocatedMemory = currentAllocatedMemory;
78
79         return ptr;
80 }
81
82 void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info)
83 {
84         WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL ? "unknown" : info));
85
86         void * ptr = malloc(size);
87
88         if (ptr == NULL)
89         {
90                 WriteLog("Failed!\n");
91                 log_done();
92                 exit(0);
93         }
94
95         memory_addMemInfo(ptr, size, info);
96         currentAllocatedMemory += size;
97
98         if (currentAllocatedMemory > maximumAllocatedMemory)
99                 maximumAllocatedMemory = currentAllocatedMemory;
100
101         *new_ptr = ptr;
102         WriteLog("OK\n");
103 }
104
105 void memory_free(void * ptr)
106 {
107 //      sMemBlockInfo * alias= &memoryInfo;
108 //      alias = alias->next;
109         sMemBlockInfo * alias= memoryInfo.next;
110
111         while (alias->ptr != ptr)
112                 alias = alias->next;
113
114         WriteLog("Memory: Freeing %i bytes from <%s>...\n", (int)alias->size, alias->info);
115
116         free(ptr);
117         currentAllocatedMemory -= alias->size;
118         alias->prev->next = alias->next;
119
120         if (alias->next != NULL)
121                 alias->next->prev = alias->prev;
122
123         free(alias);
124 }
125
126 void memory_memoryUsage(FILE * fp)
127 {
128         UINT32 total = 0;
129
130         fprintf(fp, "Memory usage:\n");
131
132 //      sMemBlockInfo * alias = &memoryInfo;
133 //      alias = alias->next;
134         sMemBlockInfo * alias= memoryInfo.next;
135
136         while (alias)
137         {
138                 fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
139                 total += alias->size;
140                 alias = alias->next;
141         }
142
143         fprintf(fp, "\n\t%16i bytes total(%i Mb)\n", (int)total, (int)(total >> 20));
144         fprintf(fp, "\n\t%16i bytes memory peak(%i Mb)\n", (int)maximumAllocatedMemory, (int)(maximumAllocatedMemory >> 20));
145 }