diff --git a/src/core/mem.c b/src/core/mem.c index 407d1a87..115e126d 100644 --- a/src/core/mem.c +++ b/src/core/mem.c @@ -71,9 +71,9 @@ void * mem_malloc(mem_size_t size) { struct mem_helper *element; - int poolnr; + memp_t poolnr; - for (poolnr = MEMP_MEM_POOL_1; poolnr < (MEMP_MEM_POOL_1 + MEM_POOL_COUNT); poolnr++) { + for (poolnr = MEMP_POOL_START; poolnr <= MEMP_POOL_END; poolnr++) { if ((size + sizeof(struct mem_helper)) <= memp_sizes[poolnr]) { break; } diff --git a/src/core/memp.c b/src/core/memp.c index b70b9d7e..00cd5a55 100644 --- a/src/core/memp.c +++ b/src/core/memp.c @@ -109,79 +109,18 @@ static struct memp *memp_tab[MEMP_MAX]; static #endif const u16_t memp_sizes[MEMP_MAX] = { - MEMP_ALIGN_SIZE(sizeof(struct pbuf)), - MEMP_ALIGN_SIZE(sizeof(struct raw_pcb)), - MEMP_ALIGN_SIZE(sizeof(struct udp_pcb)), - MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb)), - MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb_listen)), - MEMP_ALIGN_SIZE(sizeof(struct tcp_seg)), - MEMP_ALIGN_SIZE(sizeof(struct netbuf)), - MEMP_ALIGN_SIZE(sizeof(struct netconn)), - MEMP_ALIGN_SIZE(sizeof(struct tcpip_msg)), - MEMP_ALIGN_SIZE(sizeof(struct tcpip_msg)), -#if ARP_QUEUEING - MEMP_ALIGN_SIZE(sizeof(struct etharp_q_entry)), -#endif - MEMP_ALIGN_SIZE(sizeof(struct pbuf)) + MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE), - MEMP_ALIGN_SIZE(sizeof(struct sys_timeo)), -#if MEM_USE_POOLS - MEMP_ALIGN_SIZE(MEM_POOL_SIZE_1), - MEMP_ALIGN_SIZE(MEM_POOL_SIZE_2), - MEMP_ALIGN_SIZE(MEM_POOL_SIZE_3), - MEMP_ALIGN_SIZE(MEM_POOL_SIZE_4), -#endif +#define LWIP_MEMPOOL(name,num,size,desc) MEMP_ALIGN_SIZE(size), +#include "lwip/memp_std.h" }; static const u16_t memp_num[MEMP_MAX] = { - MEMP_NUM_PBUF, - MEMP_NUM_RAW_PCB, - MEMP_NUM_UDP_PCB, - MEMP_NUM_TCP_PCB, - MEMP_NUM_TCP_PCB_LISTEN, - MEMP_NUM_TCP_SEG, - MEMP_NUM_NETBUF, - MEMP_NUM_NETCONN, - MEMP_NUM_TCPIP_MSG_API, - MEMP_NUM_TCPIP_MSG_INPKT, -#if ARP_QUEUEING - MEMP_NUM_ARP_QUEUE, -#endif - PBUF_POOL_SIZE, - MEMP_NUM_SYS_TIMEOUT, -#if MEM_USE_POOLS - MEM_POOL_NUM_1, - MEM_POOL_NUM_2, - MEM_POOL_NUM_3, - MEM_POOL_NUM_4, -#endif +#define LWIP_MEMPOOL(name,num,size,desc) (num), +#include "lwip/memp_std.h" }; -#define MEMP_TYPE_SIZE(qty, type) \ - ((qty) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(type)))) - -static u8_t memp_memory[MEM_ALIGNMENT - 1 + - MEMP_TYPE_SIZE(MEMP_NUM_PBUF, struct pbuf) + - MEMP_TYPE_SIZE(MEMP_NUM_RAW_PCB, struct raw_pcb) + - MEMP_TYPE_SIZE(MEMP_NUM_UDP_PCB, struct udp_pcb) + - MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB, struct tcp_pcb) + - MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB_LISTEN, struct tcp_pcb_listen) + - MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) + - MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) + - MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) + - MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG_API, struct tcpip_msg) + - MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG_INPKT, struct tcpip_msg) + -#if ARP_QUEUEING - MEMP_TYPE_SIZE(MEMP_NUM_ARP_QUEUE, struct etharp_q_entry) + -#endif - MEMP_TYPE_SIZE(PBUF_POOL_SIZE, struct pbuf) + - ((PBUF_POOL_SIZE) * MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE)) + - MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo) -#if MEM_USE_POOLS - + ((MEM_POOL_NUM_1) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_1)) - + ((MEM_POOL_NUM_2) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_2)) - + ((MEM_POOL_NUM_3) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_3)) - + ((MEM_POOL_NUM_4) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_4)) -#endif +static u8_t memp_memory[MEM_ALIGNMENT - 1 +#define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) ) +#include "lwip/memp_std.h" ]; #if MEMP_SANITY_CHECK diff --git a/src/core/stats.c b/src/core/stats.c index 9392302b..7da7a417 100644 --- a/src/core/stats.c +++ b/src/core/stats.c @@ -106,26 +106,8 @@ stats_display(void) #if MEMP_STATS s16_t i; char * memp_names[] = { - "PBUF_REF/ROM", - "RAW_PCB", - "UDP_PCB", - "TCP_PCB", - "TCP_PCB_LISTEN", - "TCP_SEG", - "NETBUF", - "NETCONN", - "TCPIP_MSG", -#if ARP_QUEUEING - "ARP_QUEUE", -#endif - "PBUF_POOL", - "SYS_TIMEOUT", -#if MEM_USE_POOLS - "MEMP_MEM_POOL_1", - "MEMP_MEM_POOL_2", - "MEMP_MEM_POOL_3", - "MEMP_MEM_POOL_4", -#endif +#define LWIP_MEMPOOL(name,num,size,desc) desc, +#include "lwip/memp.def" }; #endif #if LINK_STATS diff --git a/src/include/lwip/memp.h b/src/include/lwip/memp.h index 217c5fb8..2d5f41e2 100644 --- a/src/include/lwip/memp.h +++ b/src/include/lwip/memp.h @@ -39,34 +39,44 @@ extern "C" { #endif -/* If you change this enum, think to update memp_names in stats_display (stats.c) */ +/* Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */ typedef enum { - MEMP_PBUF, - MEMP_RAW_PCB, - MEMP_UDP_PCB, - MEMP_TCP_PCB, - MEMP_TCP_PCB_LISTEN, - MEMP_TCP_SEG, - - MEMP_NETBUF, - MEMP_NETCONN, - MEMP_TCPIP_MSG_API, - MEMP_TCPIP_MSG_INPKT, -#if ARP_QUEUEING - MEMP_ARP_QUEUE, -#endif - MEMP_PBUF_POOL, - MEMP_SYS_TIMEOUT, -#if MEM_USE_POOLS - MEMP_MEM_POOL_1, - MEMP_MEM_POOL_2, - MEMP_MEM_POOL_3, - MEMP_MEM_POOL_4, -#endif - +#define LWIP_MEMPOOL(name,num,size,desc) MEMP_##name, +#include "lwip/memp_std.h" MEMP_MAX } memp_t; +#if MEM_USE_POOLS +/* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */ +typedef enum { + /* Get the first (via: + MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0)*/ + MEMP_POOL_HELPER_FIRST = ((u8_t) +#define LWIP_MEMPOOL(name,num,size,desc) +#define LWIP_MALLOC_MEMPOOL_START 1 +#define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0 +#define LWIP_MALLOC_MEMPOOL_END +#include "lwip/memp_std.h" + ) , + /* Get the last (via: + MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */ + MEMP_POOL_HELPER_LAST = ((u8_t) +#define LWIP_MEMPOOL(name,num,size,desc) +#define LWIP_MALLOC_MEMPOOL_START +#define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size * +#define LWIP_MALLOC_MEMPOOL_END 1 +#include "lwip/memp_std.h" + ) +} memp_pool_helper_t; + +/* The actual start and stop values are here (cast them over) + We use this helper type and these defines so we can avoid using const memp_t values */ +#define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST) +#define MEMP_POOL_LAST ((memp_t) MEMP_POOL_HELPER_LAST) + +extern const u16_t memp_sizes[MEMP_MAX]; +#endif /* MEM_USE_POOLS */ + void memp_init(void); #if MEMP_OVERFLOW_CHECK @@ -77,10 +87,6 @@ void *memp_malloc(memp_t type); #endif void memp_free(memp_t type, void *mem); -#if MEM_USE_POOLS -extern const u16_t memp_sizes[MEMP_MAX]; -#endif - #ifdef __cplusplus } #endif diff --git a/src/include/lwip/memp_std.h b/src/include/lwip/memp_std.h new file mode 100644 index 00000000..52a80af0 --- /dev/null +++ b/src/include/lwip/memp_std.h @@ -0,0 +1,89 @@ +/* + * SETUP: Make sure we define everything we will need. + * + * We have create three types of pools: + * 1) MEMPOOL - standard pools + * 2) MALLOC_MEMPOOL - to be used by mem_malloc in mem.c + * 3) PBUF_MEMPOOL - a mempool of pbuf's, so include space for the pbuf struct + * + * If the include'r doesn't require any special treatment of each of the types + * above, then will declare #2 & #3 to be just standard mempools. + */ +#ifndef LWIP_MALLOC_MEMPOOL +// This treats "malloc pools" just like any other pool +#define LWIP_MALLOC_MEMPOOL(num, size) LWIP_MEMPOOL(POOL_##size, num, size, "MALLOC_"#size) +#define LWIP_MALLOC_MEMPOOL_START +#define LWIP_MALLOC_MEMPOOL_END +#endif /* LWIP_MALLOC_MEMPOOL */ + +#ifndef LWIP_PBUF_MEMPOOL +// This treats "pbuf pools" just like any other pool. +// Allocates buffers for a pbuf struct AND a payload size +#define LWIP_PBUF_MEMPOOL(name, num, payload, desc) LWIP_MEMPOOL(name, num, (MEMP_ALIGN_SIZE(sizeof(struct pbuf)) + MEMP_ALIGN_SIZE(payload)), desc) +#endif /* LWIP_PBUF_MEMPOOL */ + + +/* + * A list of internal pools used by LWIP. + * + * LWIP_MEMPOOL(pool_name, number_elements, element_size, pool_description) + * creates a pool name MEMP_pool_name. description is used in stats.c + */ +#if LWIP_RAW +LWIP_MEMPOOL(RAW_PCB, MEMP_NUM_RAW_PCB, sizeof(struct raw_pcb), "RAW_PCB") +#endif + +#if LWIP_UDP +LWIP_MEMPOOL(UDP_PCB, MEMP_NUM_UDP_PCB, sizeof(struct udp_pcb), "UDP_PCB") +#endif + +#if LWIP_TCP +LWIP_MEMPOOL(TCP_PCB, MEMP_NUM_TCP_PCB, sizeof(struct tcp_pcb), "TCP_PCB") +LWIP_MEMPOOL(TCP_PCB_LISTEN, MEMP_NUM_TCP_PCB_LISTEN, sizeof(struct tcp_pcb_listen), "TCP_PCB_LISTEN") +LWIP_MEMPOOL(TCP_SEG, MEMP_NUM_TCP_SEG, sizeof(struct tcp_seg), "TCP_SEG") +#endif + +LWIP_MEMPOOL(NETBUF, MEMP_NUM_NETBUF, sizeof(struct netbuf), "NETBUF") +LWIP_MEMPOOL(NETCONN, MEMP_NUM_NETCONN, sizeof(struct netconn), "NETCONN") + +LWIP_MEMPOOL(TCPIP_MSG_API, MEMP_NUM_TCPIP_MSG, sizeof(struct tcpip_msg), "TCPIP_MSG_API") +LWIP_MEMPOOL(TCPIP_MSG_INPKT,MEMP_NUM_TCPIP_MSG, sizeof(struct tcpip_msg), "TCPIP_MSG_INPKT") + +#if ARP_QUEUEING +LWIP_MEMPOOL(ARP_QUEUE, MEMP_NUM_ARP_QUEUE, sizeof(struct etharp_q_entry), "ARP_QUEUE") +#endif + +#if NO_SYS==0 +LWIP_MEMPOOL(SYS_TIMEOUT, MEMP_NUM_SYS_TIMEOUT, sizeof(struct sys_timeo), "SYS_TIMEOUT") +#endif + + +/* + * A list of pools of pbuf's used by LWIP. + * + * LWIP_PBUF_MEMPOOL(pool_name, number_elements, pbuf_payload_size, pool_description) + * creates a pool name MEMP_pool_name. description is used in stats.c + * This allocates enough space for the pbuf struct and a payload. + * (Example: pbuf_payload_size=0 allocates only size for the struct) + */ +LWIP_PBUF_MEMPOOL(PBUF, MEMP_NUM_PBUF, 0, "PBUF_REF/ROM") +LWIP_PBUF_MEMPOOL(PBUF_POOL, PBUF_POOL_SIZE, PBUF_POOL_BUFSIZE, "PBUF_POOL") + + +/* + * Allow for user-defined pools; this must be explicitly set in lwipopts.h + * since the default is to NOT look for lwippools.h + */ +#if MEMP_USE_CUSTOM_POOLS +#include "lwippools.h" +#endif + +/* + * REQUIRED CLEANUP: Clear up so we don't get "multiply defined" error later + * (#undef is ignored for something that is not defined) + */ +#undef LWIP_MEMPOOL +#undef LWIP_MALLOC_MEMPOOL +#undef LWIP_MALLOC_MEMPOOL_START +#undef LWIP_MALLOC_MEMPOOL_END +#undef LWIP_PBUF_MEMPOOL