Fixed memory leak when RAM free size is insufficient or setting ext scan parameters failed on ESP32-C2

This commit is contained in:
zwl
2022-11-29 16:29:26 +08:00
parent 2f23825f60
commit 1732a6d044
8 changed files with 97 additions and 88 deletions
@@ -102,7 +102,7 @@ struct npl_funcs_t {
ble_npl_error_t (*p_ble_npl_sem_pend)(struct ble_npl_sem *, ble_npl_time_t);
ble_npl_error_t (*p_ble_npl_sem_release)(struct ble_npl_sem *);
uint16_t (*p_ble_npl_sem_get_count)(struct ble_npl_sem *);
void (*p_ble_npl_callout_init)(struct ble_npl_callout *, struct ble_npl_eventq *, ble_npl_event_fn *, void *);
int (*p_ble_npl_callout_init)(struct ble_npl_callout *, struct ble_npl_eventq *, ble_npl_event_fn *, void *);
ble_npl_error_t (*p_ble_npl_callout_reset)(struct ble_npl_callout *, ble_npl_time_t);
void (*p_ble_npl_callout_stop)(struct ble_npl_callout *);
void (*p_ble_npl_callout_deinit)(struct ble_npl_callout *);
@@ -259,12 +259,13 @@ IRAM_ATTR ble_npl_sem_get_count(struct ble_npl_sem *sem)
return npl_funcs->p_ble_npl_sem_get_count(sem);
}
static inline void
static inline int
IRAM_ATTR ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
return npl_funcs->p_ble_npl_callout_init(co, evq, ev_cb, ev_arg);
}
static inline void
IRAM_ATTR ble_npl_callout_deinit(struct ble_npl_callout *co)
{
@@ -72,7 +72,7 @@ ble_npl_error_t npl_freertos_sem_pend(struct ble_npl_sem *sem,
ble_npl_error_t npl_freertos_sem_release(struct ble_npl_sem *sem);
void npl_freertos_callout_init(struct ble_npl_callout *co,
int npl_freertos_callout_init(struct ble_npl_callout *co,
struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg);
@@ -676,7 +676,7 @@ IRAM_ATTR os_callout_timer_cb(TimerHandle_t timer)
}
#endif
void
int
npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb, void *ev_arg)
{
@@ -692,61 +692,82 @@ npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
callout->evq = evq;
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
ESP_ERROR_CHECK(esp_timer_create(&create_args, &callout->handle));
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
#endif
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
os_memblock_put(&ble_freertos_co_pool,callout);
co->co = NULL;
return -1;
}
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
} else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#else
if(!co->co) {
co->co = malloc(sizeof(struct ble_npl_callout_freertos));
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
if (!callout) {
return -1;
}
memset(callout, 0, sizeof(*callout));
memset(callout, 0, sizeof(*callout));
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
callout->evq = evq;
callout->evq = evq;
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
esp_timer_create_args_t create_args = {
.callback = ble_npl_event_fn_wrapper,
.arg = callout,
.name = "nimble_timer"
};
ESP_ERROR_CHECK(esp_timer_create(&create_args, &callout->handle));
if (esp_timer_create(&create_args, &callout->handle) != ESP_OK) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#else
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
#endif
callout->handle = xTimerCreate("co", 1, pdFALSE, callout, os_callout_timer_cb);
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
ble_npl_event_deinit(&callout->ev);
free((void *)callout);
co->co = NULL;
return -1;
}
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
}
else {
callout = (struct ble_npl_callout_freertos *)co->co;
BLE_LL_ASSERT(callout);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
callout->evq = evq;
ble_npl_event_init(&callout->ev, ev_cb, ev_arg);
}
#endif
#endif // OS_MEM_ALLOC
return 0;
}
void
@@ -756,11 +777,14 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co)
/* Since we dynamically deinit timers, function can be called for NULL timers. Return for such scenarios */
if (!callout) {
return;
return;
}
BLE_LL_ASSERT(callout->handle);
if (!callout->handle) {
return;
}
ble_npl_event_deinit(&callout->ev);
#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
esp_err_t err = esp_timer_stop(callout->handle);
if(err != ESP_OK) {
@@ -773,17 +797,13 @@ npl_freertos_callout_deinit(struct ble_npl_callout *co)
ESP_LOGW(TAG, "Timer not deleted");
}
#else
xTimerDelete(callout->handle, portMAX_DELAY);
ble_npl_event_deinit(&callout->ev);
#if OS_MEM_ALLOC
os_memblock_put(&ble_freertos_co_pool,callout);
#else
free((void *)callout);
#endif
#endif
#endif // OS_MEM_ALLOC
#endif // CONFIG_BT_NIMBLE_USE_ESP_TIMER
co->co = NULL;
memset(co, 0, sizeof(struct ble_npl_callout));
}