]> Shamusworld >> Repos - virtualjaguar/blob - src/memory.cpp
Virtual Jaguar 1.0.4 update (Shamus)
[virtualjaguar] / src / memory.cpp
1 //
2 // Memory handler
3 //
4 // by 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 typedef struct sMemBlockInfo
12 {
13     void * ptr;
14     char * info;
15     UINT32 size;
16     struct sMemBlockInfo * next;
17     struct sMemBlockInfo * prev;
18 } sMemBlockInfo;
19
20 sMemBlockInfo memoryInfo;
21 UINT32 memoryMaxAllocated;
22 UINT32 currentAllocatedMemory;
23 UINT32 maximumAllocatedMemory;
24
25
26 void memory_addMemInfo(void * ptr, UINT32 size, char * info)
27 {
28     sMemBlockInfo * alias;
29
30     alias = &memoryInfo;
31     while (alias->next)
32         alias=alias->next;
33     alias->next = (sMemBlockInfo *)malloc(sizeof(sMemBlockInfo));
34     if (alias->next == NULL)
35     {
36                 exit(0);
37         return;
38     }
39     alias->next->prev = alias;
40     alias = alias->next;
41     alias->next = NULL;
42     alias->size = size;
43     alias->ptr = ptr;
44     alias->info = info;
45 }
46
47 void memory_init(void)
48 {
49     memoryInfo.next = NULL;
50     memoryInfo.prev = NULL;
51     currentAllocatedMemory = 0;
52     maximumAllocatedMemory = 0;
53 }
54
55 void * memory_malloc(UINT32 size, char * info)
56 {
57     void * ptr;
58
59     ptr = (void *)malloc(size);
60     if (ptr == NULL)
61         return NULL;
62
63     memory_addMemInfo(ptr, size, info);
64     currentAllocatedMemory += size;
65
66     if (currentAllocatedMemory > maximumAllocatedMemory)
67         maximumAllocatedMemory = currentAllocatedMemory;
68
69     return ptr;
70 }
71
72 void memory_malloc_secure(void ** new_ptr, UINT32 size, char * info)
73 {
74     void * ptr;
75
76     WriteLog("Memory: Allocating %i bytes of memory for <%s>...", size, (info == NULL) ? "unknown" : info);
77     ptr = (void *)malloc(size);
78     if (ptr == NULL)
79     {
80                 WriteLog("Failed!\n");
81                 log_done();
82                 exit(0);
83     }
84     memory_addMemInfo(ptr, size, info);
85     currentAllocatedMemory += size;
86     if (currentAllocatedMemory > maximumAllocatedMemory)
87         maximumAllocatedMemory = currentAllocatedMemory;
88     *new_ptr = ptr;
89         WriteLog("OK\n");
90 }
91
92 void memory_memoryUsage(FILE * fp)
93 {
94     sMemBlockInfo * alias;
95     UINT32 total = 0;
96
97     fprintf(fp, "Memory usage:\n");
98     alias = &memoryInfo;
99     alias = alias->next;
100     while (alias)
101     {
102         fprintf(fp, "\t%16i bytes: <%s> (@ %08X)\n", (int)alias->size, alias->info, (unsigned int)alias->ptr);
103         total += alias->size;
104         alias = alias->next;
105     }
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));
108 }
109
110 void memory_done(void)
111 {
112 }
113
114 void memory_free(void * ptr)
115 {
116     sMemBlockInfo * alias;
117
118     alias = &memoryInfo;
119     alias = alias->next;
120     while (alias->ptr != ptr)
121         alias = alias->next;
122     free(ptr);
123     currentAllocatedMemory -= alias->size;
124     alias->prev->next = alias->next;
125     if (alias->next != NULL)
126         alias->next->prev = alias->prev;
127     free(alias);
128 }