-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
169 lines (146 loc) · 4.84 KB
/
test.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <unistd.h>
#define TEST
#include "malloc.h"
#define SHORTER
char
get_alpha(void **ptrs, header_t *hptr)
{
for (int i = 0; i < 'z' - 'a' + 1; i++)
if (ptrs[i] == hptr + 1)
return 'a' + i;
return ' ';
}
static void
print_mem_map(void **ptrs)
{
page_t *pptr;
node_t *nptr;
header_t *hptr;
for (pptr = __debug_get_head(); pptr != NULL; pptr = pptr->next)
{
puts("+---------------------+");
printf("\e[32m"); // green
printf("|PAGE %1d (size: %5lu) | %p [page_t]\n", pptr->page_id,
pptr->size, pptr);
printf("|prev: %14p | PAGE\n", pptr->prev);
printf("|next: %14p |\n", pptr->next);
printf("\e[0m"); // reset
puts("+---------------------+");
nptr = (node_t *) ((char *) pptr + sizeof(page_t));
do
{
printf("\e[34m"); // blue
printf("|size: %14lu | %p [node_t]\n", nptr->size, nptr);
printf("|prev: %14p | FREE\n", nptr->prev);
printf("|next: %14p | \n", ((node_t *) nptr)->next);
printf("|page: %14p |\n", nptr->page);
#ifndef SHORTER
puts("| - - - - - - - - - - |");
if (nptr->size > 0)
{
puts("|free |");
puts("+---------------------+");
}
#else
printf("\e[0m"); // reset
puts("+---------------------+");
#endif
hptr = (void *) nptr + nptr->size + sizeof(node_t);
while ((void *) hptr != (void *) nptr->next &&
(void *) hptr < (void *) pptr + pptr->size)
{
if (hptr->magic == MAGIC)
{
printf("\e[33m"); // orange
printf("|size: %14lu | %p [header_t]\n", hptr->size, hptr);
printf("|magic: %13x | USED (%c)\n", hptr->magic,
get_alpha(ptrs, hptr));
printf("|page: %14p |\n", hptr->page);
#ifndef SHORTER
puts("| - - - - - - - - - - |");
puts("|used |");
#endif
printf("\e[0m"); // reset
puts("+---------------------+");
hptr = (void *) hptr + hptr->size + sizeof(header_t);
}
else
{
printf("\e[34m"); // blue
printf("|size: %14lu | %p [node_t]\n",
((node_t *) hptr)->size, hptr);
printf("|prev: %14p | FREE\n", ((node_t *) hptr)->prev);
printf("|next: %14p | \n", ((node_t *) hptr)->next);
printf("|page: %14p |\n", hptr->page);
#ifndef SHORTER
puts("| - - - - - - - - - - |");
puts("|free |");
#endif
printf("\e[0m"); // reset
puts("+---------------------+");
hptr = (void *) hptr + hptr->size + sizeof(node_t);
}
}
nptr = nptr->next;
} while (nptr != NULL);
}
}
int
main(int argc, char *argv[])
{
void *ptrs['z' - 'a' + 1] = { 0 };
char action;
char c;
int n;
ptrs[0] = __malloc(100); // a
ptrs[1] = __malloc(100); // b
ptrs[2] = __malloc(400); // c
ptrs[2] = __realloc(ptrs[2], 3760);
while (1)
{
puts("\e[H\e[2J");
print_mem_map(ptrs);
for (int i = 0; i < 'z' - 'a' + 1; i++)
if (ptrs[i] != NULL)
printf("[ptr] %c = %p\n", 'a' + i, ptrs[i] - sizeof(header_t));
puts("[m]: Malloc id[a-z] size[0,...] | [q]: Quit");
puts("[r]: Realloc id[a-z] size[0,...] | [c]: Clear");
puts("[f]: Free id[a-z] |");
printf("[>]: ");
scanf(" %c", &action);
switch (action)
{
case 'm':
scanf(" %c %d", &c, &n);
if (ptrs[c - 'a'] != NULL)
puts("Variable already used!");
else
ptrs[c - 'a'] = __malloc(n);
break;
case 'r':
scanf(" %c %d", &c, &n);
ptrs[c - 'a'] = __realloc(ptrs[c - 'a'], n);
break;
case 'f':
scanf(" %c", &c);
__free(ptrs[c - 'a']);
ptrs[c - 'a'] = NULL;
break;
case 'c':
for (int i = 0; i < 'z' - 'a' + 1; i++)
if (ptrs[i] != NULL)
{
__free(ptrs[i]);
ptrs[i] = NULL;
}
break;
case 'q':
return 0;
default:
fseek(stdin, 0, SEEK_END);
break;
}
}
return 0;
}