Currently i am trying to port a LWIP ( Light weight TCP/IP stack ) on embedded board.
During looking code i come up with one array declaration ( in memp.c
file) which look's me strange declaration i never seen this type of declaration past.
Although it's valid declaration but i am confusing regarding this means how much space it occupy and how can i calculate it? how LWIP_MEMPOOL
macro expanded?
Declaration looks like
/** This is the actual memory used by the pools (all pools in one big block). */
static unsigned char memp_memory[MEM_ALIGNMENT - 1
#define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
#include "memp_std.h"
];
And
/** This array holds the number of elements in each pool. */
static const unsigned int memp_num[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc) (num),
#include "memp_std.h"
};
Now let me give you each macro defination used in above array
declaration
/* 32-bit alignment */
#define MEM_ALIGNMENT 4
#define MEMP_SIZE 0
#define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1) & ~(MEM_ALIGNMENT-1))
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
MEMP_MAX
defined here
/* Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */
typedef enum {
#define LWIP_MEMPOOL(name,num,size,desc) MEMP_##name,
#include "memp_std.h"
MEMP_MAX <--------------------//MEMP_MAX
} memp_t;
And here is memp_std.h
In memp_std.h
there are many structure name used for sizeof(struct xxx)
but i am not able to include definition for all structures. So you can assume it have some size.
Can you explain how the LWIP_MEMPOOL
macro used? how the arrays defined? how the size of that array known?
It seems to use X-macros.
Idea is that memp_std.h
contains the data, and we can define LWIP_MEMPOOL
macro to filter out the data we need.
Since X-macros can get complicated really fast, I would advise you to enable preprocessor output on your compiler to see the actual output after preprocessing is done.
Here's what the first code sample will roughly look like (white space adjusted, and not all macro symbols expanded):
static unsigned char memp_memory[MEM_ALIGNMENT - 1
+ ( (MEMP_NUM_UDP_PCB) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct udp_pcb) ) ) )
+ ( (MEMP_NUM_TCP_PCB) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb) ) ) )
+ ( (MEMP_NUM_TCP_PCB_LISTEN) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb_listen)) ) )
+ ( (MEMP_NUM_TCP_SEG) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct tcp_seg) ) ) )
+ ( (MEMP_NUM_NETBUF) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct netbuf) ) ) )
+ ( (MEMP_NUM_NETCONN) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct netconn) ) ) )
];