1

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?

4

1 に答える 1

1

Xマクロを使っているようです。

アイデアはmemp_std.hデータを含むことであり、マクロを定義LWIP_MEMPOOLして必要なデータを除外できます。

X マクロはすぐに複雑になる可能性があるため、コンパイラでプリプロセッサ出力を有効にして、前処理が完了した後に実際の出力を確認することをお勧めします。

最初のコード サンプルは大まかに次のようになります (空白が調整され、すべてのマクロ シンボルが展開されているわけではありません)。

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)       ) ) )
];
于 2014-06-19T06:44:22.973 に答える