pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

Add ability to provide custom memory allocation for LWIP

Open Memotech-Bill opened this issue 1 year ago • 0 comments

In order to resolve an issue that has been raised against the Pico version of BBC BASIC, I would like to be able to provide my own memory allocation for LWIP, replacing the routines in either ${PICO_LWIP_PATH}/src/core/mem.c and / or ${PICO_LWIP_PATH}/src/core/memp.c, however I cannot find any way to do this.

While there are CPP macro definitions such as MEMP_MEM_MALLOC and MEM_LIBC_MALLOC which select different memory allocators, there does not seem to be anything that allows providing user routines.

Using #define macros in lwipopts.h (or another include file) to rename the functions does not work since it renames both the function calls and the function definitions.

I have tried using the linker --wrap=... option, but that does not work as the symbols are defined in object files that are explicitly included in the link command, rather than being undefined symbols to be found by a library search.

Since the compilation of the lwip code is controlled by pico-sdk/src/rp2_common/pico_lwip/CMakeLists.txt it should be possible to add this feature to the SDK without requiring any modification to the lwip sub-module.

One possible approach would be to define three extra import libraries:

  • pico_lwip_nomem - Omits mem.c and memp.c
  • pico_lwip_mem - Provides mem.c
  • pico_lwip_memp - Provides memp.c

Another option would be to define CMake variables PICO_LWIP_NO_MEM and PICO_LWIP_NO_MEMP to control the inclusion / exclusion of mem.c and memp.c in the pico_lwip library.

Memotech-Bill avatar Jul 08 '24 15:07 Memotech-Bill