Search code examples
cmemory-managementglobal-variablesfreertos

Storage area of public variables in a freeRTOS environment


Where do the global public variables (not declared inside a task body) get stored in a FreeRTOS environment?

What memory size configuration in the system will ensure that they get enough (non-overlapping) space for storage like either:

  1. configTOTAL_HEAP_SIZE defined in FreeRTOSConfig.h, or

  2. these parameters defined in the linker script:
    _Min_Heap_Size = 0x200; /* required amount of heap */ _Min_Stack_Size = 0x400; /* required amount of stack */


Solution

  • They are stored in the global memory, but not "on the heap" in the sense that the C compiler won't generate calls to the FreeRTOS heap_x.c (where x is one of the heap implementation numbers) to allocate this memory, it will simply stuff it into the correct section, and the linker will position that section somewhere in memory.

    The memory that is used as the heap's backing store will generally also be of the same form, i.e. we have this in heap_2.c:

    static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
    

    that is simply declaring a global variable that is an array of the specified size; this will then be located in actual memory by the linker as would any other variable.

    This is assuming a flash-based platform, i.e. not one where the FreeRTOS program is dynamically loaded at run-time by some code, in that case of course that code must fit all the sections of the program into memory and might indeed allocate heap memory to do so.