diff --git a/nimble/host/src/ble_att_svr.c b/nimble/host/src/ble_att_svr.c index 94333b235..c3b54c9b1 100644 --- a/nimble/host/src/ble_att_svr.c +++ b/nimble/host/src/ble_att_svr.c @@ -3409,6 +3409,11 @@ ble_att_svr_reset(void) { struct ble_att_svr_entry *entry; +#if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) + if (ble_att_svr_ctx == NULL) { + return; + } +#endif while ((entry = STAILQ_FIRST(&ble_att_svr_list)) != NULL) { STAILQ_REMOVE_HEAD(&ble_att_svr_list, ha_next); ble_att_svr_entry_free(entry); @@ -3438,6 +3443,7 @@ ble_att_svr_free_start_mem(void) nimble_platform_mem_free(ble_att_svr_entry_mem); ble_att_svr_entry_mem = NULL; } + os_mempool_unregister(&ble_att_svr_entry_pool); #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) memset(&ble_att_svr_entry_pool, 0, sizeof(ble_att_svr_entry_pool)); #endif @@ -3458,7 +3464,7 @@ ble_att_svr_start(void) ble_att_svr_free_start_mem(); if (ble_hs_max_attrs > 0) { - #if !MYNEWT_VAL(MP_RUNTIME_ALLOC) +#if !MYNEWT_VAL(MP_RUNTIME_ALLOC) ble_att_svr_entry_mem = nimble_platform_mem_calloc(1, OS_MEMPOOL_BYTES(ble_hs_max_attrs, sizeof (struct ble_att_svr_entry))); @@ -3466,8 +3472,7 @@ ble_att_svr_start(void) rc = BLE_HS_ENOMEM; goto err; } - #endif - +#endif rc = os_mempool_init(&ble_att_svr_entry_pool, ble_hs_max_attrs, sizeof (struct ble_att_svr_entry), ble_att_svr_entry_mem, "ble_att_svr_entry_pool"); @@ -3499,6 +3504,7 @@ ble_att_svr_deinit(void) nimble_platform_mem_free(ble_att_svr_prep_entry_mem); ble_att_svr_prep_entry_mem = NULL; } + os_mempool_unregister(&ble_att_svr_prep_entry_pool); memset(&ble_att_svr_prep_entry_pool, 0, sizeof(ble_att_svr_prep_entry_pool)); ble_att_svr_free_start_mem(); @@ -3534,7 +3540,7 @@ ble_att_svr_init(void) if (!ble_att_svr_prep_entry_mem) { return BLE_HS_ENOMEM; } -#endif +#endif // !MYNEWT_VAL(MP_RUNTIME_ALLOC) #endif rc = os_mempool_init(&ble_att_svr_prep_entry_pool, MYNEWT_VAL(BLE_ATT_SVR_MAX_PREP_ENTRIES), @@ -3547,6 +3553,7 @@ ble_att_svr_init(void) nimble_platform_mem_free(ble_att_svr_prep_entry_mem); ble_att_svr_prep_entry_mem = NULL; #endif + os_mempool_unregister(&ble_att_svr_prep_entry_pool); memset(&ble_att_svr_prep_entry_pool, 0, sizeof(ble_att_svr_prep_entry_pool)); #endif diff --git a/nimble/host/src/ble_eatt.c b/nimble/host/src/ble_eatt.c index fd6d816d0..ef8b3ae6d 100644 --- a/nimble/host/src/ble_eatt.c +++ b/nimble/host/src/ble_eatt.c @@ -675,6 +675,8 @@ void ble_eatt_deinit(void) ble_eatt_conn_mem = NULL; } #endif + os_mempool_unregister(&ble_eatt_sdu_mbuf_mempool); + os_mempool_unregister(&ble_eatt_conn_pool); nimble_platform_mem_free(ble_eatt_ctx); ble_eatt_ctx = NULL; } diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c index 00dd91fc4..ad34d3bec 100644 --- a/nimble/host/src/ble_gap.c +++ b/nimble/host/src/ble_gap.c @@ -9614,6 +9614,7 @@ err: ble_gap_update_entry_mem = NULL; } #endif + os_mempool_unregister(&ble_gap_update_entry_pool); nimble_platform_mem_free(ble_gap_vars); ble_gap_vars = NULL; } @@ -10059,6 +10060,7 @@ ble_gap_deinit(void) ble_gap_update_entry_mem = NULL; } #endif + os_mempool_unregister(&ble_gap_update_entry_pool); nimble_platform_mem_free(ble_gap_vars); ble_gap_vars = NULL; } diff --git a/nimble/host/src/ble_gattc.c b/nimble/host/src/ble_gattc.c index 68d9fbfc2..aaf913542 100644 --- a/nimble/host/src/ble_gattc.c +++ b/nimble/host/src/ble_gattc.c @@ -6540,6 +6540,7 @@ ble_gattc_init(void) nimble_platform_mem_free(ble_gattc_proc_mem); ble_gattc_proc_mem = NULL; #endif + os_mempool_unregister(&ble_gattc_proc_pool); memset(&ble_gattc_proc_pool, 0, sizeof(ble_gattc_proc_pool)); return rc; } @@ -6600,6 +6601,7 @@ ble_gattc_deinit(void) ble_gattc_proc_mem = NULL; } #endif + os_mempool_unregister(&ble_gattc_proc_pool); memset(&ble_gattc_proc_pool, 0, sizeof(ble_gattc_proc_pool)); STAILQ_INIT(&ble_gattc_procs); #if MYNEWT_VAL(BLE_GATTC_AUTO_PAIR) diff --git a/nimble/host/src/ble_gattc_cache_conn.c b/nimble/host/src/ble_gattc_cache_conn.c index bdbbd210e..3468a56bb 100644 --- a/nimble/host/src/ble_gattc_cache_conn.c +++ b/nimble/host/src/ble_gattc_cache_conn.c @@ -2227,28 +2227,33 @@ ble_gattc_cache_conn_free_mem(void) nimble_platform_mem_free(ble_gattc_cache_conn_mem); ble_gattc_cache_conn_mem = NULL; } + os_mempool_unregister(&ble_gattc_cache_conn_pool); if (ble_gattc_cache_conn_svc_mem) { nimble_platform_mem_free(ble_gattc_cache_conn_svc_mem); ble_gattc_cache_conn_svc_mem = NULL; } + os_mempool_unregister(&ble_gattc_cache_conn_svc_pool); #if MYNEWT_VAL(BLE_GATT_CACHING_INCLUDE_SERVICES) if (ble_gattc_cache_conn_incl_svc_mem) { nimble_platform_mem_free(ble_gattc_cache_conn_incl_svc_mem); ble_gattc_cache_conn_incl_svc_mem = NULL; } + os_mempool_unregister(&ble_gattc_cache_conn_incl_svc_pool); #endif if (ble_gattc_cache_conn_chr_mem) { nimble_platform_mem_free(ble_gattc_cache_conn_chr_mem); ble_gattc_cache_conn_chr_mem = NULL; } + os_mempool_unregister(&ble_gattc_cache_conn_chr_pool); if (ble_gattc_cache_conn_dsc_mem) { nimble_platform_mem_free(ble_gattc_cache_conn_dsc_mem); ble_gattc_cache_conn_dsc_mem = NULL; } + os_mempool_unregister(&ble_gattc_cache_conn_dsc_pool); #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) } diff --git a/nimble/host/src/ble_gatts.c b/nimble/host/src/ble_gatts.c index 3f8870662..352b47ce9 100644 --- a/nimble/host/src/ble_gatts.c +++ b/nimble/host/src/ble_gatts.c @@ -1747,6 +1747,7 @@ ble_gatts_free_mem(void) #endif nimble_platform_mem_free(ble_gatts_clt_cfg_mem); ble_gatts_clt_cfg_mem = NULL; + os_mempool_unregister(&ble_gatts_clt_cfg_pool); #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) } #endif @@ -1764,6 +1765,7 @@ ble_gatts_free_mem(void) nimble_platform_mem_free(ble_gatts_svc_entry_mem); ble_gatts_svc_entry_mem = NULL; } + os_mempool_unregister(&ble_gatts_svc_entry_pool); #else #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_hs_max_services > 0) { @@ -1862,6 +1864,10 @@ ble_gatts_start(void) #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_gatts_conn_aware_states == NULL) { ble_gatts_conn_aware_states = nimble_platform_mem_calloc(1, sizeof(struct ble_gatts_aware_state) * MYNEWT_VAL(BLE_STORE_MAX_BONDS)); + if (ble_gatts_conn_aware_states == NULL) { + rc = BLE_HS_ENOMEM; + goto done; + } } #else memset(ble_gatts_conn_aware_states, 0, sizeof ble_gatts_conn_aware_states); @@ -1925,6 +1931,7 @@ ble_gatts_start(void) goto done; } } + ble_gatts_free_svc_defs(); if (ble_gatts_num_cfgable_chrs == 0) { @@ -3305,7 +3312,7 @@ ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs) #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_gatts_ensure_ctx()) { - rc = BLE_HS_ENOMEM; + rc = BLE_HS_ENOMEM; goto done; } #endif diff --git a/nimble/host/src/ble_hs.c b/nimble/host/src/ble_hs.c index 4145d9d18..539278aea 100644 --- a/nimble/host/src/ble_hs.c +++ b/nimble/host/src/ble_hs.c @@ -1104,7 +1104,6 @@ ble_hs_deinit(void) #if (MYNEWT_VAL(BLE_HOST_BASED_PRIVACY)) ble_hs_resolv_deinit(); #endif - #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_hs_ctx) { ble_hs_ctx->parent_task = NULL; @@ -1112,6 +1111,8 @@ ble_hs_deinit(void) nimble_platform_mem_free(ble_hs_ctx->hci_os_event_buf); ble_hs_ctx->hci_os_event_buf = NULL; } + os_mempool_unregister(&ble_hs_hci_ev_pool); + nimble_platform_mem_free(ble_hs_ctx); ble_hs_ctx = NULL; } diff --git a/nimble/host/src/ble_hs_conn.c b/nimble/host/src/ble_hs_conn.c index 2fbab681d..96e963117 100644 --- a/nimble/host/src/ble_hs_conn.c +++ b/nimble/host/src/ble_hs_conn.c @@ -685,16 +685,17 @@ ble_hs_conn_init(void) if (rc != 0) { #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) #if !MYNEWT_VAL(MP_RUNTIME_ALLOC) - nimble_platform_mem_free(ble_hs_conn_elem_mem); - ble_hs_conn_elem_mem = NULL; + nimble_platform_mem_free(ble_hs_conn_elem_mem); + ble_hs_conn_elem_mem = NULL; #endif - if (ble_hs_conn_ctx) { - nimble_platform_mem_free(ble_hs_conn_ctx); - ble_hs_conn_ctx = NULL; - } - memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool)); + os_mempool_unregister(&ble_hs_conn_pool); + if (ble_hs_conn_ctx) { + nimble_platform_mem_free(ble_hs_conn_ctx); + ble_hs_conn_ctx = NULL; + } + memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool)); #endif - return BLE_HS_EOS; + return BLE_HS_EOS; } SLIST_INIT(&ble_hs_conns); @@ -716,6 +717,7 @@ ble_hs_conn_deinit(void) ble_hs_conn_elem_mem = NULL; } #endif + os_mempool_unregister(&ble_hs_conn_pool); memset(&ble_hs_conn_pool, 0, sizeof(ble_hs_conn_pool)); nimble_platform_mem_free(ble_hs_conn_ctx); ble_hs_conn_ctx = NULL; diff --git a/nimble/host/src/ble_hs_hci.c b/nimble/host/src/ble_hs_hci.c index a6df9dfd1..57e85e280 100644 --- a/nimble/host/src/ble_hs_hci.c +++ b/nimble/host/src/ble_hs_hci.c @@ -1044,7 +1044,7 @@ void ble_hs_hci_deinit(void) #endif /* Clean up mempool first to ensure blocks are free */ - os_mempool_clear(&ble_hs_hci_frag_mempool); + os_mempool_unregister(&ble_hs_hci_frag_mempool); rc = ble_npl_mutex_deinit(&ble_hs_hci_mutex); BLE_HS_DBG_ASSERT_EVAL(rc == 0); diff --git a/nimble/host/src/ble_hs_hci_cmd.c b/nimble/host/src/ble_hs_hci_cmd.c index a561b047b..5f8a82251 100644 --- a/nimble/host/src/ble_hs_hci_cmd.c +++ b/nimble/host/src/ble_hs_hci_cmd.c @@ -108,7 +108,11 @@ ble_hs_hci_cmd_send(uint16_t opcode, uint8_t len, const void *cmddata) if (rc == 0) { STATS_INC(ble_hs_stats, hci_cmd); } else { +#if MYNEWT_VAL(MP_RUNTIME_ALLOC) + ble_transport_free(BLE_HCI_CMD, buf); +#else ble_transport_free(buf); +#endif BLE_HS_LOG(DEBUG, "ble_hs_hci_cmd_send failure; rc=%d\n", rc); } diff --git a/nimble/host/src/ble_hs_hci_evt.c b/nimble/host/src/ble_hs_hci_evt.c index 21a9afa7d..c5ce4cba5 100644 --- a/nimble/host/src/ble_hs_hci_evt.c +++ b/nimble/host/src/ble_hs_hci_evt.c @@ -361,7 +361,7 @@ void ble_hs_hci_ctx_free(void) { if (ble_hci_ctx) { nimble_platform_mem_free(ble_hci_ctx); - ble_hci_ctx = NULL; + ble_hci_ctx = NULL; } } #else @@ -1655,6 +1655,7 @@ done: #else ble_transport_free((uint8_t *)ev); #endif +// #endif // CONFIG_SOC_ESP_NIMBLE_CONTROLLER return rc; } diff --git a/nimble/host/src/ble_hs_id.c b/nimble/host/src/ble_hs_id.c index 35704bac0..2ad9ba5b7 100644 --- a/nimble/host/src/ble_hs_id.c +++ b/nimble/host/src/ble_hs_id.c @@ -62,7 +62,7 @@ void ble_hs_id_ctx_free(void) { if (ble_hs_id_ctx) { nimble_platform_mem_free(ble_hs_id_ctx); - ble_hs_id_ctx = NULL; + ble_hs_id_ctx = NULL; } } #endif diff --git a/nimble/host/src/ble_hs_periodic_sync.c b/nimble/host/src/ble_hs_periodic_sync.c index e6a03ea94..c13684181 100644 --- a/nimble/host/src/ble_hs_periodic_sync.c +++ b/nimble/host/src/ble_hs_periodic_sync.c @@ -206,6 +206,7 @@ ble_hs_periodic_sync_deinit(void) ble_hs_psync_elem_mem = NULL; } #endif + os_mempool_unregister(&ble_hs_periodic_sync_pool); memset(&ble_hs_periodic_sync_pool, 0, sizeof(ble_hs_periodic_sync_pool)); nimble_platform_mem_free(ble_hs_periodic_ctx); ble_hs_periodic_ctx = NULL; @@ -286,15 +287,16 @@ ble_hs_periodic_sync_init(void) ble_hs_psync_elem_mem, "ble_hs_periodic_disc_pool"); if (rc != 0) { #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) -#if !MYNEWT_VAL(MP_RUNTIME_ALLOC) - if (ble_hs_psync_elem_mem) - { - nimble_platform_mem_free(ble_hs_psync_elem_mem); - ble_hs_psync_elem_mem = NULL; - } -#endif if (ble_hs_periodic_ctx) { +#if !MYNEWT_VAL(MP_RUNTIME_ALLOC) + if (ble_hs_psync_elem_mem) + { + nimble_platform_mem_free(ble_hs_psync_elem_mem); + ble_hs_psync_elem_mem = NULL; + } +#endif + os_mempool_unregister(&ble_hs_periodic_sync_pool); nimble_platform_mem_free(ble_hs_periodic_ctx); ble_hs_periodic_ctx = NULL; } diff --git a/nimble/host/src/ble_l2cap.c b/nimble/host/src/ble_l2cap.c index 7dbfff6c0..f2ec61223 100644 --- a/nimble/host/src/ble_l2cap.c +++ b/nimble/host/src/ble_l2cap.c @@ -604,6 +604,7 @@ ble_l2cap_init(void) nimble_platform_mem_free(ble_l2cap_chan_mem); ble_l2cap_chan_mem = NULL; #endif + os_mempool_unregister(&ble_l2cap_chan_pool); memset(&ble_l2cap_chan_pool, 0, sizeof(ble_l2cap_chan_pool)); nimble_platform_mem_free(ble_l2cap_ctx); @@ -642,6 +643,7 @@ done: nimble_platform_mem_free(ble_l2cap_chan_mem); ble_l2cap_chan_mem = NULL; #endif + os_mempool_unregister(&ble_l2cap_chan_pool); nimble_platform_mem_free(ble_l2cap_ctx); ble_l2cap_ctx = NULL; #endif @@ -659,6 +661,7 @@ ble_l2cap_deinit(void) ble_l2cap_chan_mem = NULL; } #endif + os_mempool_unregister(&ble_l2cap_chan_pool); memset(&ble_l2cap_chan_pool, 0, sizeof(ble_l2cap_chan_pool)); ble_l2cap_sig_deinit(); diff --git a/nimble/host/src/ble_l2cap_coc.c b/nimble/host/src/ble_l2cap_coc.c index cc5461414..9421fec37 100644 --- a/nimble/host/src/ble_l2cap_coc.c +++ b/nimble/host/src/ble_l2cap_coc.c @@ -766,6 +766,7 @@ ble_l2cap_coc_init(void) #if !MYNEWT_VAL(MP_RUNTIME_ALLOC) nimble_platform_mem_free(ble_l2cap_coc_srv_mem); #endif + os_mempool_unregister(&ble_l2cap_coc_srv_pool); memset(&ble_l2cap_coc_srv_pool, 0, sizeof(ble_l2cap_coc_srv_pool)); nimble_platform_mem_free(ble_l2cap_coc_ctx); ble_l2cap_coc_ctx = NULL; @@ -785,6 +786,7 @@ void ble_l2cap_coc_deinit(void) ble_l2cap_coc_srv_mem = NULL; } #endif + os_mempool_unregister(&ble_l2cap_coc_srv_pool); memset(&ble_l2cap_coc_srv_pool, 0, sizeof(ble_l2cap_coc_srv_pool)); STAILQ_INIT(&ble_l2cap_coc_srvs); nimble_platform_mem_free(ble_l2cap_coc_ctx); diff --git a/nimble/host/src/ble_l2cap_sig.c b/nimble/host/src/ble_l2cap_sig.c index 8f258015b..fe23c188d 100644 --- a/nimble/host/src/ble_l2cap_sig.c +++ b/nimble/host/src/ble_l2cap_sig.c @@ -2198,6 +2198,7 @@ ble_l2cap_sig_init(void) if (!ble_l2cap_sig_proc_mem) { // free the allocated memory nimble_platform_mem_free(ble_l2cap_sig_ctx); + ble_l2cap_sig_ctx = NULL; return BLE_HS_ENOMEM; } } @@ -2217,6 +2218,7 @@ ble_l2cap_sig_init(void) nimble_platform_mem_free(ble_l2cap_sig_proc_mem); ble_l2cap_sig_proc_mem = NULL; #endif + os_mempool_unregister(&ble_l2cap_sig_proc_pool); nimble_platform_mem_free(ble_l2cap_sig_ctx); ble_l2cap_sig_ctx = NULL; #endif @@ -2230,13 +2232,14 @@ ble_l2cap_sig_init(void) void ble_l2cap_sig_deinit(void) { -#if !MYNEWT_VAL(MP_RUNTIME_ALLOC) - if (ble_l2cap_sig_proc_mem) { - nimble_platform_mem_free(ble_l2cap_sig_proc_mem); - ble_l2cap_sig_proc_mem = NULL; - } -#endif if (ble_l2cap_sig_ctx) { +#if !MYNEWT_VAL(MP_RUNTIME_ALLOC) + if (ble_l2cap_sig_proc_mem) { + nimble_platform_mem_free(ble_l2cap_sig_proc_mem); + ble_l2cap_sig_proc_mem = NULL; + } +#endif + os_mempool_unregister(&ble_l2cap_sig_proc_pool); nimble_platform_mem_free(ble_l2cap_sig_ctx); ble_l2cap_sig_ctx = NULL; } diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c index d24f098a4..b328692f2 100644 --- a/nimble/host/src/ble_sm.c +++ b/nimble/host/src/ble_sm.c @@ -3297,6 +3297,7 @@ ble_sm_deinit(void) ble_sm_proc_mem = NULL; } #endif + os_mempool_unregister(&ble_sm_proc_pool); nimble_platform_mem_free(ble_sm_ctx); ble_sm_ctx = NULL; } diff --git a/nimble/transport/esp_ipc/src/hci_esp_ipc.c b/nimble/transport/esp_ipc/src/hci_esp_ipc.c index 827b874e7..a148139cb 100644 --- a/nimble/transport/esp_ipc/src/hci_esp_ipc.c +++ b/nimble/transport/esp_ipc/src/hci_esp_ipc.c @@ -66,8 +66,16 @@ ble_transport_alloc_cmd(void) return r_ble_hci_trans_buf_alloc(ESP_HCI_INTERNAL_BUF_CMD); } +#if MYNEWT_VAL(MP_RUNTIME_ALLOC) +void +ble_transport_free(uint8_t type, void *buf) +{ + r_ble_hci_trans_buf_free(buf); +} +#else void ble_transport_free(void *buf) { r_ble_hci_trans_buf_free(buf); } +#endif // MYNEWT_VAL(MP_RUNTIME_ALLOC) diff --git a/nimble/transport/src/transport.c b/nimble/transport/src/transport.c index 882168873..1d6facff0 100644 --- a/nimble/transport/src/transport.c +++ b/nimble/transport/src/transport.c @@ -381,8 +381,14 @@ void ble_buf_free(void) #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_trans_ctx) { + os_mempool_unregister(&pool_cmd); + os_mempool_unregister(&pool_evt); + os_mempool_unregister(&pool_evt_lo); +#if POOL_ACL_COUNT > 0 + os_mempool_unregister(&pool_acl.mpe_mp); +#endif // POOL_ACL_COUNT > 0 nimble_platform_mem_free(ble_trans_ctx); - ble_trans_ctx = NULL; + ble_trans_ctx = NULL; } #endif } diff --git a/porting/nimble/include/os/os_mempool.h b/porting/nimble/include/os/os_mempool.h deleted file mode 100644 index bed592fb5..000000000 --- a/porting/nimble/include/os/os_mempool.h +++ /dev/null @@ -1,420 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * @addtogroup OSKernel - * @{ - * @defgroup OSMempool Memory Pools - * @{ - */ - - -#ifndef _OS_MEMPOOL_H_ -#define _OS_MEMPOOL_H_ - -#include -#include "os/os.h" -#include "os/queue.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * A memory block structure. This simply contains a pointer to the free list - * chain and is only used when the block is on the free list. When the block - * has been removed from the free list the entire memory block is usable by the - * caller. - */ -struct os_memblock { - SLIST_ENTRY(os_memblock) mb_next; -}; - -/* XXX: Change this structure so that we keep the first address in the pool? */ -/* XXX: add memory debug structure and associated code */ -/* XXX: Change how I coded the SLIST_HEAD here. It should be named: - SLIST_HEAD(,os_memblock) mp_head; */ - -/** - * Memory pool - */ -struct os_mempool { - /** Size of the memory blocks, in bytes. */ - uint32_t mp_block_size; - /** The number of memory blocks. */ - uint16_t mp_num_blocks; - /** The number of free blocks left */ - uint16_t mp_num_free; - /** The lowest number of free blocks seen */ - uint16_t mp_min_free; - /** Bitmap of OS_MEMPOOL_F_[...] values. */ - uint8_t mp_flags; - /** Address of memory buffer used by pool */ - uint32_t mp_membuf_addr; - STAILQ_ENTRY(os_mempool) mp_list; - SLIST_HEAD(,os_memblock); - /** Name for memory block */ - const char *name; - /** The number of allocated blocks. */ - uint32_t mp_alloc_blocks; -}; - -/** - * Indicates an extended mempool. Address can be safely cast to - * (struct os_mempool_ext *). - */ -#define OS_MEMPOOL_F_EXT 0x01 -/* Flag to indicate runtime allocation mode */ -#define OS_MEMPOOL_F_RUNTIME 0x02 -/* Flag to indicate reuse block for runtime allocation mode */ -#define OS_MEMPOOL_F_REUSED 0x04 - -struct os_mempool_ext; - -/** - * Block put callback function. If configured, this callback gets executed - * whenever a block is freed to the corresponding extended mempool. Note: The - * os_memblock_put() function calls this callback instead of freeing the block - * itself. Therefore, it is the callback's responsibility to free the block - * via a call to os_memblock_put_from_cb(). - * - * @param ome The extended mempool that a block is being - * freed back to. - * @param data The block being freed. - * @param arg Optional argument configured along with the - * callback. - * - * @return Indicates whether the block was successfully - * freed. A non-zero value should only be - * returned if the block was not successfully - * released back to its pool. - */ -typedef os_error_t os_mempool_put_fn(struct os_mempool_ext *ome, void *data, - void *arg); - -struct os_mempool_ext { - struct os_mempool mpe_mp; - - /* Callback that is executed immediately when a block is freed. */ - os_mempool_put_fn *mpe_put_cb; - void *mpe_put_arg; -}; - -#define OS_MEMPOOL_INFO_NAME_LEN (32) - -/** - * Information describing a memory pool, used to return OS information - * to the management layer. - */ -struct os_mempool_info { - /** Size of the memory blocks in the pool */ - int omi_block_size; - /** Number of memory blocks in the pool */ - int omi_num_blocks; - /** Number of free memory blocks */ - int omi_num_free; - /** Minimum number of free memory blocks ever */ - int omi_min_free; - /** Name of the memory pool */ - char omi_name[OS_MEMPOOL_INFO_NAME_LEN]; -}; - -/** - * Get information about the next system memory pool. - * - * @param mempool The current memory pool, or NULL if starting iteration. - * @param info A pointer to the structure to return memory pool information - * into. - * - * @return The next memory pool in the list to get information about, or NULL - * when at the last memory pool. - */ -struct os_mempool *os_mempool_info_get_next(struct os_mempool *, - struct os_mempool_info *); - -/* - * To calculate size of the memory buffer needed for the pool. NOTE: This size - * is NOT in bytes! The size is the number of os_membuf_t elements required for - * the memory pool. - */ -#if MYNEWT_VAL(OS_MEMPOOL_GUARD) -/* - * Leave extra 4 bytes of guard area at the end. - */ -#define OS_MEMPOOL_BLOCK_SZ(sz) ((sz) + sizeof(os_membuf_t)) -#else -#define OS_MEMPOOL_BLOCK_SZ(sz) (sz) -#endif -#if (OS_ALIGNMENT == 4) -typedef uint32_t os_membuf_t; -#elif (OS_ALIGNMENT == 8) -typedef uint64_t os_membuf_t; -#elif (OS_ALIGNMENT == 16) -typedef __uint128_t os_membuf_t; -#else -#error "Unhandled `OS_ALIGNMENT` for `os_membuf_t`" -#endif /* OS_ALIGNMENT == * */ -#define OS_MEMPOOL_SIZE(n,blksize) ((((blksize) + ((OS_ALIGNMENT)-1)) / (OS_ALIGNMENT)) * (n)) - -/** Calculates the number of bytes required to initialize a memory pool. */ -#define OS_MEMPOOL_BYTES(n,blksize) \ - (sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize))) - -#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED -/** - * Initialize a memory pool. - * - * @param mp Pointer to a pointer to a mempool - * @param blocks The number of blocks in the pool - * @param blocks_size The size of the block, in bytes. - * @param membuf Pointer to memory to contain blocks. - * @param name Name of the pool. - * - * @return os_error_t - */ -os_error_t r_os_mempool_init(struct os_mempool *mp, uint16_t blocks, - uint32_t block_size, void *membuf, const char *name); -#define os_mempool_init r_os_mempool_init -/** - * Initializes an extended memory pool. Extended attributes (e.g., callbacks) - * are not specified when this function is called; they are assigned manually - * after initialization. - * - * @param mpe The extended memory pool to initialize. - * @param blocks The number of blocks in the pool. - * @param block_size The size of each block, in bytes. - * @param membuf Pointer to memory to contain blocks. - * @param name Name of the pool. - * - * @return os_error_t - */ -os_error_t r_os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks, - uint32_t block_size, void *membuf, const char *name); -#define os_mempool_ext_init r_os_mempool_ext_init -/** - * Removes the specified mempool from the list of initialized mempools. - * - * @param mp The mempool to unregister. - * - * @return 0 on success; - * OS_INVALID_PARM if the mempool is not - * registered. - */ -os_error_t r_os_mempool_unregister(struct os_mempool *mp); -#define os_mempool_unregister r_os_mempool_unregister - - -/** - * Clears a memory pool. - * - * @param mp The mempool to clear. - * - * @return os_error_t - */ -os_error_t r_os_mempool_clear(struct os_mempool *mp); -#define os_mempool_clear r_os_mempool_clear - - -/** - * Performs an integrity check of the specified mempool. This function - * attempts to detect memory corruption in the specified memory pool. - * - * @param mp The mempool to check. - * - * @return true if the memory pool passes the integrity - * check; - * false if the memory pool is corrupt. - */ -bool r_os_mempool_is_sane(const struct os_mempool *mp); -#define os_mempool_is_sane r_os_mempool_is_sane - - -/** - * Checks if a memory block was allocated from the specified mempool. - * - * @param mp The mempool to check as parent. - * @param block_addr The memory block to check as child. - * - * @return 0 if the block does not belong to the mempool; - * 1 if the block does belong to the mempool. - */ -int r_os_memblock_from(const struct os_mempool *mp, const void *block_addr); -#define os_memblock_from r_os_memblock_from - - -/** - * Get a memory block from a memory pool - * - * @param mp Pointer to the memory pool - * - * @return void* Pointer to block if available; NULL otherwise - */ -void *r_os_memblock_get(struct os_mempool *mp); -#define os_memblock_get r_os_memblock_get -/** - * Puts the memory block back into the pool, ignoring the put callback, if any. - * This function should only be called from a put callback to free a block - * without causing infinite recursion. - * - * @param mp Pointer to memory pool - * @param block_addr Pointer to memory block - * - * @return os_error_t - */ -os_error_t r_os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr); -#define os_memblock_put_from_cb r_os_memblock_put_from_cb - - -/** - * Puts the memory block back into the pool - * - * @param mp Pointer to memory pool - * @param block_addr Pointer to memory block - * - * @return os_error_t - */ -os_error_t r_os_memblock_put(struct os_mempool *mp, void *block_addr); -#define os_memblock_put r_os_memblock_put - -#else -/** - * Initialize a memory pool. - * - * @param mp Pointer to a pointer to a mempool - * @param blocks The number of blocks in the pool - * @param blocks_size The size of the block, in bytes. - * @param membuf Pointer to memory to contain blocks. - * @param name Name of the pool. - * - * @return os_error_t - */ -os_error_t os_mempool_init(struct os_mempool *mp, uint16_t blocks, - uint32_t block_size, void *membuf, const char *name); - -/** - * Initializes an extended memory pool. Extended attributes (e.g., callbacks) - * are not specified when this function is called; they are assigned manually - * after initialization. - * - * @param mpe The extended memory pool to initialize. - * @param blocks The number of blocks in the pool. - * @param block_size The size of each block, in bytes. - * @param membuf Pointer to memory to contain blocks. - * @param name Name of the pool. - * - * @return os_error_t - */ -os_error_t os_mempool_ext_init(struct os_mempool_ext *mpe, uint16_t blocks, - uint32_t block_size, void *membuf, const char *name); - -/** - * Removes the specified mempool from the list of initialized mempools. - * - * @param mp The mempool to unregister. - * - * @return 0 on success; - * OS_INVALID_PARM if the mempool is not - * registered. - */ -os_error_t os_mempool_unregister(struct os_mempool *mp); - -/** - * Clears a memory pool. - * - * @param mp The mempool to clear. - * - * @return os_error_t - */ -os_error_t os_mempool_clear(struct os_mempool *mp); - -/** - * Clears an extended memory pool. - * - * @param mpe The extended memory pool to clear. - * - * @return os_error_t - */ -os_error_t os_mempool_ext_clear(struct os_mempool_ext *mpe); - -/** - * Performs an integrity check of the specified mempool. This function - * attempts to detect memory corruption in the specified memory pool. - * - * @param mp The mempool to check. - * - * @return true if the memory pool passes the integrity - * check; - * false if the memory pool is corrupt. - */ -bool os_mempool_is_sane(const struct os_mempool *mp); - -/** - * Checks if a memory block was allocated from the specified mempool. - * - * @param mp The mempool to check as parent. - * @param block_addr The memory block to check as child. - * - * @return 0 if the block does not belong to the mempool; - * 1 if the block does belong to the mempool. - */ -int os_memblock_from(const struct os_mempool *mp, const void *block_addr); - -/** - * Get a memory block from a memory pool - * - * @param mp Pointer to the memory pool - * - * @return void* Pointer to block if available; NULL otherwise - */ -void *os_memblock_get(struct os_mempool *mp); - -/** - * Puts the memory block back into the pool, ignoring the put callback, if any. - * This function should only be called from a put callback to free a block - * without causing infinite recursion. - * - * @param mp Pointer to memory pool - * @param block_addr Pointer to memory block - * - * @return os_error_t - */ -os_error_t os_memblock_put_from_cb(struct os_mempool *mp, void *block_addr); - -/** - * Puts the memory block back into the pool - * - * @param mp Pointer to memory pool - * @param block_addr Pointer to memory block - * - * @return os_error_t - */ -os_error_t os_memblock_put(struct os_mempool *mp, void *block_addr); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* _OS_MEMPOOL_H_ */ - - -/** - * @} OSMempool - * @} OSKernel - */ diff --git a/porting/nimble/src/nimble_port.c b/porting/nimble/src/nimble_port.c index c1801fb1e..f1d3c07aa 100644 --- a/porting/nimble/src/nimble_port.c +++ b/porting/nimble/src/nimble_port.c @@ -96,7 +96,7 @@ static struct ble_hs_stop_listener stop_listener; extern void os_msys_init(void); extern void os_mempool_module_init(void); #if MYNEWT_VAL(MP_RUNTIME_ALLOC) -extern void os_mempool_deinit(void); +extern void os_mempool_deinit(bool is_controller); #endif #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) @@ -260,7 +260,7 @@ esp_err_t esp_nimble_deinit(void) #endif #if MYNEWT_VAL(MP_RUNTIME_ALLOC) - os_mempool_deinit(); + os_mempool_deinit(0); #endif return ESP_OK; diff --git a/porting/nimble/src/os_msys_init.c b/porting/nimble/src/os_msys_init.c index 575e0b293..e5ff4af60 100644 --- a/porting/nimble/src/os_msys_init.c +++ b/porting/nimble/src/os_msys_init.c @@ -264,12 +264,14 @@ os_msys_buf_free(void) nimble_platform_mem_free(os_msys_ctx->init_1_data); os_msys_ctx->init_1_data = NULL; } + os_mempool_unregister(&os_msys_ctx->init_1_mempool); #endif #if OS_MSYS_2_BLOCK_COUNT > 0 if (os_msys_ctx->init_2_data) { nimble_platform_mem_free(os_msys_ctx->init_2_data); os_msys_ctx->init_2_data = NULL; } + os_mempool_unregister(&os_msys_ctx->init_2_mempool); #endif nimble_platform_mem_free(os_msys_ctx); os_msys_ctx = NULL; @@ -280,11 +282,13 @@ os_msys_buf_free(void) nimble_platform_mem_free(os_msys_init_1_data); os_msys_init_1_data = NULL; + os_mempool_unregister(&os_msys_init_1_mempool); #endif #if OS_MSYS_2_BLOCK_COUNT > 0 nimble_platform_mem_free(os_msys_init_2_data); os_msys_init_2_data = NULL; + os_mempool_unregister(&os_msys_init_2_mempool); #endif #endif } diff --git a/porting/npl/freertos/src/npl_os_freertos.c b/porting/npl/freertos/src/npl_os_freertos.c index 9ef195149..f56f4da56 100644 --- a/porting/npl/freertos/src/npl_os_freertos.c +++ b/porting/npl/freertos/src/npl_os_freertos.c @@ -1332,6 +1332,11 @@ _error: #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_freertos_ctx) { + os_mempool_unregister(&ble_freertos_mutex_pool); + os_mempool_unregister(&ble_freertos_sem_pool); + os_mempool_unregister(&ble_freertos_co_pool); + os_mempool_unregister(&ble_freertos_evq_pool); + os_mempool_unregister(&ble_freertos_ev_pool); nimble_platform_mem_free(ble_freertos_ctx); ble_freertos_ctx = NULL; } @@ -1370,6 +1375,11 @@ void npl_freertos_mempool_deinit(void) #endif #if MYNEWT_VAL(BLE_STATIC_TO_DYNAMIC) if (ble_freertos_ctx) { + os_mempool_unregister(&ble_freertos_mutex_pool); + os_mempool_unregister(&ble_freertos_sem_pool); + os_mempool_unregister(&ble_freertos_co_pool); + os_mempool_unregister(&ble_freertos_evq_pool); + os_mempool_unregister(&ble_freertos_ev_pool); nimble_platform_mem_free(ble_freertos_ctx); ble_freertos_ctx = NULL; }