-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtstack_memory.c
More file actions
345 lines (323 loc) · 12 KB
/
btstack_memory.c
File metadata and controls
345 lines (323 loc) · 12 KB
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
/*
* Copyright (C) 2009-2012 by Matthias Ringwald
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at [email protected]
*
*/
/*
* btstsack_memory.h
*
* @brief BTstack memory management via configurable memory pools
*
* @note code semi-atuomatically generated by btstack_memory_generator.py
*
*/
#include "btstack_memory.h"
#include <btstack/memory_pool.h>
#include <stdlib.h>
#include "config.h"
#include "hci.h"
#include "l2cap.h"
#include "rfcomm.h"
// MARK: hci_connection_t
#ifdef MAX_NO_HCI_CONNECTIONS
#if MAX_NO_HCI_CONNECTIONS > 0
static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
static memory_pool_t hci_connection_pool;
void * btstack_memory_hci_connection_get(void){
return memory_pool_get(&hci_connection_pool);
}
void btstack_memory_hci_connection_free(void *hci_connection){
memory_pool_free(&hci_connection_pool, hci_connection);
}
#else
void * btstack_memory_hci_connection_get(void){
return NULL;
}
void btstack_memory_hci_connection_free(void *hci_connection){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_hci_connection_get(void){
return malloc(sizeof(hci_connection_t));
}
void btstack_memory_hci_connection_free(void *hci_connection){
free(hci_connection);
}
#else error "The struct hci_connection has neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS defined. Please, edit the config file."
#endif
// MARK: l2cap_service_t
#ifdef MAX_NO_L2CAP_SERVICES
#if MAX_NO_L2CAP_SERVICES > 0
static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
static memory_pool_t l2cap_service_pool;
void * btstack_memory_l2cap_service_get(void){
return memory_pool_get(&l2cap_service_pool);
}
void btstack_memory_l2cap_service_free(void *l2cap_service){
memory_pool_free(&l2cap_service_pool, l2cap_service);
}
#else
void * btstack_memory_l2cap_service_get(void){
return NULL;
}
void btstack_memory_l2cap_service_free(void *l2cap_service){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_l2cap_service_get(void){
return malloc(sizeof(l2cap_service_t));
}
void btstack_memory_l2cap_service_free(void *l2cap_service){
free(l2cap_service);
}
#else error "The struct l2cap_service has neither HAVE_MALLOC nor MAX_NO_L2CAP_SERVICES defined. Please, edit the config file."
#endif
// MARK: l2cap_channel_t
#ifdef MAX_NO_L2CAP_CHANNELS
#if MAX_NO_L2CAP_CHANNELS > 0
static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
static memory_pool_t l2cap_channel_pool;
void * btstack_memory_l2cap_channel_get(void){
return memory_pool_get(&l2cap_channel_pool);
}
void btstack_memory_l2cap_channel_free(void *l2cap_channel){
memory_pool_free(&l2cap_channel_pool, l2cap_channel);
}
#else
void * btstack_memory_l2cap_channel_get(void){
return NULL;
}
void btstack_memory_l2cap_channel_free(void *l2cap_channel){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_l2cap_channel_get(void){
return malloc(sizeof(l2cap_channel_t));
}
void btstack_memory_l2cap_channel_free(void *l2cap_channel){
free(l2cap_channel);
}
#else error "The struct l2cap_channel has neither HAVE_MALLOC nor MAX_NO_L2CAP_CHANNELS defined. Please, edit the config file."
#endif
// MARK: rfcomm_multiplexer_t
#ifdef MAX_NO_RFCOMM_MULTIPLEXERS
#if MAX_NO_RFCOMM_MULTIPLEXERS > 0
static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
static memory_pool_t rfcomm_multiplexer_pool;
void * btstack_memory_rfcomm_multiplexer_get(void){
return memory_pool_get(&rfcomm_multiplexer_pool);
}
void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
}
#else
void * btstack_memory_rfcomm_multiplexer_get(void){
return NULL;
}
void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_rfcomm_multiplexer_get(void){
return malloc(sizeof(rfcomm_multiplexer_t));
}
void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
free(rfcomm_multiplexer);
}
#else error "The struct rfcomm_multiplexer has neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS defined. Please, edit the config file."
#endif
// MARK: rfcomm_service_t
#ifdef MAX_NO_RFCOMM_SERVICES
#if MAX_NO_RFCOMM_SERVICES > 0
static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
static memory_pool_t rfcomm_service_pool;
void * btstack_memory_rfcomm_service_get(void){
return memory_pool_get(&rfcomm_service_pool);
}
void btstack_memory_rfcomm_service_free(void *rfcomm_service){
memory_pool_free(&rfcomm_service_pool, rfcomm_service);
}
#else
void * btstack_memory_rfcomm_service_get(void){
return NULL;
}
void btstack_memory_rfcomm_service_free(void *rfcomm_service){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_rfcomm_service_get(void){
return malloc(sizeof(rfcomm_service_t));
}
void btstack_memory_rfcomm_service_free(void *rfcomm_service){
free(rfcomm_service);
}
#else error "The struct rfcomm_service has neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES defined. Please, edit the config file."
#endif
// MARK: rfcomm_channel_t
#ifdef MAX_NO_RFCOMM_CHANNELS
#if MAX_NO_RFCOMM_CHANNELS > 0
static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
static memory_pool_t rfcomm_channel_pool;
void * btstack_memory_rfcomm_channel_get(void){
return memory_pool_get(&rfcomm_channel_pool);
}
void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
}
#else
void * btstack_memory_rfcomm_channel_get(void){
return NULL;
}
void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_rfcomm_channel_get(void){
return malloc(sizeof(rfcomm_channel_t));
}
void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
free(rfcomm_channel);
}
#else error "The struct rfcomm_channel has neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS defined. Please, edit the config file."
#endif
// MARK: db_mem_device_name_t
#ifdef MAX_NO_DB_MEM_DEVICE_NAMES
#if MAX_NO_DB_MEM_DEVICE_NAMES > 0
static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
static memory_pool_t db_mem_device_name_pool;
void * btstack_memory_db_mem_device_name_get(void){
return memory_pool_get(&db_mem_device_name_pool);
}
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
}
#else
void * btstack_memory_db_mem_device_name_get(void){
return NULL;
}
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_db_mem_device_name_get(void){
return malloc(sizeof(db_mem_device_name_t));
}
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
free(db_mem_device_name);
}
#else error "The struct db_mem_device_name has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES defined. Please, edit the config file."
#endif
// MARK: db_mem_device_link_key_t
#ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
#if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
static memory_pool_t db_mem_device_link_key_pool;
void * btstack_memory_db_mem_device_link_key_get(void){
return memory_pool_get(&db_mem_device_link_key_pool);
}
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
}
#else
void * btstack_memory_db_mem_device_link_key_get(void){
return NULL;
}
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_db_mem_device_link_key_get(void){
return malloc(sizeof(db_mem_device_link_key_t));
}
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
free(db_mem_device_link_key);
}
#else error "The struct db_mem_device_link_key has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS defined. Please, edit the config file."
#endif
// MARK: db_mem_service_t
#ifdef MAX_NO_DB_MEM_SERVICES
#if MAX_NO_DB_MEM_SERVICES > 0
static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
static memory_pool_t db_mem_service_pool;
void * btstack_memory_db_mem_service_get(void){
return memory_pool_get(&db_mem_service_pool);
}
void btstack_memory_db_mem_service_free(void *db_mem_service){
memory_pool_free(&db_mem_service_pool, db_mem_service);
}
#else
void * btstack_memory_db_mem_service_get(void){
return NULL;
}
void btstack_memory_db_mem_service_free(void *db_mem_service){
};
#endif
#elif defined(HAVE_MALLOC)
void * btstack_memory_db_mem_service_get(void){
return malloc(sizeof(db_mem_service_t));
}
void btstack_memory_db_mem_service_free(void *db_mem_service){
free(db_mem_service);
}
#else error "The struct db_mem_service has neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES defined. Please, edit the config file."
#endif
// init
void btstack_memory_init(void){
#if MAX_NO_HCI_CONNECTIONS > 0
memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
#endif
#if MAX_NO_L2CAP_SERVICES > 0
memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
#endif
#if MAX_NO_L2CAP_CHANNELS > 0
memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
#endif
#if MAX_NO_RFCOMM_MULTIPLEXERS > 0
memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
#endif
#if MAX_NO_RFCOMM_SERVICES > 0
memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
#endif
#if MAX_NO_RFCOMM_CHANNELS > 0
memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
#endif
#if MAX_NO_DB_MEM_DEVICE_NAMES > 0
memory_pool_create(&db_mem_device_name_pool, db_mem_device_name_storage, MAX_NO_DB_MEM_DEVICE_NAMES, sizeof(db_mem_device_name_t));
#endif
#if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
memory_pool_create(&db_mem_device_link_key_pool, db_mem_device_link_key_storage, MAX_NO_DB_MEM_DEVICE_LINK_KEYS, sizeof(db_mem_device_link_key_t));
#endif
#if MAX_NO_DB_MEM_SERVICES > 0
memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
#endif
}