-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_sim.c
More file actions
203 lines (173 loc) · 3.93 KB
/
malloc_sim.c
File metadata and controls
203 lines (173 loc) · 3.93 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Parameters
const int MAXE = 10;
const int MAXR = 9;
const int HEAPSZ = 50;
const double PROBF = 0.3;
int srandSwitch = 1; // A switch for srand, 1 is on, 0 is off
// Linked list to implement free lists and allocation list
typedef struct node{
int index;
int size;
struct node* next;
} Node;
Node* freelistHead = NULL;
Node* alloclistHead = NULL;
int numAlloc = 0;
void insertAtStart(Node** head, int index, int size) {
Node* new = malloc(sizeof(Node));
new->index = index;
new->size = size;
new->next = *head;
*head = new;
}
void insertAtEnd(Node** head, int index, int size) {
Node* new = malloc(sizeof(Node));
new->index = index;
new->size = size;
new->next = NULL;
Node* current = *head;
if (current == NULL) {
*head = new;
return;
}
while (current->next != NULL) {
current = current->next;
}
current->next = new;
}
// When allocate, update free at and sz, append to free list
void allocateMem(int heap[], int maxsize, int data) {
// Random memory block size between 1 and maxsize
int size = ((double)rand()/RAND_MAX) * (maxsize-1) + 1;
// Iterate free list
Node* current = freelistHead;
Node* prev = current;
// Find a free block with sufficient size
while (current != NULL && current->size < size) {
prev = current;
current = current->next;
}
if (current == NULL) {
printf("Unable to find block of memory with sufficient size.\n");
return;
}
// Fill data
int start = current->index;
int i = start;
for (; i < current->index + size; i++) {
heap[i] = data;
}
// Add remaining free memory of block to free list if exists
if (current->size > size) {
insertAtEnd(&freelistHead, i, current->size - size);
}
// Delete allocated block off of free list
if (current == freelistHead) {
Node* second = freelistHead->next;
free(freelistHead);
freelistHead = second;
}
else {
Node* next = current->next;
free(current);
prev->next = next;
}
// Store the allocation
insertAtEnd(&alloclistHead, start, size);
numAlloc++;
}
void freeMem(int heap[]) {
// Pick a number between 0 and numAlloc
int toFree = ((double)rand()/RAND_MAX) * numAlloc;
// Iterate allocation list til the picked allocation
Node *cur = alloclistHead;
Node *prev = cur;
int i=0;
while (i < toFree) {
prev = cur;
cur = cur->next;
i++;
}
if (cur == NULL) {
return;
}
int freeat = cur->index;
int freesz = cur->size;
// Free memory
for (int j=freeat; j < freeat + freesz; j++) {
heap[j] = -1;
}
// Remove from allocation list
if (cur == alloclistHead) {
Node *second = cur->next;
free(cur);
alloclistHead = second;
}
else {
prev->next = cur->next;
free(cur);
}
numAlloc--;
// Prepend to free list
insertAtStart(&freelistHead, freeat, freesz);
}
void printFreelist() {
printf("freeatlist=");
Node* cur = freelistHead;
if (cur == NULL) {
printf("Empty list");
} else {
while (cur != NULL) {
printf("%d", cur->index + 1);
if (cur->next != NULL) printf(";");
cur = cur->next;
}
}
printf("\nfreeszlist=");
cur = freelistHead;
if (cur == NULL) {
printf("Empty list");
} else {
while (cur != NULL) {
printf("%d", cur->size);
if (cur->next != NULL) printf(";");
cur = cur->next;
}
}
printf("\n");
}
int main() {
// Initialize heap
int heap[HEAPSZ];
for (int i=0; i<HEAPSZ; i++) {
heap[i] = -1;
}
// At start, all memory is free
insertAtStart(&freelistHead, 0, HEAPSZ);
if (srandSwitch) srand(time(NULL)); // TOASK: run this once for whole program or once per rand() call
// Simulator
for (int t=1; t <= MAXE; t++) {
// Allocate
if(((double) rand()/RAND_MAX) > PROBF) {
allocateMem(&heap[0], MAXR, t%10);
printf("t=%d\n*", t);
}
// Free
else {
freeMem(&heap[0]);
printf("Ft=%d\n*", t);
}
// Print heap and free lists
for (int i=0; i<HEAPSZ; i++) {
if (heap[i] == -1) printf("_");
else printf("%d", heap[i]);
}
printf("\n");
printFreelist();
printf("\n\n");
}
return 0;
}