Merge branch 'fix/uart_select_malloc' into 'master'

fix(vfs): fix uart malloc when locates ISR context in IRAM

See merge request espressif/esp-idf!27278
This commit is contained in:
Martin Vychodil 2023-11-24 18:57:30 +08:00
commit 76011fc8f7
3 changed files with 21 additions and 13 deletions

View File

@ -1,9 +1,10 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include "esp_vfs.h"
#include "esp_vfs_common.h"
@ -11,6 +12,12 @@
extern "C" {
#endif
#if CONFIG_VFS_SELECT_IN_RAM
#define VFS_MALLOC_FLAGS MALLOC_CAP_INTERNAL
#else
#define VFS_MALLOC_FLAGS MALLOC_CAP_DEFAULT
#endif
typedef struct vfs_entry_ {
esp_vfs_t vfs; // contains pointers to VFS functions
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS

View File

@ -87,7 +87,7 @@ esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_v
return ESP_ERR_INVALID_ARG;
}
}
vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(sizeof(vfs_entry_t), VFS_MALLOC_FLAGS);
if (entry == NULL) {
return ESP_ERR_NO_MEM;
}
@ -970,7 +970,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
// because that could block the registration of new driver.
const size_t vfs_count = s_vfs_count;
fds_triple_t *vfs_fds_triple;
if ((vfs_fds_triple = calloc(vfs_count, sizeof(fds_triple_t))) == NULL) {
if ((vfs_fds_triple = heap_caps_calloc(vfs_count, sizeof(fds_triple_t), VFS_MALLOC_FLAGS)) == NULL) {
__errno_r(r) = ENOMEM;
ESP_LOGD(TAG, "calloc is unsuccessful");
return -1;
@ -1047,7 +1047,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds
}
}
void **driver_args = calloc(vfs_count, sizeof(void *));
void **driver_args = heap_caps_calloc(vfs_count, sizeof(void *), VFS_MALLOC_FLAGS);
if (driver_args == NULL) {
free(vfs_fds_triple);

View File

@ -11,16 +11,17 @@
#include <sys/lock.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include "sdkconfig.h"
#include "esp_attr.h"
#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "esp_attr.h"
#include "soc/uart_periph.h"
#include "driver/uart.h"
#include "sdkconfig.h"
#include "driver/uart_select.h"
#include "esp_vfs_private.h"
#include "esp_rom_uart.h"
#include "soc/soc_caps.h"
#include "driver/uart.h"
#include "driver/uart_select.h"
#include "hal/uart_ll.h"
#include "soc/soc_caps.h"
#include "soc/uart_periph.h"
#define UART_NUM SOC_UART_HP_NUM
@ -365,7 +366,7 @@ static esp_err_t register_select(uart_select_args_t *args)
portENTER_CRITICAL(&s_registered_select_lock);
const int new_size = s_registered_select_num + 1;
uart_select_args_t **new_selects;
if ((new_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *))) == NULL) {
if ((new_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS)) == NULL) {
ret = ESP_ERR_NO_MEM;
} else {
s_registered_selects = new_selects;
@ -391,7 +392,7 @@ static esp_err_t unregister_select(uart_select_args_t *args)
// The item is removed by overwriting it with the last item. The subsequent rellocation will drop the
// last item.
s_registered_selects[i] = s_registered_selects[new_size];
s_registered_selects = realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *));
s_registered_selects = heap_caps_realloc(s_registered_selects, new_size * sizeof(uart_select_args_t *), VFS_MALLOC_FLAGS);
// Shrinking a buffer with realloc is guaranteed to succeed.
s_registered_select_num = new_size;
ret = ESP_OK;
@ -448,7 +449,7 @@ static esp_err_t uart_start_select(int nfds, fd_set *readfds, fd_set *writefds,
}
}
uart_select_args_t *args = malloc(sizeof(uart_select_args_t));
uart_select_args_t *args = heap_caps_malloc(sizeof(uart_select_args_t), VFS_MALLOC_FLAGS);
if (args == NULL) {
return ESP_ERR_NO_MEM;