fix: add os mempool unregister to prevent mem trampling

This commit is contained in:
cjin
2026-02-03 15:54:26 +08:00
parent d166751d69
commit 27db5fca0e
23 changed files with 105 additions and 453 deletions
+11 -4
View File
@@ -3413,6 +3413,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);
@@ -3442,6 +3447,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
@@ -3462,7 +3468,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)));
@@ -3470,8 +3476,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");
@@ -3503,6 +3508,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();
@@ -3538,7 +3544,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),
@@ -3551,6 +3557,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
+2
View File
@@ -681,6 +681,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;
}
+2
View File
@@ -10280,6 +10280,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;
}
@@ -10725,6 +10726,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;
}
+2
View File
@@ -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)
+5
View File
@@ -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)
}
+8 -1
View File
@@ -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
+2 -1
View File
@@ -1129,7 +1129,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;
@@ -1137,6 +1136,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;
}
+10 -8
View File
@@ -675,16 +675,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);
@@ -706,6 +707,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;
+1 -1
View File
@@ -1059,7 +1059,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);
+4
View File
@@ -102,7 +102,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);
}
+2 -1
View File
@@ -393,7 +393,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
@@ -1918,6 +1918,7 @@ done:
#else
ble_transport_free((uint8_t *)ev);
#endif
// #endif // CONFIG_SOC_ESP_NIMBLE_CONTROLLER
return rc;
}
+1 -1
View File
@@ -64,7 +64,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
+9 -7
View File
@@ -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;
}
+3
View File
@@ -539,6 +539,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);
@@ -577,6 +578,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
@@ -594,6 +596,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();
+2
View File
@@ -801,6 +801,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;
@@ -820,6 +821,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);
+9 -6
View File
@@ -2196,6 +2196,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;
}
}
@@ -2215,6 +2216,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
@@ -2228,13 +2230,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;
}
+1
View File
@@ -3345,6 +3345,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;
}
@@ -71,8 +71,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)
+7 -1
View File
@@ -488,8 +488,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
}
-420
View File
@@ -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 <stdbool.h>
#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
*/
+2 -2
View File
@@ -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;
+4
View File
@@ -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
}
@@ -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;
}