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 #else 91 #error BTstack needs at least one l2cap_service_t, but neither MAX_NO_L2CAP_SERVICES nor HAVE_MALLOC are defined 92 #endif 93 94 95 // MARK: l2cap_channel_t 96 #ifdef MAX_NO_L2CAP_CHANNELS 97 static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS]; 98 static memory_pool_t l2cap_channel_pool; 99 void * btstack_memory_l2cap_channel_get(void){ 100 return memory_pool_get(&l2cap_channel_pool); 101 } 102 void btstack_memory_l2cap_channel_free(void *l2cap_channel){ 103 memory_pool_free(&l2cap_channel_pool, l2cap_channel); 104 } 105 #elif defined(HAVE_MALLOC) 106 void * btstack_memory_l2cap_channel_get(void){ 107 return malloc(sizeof(l2cap_channel_t)); 108 } 109 void btstack_memory_l2cap_channel_free(void *l2cap_channel){ 110 free(l2cap_channel); 111 } 112 #else 113 #error BTstack needs at least one l2cap_channel_t, but neither MAX_NO_L2CAP_CHANNELS nor HAVE_MALLOC are defined 114 #endif 115 116 117 // MARK: rfcomm_multiplexer_t 118 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS 119 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS]; 120 static memory_pool_t rfcomm_multiplexer_pool; 121 void * btstack_memory_rfcomm_multiplexer_get(void){ 122 return memory_pool_get(&rfcomm_multiplexer_pool); 123 } 124 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ 125 memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer); 126 } 127 #elif defined(HAVE_MALLOC) 128 void * btstack_memory_rfcomm_multiplexer_get(void){ 129 return malloc(sizeof(rfcomm_multiplexer_t)); 130 } 131 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ 132 free(rfcomm_multiplexer); 133 } 134 #else 135 #error BTstack needs at least one rfcomm_multiplexer_t, but neither MAX_NO_RFCOMM_MULTIPLEXERS nor HAVE_MALLOC are defined 136 #endif 137 138 139 // MARK: rfcomm_service_t 140 #ifdef MAX_NO_RFCOMM_SERVICES 141 static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES]; 142 static memory_pool_t rfcomm_service_pool; 143 void * btstack_memory_rfcomm_service_get(void){ 144 return memory_pool_get(&rfcomm_service_pool); 145 } 146 void btstack_memory_rfcomm_service_free(void *rfcomm_service){ 147 memory_pool_free(&rfcomm_service_pool, rfcomm_service); 148 } 149 #elif defined(HAVE_MALLOC) 150 void * btstack_memory_rfcomm_service_get(void){ 151 return malloc(sizeof(rfcomm_service_t)); 152 } 153 void btstack_memory_rfcomm_service_free(void *rfcomm_service){ 154 free(rfcomm_service); 155 } 156 #else 157 #error BTstack needs at least one rfcomm_service_t, but neither MAX_NO_RFCOMM_SERVICES nor HAVE_MALLOC are defined 158 #endif 159 160 161 // MARK: rfcomm_channel_t 162 #ifdef MAX_NO_RFCOMM_CHANNELS 163 static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS]; 164 static memory_pool_t rfcomm_channel_pool; 165 void * btstack_memory_rfcomm_channel_get(void){ 166 return memory_pool_get(&rfcomm_channel_pool); 167 } 168 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ 169 memory_pool_free(&rfcomm_channel_pool, rfcomm_channel); 170 } 171 #elif defined(HAVE_MALLOC) 172 void * btstack_memory_rfcomm_channel_get(void){ 173 return malloc(sizeof(rfcomm_channel_t)); 174 } 175 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ 176 free(rfcomm_channel); 177 } 178 #else 179 #error BTstack needs at least one rfcomm_channel_t, but neither MAX_NO_RFCOMM_CHANNELS nor HAVE_MALLOC are defined 180 #endif 181 182 // init 183 void btstack_memory_init(void){ 184 #ifdef MAX_NO_HCI_CONNECTIONS 185 memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t)); 186 #endif 187 #ifdef MAX_NO_L2CAP_SERVICES 188 memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t)); 189 #endif 190 #ifdef MAX_NO_L2CAP_CHANNELS 191 memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t)); 192 #endif 193 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS 194 memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t)); 195 #endif 196 #ifdef MAX_NO_RFCOMM_SERVICES 197 memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t)); 198 #endif 199 #ifdef MAX_NO_RFCOMM_CHANNELS 200 memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); 201 #endif 202 } 203