xref: /btstack/src/btstack_memory.c (revision c165515e5da7ce23cf724e023c6808f6ead9b2ac)
1 /*
2  * Copyright (C) 2011 by Matthias Ringwald
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  btstsack_memory.h
34  *
35  *  @brief BTstack memory management via configurable memory pools
36  *
37  *  @note code semi-atuomatically generated by btstack_memory_generate.py
38  *
39  */
40 
41 #include "btstack_memory.h"
42 #include <btstack/memory_pool.h>
43 
44 #include <stdlib.h>
45 
46 #include "../config.h"
47 #include "hci.h"
48 #include "l2cap.h"
49 #include "rfcomm.h"
50 
51 // MARK: hci_connection_t
52 #ifdef MAX_NO_HCI_CONNECTIONS
53 static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
54 static memory_pool_t hci_connection_pool;
55 void * btstack_memory_hci_connection_get(void){
56     return memory_pool_get(&hci_connection_pool);
57 }
58 void   btstack_memory_hci_connection_free(void *hci_connection){
59     memory_pool_free(&hci_connection_pool, hci_connection);
60 }
61 #elif defined(HAVE_MALLOC)
62 void * btstack_memory_hci_connection_get(void){
63     return malloc(sizeof(hci_connection_t));
64 }
65 void  btstack_memory_hci_connection_free(void *hci_connection){
66     free(hci_connection);
67 }
68 #else
69 #error BTstack needs at least one hci_connection_t, but neither MAX_NO_HCI_CONNECTIONS nor HAVE_MALLOC are defined
70 #endif
71 
72 
73 // MARK: l2cap_service_t
74 #ifdef MAX_NO_L2CAP_SERVICES
75 static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
76 static memory_pool_t l2cap_service_pool;
77 void * btstack_memory_l2cap_service_get(void){
78     return memory_pool_get(&l2cap_service_pool);
79 }
80 void   btstack_memory_l2cap_service_free(void *l2cap_service){
81     memory_pool_free(&l2cap_service_pool, l2cap_service);
82 }
83 #elif defined(HAVE_MALLOC)
84 void * btstack_memory_l2cap_service_get(void){
85     return malloc(sizeof(l2cap_service_t));
86 }
87 void  btstack_memory_l2cap_service_free(void *l2cap_service){
88     free(l2cap_service);
89 }
90 #endif
91 
92 
93 // MARK: l2cap_channel_t
94 #ifdef MAX_NO_L2CAP_CHANNELS
95 static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
96 static memory_pool_t l2cap_channel_pool;
97 void * btstack_memory_l2cap_channel_get(void){
98     return memory_pool_get(&l2cap_channel_pool);
99 }
100 void   btstack_memory_l2cap_channel_free(void *l2cap_channel){
101     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
102 }
103 #elif defined(HAVE_MALLOC)
104 void * btstack_memory_l2cap_channel_get(void){
105     return malloc(sizeof(l2cap_channel_t));
106 }
107 void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
108     free(l2cap_channel);
109 }
110 #else
111 #error BTstack needs at least one l2cap_channel_t, but neither MAX_NO_L2CAP_CHANNELS nor HAVE_MALLOC are defined
112 #endif
113 
114 
115 // MARK: rfcomm_multiplexer_t
116 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
117 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
118 static memory_pool_t rfcomm_multiplexer_pool;
119 void * btstack_memory_rfcomm_multiplexer_get(void){
120     return memory_pool_get(&rfcomm_multiplexer_pool);
121 }
122 void   btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
123     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
124 }
125 #elif defined(HAVE_MALLOC)
126 void * btstack_memory_rfcomm_multiplexer_get(void){
127     return malloc(sizeof(rfcomm_multiplexer_t));
128 }
129 void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
130     free(rfcomm_multiplexer);
131 }
132 #endif
133 
134 
135 // MARK: rfcomm_service_t
136 #ifdef MAX_NO_RFCOMM_SERVICES
137 static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
138 static memory_pool_t rfcomm_service_pool;
139 void * btstack_memory_rfcomm_service_get(void){
140     return memory_pool_get(&rfcomm_service_pool);
141 }
142 void   btstack_memory_rfcomm_service_free(void *rfcomm_service){
143     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
144 }
145 #elif defined(HAVE_MALLOC)
146 void * btstack_memory_rfcomm_service_get(void){
147     return malloc(sizeof(rfcomm_service_t));
148 }
149 void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
150     free(rfcomm_service);
151 }
152 #endif
153 
154 
155 // MARK: rfcomm_channel_t
156 #ifdef MAX_NO_RFCOMM_CHANNELS
157 static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
158 static memory_pool_t rfcomm_channel_pool;
159 void * btstack_memory_rfcomm_channel_get(void){
160     return memory_pool_get(&rfcomm_channel_pool);
161 }
162 void   btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
163     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
164 }
165 #elif defined(HAVE_MALLOC)
166 void * btstack_memory_rfcomm_channel_get(void){
167     return malloc(sizeof(rfcomm_channel_t));
168 }
169 void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
170     free(rfcomm_channel);
171 }
172 #endif
173 
174 // MARK: db_mem_device_t
175 #ifdef MAX_NO_DB_MEM_DEVICES
176 static db_mem_device_t db_mem_device_storage[MAX_NO_DB_MEM_DEVICES];
177 static memory_pool_t db_mem_device_pool;
178 void * btstack_memory_db_mem_device_get(void){
179     return memory_pool_get(&db_mem_device_pool);
180 }
181 void   btstack_memory_db_mem_device_free(void *db_mem_device){
182     memory_pool_free(&db_mem_device_pool, db_mem_device);
183 }
184 #elif defined(HAVE_MALLOC)
185 void * btstack_memory_db_mem_device_get(void){
186     return malloc(sizeof(db_mem_device_t));
187 }
188 void  btstack_memory_db_mem_device_free(void *db_mem_device){
189     free(db_mem_device);
190 }
191 #endif
192 
193 
194 // MARK: db_mem_service_t
195 #ifdef MAX_NO_DB_MEM_SERVICES
196 static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
197 static memory_pool_t db_mem_service_pool;
198 void * btstack_memory_db_mem_service_get(void){
199     return memory_pool_get(&db_mem_service_pool);
200 }
201 void   btstack_memory_db_mem_service_free(void *db_mem_service){
202     memory_pool_free(&db_mem_service_pool, db_mem_service);
203 }
204 #elif defined(HAVE_MALLOC)
205 void * btstack_memory_db_mem_service_get(void){
206     return malloc(sizeof(db_mem_service_t));
207 }
208 void  btstack_memory_db_mem_service_free(void *db_mem_service){
209     free(db_mem_service);
210 }
211 #endif
212 
213 
214 // init
215 void btstack_memory_init(void){
216 #ifdef MAX_NO_HCI_CONNECTIONS
217     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
218 #endif
219 #ifdef MAX_NO_L2CAP_SERVICES
220     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
221 #endif
222 #ifdef MAX_NO_L2CAP_CHANNELS
223     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
224 #endif
225 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
226     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
227 #endif
228 #ifdef MAX_NO_RFCOMM_SERVICES
229     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
230 #endif
231 #ifdef MAX_NO_RFCOMM_CHANNELS
232     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
233 #endif
234 #ifdef MAX_NO_DB_MEM_DEVICES
235     memory_pool_create(&db_mem_device_pool, db_mem_device_storage, MAX_NO_DB_MEM_DEVICES, sizeof(db_mem_device_t));
236 #endif
237 #ifdef MAX_NO_DB_MEM_SERVICES
238     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
239 #endif
240 }
241