mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 18:57:19 +02:00
esp_flash: fix the cleanup when add device fails
This commit is contained in:
@ -112,17 +112,28 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d
|
|||||||
if (config->host_id == SPI_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
|
if (config->host_id == SPI_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
|
||||||
|
|
||||||
chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
|
chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
|
||||||
host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
|
if (!chip) {
|
||||||
host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
|
|
||||||
if (!chip || !host || !host_data) {
|
|
||||||
ret = ESP_ERR_NO_MEM;
|
ret = ESP_ERR_NO_MEM;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
|
||||||
*chip = (esp_flash_t) {
|
*chip = (esp_flash_t) {
|
||||||
.read_mode = config->io_mode,
|
.read_mode = config->io_mode,
|
||||||
.host = host,
|
.host = host,
|
||||||
};
|
};
|
||||||
|
if (!host) {
|
||||||
|
ret = ESP_ERR_NO_MEM;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
|
||||||
|
host->driver_data = host_data;
|
||||||
|
if (!host_data) {
|
||||||
|
ret = ESP_ERR_NO_MEM;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
esp_err_t err = esp_flash_init_os_functions(chip, config->host_id);
|
esp_err_t err = esp_flash_init_os_functions(chip, config->host_id);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ret = err;
|
ret = err;
|
||||||
@ -147,6 +158,7 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d
|
|||||||
*out_chip = chip;
|
*out_chip = chip;
|
||||||
return ret;
|
return ret;
|
||||||
fail:
|
fail:
|
||||||
|
// The memory allocated are free'd in the `spi_bus_remove_flash_device`.
|
||||||
spi_bus_remove_flash_device(chip);
|
spi_bus_remove_flash_device(chip);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user