[ABANDONED] Heap function: lock() / unlock()

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [ABANDONED] Heap function: lock() / unlock()

    Hello,
    it's me again.

    There is a section in the document "emRun User Guide & Reference manual" [UM12007, Date: November 2, 2022] for the runtime library, indicating several functions:

    Source Code

    1. __SEGGER_RTL_init_heap();
    2. __SEGGER_RTL_X_heap_lock();
    3. __SEGGER_RTL_X_heap_unlock();

    I have tried using them in my code. A search in all project files showed that these functions are marked with the attribute "weak" which means I have to define a function with the same name to use. But it doesn't work. After compilation, these functions are removed (I looked at the map file). At the same time, in the disassembler window, in the same map file, there are functions with names:

    Source Code

    1. <__heap_lock>
    2. <__heap_unlock>
    Which I could already use in my code like this:

    Source Code: main.cpp

    1. //void __SEGGER_RTL_X_heap_lock(){
    2. extern "C" void __heap_lock (void){
    3. printf("lock \n");
    4. }
    5. //extern "C" void __SEGGER_RTL_X_heap_unlock(){
    6. extern "C" void __heap_unlock (void){
    7. printf("unlock \n");
    8. }
    9. class test {
    10. public:
    11. test() {
    12. printf("construct test class\n");
    13. }
    14. private:
    15. };
    16. int main(void) {
    17. test* t = new test();
    18. (void)t;
    19. for(;;){
    20. }
    21. }
    Display All
    In this regard, questions:
    1. Is there an error in the function description in the documentation?
    2. When using the C++ compiler, can these functions be used or are there analogues?
    3. How can I control the amount of free heap space? I would like to know how much my application will "eat up" in the worst case scenario and if there are any memory leaks.

    thanks
  • Hello,

    Thank you for your inquiry.
    The emRun manual refers to the standalone project emRun and the manual is meant for implementers of the standalone lib.

    Embedded Studio is one of the implementers so it is possible that certain hooks and APIs are named or used differently than what the emRun manual documents.

    If you are looking to replace the heap routines in Embedded Studio see here for documentation:
    wiki.segger.com/Embedded_Studio_Library_Heap_Locking

    To answer your other questions.
    1: No. Just different documentation for different things.
    2: Same functions are used. emRun++ is a superset of emRun.
    3: The straight forward approach is to initialize heap to some pattern e.g. 0xCDCD. Then let your worst case test run and check the usage in the memory window.
    Alternatively a watermark variable could be used, but this will have some impact on system runtime.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hello, Nino!


    SEGGER - Nino wrote:

    To answer your other questions.
    1: No. Just different documentation for different things.
    2: Same functions are used. emRun++ is a superset of emRun.
    Ok, understood.

    SEGGER - Nino wrote:

    3: The straight forward approach is to initialize heap to some pattern e.g. 0xCDCD. Then let your worst case test run and check the usage in the memory window.
    Alternatively a watermark variable could be used, but this will have some impact on system runtime.
    I found several defines:

    C Source Code: __SEGGER_RTL_RTHEAP_ConfDefaults.h

    1. //
    2. // Default fill values indicating heap memory state.
    3. //
    4. #ifndef __SEGGER_RTL_RTHEAP_FILL_ALLOCATED
    5. #define __SEGGER_RTL_RTHEAP_FILL_ALLOCATED 0xAA
    6. #endif
    7. #ifndef __SEGGER_RTL_RTHEAP_FILL_FREED
    8. #define __SEGGER_RTL_RTHEAP_FILL_FREED 0xDD
    9. #endif
    10. #ifndef __SEGGER_RTL_RTHEAP_FILL_UNTOUCHED
    11. #define __SEGGER_RTL_RTHEAP_FILL_UNTOUCHED 0xBB
    12. #endif
    Display All
    but I can't find their usage, since the library is supplied in a compiled form (files heapops_xxx.a).

    I understand that these definitions are used inside the library for the memory scheduler when allocating / freeing memory sections. But their use outside the user is not available?
    and in that case i should add my mechanism?

    I roughly imagine the mechanics of the water mark (I used it in freertos), I’m ready to put up with overhead costs in runtime. But not yet imagining how they can be added to a closed library if there is no access to the current memory allocation functions.

    I could not find it on the wiki and in the documentation, do you have a garbage collector?

    _____________________________________________
    best Regards,
    Max
  • Hi Max,

    If I understand you correctly you are looking to track your heap usage. Correct?
    In that case the general approach to overwrite the heap_lock and _unlock functions is wrong.
    That functions are only meant to assure interrupt or mutex locking as explained in the Wiki article.
    They are not meant to be misused for other purposes like memory management.

    Instead our recommendation if you plan to track your heap usage to simply write your own external routines either by calling them periodically or by wrapping the alloc() and free() standard library calls.
    The SEGGER Linker will supply you with any symbols and features that you need for that e.g. heap start address, size and fill pattern.

    For completion here the answers to you remaining questions:

    CheMax wrote:

    I understand that these definitions are used inside the library for the memory scheduler when allocating / freeing memory sections. But their use outside the user is not available?
    and in that case i should add my mechanism?
    Correct. Yes.


    CheMax wrote:

    I could not find it on the wiki and in the documentation, do you have a garbage collector?
    No C embedded standard library has this. Memory management is usually done custom on application level as there is no one size fits all solution for this as embedded targets have wildly different resource constraints and use cases.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hello, Nino!

    Thanks for your answer. While I was waiting for your answer, I did a little research on this topic.
    As I understand it, I can use my own memory allocation mechanisms (for example from FreeRTOS) and for this I just need to redefine three functions (after choose "None" in heap library option)?

    C Source Code

    1. void __SEGGER_RTL_free(void* ptr)
    2. {
    3. ...
    4. printf("delete\n");
    5. }
    6. void* __SEGGER_RTL_alloc(size_t size)
    7. {
    8. ...
    9. printf("allocation\n");
    10. }
    11. void __SEGGER_RTL_init_heap(void *__ptr, __SEGGER_RTL_SIZE_T __size)
    12. {
    13. ...
    14. printf("heap init\n");
    15. }
    Display All
    _____________________________________________
    best Regards,
    Max
  • Hello Max,

    no, you just wrap the malloc etc. calls directly on high level.
    Do not overwrite standard functions if not necessary.

    So you would write e.g.

    C Source Code

    1. void* My_Malloc(size_t size) {
    2. printf("My_Malloc Log\n");
    3. ...
    4. return malloc(size);
    5. }

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.