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
48
49
50
51
52
53
54
57
58
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
92
93
94
95
96
/* ... */
#define BTSTACK_FILE__ "btstack_memory_pool.c"
/* ... */
#include "btstack_memory_pool.h"
#include <stddef.h>
#include "btstack_debug.h"
typedef struct node {
struct node * next;
...} node_t;
void btstack_memory_pool_create(btstack_memory_pool_t *pool, void * storage, int count, int block_size){
node_t *free_blocks = (node_t*) pool;
char *mem_ptr = (char *) storage;
int i;
free_blocks->next = NULL;
for (i = 0 ; i < count ; i++){
btstack_memory_pool_free(pool, mem_ptr);
mem_ptr += block_size;
}for (i = 0 ; i < count ; i++) { ... }
}{ ... }
void * btstack_memory_pool_get(btstack_memory_pool_t *pool){
node_t *free_blocks = (node_t*) pool;
if (!free_blocks->next) return NULL;
node_t *node = free_blocks->next;
free_blocks->next = node->next;
return (void*) node;
}{ ... }
void btstack_memory_pool_free(btstack_memory_pool_t *pool, void * block){
node_t *free_blocks = (node_t*) pool;
node_t *node = (node_t*) block;
node_t * it;
for (it = free_blocks->next; it != NULL; it = it->next){
btstack_assert(it != node);
}for (it = free_blocks->next; it != NULL; it = it->next) { ... }
node->next = free_blocks->next;
free_blocks->next = node;
}{ ... }