lwIP: Use tlsf for all memory allocations, reduce periodic timer inte… · niederr/circuitpython@a3b32b8 · GitHub
Skip to content

Commit a3b32b8

Browse files
committed
lwIP: Use tlsf for all memory allocations, reduce periodic timer interval.
Update lwIP to current top of tree with local changes.
1 parent 0a53eb8 commit a3b32b8

6 files changed

Lines changed: 114 additions & 16 deletions

File tree

.gitmodules

Lines changed: 2 additions & 2 deletions

ports/raspberrypi/lib/lwip

Submodule lwip updated 150 files
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2025 Bob Abeles
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include <stddef.h>
10+
#include <stdbool.h>
11+
12+
void *lwip_heap_malloc(size_t size);
13+
void lwip_heap_free(void *ptr);
14+
void *lwip_heap_calloc(size_t num, size_t size);

ports/raspberrypi/lwip_inc/lwipopts.h

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Common settings used in most of the pico_w examples
66
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)
77

8+
#include "lwip_mem.h"
9+
810
// allow override in some examples
911
#ifndef NO_SYS
1012
#define NO_SYS 1
@@ -20,14 +22,62 @@
2022
#define MEM_LIBC_MALLOC 0
2123
#endif
2224
#define MEM_ALIGNMENT 4
23-
#define MEM_SIZE 4000
24-
#define MEMP_NUM_TCP_SEG 32
25-
#define MEMP_NUM_ARP_QUEUE 10
26-
#define PBUF_POOL_SIZE 24
25+
// MEM_USE_POOLS: mem_malloc uses pools of fixed size memory blocks. Default is 0.
26+
#define MEM_USE_POOLS 0
27+
// MEM_USE_POOLS_TRY_BIGGER_POOL: if one pool is empty, try the next bigger pool. Default is 0.
28+
#define MEM_USE_POOLS_TRY_BIGGER_POOL 0
29+
// MEMP_USE_CUSTOM_POOLS: Use custom pools defined in lwippools.h. Default is 0.
30+
#define MEMP_USE_CUSTOM_POOLS 0
31+
// MEMP_MEM_MALLOC: Use mem_malloc() for pool memory. Default is 0.
32+
#define MEMP_MEM_MALLOC 1
33+
#define MEM_CUSTOM_ALLOCATOR 1
34+
#define MEM_CUSTOM_FREE lwip_heap_free
35+
#define MEM_CUSTOM_MALLOC lwip_heap_malloc
36+
#define MEM_CUSTOM_CALLOC lwip_heap_calloc
37+
38+
// MEM_SIZE: The LWIP heap size. Memory for mem_malloc and mem_calloc are allocated from
39+
// this heap. If MEMP_MEM_MALLOC is set to 1, memory for memp_malloc is also allocated from
40+
// this heap; if it is 0, memory is statically pre-allocated for each pool.
41+
// Default is 1600.
42+
#define MEM_SIZE 1600
43+
// MEMP_NUM_PBUF: memp pbufs used when sending from static memory. Default is 16.
44+
#define MEMP_NUM_PBUF 16
45+
// MEMP_NUM_RAW_PCB: Number of raw connection PCBs. Default is 4.
46+
#define MEMP_NUM_RAW_PCB 4
47+
// MEMP_NUM_UDP_PCB: Number of UDP PCBs. Default is 4.
48+
#define MEMP_NUM_UDP_PCB 4
49+
// MEMP_NUM_TCP_PCB: Number of simultaneously active TCP connections. Default is 5.
50+
#define MEMP_NUM_TCP_PCB 5
51+
// MEMP_NUM_TCP_PCB_LISTEN: Number of listening TCP PCBs. Default is 8.
52+
#define MEMP_NUM_TCP_PCB_LISTEN 8
53+
// MEMP_NUM_TCP_SEG: Number of simultaneously queued TCP segments. Default is 16.
54+
#define MEMP_NUM_TCP_SEG 16
55+
// MEMP_NUM_ALTCP_PCB: Number of simultaneously active altcp connections. Default is 5.
56+
#define MEMP_NUM_ALTCP_PCB 5
57+
// MEMP_NUM_REASSDATA: Number of simultaneously IP packets queued for reassembly. Default is 5.
58+
#define MEMP_NUM_REASSDATA 5
59+
// MEMP_NUM_FRAG_PBUF: Number of simultaneously IP fragments. Default is 15.
60+
#define MEMP_NUM_FRAG_PBUF 15
61+
// MEMP_NUM_ARP_QUEUE: Number of simultaneously queued ARP packets. Default is 30.
62+
#define MEMP_NUM_ARP_QUEUE 30
63+
// MEMP_NUM_IGMP_GROUP: Number of simultaneously active IGMP groups. Default is 8.
64+
#define MEMP_NUM_IGMP_GROUP 8
65+
// MEMP_NUM_SYS_TIMEOUT: Number of simultaneously active timeouts.
66+
// Use calculated default based on enabled modules.
67+
68+
// PBUF_POOL_SIZE: Number of pbufs in the pbuf pool. Default is 16.
69+
#define PBUF_POOL_SIZE 16
70+
71+
// LWIP's default 250 ms periodic timer interval is too long, resulting in network
72+
// performance issues. We reduce it to 25 ms giving a slow-timer of 50 ms and a
73+
// fast-timer of 25 ms.
74+
#define TCP_TMR_INTERVAL 25
75+
2776
#define LWIP_ARP 1
2877
#define LWIP_ETHERNET 1
2978
#define LWIP_ICMP 1
3079
#define LWIP_RAW 1
80+
3181
#define TCP_WND (8 * TCP_MSS)
3282
#define TCP_MSS 1460
3383
#define TCP_SND_BUF (8 * TCP_MSS)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2025 Bob Abeles
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include <stdint.h>
8+
#include <string.h>
9+
#include "lib/tlsf/tlsf.h"
10+
#include "lwip_mem.h"
11+
#include "supervisor/port_heap.h"
12+
13+
void *lwip_heap_malloc(size_t size) {
14+
return port_malloc(size, true);
15+
}
16+
17+
void lwip_heap_free(void *ptr) {
18+
port_free(ptr);
19+
}
20+
21+
void *lwip_heap_calloc(size_t num, size_t size) {
22+
void *ptr = lwip_heap_malloc(num * size);
23+
if (ptr != NULL) {
24+
memset(ptr, 0, num * size);
25+
}
26+
return ptr;
27+
}

ports/raspberrypi/supervisor/port.c

Lines changed: 16 additions & 9 deletions

0 commit comments

Comments
 (0)