Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation of heap_caps_malloc is incorrect (IDFGH-5971) #7659

Closed
jgamble-simple opened this issue Oct 7, 2021 · 1 comment
Closed

Documentation of heap_caps_malloc is incorrect (IDFGH-5971) #7659

jgamble-simple opened this issue Oct 7, 2021 · 1 comment
Assignees
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@jgamble-simple
Copy link

According to the documentation:

https://docs.espressif.com/projects/esp-idf/en/release-v3.3/api-reference/system/mem_alloc.html#_CPPv416heap_caps_malloc6size_t8uint32_t

malloc is equivalent to heap_caps_malloc(size, MALLOC_CAP_8BIT).

I have a small piece of code that demonstrates that this is not true:

while (1)
{
    void* p = malloc(1024);
    printf("Malloc another '%p'\n", p);
    if (!p) {
        printf("MALLOC_CAP_8BIT\n");
        heap_caps_print_heap_info(MALLOC_CAP_8BIT);

        void* p = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
        printf("Malloc another (MALLOC_CAP_8BIT) '%p'\n", p);
        while (1) {}
    }
}

This is the tail end of the resulting log:

Malloc another '0x3fffddc4'
Malloc another '0x3fffe1d4'
Malloc another '0x3fffe5e4'
Malloc another '0x3fffe9f4'
Malloc another '0x3fffee04'
Malloc another '0x3ffff214'
Malloc another '0x3ffff624'
Malloc another '0x3ffffa34'
Malloc another '0x0'
Malloc another (MALLOC_CAP_8BIT) '0x3f800024'

I am using platformio, which I believe is using esp-idf version 3.3

@espressif-bot espressif-bot added the Status: Opened Issue is new label Oct 7, 2021
@github-actions github-actions bot changed the title Documentation of heap_caps_malloc is incorrect Documentation of heap_caps_malloc is incorrect (IDFGH-5971) Oct 7, 2021
@igrr
Copy link
Member

igrr commented Oct 7, 2021

Thanks for pointing this out @jgamble-simple, the function description of what malloc does is indeed oversimplified.

The actual behavior of malloc in terms of external RAM allocations depends on CONFIG_SPIRAM_USE choice (https://docs.espressif.com/projects/esp-idf/en/release-v4.3/esp32/api-reference/kconfig.html#config-spiram-use):

  • Integrate RAM into memory map (CONFIG_SPIRAM_USE_MEMMAP)
  • Make RAM allocatable using heap_caps_malloc(…, MALLOC_CAP_SPIRAM) (CONFIG_SPIRAM_USE_CAPS_ALLOC)
  • Make RAM allocatable using malloc() as well (CONFIG_SPIRAM_USE_MALLOC). In this case CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL sets the threshold of allocation size, below which the allocation will be done from internal RAM by default.

I think the description of that function predates these additional configuration options, and was never updated.

Since these options are documented in detail in https://docs.espressif.com/projects/esp-idf/en/release-v4.3/esp32/api-guides/external-ram.html#configuring-external-ram, I think the easiest option is to remove the phrase malloc is equivalent to heap_caps_malloc... from the doxygen comment.

@zikalino zikalino self-assigned this Feb 22, 2022
@espressif-bot espressif-bot added Status: Selected for Development Issue is selected for development and removed Status: Opened Issue is new labels Aug 22, 2022
@espressif-bot espressif-bot assigned ESP-Marius and unassigned zikalino Sep 1, 2022
@espressif-bot espressif-bot added Status: Reviewing Issue is being reviewed Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Selected for Development Issue is selected for development Status: Reviewing Issue is being reviewed Resolution: NA Issue resolution is unavailable labels Sep 1, 2022
espressif-bot pushed a commit that referenced this issue Sep 3, 2022
…ps_malloc(p, MALLOC_CAP_8BIT)

The actual memory allocated for malloc() depends on a lot of factors, see heap_caps_malloc_default()

Closes #7659
Jason2866 added a commit to Jason2866/esp-idf that referenced this issue Sep 3, 2022
* timer: propagate isr register failure

Closes espressif#9651

* mcpwm: fix multiplication overflow in converting us to compare ticks

Closes espressif#9648

* heap: remove misleading info about malloc being equivalent to heap_caps_malloc(p, MALLOC_CAP_8BIT)

The actual memory allocated for malloc() depends on a lot of factors, see heap_caps_malloc_default()

Closes espressif#7659

* esp-rom: fixed error in miniz header documention for tdefl_init

Closes espressif#8435

* temperature_sensor: Fix issue that value is not accurate on ESP32-S3

* esp_https_ota: fix bug where `http_client_init_cb` is called after `esp_http_client_perform()` instead of before.

Closes espressif#9581

* Tasmota changes

* Fix linker error for C3

* Avoid bootloop if chip is unknown
   In case the PSIRAM chip is unknown, return an error and disable PSRAM
   instead of calling abort() and causing a bootloop
* Support for xiaomi single core ESP32
* Fix linker error for rom_temp_to_power
* fix linker error r_lld_ext_adv_dynamic_aux_pti_process

* Hide download percent when not interactive

* list(APPEND esptool_elf2image_args --dont-append-digest)

* Use native Apple ARM toolchains

* add package.json

* add submodules

* 8575d75

Co-authored-by: morris <[email protected]>
Co-authored-by: Marius Vikhammer <[email protected]>
Co-authored-by: Cao Sen Miao <[email protected]>
Co-authored-by: Harshit Malpani <[email protected]>
Co-authored-by: Mahavir Jain <[email protected]>
Jason2866 added a commit to Jason2866/esp-idf that referenced this issue Sep 3, 2022
* timer: propagate isr register failure

Closes espressif#9651

* mcpwm: fix multiplication overflow in converting us to compare ticks

Closes espressif#9648

* heap: remove misleading info about malloc being equivalent to heap_caps_malloc(p, MALLOC_CAP_8BIT)

The actual memory allocated for malloc() depends on a lot of factors, see heap_caps_malloc_default()

Closes espressif#7659

* esp-rom: fixed error in miniz header documention for tdefl_init

Closes espressif#8435

* temperature_sensor: Fix issue that value is not accurate on ESP32-S3

* esp_https_ota: fix bug where `http_client_init_cb` is called after `esp_http_client_perform()` instead of before.

Closes espressif#9581

* Tasmota changes

* Fix linker error for C3

* Avoid bootloop if chip is unknown
   In case the PSIRAM chip is unknown, return an error and disable PSRAM
   instead of calling abort() and causing a bootloop
* Support for xiaomi single core ESP32
* Fix linker error for rom_temp_to_power
* fix linker error r_lld_ext_adv_dynamic_aux_pti_process

* Hide download percent when not interactive

* list(APPEND esptool_elf2image_args --dont-append-digest)

* Use native Apple ARM toolchains

* add package.json

* add submodules

* 8575d75

* Arduino tinyusb v0.14.0 stripped

Co-authored-by: morris <[email protected]>
Co-authored-by: Marius Vikhammer <[email protected]>
Co-authored-by: Cao Sen Miao <[email protected]>
Co-authored-by: Harshit Malpani <[email protected]>
Co-authored-by: Mahavir Jain <[email protected]>
espressif-bot pushed a commit that referenced this issue Sep 22, 2022
…ps_malloc(p, MALLOC_CAP_8BIT)

The actual memory allocated for malloc() depends on a lot of factors, see heap_caps_malloc_default()

Closes #7659
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

5 participants