-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAM_guard.cpp
145 lines (137 loc) · 3.82 KB
/
RAM_guard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//-----------------------------------------------------------------------------
#include "debug.h"
#include "RAM_guard.h"
#ifdef DEBUG
//-----------------------------------------------------------------------------
struct __free_list
{
WORD sz;
__free_list* nx;
};
//-----------------------------------------------------------------------------
extern int __data_start;
extern int __data_end;
extern int __bss_start;
extern int __bss_end;
extern int __heap_start;
extern void* __brkval;
extern __free_list* __flp;
//-----------------------------------------------------------------------------
__inline void align_dec(WORD value)
{
for(WORD reg = 10000; reg > 1; reg /= 10)
{
if(value < reg)
{
DSTRN(" ");
}
}
}
//-----------------------------------------------------------------------------
#define debug_print_dec_aligned(value) align_dec(value); debug_print_dec(value);
#define RAM_GUARD_MAGIC_NUM 0xFEFEFEFE
#ifdef RAM_GUARD_ENABLED
//-----------------------------------------------------------------------------
void init()
{
// prefill whole the RAM with a magic 0xFE
for(volatile register BYTE* ptr = (BYTE*)RAMSTART; ptr < (BYTE*)RAMEND; ptr++)
{
*ptr = RAM_GUARD_MAGIC_NUM >> 24;
}
}
//-----------------------------------------------------------------------------
void memory_usage()
{
DSTR("");
DSTR("===================");
DSTR(" RAM USAGE");
DSTR("===================");
// .data section size
DSTRN(".data ");
debug_print_hex((WORD) &__data_start, false);
DSTRN(" ");
WORD data = (WORD) &__data_end - (WORD) &__data_start;
debug_print_dec_aligned(data);
// .bss section size
DSTRN(".bss ");
debug_print_hex((WORD) &__bss_start, false);
DSTRN(" ");
WORD bss = (WORD) &__bss_end - (WORD) &__bss_start;
debug_print_dec_aligned(bss);
// heap size
WORD heap = 0;
DSTRN("heap ");
debug_print_hex((WORD)&__heap_start, false);
DSTRN(" ");
if(__brkval)
{
heap = (WORD)__brkval - (WORD)&__heap_start;
}
debug_print_dec_aligned(heap);
// stack depth
DSTRN("stack ");
DWORD tracker = 0;
BYTE found = false;
volatile register BYTE* ptr = (BYTE*)RAMEND;
while(ptr-- > (BYTE*)RAMSTART)
{
tracker = (tracker << 8) | *ptr;
if(tracker == RAM_GUARD_MAGIC_NUM)
{
found = true;
break;
}
}
WORD stack = 0;
if(found)
{
stack = RAMEND - (WORD) ptr - sizeof(tracker);
}
debug_print_hex((WORD) ptr + sizeof(tracker), false);
DSTRN(" ");
debug_print_dec_aligned(stack);
DSTR("-------------------");
// total RAM used
DSTRN("total ");
WORD total = data + bss + heap + stack;
debug_print_dec_aligned(total);
// free RAM
DSTRN("free ");
WORD free = RAMEND - RAMSTART - total;
debug_print_dec_aligned(free);
}
//-----------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
void heap_info()
{
DSTR("");
DSTR("=======================");
DSTR(" HEAP INFO");
DSTR("=======================");
BYTE* ptr = (BYTE*)&__heap_start;
struct __free_list* fp = __flp;
DSTR("PTR SIZE STATUS");
while(ptr < __brkval)
{
void* data = ptr + 2;
WORD size = *(WORD*) ptr;
debug_print_hex(data, false);
DSTRN(" ");
align_dec(size);
debug_print_dec(size, false);
if((WORD)ptr == (WORD)fp)
{
DSTR(" freed");
fp = fp->nx;
}
else
{
DSTR(" alloc");
}
ptr += (2 + size);
}
}
//-----------------------------------------------------------------------------
#endif