-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.c
More file actions
462 lines (336 loc) · 10.5 KB
/
Copy pathheap.c
File metadata and controls
462 lines (336 loc) · 10.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "heap.h"
#include "tested_declarations.h"
#include "rdebug.h"
int heap_setup(void)
{
memory_manager.memory_size = 0;
memory_manager.memory_start = custom_sbrk(0);
memory_manager.first_memory_chunk = NULL;
if(memory_manager.memory_start == (void*)-1) {
return -1;
}
return 0;
}
void hash_calculate(struct memory_chunk_t *mem)
{
int val = 0;
for(size_t i = 0; i < sizeof(struct memory_chunk_t) - sizeof(int); i++) {
val += *((char*)mem+i);
}
mem->CODE = val;
}
void heap_clean(void)
{
custom_sbrk(memory_manager.memory_size*(-1));
memory_manager.memory_size = 0;
memory_manager.first_memory_chunk = NULL;
memory_manager.memory_start = NULL;
}
struct memory_chunk_t *check_mem_holes(size_t needed_size)
{
struct memory_chunk_t *tmp = memory_manager.first_memory_chunk;
while(tmp->next != NULL) {
if(tmp->free == 1 && tmp->size >= needed_size) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
void* heap_malloc(size_t size)
{
if(size == 0) {
return NULL;
}
if(memory_manager.first_memory_chunk == NULL) {
memory_manager.memory_size = size + PLOTEK*2 + sizeof(struct memory_chunk_t);
memory_manager.memory_start = custom_sbrk(size + PLOTEK*2 + sizeof(struct memory_chunk_t));
if (memory_manager.memory_start == (void *) -1) {
memory_manager.memory_start = NULL;
return NULL;
}
struct memory_chunk_t *tmp = (struct memory_chunk_t *) memory_manager.memory_start;
tmp->next = NULL;
tmp->prev = NULL;
tmp->free = 0;
tmp->size = size;
for (int i = 0; i < PLOTEK; ++i) {
*((char *) tmp + i + sizeof(struct memory_chunk_t)) = '#';
*((char *) tmp + i + size + sizeof(struct memory_chunk_t) + PLOTEK) = '#';
}
memory_manager.first_memory_chunk = tmp;
hash_calculate(tmp);
return (char *)tmp + sizeof(struct memory_chunk_t) + PLOTEK;
}
//check if any free blocs of sufficient size exist
struct memory_chunk_t *tmp;
tmp = check_mem_holes(size);
if(tmp != NULL) {
tmp->size = size;
tmp->free = 0;
for(int i = 0; i < PLOTEK; i++) {
*((char*)tmp + sizeof(struct memory_chunk_t) + i) = '#';
*((char*)tmp + sizeof(struct memory_chunk_t) + i + tmp->size + PLOTEK) = '#';
}
hash_calculate(tmp);
return (char *)tmp + sizeof(struct memory_chunk_t) + PLOTEK;
}
//########## NEXT BLOCK ##########
tmp = memory_manager.first_memory_chunk;
while(tmp->next != NULL) {
tmp = tmp->next;
}
void *new_mem = custom_sbrk(size + PLOTEK*2 + sizeof(struct memory_chunk_t));
if (new_mem == (void *) -1) {
new_mem = NULL;
return NULL;
}
memory_manager.memory_size += size + PLOTEK*2 + sizeof(struct memory_chunk_t);
struct memory_chunk_t *new = (struct memory_chunk_t *) new_mem;
new->next = NULL;
new->prev = tmp;
new->free = 0;
new->size = size;
hash_calculate(new);
for (int i = 0; i < PLOTEK; ++i) {
*((char *) new + i + sizeof(struct memory_chunk_t)) = '#';
*((char *) new + i + size + sizeof(struct memory_chunk_t) + PLOTEK) = '#';
}
tmp->next = new;
hash_calculate(tmp);
return (char *)new + sizeof(struct memory_chunk_t) + PLOTEK;
}
void* heap_calloc(size_t number, size_t size)
{
if(number*size == 0) {
return NULL;
}
char *tmp = heap_malloc(number*size);
if(!tmp) {
return NULL;
}
memset(tmp, 0, size*number);
return tmp;
}
void* heap_realloc(void* memblock, size_t count)
{
if(get_pointer_type(memblock) != pointer_valid && memblock != NULL) {
return NULL;
}
if(memory_manager.memory_start == NULL) {
return NULL;
}
if(count == 0) {
heap_free(memblock);
return NULL;
}
if(memblock == NULL) {
char *tmp = heap_malloc(count);
if(!tmp) {
return NULL;
}
return tmp;
}
struct memory_chunk_t *tmp;
tmp = (struct memory_chunk_t*)((char*)memblock - sizeof(struct memory_chunk_t) - PLOTEK);
if(tmp->next != NULL) {
size_t size = (char*)tmp->next - (char*)tmp - sizeof(struct memory_chunk_t) - PLOTEK*2;
if(size > tmp->size) {
tmp->size = size;
}
}
if(tmp->size >= count) {
tmp->size = count;
memset((char*)memblock + tmp->size, '#', 4);
hash_calculate(tmp);
return memblock;
}
if(tmp->size == count) {
return memblock;
}
//spr. czy nast blok == NULL
if(tmp->next == NULL) {
void *new_mem = custom_sbrk(count - tmp->size);
if (new_mem == (void *) -1) {
new_mem = NULL;
return NULL;
}
memory_manager.memory_size += count - tmp->size;
tmp->size = count;
memset((char*)memblock+tmp->size, '#', 4);
hash_calculate(tmp);
return memblock;
}
//sprawdzic nast. blok
heap_connect(memory_manager.first_memory_chunk);
if(tmp->next && tmp->next->free && tmp->size+tmp->next->size + sizeof(struct memory_chunk_t) + PLOTEK*2 >= count) {
tmp->next = tmp->next->next;
tmp->size = count;
hash_calculate(tmp);
if(tmp->next->next) {
tmp->next->next->prev = tmp;
hash_calculate(tmp->next->next);
}
memset((char*)memblock+tmp->size, '#', 4);
return memblock;
}
char *new = heap_malloc(count);
if(!new) {
return NULL;
}
for(size_t i = 0; i < tmp->size; i++) {
*((char*)new+i) = *((char*)tmp + PLOTEK + sizeof(struct memory_chunk_t)+i);
}
heap_free((char *)tmp + PLOTEK + sizeof(struct memory_chunk_t));
return new;
}
void heap_connect(struct memory_chunk_t *mem)
{
struct memory_chunk_t *tmp = (struct memory_chunk_t*)mem;
while(mem) {
if(mem->free == 1) {
tmp = mem;
}
mem = mem->next;
if(tmp->free && mem && mem->free == 1) {
tmp->size += mem->size + sizeof(struct memory_chunk_t) + PLOTEK*2;
tmp->next = mem->next;
hash_calculate(tmp);
if(mem->next) {
mem->next->prev = tmp;
hash_calculate(mem);
}
} else {
tmp = tmp->next;
hash_calculate(tmp);
}
if(mem->next == NULL) {
break;
}
mem = tmp;
}
}
void check_destroy(struct memory_chunk_t *mem)
{
int flag = 1;
while(mem != NULL) {
if(!mem->free) {
flag = 0;
}
mem = mem->next;
}
if(flag) {
custom_sbrk(memory_manager.memory_size*(-1));
memory_manager.memory_size = 0;
memory_manager.first_memory_chunk = NULL;
}
}
void heap_free(void* memblock)
{
if(memblock == NULL) return;
if(get_pointer_type(memblock) != pointer_valid) {
return;
}
struct memory_chunk_t *tmp = memory_manager.first_memory_chunk;
while (tmp != NULL) {
if((char*)tmp +PLOTEK + sizeof(struct memory_chunk_t) == (char*)memblock ) {
tmp->free = 1;
if(tmp->next != NULL) {
size_t size = (char*)tmp->next - (char*)tmp - sizeof(struct memory_chunk_t) - PLOTEK*2;
if(size > tmp->size) {
tmp->size = size;
}
}
hash_calculate(tmp);
check_destroy(tmp);
heap_connect((struct memory_chunk_t*)memory_manager.first_memory_chunk);
/*if(tmp->next == NULL && tmp->free) {
if(tmp->prev != NULL) {
tmp->prev->next = NULL;
}
}*/
break;
}
tmp = tmp->next;
}
}
size_t heap_get_largest_used_block_size(void)
{
if(memory_manager.memory_size == 0 || memory_manager.first_memory_chunk == NULL) {
return 0;
}
if(heap_validate() != 0) {
return 0;
}
struct memory_chunk_t *tmp = memory_manager.first_memory_chunk;
size_t res = 0;
while(tmp != NULL) {
if(tmp->size > res && !tmp->free) res = tmp->size;
tmp = tmp->next;
}
return res;
}
enum pointer_type_t get_pointer_type(const void* const pointer)
{
if(pointer == NULL) {
return pointer_null;
}
if(heap_validate() == 1) {
return pointer_heap_corrupted;
}
struct memory_chunk_t *tmp = memory_manager.first_memory_chunk;
while(tmp){
if(!tmp->free) {
if((char*)tmp <= (char*)pointer && (char*)tmp + sizeof(struct memory_chunk_t) > (char*)pointer) {
return pointer_control_block;
}
if((char*)pointer >= (char*)tmp + sizeof(struct memory_chunk_t) && (char*)pointer < (char*)tmp + sizeof(struct memory_chunk_t) + PLOTEK) {
return pointer_inside_fences;
}
if((char*)pointer == (char*)tmp + sizeof(struct memory_chunk_t) + PLOTEK) {
return pointer_valid;
}
if((char*)pointer > (char*)tmp + sizeof(struct memory_chunk_t) + PLOTEK && (char*)pointer < (char*)tmp + sizeof(struct memory_chunk_t) + PLOTEK + tmp->size) {
return pointer_inside_data_block;
}
if((char*)pointer >= (char*)tmp + sizeof(struct memory_chunk_t) + PLOTEK + tmp->size && (char*)pointer < (char*)tmp + sizeof(struct memory_chunk_t) + 2*PLOTEK + tmp->size) {
return pointer_inside_fences;
}
}
tmp = tmp->next;
}
return pointer_unallocated;
}
int hash_check(struct memory_chunk_t *mem)
{
int val = 0;
for(size_t i = 0; i < sizeof(struct memory_chunk_t) - sizeof(int); i++) {
val += *((char*)mem+i);
}
if(val != mem->CODE) {
return 1;
}
return 0;
}
int heap_validate(void)
{
if(memory_manager.memory_start == NULL && memory_manager.first_memory_chunk == NULL) {
return 2;
}
struct memory_chunk_t *tmp = memory_manager.first_memory_chunk;
if(tmp == NULL) {
return 0;
}
do {
if(hash_check(tmp)) {
return 3;
}
for(int i = 0; i < PLOTEK; i++) {
if(*((char*)tmp + sizeof(struct memory_chunk_t) + i) != '#' || *((char*)tmp+ sizeof(struct memory_chunk_t) + i + 4 + tmp->size) != '#') {
return 1;
}
}
tmp = tmp->next;
} while(tmp != NULL);
return 0;
}