-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.c
243 lines (220 loc) · 4.93 KB
/
printing.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my_malloc.h"
#include "printing.h"
#define MALLOC_COLOR "MALLOC_DEBUG_COLOR"
static bool check_env;
static bool use_color;
/**
* @brief Print just the block's size
*
* @param block The block to print
*/
void basic_print(Header * block) {
printf("[%zd] -> ", getSize(block));
}
/**
* @brief Print just the block's size
*
* @param block The block to print
*/
void print_list(Header * block) {
printf("[%zd]\n", getSize(block));
}
/**
* @brief return a string representing the allocation status
*
* @param allocated The allocation status field
*
* @return A string representing the allocation status
*/
static inline const char * allocated_to_string(char allocated) {
switch(allocated) {
case UNALLOCATED:
return "false";
case ALLOCATED:
return "true";
case FENCEPOST:
return "fencepost";
}
assert(false);
}
static bool check_color() {
if (!check_env) {
// genenv allows accessing environment varibles
const char * var = getenv(MALLOC_COLOR);
use_color = var != NULL && !strcmp(var, "1337_CoLoRs");
check_env = true;
}
return use_color;
}
/**
* @brief Change the tty color based on the block's allocation status
*
* @param block The block to print the allocation status of
*/
static void print_color(Header * block) {
if (!check_color()) {
return;
}
switch(getState(block)) {
case UNALLOCATED:
printf("\033[0;32m");
break;
case ALLOCATED:
printf("\033[0;34m");
break;
case FENCEPOST:
printf("\033[0;33m");
break;
}
}
static void clear_color() {
if (check_color()) {
printf("\033[0;0m");
}
}
static inline bool is_sentinel(void * p) {
for (int i = 0; i < NUM_LISTS; i++) {
if (&freelists[i] == p) {
return true;
}
}
return false;
}
/**
* @brief Print the free list pointers if RELATIVE_POINTERS is set to true
* then print the pointers as an offset from the base of the heap. This allows
* for determinism in testing.
* (due to ASLR https://en.wikipedia.org/wiki/Address_space_layout_randomization#Linux)
*
* @param p The pointer to print
*/
void print_pointer(void * p) {
if (is_sentinel(p)) {
printf("SENTINEL");
} else {
if (RELATIVE_POINTERS) {
printf("%04zd", p - base);
} else {
printf("%p", p);
}
}
}
/**
* @brief Verbose printing of all of the metadata fields of each block
*
* @param block The block to print
*/
void print_object(Header * block) {
print_color(block);
printf("[\n");
printf("\taddr: ");
print_pointer(block);
puts("");
printf("\tsize: %zd\n", getSize(block) );
printf("\tleft_size: %zd\n", block->leftSize);
printf("\tallocated: %s\n", allocated_to_string(getState(block)));
if (!getState(block)) {
printf("\tprev: ");
print_pointer(block->prev);
puts("");
printf("\tnext: ");
print_pointer(block->next);
puts("");
}
printf("]\n");
clear_color();
}
/**
* @brief Simple printer that just prints the allocation status of each block
*
* @param block The block to print
*/
void print_status(Header * block) {
print_color(block);
switch(getState(block)) {
case UNALLOCATED:
printf("[U]");
break;
case ALLOCATED:
printf("[A]");
break;
case FENCEPOST:
printf("[F]");
break;
}
clear_color();
}
/*
static void print_bitmap() {
printf("bitmap: [");
for(int i = 0; i < NUM_LISTS; i++) {
if ((freelist_bitmap[i >> 3] >> (i & 7)) & 1) {
printf("\033[32m#\033[0m");
} else {
printf("\033[34m_\033[0m");
}
if (i % 8 == 7) {
printf(" ");
}
}
puts("]");
}
*/
/**
* @brief Print a linked list between two nodes using a provided print function
*
* @param pf Function to perform the actual printing
* @param start Node to start printing at
* @param end Node to stop printing at
*/
void print_sublist(printFormatter pf, Header * start, Header * end) {
for (Header * cur = start; cur != end; cur = cur->next) {
pf(cur);
}
}
/**
* @brief print the full freelist
*
* @param pf Function to perform the Header printing
*/
void freelist_print(printFormatter pf) {
if (!pf) {
return;
}
for (size_t i = 0; i < NUM_LISTS; i++) {
Header * freelist = &freelists[i];
if (freelist->next != freelist) {
printf("L%zu: ", i);
print_sublist(pf, freelist->next, freelist);
puts("");
}
fflush(stdout);
}
}
/**
* @brief print the boundary tags from each chunk from the OS
*
* @param pf Function to perform the Header printing
*/
void tags_print(printFormatter pf) {
if (!pf) {
return;
}
for (size_t i = 0; i < numChunks; i++) {
Header * chunk = chunkList[i];
pf(chunk);
for (chunk = getRightHeader(chunk);
getState(chunk) != FENCEPOST;
chunk = getRightHeader(chunk)) {
pf(chunk);
}
pf(chunk);
fflush(stdout);
}
}