b65587b09aff6690f63f6f0c7b9d8a42e98d5cd9
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / heapallocatorbrowser.cpp
1 //
2 // heapallocatorbrowser.cpp: Memory heap allocation
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -----------------------------------------------------------
10 // JPM 01/08/2017 Created this file
11 // JPM 09/05/2018 Support of the DRAM size limit option
12 // JPM 09/05/2018 Use definitions for error instead of hard values
13 // JPM 09/05/2018 Detect if heap allocation shares space with SP (Stack)
14 //
15
16 // STILL TO DO:
17 // Better information display
18 //
19
20 #include "settings.h"
21 #include "debugger/heapallocatorbrowser.h"
22 #include "memory.h"
23 #include "debugger/DBGManager.h"
24 #include "m68000/m68kinterface.h"
25
26
27 HeapAllocatorBrowserWindow::HeapAllocatorBrowserWindow(QWidget * parent/*= 0*/) : QWidget(parent, Qt::Dialog),
28 layout(new QVBoxLayout), text(new QTextBrowser),
29 Adr(0)
30 {
31 setWindowTitle(tr("Heap Allocation"));
32
33 QFont fixedFont("Lucida Console", 8, QFont::Normal);
34 fixedFont.setStyleHint(QFont::TypeWriter);
35 text->setFont(fixedFont);
36 setLayout(layout);
37
38 layout->addWidget(text);
39 }
40
41
42 //
43 HeapAllocatorBrowserWindow::~HeapAllocatorBrowserWindow(void)
44 {
45 }
46
47
48 //
49 void HeapAllocatorBrowserWindow::RefreshContents(void)
50 {
51 char string[1024];
52 QString HA;
53 size_t Adr68K;
54 size_t Error = HA_NOERROR;
55 HeapAllocation HeapAllocation;
56
57 if (isVisible())
58 {
59 if (Adr68K = Adr)
60 {
61 do
62 {
63 if ((Adr68K >= 0x4000) && (Adr68K < vjs.DRAM_size))
64 {
65 if (Adr68K < m68k_get_reg(NULL, M68K_REG_SP))
66 {
67 memcpy(&HeapAllocation, &jaguarMainRAM[Adr68K], sizeof(HeapAllocation));
68
69 if (HeapAllocation.size = ((HeapAllocation.size & 0xff) << 24) + ((HeapAllocation.size & 0xff00) << 8) + ((HeapAllocation.size & 0xff0000) >> 8) + ((HeapAllocation.size & 0xff000000) >> 24))
70 {
71 if (HeapAllocation.size <= (vjs.DRAM_size - 0x4000))
72 {
73 if ((HeapAllocation.used = ((HeapAllocation.used & 0xff) << 8) + ((HeapAllocation.used & 0xff00) >> 8)) <= 1)
74 {
75 HeapAllocation.nextalloc = ((HeapAllocation.nextalloc & 0xff) << 24) + ((HeapAllocation.nextalloc & 0xff00) << 8) + ((HeapAllocation.nextalloc & 0xff0000) >> 8) + ((HeapAllocation.nextalloc & 0xff000000) >> 24);
76
77 if ((HeapAllocation.nextalloc >= 0x4000) && (HeapAllocation.nextalloc < vjs.DRAM_size))
78 {
79 sprintf(string, "0x%06x | 0x%06x (%zi) | %s | 0x%06x<br>", Adr68K, HeapAllocation.size - sizeof(HeapAllocation), HeapAllocation.size - sizeof(HeapAllocation), HeapAllocation.used ? "Allocated" : "Free", HeapAllocation.nextalloc);
80 Adr68K = HeapAllocation.nextalloc;
81 }
82 else
83 {
84 sprintf(string, "<br><font color='#ff0000'><b>Unable to determine the next memory allocation</b></font>");
85 Error = HA_UNABLENEXTMEMORYALLOC;
86 }
87 }
88 else
89 {
90 sprintf(string, "<br><font color='#ff0000'><b>Unable to determine if the allocated memory is used or not</b></font>");
91 Error = HA_UNABLEALLOCATEMEMORYUSAGE;
92 }
93 }
94 else
95 {
96 sprintf(string, "<br><font color='#ff0000'><b>Memory bloc size has a problem</b></font>");
97 Error = HA_MEMORYBLOCKSIZEPROBLEM;
98 }
99 }
100 else
101 {
102 sprintf(string, "<br><font color='#0000ff'><b>Memory allocations browsing successfully completed</b></font>");
103 }
104 }
105 else
106 {
107 sprintf(string, "<br><font color='#ff0000'><b>Memory allocations and Stack are sharing the same space</b></font>");
108 Error = HA_HAANDSPSHARESPACE;
109 }
110 }
111 else
112 {
113 sprintf(string, "<br><font color='#ff0000'><b>Memory allocations may have a problem</b></font>");
114 Error = HA_MEMORYALLOCATIONPROBLEM;
115 }
116
117 HA += QString(string);
118
119 }
120 while (HeapAllocation.size && !Error);
121 }
122 else
123 {
124 if (Adr = DBGManager_GetAdrFromSymbolName((char *)"__HeapBase"))
125 {
126 if (Adr68K = DBGManager_GetGlobalVariableAdrFromName((char *)"alloc"))
127 {
128 if (!(Adr68K = (jaguarMainRAM[Adr68K] << 24) + (jaguarMainRAM[Adr68K + 1] << 16) + (jaguarMainRAM[Adr68K + 2] << 8) + (jaguarMainRAM[Adr68K + 3])) || ((Adr68K < 0x4000) || (Adr68K >= 0x200000)))
129 {
130 sprintf(string, "<font color='#ff0000'><b>Memory allocator not yet initialised</b></font>");
131 Adr = 0;
132 }
133 else
134 {
135 return RefreshContents();
136 }
137 }
138 else
139 {
140 sprintf(string, "<font color='#ff0000'><b>Memory allocator is not compatible</b></font>");
141 Adr = 0;
142 }
143 }
144 else
145 {
146 sprintf(string, "<font color='#ff0000'><b>Memory allocator doesn't exist</b></font>");
147 }
148
149 HA += QString(string);
150 }
151
152 text->clear();
153 text->setText(HA);
154 }
155 }
156
157
158 //
159 void HeapAllocatorBrowserWindow::Reset(void)
160 {
161 size_t Adr68K;
162
163 if (DBGManager_GetAdrFromSymbolName((char *)"__HeapBase"))
164 {
165 if (Adr68K = DBGManager_GetGlobalVariableAdrFromName((char *)"alloc"))
166 {
167 jaguarMainRAM[Adr68K] = jaguarMainRAM[Adr68K + 1] = jaguarMainRAM[Adr68K + 2] = jaguarMainRAM[Adr68K + 3] = 0;
168 Adr = 0;
169 }
170 }
171 }
172
173
174 //
175 void HeapAllocatorBrowserWindow::keyPressEvent(QKeyEvent * e)
176 {
177 if (e->key() == Qt::Key_Escape)
178 {
179 hide();
180 }
181 }
182