From e19f6126060bbe89a162c058e6784657c579805e Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Tue, 4 Dec 2018 11:35:59 +0800 Subject: [PATCH] fix(lwip): fix TCP pbuf memory leak Disable TCP cache and retry function. --- components/lwip/lwip/src/core/timeouts.c | 2 ++ components/lwip/port/esp8266/freertos/heap.c | 34 ++++--------------- .../lwip/port/esp8266/include/lwipopts.h | 6 ++-- .../lwip/port/esp8266/netif/ethernetif.c | 10 +++++- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/components/lwip/lwip/src/core/timeouts.c b/components/lwip/lwip/src/core/timeouts.c index 3f8b9b1f..4981803d 100644 --- a/components/lwip/lwip/src/core/timeouts.c +++ b/components/lwip/lwip/src/core/timeouts.c @@ -427,8 +427,10 @@ again: return; } +#if ESP_TCP extern void send_from_list(); send_from_list(); +#endif #if ESP_UDP udp_sync_proc(); diff --git a/components/lwip/port/esp8266/freertos/heap.c b/components/lwip/port/esp8266/freertos/heap.c index fc6f18d0..5d14dd74 100644 --- a/components/lwip/port/esp8266/freertos/heap.c +++ b/components/lwip/port/esp8266/freertos/heap.c @@ -19,32 +19,12 @@ #include "esp8266/eagle_soc.h" #ifdef ESP_LWIP -/* - * @brief allocate an only DRAM memory block for LWIP pbuf - * - * @param s memory size - * - * @return memory block pointer - */ -void *mem_malloc_ll(size_t s) +#if MEMP_SIZE != 0 +#error "MEMP_SIZE must be 0" +#else /* MEMP_SIZE != 0 */ +size_t memp_malloc_get_size(size_t type) { - void *return_addr = (void *)__builtin_return_address(0); - - return _heap_caps_malloc(s, MALLOC_CAP_8BIT, return_addr, 0); + return memp_pools[type]->size; } - -void *memp_malloc_ll(size_t type) -{ - extern const struct memp_desc* const memp_pools[MEMP_MAX]; - - uint8_t *p; - const struct memp_desc *desc = memp_pools[type]; - - p = (uint8_t *)mem_malloc_ll(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size)); - if (p) - p += MEMP_SIZE; - - return p; -} - -#endif +#endif /* MEMP_SIZE != 0 */ +#endif /* ESP_LWIP */ diff --git a/components/lwip/port/esp8266/include/lwipopts.h b/components/lwip/port/esp8266/include/lwipopts.h index 242d6cef..97a0bb32 100644 --- a/components/lwip/port/esp8266/include/lwipopts.h +++ b/components/lwip/port/esp8266/include/lwipopts.h @@ -213,6 +213,8 @@ #define ESP_LWIP_MEM_DBG 1 #endif +size_t memp_malloc_get_size(size_t type); + /* * @brief allocate an only DRAM memory block for LWIP pbuf * @@ -220,7 +222,7 @@ * * @return memory block pointer */ -void *mem_malloc_ll(size_t s); +#define mem_malloc_ll(s) heap_caps_malloc(s, MALLOC_CAP_8BIT) /* * @brief allocate an only DRAM memory pool for LWIP pbuf @@ -229,7 +231,7 @@ void *mem_malloc_ll(size_t s); * * @return memory pool pointer */ -void *memp_malloc_ll(size_t type); +#define memp_malloc_ll(type) heap_caps_malloc(memp_malloc_get_size(type), MALLOC_CAP_8BIT) #endif /** diff --git a/components/lwip/port/esp8266/netif/ethernetif.c b/components/lwip/port/esp8266/netif/ethernetif.c index 9f89deff..a35a6808 100644 --- a/components/lwip/port/esp8266/netif/ethernetif.c +++ b/components/lwip/port/esp8266/netif/ethernetif.c @@ -34,7 +34,7 @@ void wifi_station_set_default_hostname(uint8_t* hwaddr); #define IFNAME0 'e' #define IFNAME1 'n' - +#if ESP_TCP typedef struct pbuf_send_list { struct pbuf_send_list* next; struct pbuf* p; @@ -44,8 +44,10 @@ typedef struct pbuf_send_list { static pbuf_send_list_t* pbuf_list_head = NULL; static int pbuf_send_list_num = 0; +#endif static int low_level_send_cb(esp_aio_t* aio); +#if ESP_TCP static inline bool check_pbuf_to_insert(struct pbuf* p) { uint8_t* buf = (uint8_t*)p->payload; @@ -172,6 +174,7 @@ void send_from_list() } } } +#endif /** * In this function, the hardware should be initialized. @@ -213,6 +216,8 @@ static void low_level_init(struct netif* netif) static int low_level_send_cb(esp_aio_t* aio) { struct pbuf* pbuf = aio->arg; + +#if ESP_TCP wifi_tx_status_t* status = (wifi_tx_status_t*) & (aio->ret); if ((TX_STATUS_SUCCESS != status->wifi_tx_result) && check_pbuf_to_insert(pbuf)) { @@ -235,6 +240,7 @@ static int low_level_send_cb(esp_aio_t* aio) status->wifi_tx_result, status->wifi_tx_lrc, status->wifi_tx_src, status->wifi_tx_rate)); insert_to_list(aio->fd, aio->arg); } +#endif pbuf_free(pbuf); @@ -350,7 +356,9 @@ static int8_t low_level_output(struct netif* netif, struct pbuf* p) if (err != ERR_OK) { if (err == ERR_MEM) { +#if ESP_TCP insert_to_list(aio.fd, p); +#endif err = ERR_OK; }