How can I change maximum available heap size for a task in FreeRTOS?

前端 未结 2 491
名媛妹妹
名媛妹妹 2021-01-25 07:51

I\'m creating a list of elements inside a task in the following way:

        l = (dllist*)pvPortMalloc(sizeof(dllist));

dllist is 32 byte big.

相关标签:
2条回答
  • 2021-01-25 08:06

    For the standard allocators you will find a config option in FreeRTOSConfig.h .

    However: It is very well possible you run out of memory already, depending on the allocator used. IIRC there is one that does not free() any blocks (free() is just a dummy). So any block returned will be lost. This is still useful if you only allocate memory e.g. at startup, but then work with what you've got.

    Other allocators might just not merge adjacent blocks once returned, increasing fragmentation much faster than a full-grown allocator.

    Also, you might loose memory to fragmentation. Depending on your alloc/free pattern, you quickly might end up with a heap looking like swiss cheese: Many holes between allocated blocks. So while there is still enough free memory, no single block is big enough for the size required.

    If you only allocate blocks that size there, you might be better of using your own allocator or a pool (blocks of fixed size). Thaqt would be statically allocated (e.g. array) and chained as a linked list during startup. Alloc/free would then just be push/pop on a stack (or put/get on a queue). That would also be very fast and have complexity O(1) (interrupt-safe if properly written).

    Note that normal malloc()/free() are not interrupt-safe.

    Finally: Do not cast void *. (Well, that's actually what standard malloc() returns and I expect that FreeRTOS-variant does the same).

    0 讨论(0)
  • 2021-01-25 08:24

    (Yes - FreeRTOS pvPortMalloc() returns void*.)

    If you have 60K of SRAM, and configTOTAL_HEAP_SIZE is large, then it is unlikely you are going to run out of heap after allocating 256 bytes unless you had hardly any heap remaining before hand. Many FreeRTOS demos will just keep creating objects until all the heap is used, so if your application is based on one of those, then you would be low on heap before your code executed. You may have also done something like use up loads of heap space by creating tasks with huge stacks.

    heap_4 and heap_5 will combine adjacent blocks, which will minimise fragmentation as far as practical, but I don't think that will be your problem - especially as you don't mention freeing anything anywhere.

    Unless you are using heap_3.c (which just makes the standard C library malloc and free thread safe) you can call xPortGetFreeHeapSize() to see how much free heap you have. You may also have xPortGetMinimumEverFreeHeapSize() available to query how close you have ever come to running out of heap. More information: http://www.freertos.org/a00111.html

    You could also define a malloc() failed hook (http://www.freertos.org/a00016.html) to get instant notification of pvPortMalloc() returning NULL.

    0 讨论(0)
提交回复
热议问题