-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryManagement.c
578 lines (483 loc) · 13.8 KB
/
memoryManagement.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <stdlib.h>
#include <stdio.h>
#include "graph.h"
#include "memoryManagement.h"
const int MEM_DEBUG = 0;
/**
Object pool creation method. One can specify a number of elements that will be allocated as array.
This method can be optimized further by excluding the part for allocation in a way that it is possible
to allocate memory blocks later on
*/
struct ListPool* createListPool(unsigned int initNumberOfElements){
struct ListPool* newPool;
if (MEM_DEBUG) {
// this allows the dump methods to free everything that is dumped
initNumberOfElements = 1;
}
if ((newPool = malloc(sizeof(struct ListPool)))) {
if ((newPool->unused = malloc(initNumberOfElements * sizeof(struct VertexList)))) {
unsigned int i;
/* we have allocated an array, but need a list, thus set ponters accordingly */
for (i=0; i<initNumberOfElements-1; ++i) {
(newPool->unused[i]).next = &(newPool->unused[i+1]);
}
(newPool->unused[initNumberOfElements-1]).next = NULL;
newPool->poolPointer = newPool->unused;
newPool->initNumberOfElements = initNumberOfElements;
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
return newPool;
}
/**
Frees the object pool and all elements in its unused list
*/
void freeListPool(struct ListPool *p) {
while (p->unused) {
p->tmp = p->unused->next;
if ((p->unused < p->poolPointer) || (p->unused > &(p->poolPointer[p->initNumberOfElements-1]))) {
free(p->unused);
}
p->unused = p->tmp;
}
if (!MEM_DEBUG) {
free(p->poolPointer);
}
free(p);
}
/**
Get a List Element out of ObjectPool p. If p does not contain unused elements, new memory is
allocated.
The output of this function is initialized with zero / null values.
*/
struct VertexList* getVertexList(struct ListPool* p){
if (p->unused) {
/* get first of the unused elements */
p->tmp = p->unused;
p->unused = p->tmp->next;
} else {
/* allocate memory for new struct */
if (!(p->tmp = malloc(sizeof(struct VertexList)))) {
/* malloc did not work */
printf("Error allocating memory\n");
return NULL;
}
}
/* The malloc worked or we had an element left. So we initialize the struct */
wipeVertexList(p->tmp);
return p->tmp;
}
/**
Set all variables in the struct to 0 or NULL
*/
void wipeVertexList(struct VertexList* e) {
e->startPoint = NULL;
e->endPoint = NULL;
e->label = NULL;
e->next = NULL;
e->used = 0;
e->isStringMaster = 0;
e->flag = 0;
}
/**
Return unused element to the pool. This method should always replace free during the algorithm.
If the dumped element is the holder of label information, then that string is freed, so be careful to dump the
"real" edge not before using one of its copies.
*/
void dumpVertexList(struct ListPool* p, struct VertexList* l) {
/* free the label string, if l manages it */
if (l->isStringMaster) {
free(l->label);
}
if (MEM_DEBUG) {
free(l);
} else {
/* add l to the unused list */
l->next = p->unused;
p->unused = l;
}
}
/**
* Given a pointer to a VertexList struct e, this method dumps all VertexList structs
* that are reachable from e by following the ->next pointers.
*/
void dumpVertexListRecursively(struct ListPool* p, struct VertexList* e) {
if (e) {
dumpVertexListRecursively(p, e->next);
dumpVertexList(p, e);
}
}
/**
* Given a pointer to a VertexList struct e, this method dumps all VertexList structs
* that are reachable from e by following the ->next pointers.
*/
void dumpVertexListLinearly(struct ListPool* p, struct VertexList* e) {
struct VertexList* tmp;
while (e) {
tmp = e->next;
dumpVertexList(p,e);
e = tmp;
}
}
/******************************/
/**
Object pool creation method. One can specify a number of elements that will be allocated as array.
This method can be optimized further by excluding the part for allocation in a way that it is possible
to allocate memory blocks later on
*/
struct VertexPool* createVertexPool(unsigned int initNumberOfElements){
struct VertexPool* newPool;
if (MEM_DEBUG) {
// this allows the dump methods to free everything that is dumped
initNumberOfElements = 1;
}
if ((newPool = malloc(sizeof(struct VertexPool)))) {
if ((newPool->unused = malloc(initNumberOfElements * sizeof(struct Vertex)))) {
unsigned int i;
/* we have allocated an array, but need a list, thus set ponters accordingly */
for (i=0; i<initNumberOfElements-1; ++i) {
(newPool->unused[i]).next = &(newPool->unused[i+1]);
}
(newPool->unused[initNumberOfElements-1]).next = NULL;
newPool->poolPointer = newPool->unused;
newPool->initNumberOfElements = initNumberOfElements;
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
return newPool;
}
/**
Frees the object pool and all elements in its unused list
*/
void freeVertexPool(struct VertexPool *p) {
while (p->unused) {
p->tmp = p->unused->next;
if ((p->unused < p->poolPointer) || (p->unused > &(p->poolPointer[p->initNumberOfElements-1]))) {
free(p->unused);
}
p->unused = p->tmp;
}
if (!MEM_DEBUG) {
free(p->poolPointer);
}
free(p);
}
/**
Obtain a vertex struct from a vertex pool. If the pool does not have any free vertices, a new struct will be malloc'd.
The returned struct is initialized with zero/null values
*/
struct Vertex* getVertex(struct VertexPool* p){
if (p->unused) {
/* get first of the unused elements */
p->tmp = p->unused;
p->unused = p->tmp->next;
} else {
/* allocate memory for new struct */
if (!(p->tmp = malloc(sizeof(struct Vertex)))) {
/* malloc did not work */
printf("Error allocating memory\n");
return NULL;
}
}
/* The malloc worked or we had an element left. So we initialize the struct */
wipeVertex(p->tmp);
return p->tmp;
}
/**
Set all variables in the struct to 0 or NULL
*/
void wipeVertex(struct Vertex* v) {
v->label = NULL;
v->next = NULL;
v->neighborhood = NULL;
v->number = 0;
v->visited = 0;
v->isStringMaster = 0;
v->lowPoint = 0;
v->d = 0;
}
/**
Set all variables in the struct to 0 or NULL, but keep the vertex number
*/
void wipeVertexButKeepNumber(struct Vertex* v) {
v->label = NULL;
v->next = NULL;
v->neighborhood = NULL;
v->visited = 0;
v->isStringMaster = 0;
v->lowPoint = 0;
v->d = 0;
}
/**
Return unused element to the pool. This method should always replace free during the algorithm.
If the dumped element is the holder of label information, then that string is freed, so be careful to dump the
"real" vertex not before using one of its copies.
*/
void dumpVertex(struct VertexPool* p, struct Vertex* v){
if (v->isStringMaster) {
free(v->label);
}
if (MEM_DEBUG) {
free(v);
} else {
/* add v to the unused list */
v->next = p->unused;
p->unused = v;
}
}
/************************************/
/**
Object pool creation method. One can specify a number of elements that will be allocated as array.
This method can be optimized further by excluding the part for allocation in a way that it is possible
to allocate memory blocks later on
*/
struct GraphPool* createGraphPool(unsigned int initNumberOfElements, struct VertexPool* vp, struct ListPool* lp) {
struct GraphPool* newPool;
if (MEM_DEBUG) {
// this allows the dump methods to free everything that is dumped
initNumberOfElements = 1;
}
if ((newPool = malloc(sizeof(struct GraphPool)))) {
/* set the listpool and vertexpool of the graphpool according to the input. good for dumping actions */
newPool->vertexPool = vp;
newPool->listPool = lp;
if ((newPool->unused = malloc(initNumberOfElements * sizeof(struct Graph)))) {
unsigned int i;
/* we have allocated an array, but need a list, thus set ponters accordingly */
for (i=0; i<initNumberOfElements-1; ++i) {
(newPool->unused[i]).next = &(newPool->unused[i+1]);
}
(newPool->unused[initNumberOfElements-1]).next = NULL;
newPool->poolPointer = newPool->unused;
newPool->initNumberOfElements = initNumberOfElements;
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
return newPool;
}
/* free a graph pool and every graph struct contained in it */
void freeGraphPool(struct GraphPool *p) {
while (p->unused) {
p->tmp = p->unused->next;
if ((p->unused < p->poolPointer) || (p->unused > &(p->poolPointer[p->initNumberOfElements-1]))) {
free(p->unused);
}
p->unused = p->tmp;
}
if (!MEM_DEBUG) {
free(p->poolPointer);
}
free(p);
}
/**
* Get a graph from the specified GraphPool
*/
struct Graph* getGraph(struct GraphPool* p) {
if (p->unused) {
/* get first of the unused elements */
p->tmp = p->unused;
p->unused = p->tmp->next;
} else {
/* allocate memory for new struct */
if (!(p->tmp = malloc(sizeof(struct Graph)))) {
/* malloc did not work */
printf("Error allocating memory\n");
return NULL;
}
}
/* malloc worked or there was an unused element left, initialize stuff */
wipeGraph(p->tmp);
return p->tmp;
}
/**
Set all variables in the struct to 0 or NULL
*/
void wipeGraph(struct Graph* g) {
g->n = 0;
g->m = 0;
g->number = 0;
g->activity = 0;
g->vertices = NULL;
g->next = NULL;
}
/**
dumpGraph dumps all vertices and edges contained in g into the respective pools and frees the
vertices array of g and g itself
*/
void dumpGraph(struct GraphPool* p, struct Graph *g) {
struct VertexList *tmp;
int i;
/* dump content */
if (g->vertices) {
for (i=0; i<g->n; ++i) {
if (g->vertices[i]) {
while (g->vertices[i]->neighborhood) {
tmp = g->vertices[i]->neighborhood->next;
dumpVertexList(p->listPool, g->vertices[i]->neighborhood);
g->vertices[i]->neighborhood = tmp;
}
dumpVertex(p->vertexPool, g->vertices[i]);
}
}
free(g->vertices);
}
if (MEM_DEBUG) {
free(g);
} else {
/* append g to the unused list */
g->next = p->unused;
p->unused = g;
}
}
/** Dump a list of struct Graphs. (e.g. the result of partitionIntoForestAndCycles()) **/
void dumpGraphList(struct GraphPool* gp, struct Graph* g) {
if (g->next != NULL) {
dumpGraphList(gp, g->next);
}
dumpGraph(gp, g);
}
/**********************************************/
/**
Object pool creation method. One can specify a number of elements that will be allocated as array.
This method can be optimized further by excluding the part for allocation in a way that it is possible
to allocate memory blocks later on
*/
struct ShallowGraphPool* createShallowGraphPool(unsigned int initNumberOfElements, struct ListPool* lp){
struct ShallowGraphPool* newPool;
if (MEM_DEBUG) {
// this allows the dump methods to free everything that is dumped
initNumberOfElements = 1;
}
if ((newPool = malloc(sizeof(struct ShallowGraphPool)))) {
/* set the listpool of the graphpool according to the input. good for dumping actions */
newPool->listPool = lp;
if ((newPool->unused = malloc(initNumberOfElements * sizeof(struct ShallowGraph)))) {
unsigned int i;
/* we have allocated an array, but need a list, thus set ponters accordingly */
for (i=0; i<initNumberOfElements-1; ++i) {
(newPool->unused[i]).next = &(newPool->unused[i+1]);
}
(newPool->unused[initNumberOfElements-1]).next = NULL;
newPool->poolPointer = newPool->unused;
newPool->initNumberOfElements = initNumberOfElements;
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
} else {
printf("Error while initializing object pool for list elements\n");
return NULL;
}
return newPool;
}
/**
* free ShallowGraphPool struct and any ShallowGraph in it.
*/
void freeShallowGraphPool(struct ShallowGraphPool *p) {
while (p->unused) {
p->tmp = p->unused->next;
if ((p->unused < p->poolPointer) || (p->unused > &(p->poolPointer[p->initNumberOfElements-1]))) {
free(p->unused);
}
p->unused = p->tmp;
}
if (!MEM_DEBUG) {
free(p->poolPointer);
}
free(p);
}
/**
Standard Object Pool getter
*/
struct ShallowGraph* getShallowGraph(struct ShallowGraphPool *p) {
if (p->unused) {
/* get first of the unused elements */
p->tmp = p->unused;
p->unused = p->tmp->next;
} else {
/* allocate memory for new struct */
if (!(p->tmp = malloc(sizeof(struct ShallowGraph)))) {
/* malloc did not work */
printf("Error allocating memory\n");
return NULL;
}
}
/* The malloc worked or we had an element left. So we initialize the struct */
wipeShallowGraph(p->tmp);
return p->tmp;
}
/**
Set all variables in the struct to 0 or NULL
*/
void wipeShallowGraph(struct ShallowGraph* g) {
g->next = NULL;
g->prev = NULL;
g->m = 0;
g->edges = NULL;
g->lastEdge = NULL;
g->data = 0;
}
/**
Standard Object Pool dumper
*/
void dumpShallowGraph(struct ShallowGraphPool *p, struct ShallowGraph* g) {
/* dump edges */
while (g->edges) {
p->listPool->tmp = g->edges;
g->edges = g->edges->next;
dumpVertexList(p->listPool, p->listPool->tmp);
}
if (MEM_DEBUG) {
free(g);
} else {
/* add g to the unused list */
g->next = p->unused;
p->unused = g;
}
}
/**
* Dump a cycle or list of ShallowGraphs
*/
void dumpShallowGraphCycle(struct ShallowGraphPool *p, struct ShallowGraph* g) {
struct ShallowGraph *idx = g, *tmp;
/* to cope with the possibility that g points to a list and not to a cycle, lazy check for NULL */
while (idx && (idx->next != g)){
tmp = idx;
idx = idx->next;
dumpShallowGraph(p, tmp);
}
/* if g did point to a cycle of length 1 */
if (idx) {
dumpShallowGraph(p, idx);
}
}
/* does not fit anywhere */
/**
* Clone an array of chars to a newly allocated array of the same size.
*/
char* copyString(char* string) {
int i;
char* copy;
for (i=0; string[i] != 0; ++i);
copy = malloc((i + 1) * sizeof(char));
for (i=0; string[i] != 0; ++i) {
copy[i] = string[i];
}
copy[i] = 0;
return copy;
}