Prusa MINI Firmware overview
Heap and memory pools
Collaboration diagram for Heap and memory pools:

Macros

#define MEM_LIBC_MALLOC   0
 
#define MEMP_MEM_MALLOC   0
 
#define MEM_ALIGNMENT   1
 
#define MEM_SIZE   1600
 
#define MEMP_OVERFLOW_CHECK   0
 
#define MEMP_SANITY_CHECK   0
 
#define MEM_USE_POOLS   0
 
#define MEM_USE_POOLS_TRY_BIGGER_POOL   0
 
#define MEMP_USE_CUSTOM_POOLS   0
 
#define LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT   0
 

Detailed Description

Macro Definition Documentation

◆ MEM_LIBC_MALLOC

#define MEM_LIBC_MALLOC   0

MEM_LIBC_MALLOC==1: Use malloc/free/realloc provided by your C-library instead of the lwip internal allocator. Can save code size if you already use it.

◆ MEMP_MEM_MALLOC

#define MEMP_MEM_MALLOC   0

MEMP_MEM_MALLOC==1: Use mem_malloc/mem_free instead of the lwip pool allocator. Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution speed (heap alloc can be much slower than pool alloc) and usage from interrupts (especially if your netif driver allocates PBUF_POOL pbufs for received frames from interrupt)! ATTENTION: Currently, this uses the heap for ALL pools (also for private pools, not only for internal pools defined in memp_std.h)!

◆ MEM_ALIGNMENT

#define MEM_ALIGNMENT   1

MEM_ALIGNMENT: should be set to the alignment of the CPU 4 byte alignment -> #define MEM_ALIGNMENT 4 2 byte alignment -> #define MEM_ALIGNMENT 2

◆ MEM_SIZE

#define MEM_SIZE   1600

MEM_SIZE: the size of the heap memory. If the application will send a lot of data that needs to be copied, this should be set high.

◆ MEMP_OVERFLOW_CHECK

#define MEMP_OVERFLOW_CHECK   0

MEMP_OVERFLOW_CHECK: memp overflow protection reserves a configurable amount of bytes before and after each memp element in every pool and fills it with a prominent default value. MEMP_OVERFLOW_CHECK == 0 no checking MEMP_OVERFLOW_CHECK == 1 checks each element when it is freed MEMP_OVERFLOW_CHECK >= 2 checks each element in every pool every time memp_malloc() or memp_free() is called (useful but slow!)

◆ MEMP_SANITY_CHECK

#define MEMP_SANITY_CHECK   0

MEMP_SANITY_CHECK==1: run a sanity check after each memp_free() to make sure that there are no cycles in the linked lists.

◆ MEM_USE_POOLS

#define MEM_USE_POOLS   0

MEM_USE_POOLS==1: Use an alternative to malloc() by allocating from a set of memory pools of various sizes. When mem_malloc is called, an element of the smallest pool that can provide the length needed is returned. To use this, MEMP_USE_CUSTOM_POOLS also has to be enabled.

◆ MEM_USE_POOLS_TRY_BIGGER_POOL

#define MEM_USE_POOLS_TRY_BIGGER_POOL   0

MEM_USE_POOLS_TRY_BIGGER_POOL==1: if one malloc-pool is empty, try the next bigger pool - WARNING: THIS MIGHT WASTE MEMORY but it can make a system more reliable.

◆ MEMP_USE_CUSTOM_POOLS

#define MEMP_USE_CUSTOM_POOLS   0

MEMP_USE_CUSTOM_POOLS==1: whether to include a user file lwippools.h that defines additional pools beyond the "standard" ones required by lwIP. If you set this to 1, you must have lwippools.h in your include path somewhere.

◆ LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT

#define LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT   0

Set this to 1 if you want to free PBUF_RAM pbufs (or call mem_free()) from interrupt context (or another context that doesn't allow waiting for a semaphore). If set to 1, mem_malloc will be protected by a semaphore and SYS_ARCH_PROTECT, while mem_free will only use SYS_ARCH_PROTECT. mem_malloc SYS_ARCH_UNPROTECTs with each loop so that mem_free can run.

ATTENTION: As you can see from the above description, this leads to dis-/ enabling interrupts often, which can be slow! Also, on low memory, mem_malloc can need longer.

If you don't want that, at least for NO_SYS=0, you can still use the following functions to enqueue a deallocation call which then runs in the tcpip_thread context:

  • pbuf_free_callback(p);
  • mem_free_callback(m);