-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared_memory.c
286 lines (217 loc) · 7.7 KB
/
shared_memory.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
//
// Created by thomas on 19/11/18.
//
#include "shared_memory.h"
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "tools_ubpf_api.h"
#include "tommy.h"
/**
* BUMP ALLOC
* Stack like allocation for plugins that do not want to
* save the memory between two consecutive calls
*/
void init_bump(bump_t *pool, uint32_t mem_size, uint8_t *mem_area) {
if (!pool) return;
pool->total_mem = mem_size;
pool->available_size = mem_size;
pool->mem_start = mem_area;
pool->next_avail = mem_area;
}
void *bump_alloc(bump_t *pool, size_t size) {
if (!pool) return NULL;
void *ptr;
size_t aligned_size = (size + 7u) & (-8u);
if (aligned_size > pool->available_size) return NULL;
ptr = pool->next_avail;
pool->next_avail += aligned_size;
return ptr;
}
void reset_bump(bump_t *pool) {
if (!pool) return;
pool->available_size = pool->total_mem;
pool->next_avail = pool->mem_start;
}
/** MEMORY MANAGEMENT */
static inline void michelfra_init(mem_context_t *pool, void *mem_area, uint32_t mem_size) {
memset(pool, 0, sizeof(*pool));
pool->michelfra.memory_start = mem_area;
pool->michelfra.memory_max_size = mem_size;
pool->michelfra.memory_current_end = mem_area;
}
static inline void *michelfra_malloc(mem_context_t *pool, size_t size) {
return michelfralloc(&pool->michelfra, size);
}
static inline void michelfra_free(mem_context_t *pool, void *ptr) {
return michelfree(&pool->michelfra, ptr);
}
static inline void *michelfra_realloc(mem_context_t *pool, void *ptr, size_t size) {
return michelfrealloc(&pool->michelfra, ptr, size);
}
static inline void michelfra_reset(mem_context_t *pool) {
pool->michelfra.memory_current_end = pool->michelfra.memory_start;
memset(&pool->michelfra.dlmalloc_state, 0, sizeof(pool->michelfra.dlmalloc_state));
}
static void bump_init(mem_context_t *pool, void *mem_area, uint32_t mem_size) {
memset(pool, 0, sizeof(*pool));
init_bump(&pool->bump, mem_size, mem_area);
}
static inline void *bump_malloc(mem_context_t *pool, size_t size) {
return bump_alloc(&pool->bump, size);
}
static inline void bump_free(mem_context_t *pool UNUSED, void *ptr UNUSED) {
// bump is stack like. Thus, unable to free
// memory
}
static inline void *bump_realloc(mem_context_t *pool UNUSED, void *ptr UNUSED, size_t size UNUSED) {
return NULL; // realloc unsupported for bump memory
}
static inline void bump_reset(mem_context_t *pool) {
reset_bump(&pool->bump);
}
static const struct memory_manager mem_mgrs[] = {
[MICHELFRA_MEM] = {
.malloc = michelfra_malloc,
.free = michelfra_free,
.realloc = michelfra_realloc,
.init = michelfra_init,
.reset = michelfra_reset,
.usable = 0, // make sure this field is
// set to zero (will be set to 1 if
// michelfra is init)
},
[BUMP_MEM] = {
.malloc = bump_malloc,
.free = bump_free,
.realloc = bump_realloc,
.reset = bump_reset,
.init = bump_init,
.usable = 0, // same as above
}
};
static struct {
const char *str;
size_t str_len;
mem_type_t type;
} memtype_info[] = {
{.str = "michelfra", .type = MICHELFRA_MEM, .str_len = 9},
{.str = "bump", .type = BUMP_MEM, .str_len = 4}
};
mem_type_t str_memtype_to_enum(const char *memtype) {
size_t array_len;
size_t i;
array_len = sizeof(memtype_info) / sizeof(memtype_info[0]);
for (i = 0; i < array_len; i++) {
if (strncmp(memtype_info[i].str, memtype, memtype_info[i].str_len) == 0) {
return memtype_info[i].type;
}
}
return MIN_MEM;
}
int init_memory_manager(struct memory_manager *mgr, mem_type_t mem_type) {
if (mem_type <= MIN_MEM || mem_type >= MAX_MEM) {
return -1;
}
memcpy(mgr, &mem_mgrs[mem_type], sizeof(*mgr));
switch (mem_type) {
case MICHELFRA_MEM:
memset(&mgr->memory_ctx.michelfra, 0, sizeof(mgr->memory_ctx.michelfra));
break;
case BUMP_MEM:
memset(&mgr->memory_ctx.bump, 0, sizeof(mgr->memory_ctx.bump));
break;
default:
return -1;
}
mgr->usable = 1;
return 0;
}
void destroy_memory_manager(struct memory_manager *mgr UNUSED) {
// for now, nothing is dynamically allocated inside the
// memory manager
return;
}
void mem_init(struct memory_manager *mgr, void *start_mem, size_t tot_mem_size) {
if (!mem_mgr_initialized(mgr)) return;
return mgr->init(&mgr->memory_ctx, start_mem, tot_mem_size);
}
void *mem_alloc(struct memory_manager *mgr, size_t size) {
if (!mem_mgr_initialized(mgr)) return NULL;
return mgr->malloc(&mgr->memory_ctx, size);
}
void *mem_realloc(struct memory_manager *mgr, void *ptr, size_t size) {
if (!mem_mgr_initialized(mgr)) return NULL;
return mgr->realloc(&mgr->memory_ctx, ptr, size);
}
void mem_free(struct memory_manager *mgr, void *ptr) {
if (!mem_mgr_initialized(mgr)) return;
mgr->free(&mgr->memory_ctx, ptr);
}
void mem_reset(struct memory_manager *mgr) {
if (!mem_mgr_initialized(mgr)) return;
mgr->reset(&mgr->memory_ctx);
}
///* shared memory related functions *///
static inline int search_shared_block(const void *arg, const void *obj) {
const int *arg_id = arg;
const map_shared_t *elem = obj;
return *arg_id != elem->id;
}
void init_shared_hash(tommy_hashdyn *hashdyn) {
if (!hashdyn) return;
tommy_hashdyn_init(hashdyn);
}
void *shared_new(struct memory_manager *mgr, tommy_hashdyn *shared, key_t key, size_t size) {
uint8_t *block;
map_shared_t *shared_block;
uint32_t hash_key = tommy_inthash_u32(key);
shared_block = tommy_hashdyn_search(shared, search_shared_block,
&key, hash_key);
if (shared_block) return NULL; // key already assigned, this is so sad :/
block = mgr->malloc(&mgr->memory_ctx, size);
if (!block) {
fprintf(stderr, "Unable to allocate memory\n");
return NULL;
}
shared_block = calloc(sizeof(map_shared_t), 1);
if (!shared_block) {
mgr->free(&mgr->memory_ctx, block);
return NULL;
}
shared_block->id = key;
shared_block->data = block;
tommy_hashdyn_insert(shared, &shared_block->hash_node,
shared_block, hash_key);
return block;
}
void *shared_get(struct memory_manager *mgr UNUSED, tommy_hashdyn *shared, key_t key) {
map_shared_t *shared_block;
uint32_t hash_key = tommy_inthash_u32(key);
shared_block = tommy_hashdyn_search(shared, search_shared_block,
&key, hash_key);
if (!shared_block) return NULL; // key not assigned in a shared block;
return shared_block->data;
}
void shared_rm(struct memory_manager *mgr, tommy_hashdyn *shared, key_t key) {
map_shared_t *shared_block;
uint32_t hash_key = tommy_inthash_u32(key);
shared_block = tommy_hashdyn_search(shared, search_shared_block,
&key, hash_key);
if (!shared_block) return;
mgr->free(&mgr->memory_ctx, shared_block->data);
tommy_hashdyn_remove_existing(shared, &shared_block->hash_node);
free(shared_block);
}
static inline void free_map_shared_t(void *mgr, void* obj) {
map_shared_t *ms = obj;
struct memory_manager *manager = mgr;
manager->free(&manager->memory_ctx, ms->data);
free(ms);
}
void destroy_shared_map(struct memory_manager *mgr, tommy_hashdyn *shared) {
if (!shared) return;
tommy_hashdyn_foreach_arg(shared, free_map_shared_t, mgr);
tommy_hashdyn_done(shared);
}