-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path537malloc.c
More file actions
executable file
·281 lines (215 loc) · 6.42 KB
/
537malloc.c
File metadata and controls
executable file
·281 lines (215 loc) · 6.42 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "range_tree.h"
#include "537malloc.h"
//Tree to hold allocations for main program functionality
static tree *tree_main;
//Variables used to keep track of orgin address allocations for extra credit
static addr_node* addr_arr[BUFF_SIZE];
static int arr_index = 0;
//Extra Credit- This function adds an origin address to the list
void add_addr(void* address, size_t size)
{
int found = 0;
//Update an existing origin array if found
for(int i = 0; i < arr_index; i++)
{
if(addr_arr[i]->addr == address)
{
found = 1;
addr_arr[i]->allocated_bytes = addr_arr[i]->allocated_bytes + size;
addr_arr[i]->num_allocations++;
break;
}
}
//If this is a new origin address, add it to the array
if(!found)
{
addr_arr[arr_index] = malloc(sizeof(addr_node));
addr_arr[arr_index]->addr = address;
addr_arr[arr_index]->allocated_bytes = size;
addr_arr[arr_index]->num_allocations = 1;
arr_index++;
}
}
//Extra Credit- This function prints all origin addresses, the number of times that the origin address
//was called, and the total allocation by the origin address
void view_allocations()
{
for(int i = 0; i < arr_index; i++)
{
printf("The instruction at address: %p called malloc537 %d times to allocate a total of %ld bytes of memory\n", addr_arr[i]->addr, addr_arr[i]->num_allocations, addr_arr[i]->allocated_bytes);
}
}
void *malloc537(size_t size)
{
// int test_sp = 0;
// for(int i = 0; i < 48; i=i+4){
// printf("Address of tp: %p offset: %d\n", (char*)*((&test_sp)-i), i );
// }
// printf("\n\n");
volatile long testarr[2];
long sneak[1];
testarr[1] = 12;
if(testarr[1])
{
}
//feel free to adjust the search limits
for( int i = -32; i <= 32; ++i) {
printf("offset %3d: data 0x%08X\n", i, sneak[i]);
//printf("Address form: %p\n", (char*)sneak[i]);
}
if(size == 0) {
fprintf(stderr, "Warning: Allocating memory of size 0\n");
}
//If this is the first malloc, create the tree
if(tree_main == NULL)
{
tree_main = tree_create();
}
void* retVal = malloc(size);
if(retVal == NULL)
{
fprintf(stderr, "Malloc failed");
exit(EXIT_FAILURE);
}
//Previous node in tree to the newly allocated memory
node *prevPtr = tree_find_GLT(tree_main, retVal);
//Previous node to end of tree, indicating a node exists in the middle
//of the potentially allocated memory
node *containPtr = tree_find_GLT(tree_main, (retVal + size));
//Potentially Split Node
//If the node previous to the potential node is free and is overlapping
//The space of the potential node, then split the node
if(prevPtr != NULL)
{
if((prevPtr->free_flag == 1) && ((prevPtr->addr + prevPtr->length) > retVal))
{
//Reduce length of free node to be new (pointer - previous pointer)
prevPtr->length = (retVal - prevPtr->addr);
}
}
//Else if the range of the potential node includes the start of a an existing free node
else if(containPtr != NULL)
{
if((retVal < containPtr->addr) && (containPtr->free_flag == 1))
{
//Delete the free node from the tree
tree_erase(tree_main, prevPtr->addr);
}
}
//Add the allocation to the tree
node_insert(tree_main, retVal, size);
// printf("offset %3d: data 0x%08X\n", 3, sneak[3 + sizeof(testarr[1])]);
// printf("Address form: %p\n", (char*)sneak[]);
//Add the origin address and allocation size to the list
add_addr(__builtin_return_address(0), size);
//USE THIS TO INSERT
//add_addr((char*)sneak[3], size);
return retVal;
}
void free537(void *ptr) {
if (ptr == NULL) {
fprintf(stderr, "Null pointer err\n");
exit(EXIT_FAILURE);
}
//check if ptr points to the first byte
// or memory not allocated by 537malloc()
if (tree_find(tree_main,ptr) == NULL) {
fprintf(stderr, "Mem not alocated by 537malloc() or bad pointer\n");
exit(EXIT_FAILURE);
}
if (node_isfree(tree_main,ptr)) {
fprintf(stderr, "Node has already been freed\n");
exit(EXIT_FAILURE);
}
//set the free_flag to 1
node_setfree(tree_main,ptr,1);
free(ptr);
}
void *realloc537(void *ptr, size_t size) {
if(size == 0) {
fprintf(stderr, "Warning: Allocating memory of size 0\n");
}
if (ptr == NULL) {
return malloc537(size);
}
if ( ptr != NULL && size == 0) {
free537(ptr);
return NULL;
//node_setfree(tree_main,ptr,0); ????
}
node_setfree(tree_main,ptr,1);
void* rtn_ptr = realloc(ptr, size);
/////
//Previous node in tree to the newly allocated memory
node *prevPtr = tree_find_GLT(tree_main, rtn_ptr);
//Previous node to end of tree, indicating a node exists in the middle
//of the potentially allocated memory
node *containPtr = tree_find_GLT(tree_main, (rtn_ptr + size));
//Potentially Split Node
//If the node previous to the potential node is free and is overlapping
//The space of the potential node, then split the node
if(prevPtr != NULL)
{
if((prevPtr->free_flag == 1) && ((prevPtr->addr + prevPtr->length) > rtn_ptr))
{
//Reduce length of free node to be new (pointer - previous pointer)
prevPtr->length = (rtn_ptr - prevPtr->addr);
}
}
//Else if the range of the potential node includes the start of a an existing free node
else if(containPtr != NULL)
{
if((rtn_ptr < containPtr->addr) && (containPtr->free_flag == 1))
{
//Delete the free node from the tree
tree_erase(tree_main, prevPtr->addr);
}
}
////////
node_insert(tree_main,rtn_ptr,size);
return rtn_ptr;
}
void memcheck537(void *ptr, size_t size) {
if(size == 0) {
fprintf(stderr, "Warning: Allocating memory of size 0\n");
}
if(ptr == NULL) {
fprintf(stderr, "Invalid address- Pointer is NULL\n");
exit(EXIT_FAILURE);
}
node *nodePtr = tree_find(tree_main,ptr);
//Node is allocated in the tree
if(nodePtr != NULL)
{
if (size > nodePtr->length) {
fprintf(stderr, "Memory out of allocated bounds\n");
exit(EXIT_FAILURE);
}
}
else
{
//ptr is potentially in the middle of an allocated block
//GLT -> finds greatest allocated node that is less than ptr
nodePtr = tree_find_GLT(tree_main, ptr);
if(nodePtr == NULL)
{
fprintf(stderr, "Starting address exists before address of first allocated memory adddress\n");
exit(EXIT_FAILURE);
}
if(ptr > (nodePtr->addr + nodePtr->length))
{
fprintf(stderr, "Starting address is out of bounds\n");
exit(EXIT_FAILURE);
}
if((ptr + size) > (nodePtr->addr + nodePtr->length))
{
fprintf(stderr, "Ending address is out of bounds\n");
exit(EXIT_FAILURE);
}
}
}