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