-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmpool.c
165 lines (147 loc) · 3.91 KB
/
mpool.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
/*
* rv32emu is freely redistributable under the MIT License. See the file
* "LICENSE" for information on usage and redistribution of this file.
*/
#include <stdlib.h>
#include <string.h>
#if HAVE_MMAP
#include <sys/mman.h>
#include <unistd.h>
#endif
#include "mpool.h"
typedef struct memchunk {
struct memchunk *next;
} memchunk_t;
typedef struct area {
char *mapped;
struct area *next;
} area_t;
typedef struct mpool {
size_t chunk_count;
size_t page_count;
size_t chunk_size;
struct memchunk *free_chunk_head;
area_t area;
} mpool_t;
static void *mem_arena(size_t sz)
{
void *p;
#if HAVE_MMAP
p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (p == MAP_FAILED)
return NULL;
#else
p = malloc(sz);
if (!p)
return NULL;
#endif
return p;
}
mpool_t *mpool_create(size_t pool_size, size_t chunk_size)
{
mpool_t *new_mp = malloc(sizeof(mpool_t));
if (!new_mp)
return NULL;
new_mp->area.next = NULL;
size_t pgsz = getpagesize();
if (pool_size < chunk_size + sizeof(memchunk_t))
pool_size += sizeof(memchunk_t);
size_t page_count = (pool_size + pgsz - 1) / pgsz;
char *p = mem_arena(page_count * pgsz);
if (!p) {
free(new_mp);
return NULL;
}
new_mp->area.mapped = p;
new_mp->page_count = page_count;
new_mp->chunk_count = pool_size / (sizeof(memchunk_t) + chunk_size);
new_mp->chunk_size = chunk_size;
new_mp->free_chunk_head = (memchunk_t *) p;
memchunk_t *cur = new_mp->free_chunk_head;
for (size_t i = 0; i < new_mp->chunk_count - 1; i++) {
cur->next =
(memchunk_t *) ((char *) cur + (sizeof(memchunk_t) + chunk_size));
cur = cur->next;
}
cur->next = NULL;
return new_mp;
}
static void *mpool_extend(mpool_t *mp)
{
size_t pool_size = mp->page_count * getpagesize();
char *p = mem_arena(pool_size);
if (!p)
return NULL;
area_t *new_area = malloc(sizeof(area_t));
if (!new_area)
return NULL;
new_area->mapped = p;
new_area->next = NULL;
size_t chunk_count = pool_size / (sizeof(memchunk_t) + mp->chunk_size);
mp->free_chunk_head = (memchunk_t *) p;
memchunk_t *cur = mp->free_chunk_head;
for (size_t i = 0; i < chunk_count - 1; i++) {
cur->next = (memchunk_t *) ((char *) cur +
(sizeof(memchunk_t) + mp->chunk_size));
cur = cur->next;
}
mp->chunk_count += chunk_count;
/* insert new mapped */
area_t *cur_area = &mp->area;
while (cur_area->next)
cur_area = cur_area->next;
cur_area->next = new_area;
return p;
}
FORCE_INLINE void *mpool_alloc_helper(mpool_t *mp)
{
char *ptr = (char *) mp->free_chunk_head + sizeof(memchunk_t);
mp->free_chunk_head = mp->free_chunk_head->next;
mp->chunk_count--;
return ptr;
}
void *mpool_alloc(mpool_t *mp)
{
if (!mp->chunk_count && !(mpool_extend(mp)))
return NULL;
return mpool_alloc_helper(mp);
}
void *mpool_calloc(mpool_t *mp)
{
if (!mp->chunk_count && !(mpool_extend(mp)))
return NULL;
char *ptr = mpool_alloc_helper(mp);
memset(ptr, 0, mp->chunk_size);
return ptr;
}
void mpool_free(mpool_t *mp, void *target)
{
memchunk_t *ptr = (memchunk_t *) ((char *) target - sizeof(memchunk_t));
ptr->next = mp->free_chunk_head;
mp->free_chunk_head = ptr;
mp->chunk_count++;
}
void mpool_destroy(mpool_t *mp)
{
#if HAVE_MMAP
size_t mem_size = mp->page_count * getpagesize();
area_t *cur = &mp->area, *tmp = NULL;
while (cur) {
tmp = cur;
cur = cur->next;
munmap(tmp->mapped, mem_size);
if (tmp != &mp->area)
free(tmp);
}
#else
area_t *cur = &mp->area, *tmp = NULL;
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp->mapped);
if (tmp != &mp->area)
free(tmp);
}
#endif
free(mp);
}