-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemoryManager.c
104 lines (98 loc) · 3.3 KB
/
MemoryManager.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
#include "MemoryManager.h"
int totalAllocSize = 0;
#if 0
MemoryManager* initializeMem() {
MemoryManager* mm = (MemoryManager*)malloc(sizeof(MemorySpace));
mm->index = 0;
mm->space = NULL;
return mm;
}
void* allocateMem(MemoryManager* mm, int size) {
void* result;
if(size == 0) return NULL;
if(mm->space == NULL || (mm->index + size) > DEFAULT_MEMORY_ALLOCATION_SIZE) {
MemorySpace* newSpace = (MemorySpace*)malloc(sizeof(MemorySpace));
if(size > DEFAULT_MEMORY_ALLOCATION_SIZE) {
newSpace->space = (void*)malloc(size);
}else {
newSpace->space = (void*)malloc(DEFAULT_MEMORY_ALLOCATION_SIZE);
}
if(verbose) fprintf(stderr, "Allocated Memory %p %p (alloc size: %d)\n", newSpace, newSpace->space , size);
newSpace->next = mm->space;
mm->space = newSpace;
mm->index = 0;
}
result = (void*)(mm->space->space + mm->index);
mm->index = mm->index + size;
return result;
}
void freeAllMem(MemoryManager* mm) {
MemorySpace* curr = mm->space;
while(curr != NULL) {
MemorySpace* next = curr->next;
if(verbose) fprintf(stderr, "Free All Memory %p %p\n", curr, curr->space);
free(curr->space);
free(curr);
curr = next;
}
mm->space = NULL;
free(mm);
}
#endif
MemoryManager* initializeMem() {
MemoryManager* mm = (MemoryManager*)malloc(sizeof(MemorySpace));
//mm->index = 0;
mm->space = NULL;
return mm;
}
void* allocateMem(MemoryManager* mm, int size) {
void* result;
if(size == 0) return NULL;
MemorySpace* newSpace = (MemorySpace*)malloc(sizeof(MemorySpace));
newSpace->space = (void*)malloc(size);
memset(newSpace->space, 0, size);
newSpace->size = size;
newSpace->next = mm->space;
mm->space = newSpace;
totalAllocSize += size;
if(verbose) fprintf(stderr, "Allocated Memory %p %p (alloc size: %d, total size: %d)\n", newSpace, newSpace->space , size, totalAllocSize);
return mm->space->space;
}
void freeMem(MemoryManager* mm, void* mem) {
MemorySpace* curr;
if(mm == NULL || mm->space == NULL) return;
curr = mm->space;
if(curr->space == mem) {
totalAllocSize -= curr->size;
mm->space = curr->next;
if(verbose) fprintf(stderr, "Freed Memory %p %p (free size: %d, total size: %d)\n", curr, curr->space, curr->size, totalAllocSize);
free(curr->space);
free(curr);
return;
}
while(curr != NULL) {
MemorySpace* next = curr->next;
if(next != NULL && next->space == mem) {
curr->next = next->next;
totalAllocSize -= next->size;
if(verbose) fprintf(stderr, "Freed Memory %p %p (free size: %d, total size: %d)\n", next, next->space, next->size, totalAllocSize);
free(next->space);
free(next);
return;
}
curr = next;
}
}
void freeAllMem(MemoryManager* mm) {
MemorySpace* curr = mm->space;
while(curr != NULL) {
MemorySpace* next = curr->next;
totalAllocSize -= curr->size;
if(verbose) fprintf(stderr, "Free All Memory %p %p (free size: %d, total size: %d\n", curr, curr->space, curr->size, totalAllocSize);
free(curr->space);
free(curr);
curr = next;
}
mm->space = NULL;
free(mm);
}