-
Notifications
You must be signed in to change notification settings - Fork 0
/
mempool.c
123 lines (107 loc) · 3.13 KB
/
mempool.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
#include "mempool.h"
// see mempool.h for the license on this code
// simple helper lib to make it easier to maintain local memory
mempool *mempool_create() {
mempool *_mp = NULL;
_mp = malloc(sizeof(mempool));
_mp->first_allocation = (mempool_allocation *) NULL;
return _mp;
}
void fit_as_last(mempool *pool, mempool_allocation *new_allocation) {
if (pool->first_allocation == NULL) {
pool->first_allocation = new_allocation;
} else {
mempool_allocation *current = pool->first_allocation;
while (current->next != NULL) {
current = current->next;
}
current->next = new_allocation;
}
}
void *mempool_malloc(mempool *pool, size_t size) {
void *allocated = malloc(size);
if (allocated == NULL) {
// memory allocation failed
return allocated;
}
mempool_allocation *new_allocation = malloc(sizeof(mempool_allocation));
if (new_allocation == NULL) {
//failed to allocate the holder for allocation ? :D
free(allocated);
return NULL;
}
new_allocation->next = NULL;
new_allocation->pointer = allocated;
fit_as_last(pool, new_allocation);
return allocated;
}
void *mempool_calloc(mempool *pool, size_t nmemb, size_t size){
void *allocated = calloc(nmemb, size);
if (allocated == NULL) {
// memory allocation failed
return allocated;
}
mempool_allocation *new_allocation = malloc(sizeof(mempool_allocation));
if (new_allocation == NULL) {
//failed to allocate the holder for allocation ? :D
free(allocated);
return NULL;
}
new_allocation->next = NULL;
new_allocation->pointer = allocated;
fit_as_last(pool, new_allocation);
return allocated;
}
void *mempool_realloc(mempool *pool, void *ptr, size_t size){
void *reallocated = realloc(ptr, size);
if (reallocated == NULL) {
// fail grande :)
return reallocated;
}
// reallocate succeeded, replace the pointer in our stack
mempool_allocation *current = pool->first_allocation;
while (current != NULL) {
if (current->pointer == ptr) {
current->pointer = reallocated;
break;
}
current = current->next;
}
// if current is null then the initial ptr never belonged to our stack, no idea how to continue, will panic
assert(current != NULL && "we were given a pointer that was not initially a part of this pool");
return reallocated;
}
void mempool_free(mempool *pool,void *ptr){
// run through the pool, find where this ptr is, cut it out from there
if (pool->first_allocation->pointer == ptr) {
pool->first_allocation = pool->first_allocation->next;
return;
}
// it is not the first
mempool_allocation *current = pool->first_allocation->next;
mempool_allocation *tmp = pool->first_allocation;
while (current != NULL) {
if (current->pointer == ptr) {
// found you :)
// lets link the previous to the next
tmp->next = current->next;
// now get rid of the current
free(current->pointer);
free(current);
return;
}
tmp = current;
current = current->next;
}
}
void mempool_clean(mempool *pool) {
mempool_allocation *current = pool->first_allocation;
mempool_allocation *tmp;
while (current != NULL) {
tmp = current->next;
free(current->pointer);
free(current);
current = tmp;
}
pool->first_allocation = NULL;
}