Heap function: lock() / unlock()

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

    • 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