Extensions

When creating a deeply-embedded network application some features of lwIP can be used by the general application code to save on the code footprint of duplicating similar support. One major area that can easily be made use of in such a way is the lwIP memory allocation support.

If the CDL option CYGFUN_LWIP_MEMP_USE_CUSTOM_POOLS is enabled then the user can supply the lwippools.h header file accessible to the lwIP build. This header file is included by lwIP when defining the memory pool structures, and can include application specific custom pools. The following example implementation provides a new application specific 16 entry memory pool containing 64-byte buffers:

/*> lwippools.h <*/

/* NOTE: We do NOT have the standard header file one-time inclusion conditional
   checks since this source file is referenced multiple-times, with different
   macro definitions, depending on the part of the memory pool support being
   instantiated. */

LWIP_MEMPOOL(APP_BUFFER, 16, 64, "APP_BUFFER")

/*> EOF lwippools.h <*/
      
The custom memory pool space is located within the configured lwIP memory pool space. The pool can then be accessed via the memp_malloc() and memp_free() as normal:
pointer = (someptr *)memp_malloc(MEMP_APP_BUFFER);
/* Do some work with the buffer ... */
memp_free(MEMP_APP_BUFFER, pointer);
      
For eCos the memory pool allocation support is protected using the SYS_ARCH_PROTECT (DSR) serialization support code, allowing the calls to be made from any thread.

Similarly if lwIP is configured with CYGFUN_LWIP_MEM_USE_POOLS then it will use fixed size memory pools instead of a heap for the mem_malloc() calls. The lwippools.h header file can then be used to define the specific fixed size memory pools to be used by including suitable LWIP_MALLOC_MEMPOOL() macro calls. For example the following fragment will define a set of fixed size pools:

/* Define three pools with buffer sizes of 256, 512, and 1512 bytes
   respectively. */
LWIP_MALLOC_MEMPOOL_START
LWIP_MALLOC_MEMPOOL(20,  256)
LWIP_MALLOC_MEMPOOL(10,  512)
LWIP_MALLOC_MEMPOOL( 5, 1512)
/* More pools can be added as required. */
LWIP_MALLOC_MEMPOOL_END
      

2017-02-09
Documentation license for this page: eCosPro License