Skip to content

Commit b2a0b7a

Browse files
authored
lwIP: Refresh pbuf code with cherry-picked upstream (#106)
1 parent e61435a commit b2a0b7a

File tree

3 files changed

+13
-121
lines changed

3 files changed

+13
-121
lines changed

gc/lwip/memp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ typedef enum {
4747
MEMP_NETCONN,
4848
MEMP_API_MSG,
4949
MEMP_TCPIP_MSG,
50-
50+
MEMP_PBUF_POOL,
5151
MEMP_SYS_TIMEOUT,
5252

5353
MEMP_MAX

lwip/core/memp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static const u16_t memp_sizes[MEMP_MAX] = {
6464
sizeof(struct netconn),
6565
sizeof(struct api_msg),
6666
sizeof(struct net_msg),
67+
sizeof(struct pbuf) + PBUF_POOL_BUFSIZE,
6768
sizeof(struct sys_timeout)
6869
};
6970

@@ -78,6 +79,7 @@ static const u16_t memp_num[MEMP_MAX] = {
7879
MEMP_NUM_NETCONN,
7980
MEMP_NUM_API_MSG,
8081
MEMP_NUM_TCPIP_MSG,
82+
PBUF_POOL_SIZE,
8183
MEMP_NUM_SYS_TIMEOUT
8284
};
8385

@@ -111,6 +113,11 @@ static u8_t memp_memory[(MEMP_NUM_PBUF *
111113
MEMP_NUM_TCPIP_MSG *
112114
MEM_ALIGN_SIZE(sizeof(struct net_msg) +
113115
sizeof(struct memp)) +
116+
PBUF_POOL_SIZE *
117+
MEM_ALIGN_SIZE(sizeof(struct pbuf) +
118+
sizeof(struct memp)) +
119+
PBUF_POOL_SIZE *
120+
MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) +
114121
MEMP_NUM_SYS_TIMEOUT *
115122
MEM_ALIGN_SIZE(sizeof(struct sys_timeout) +
116123
sizeof(struct memp)))];

lwip/core/pbuf.c

Lines changed: 5 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@
7373
#include "lwip/sys.h"
7474
#include "arch/perf.h"
7575

76-
static u8_t pbuf_pool_memory[MEM_ALIGNMENT - 1 + PBUF_POOL_SIZE * MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf))];
77-
78-
#if !SYS_LIGHTWEIGHT_PROT
79-
static volatile u8_t pbuf_pool_free_lock, pbuf_pool_alloc_lock;
80-
static sys_sem pbuf_pool_free_sem;
81-
#endif
82-
83-
static struct pbuf *pbuf_pool = NULL;
84-
8576
/**
8677
* Initializes the pbuf module.
8778
*
@@ -97,85 +88,6 @@ static struct pbuf *pbuf_pool = NULL;
9788
void
9889
pbuf_init(void)
9990
{
100-
struct pbuf *p, *q = NULL;
101-
u16_t i;
102-
103-
pbuf_pool = (struct pbuf *)MEM_ALIGN(pbuf_pool_memory);
104-
105-
#if PBUF_STATS
106-
lwip_stats.pbuf.avail = PBUF_POOL_SIZE;
107-
#endif /* PBUF_STATS */
108-
109-
/* Set up ->next pointers to link the pbufs of the pool together */
110-
p = pbuf_pool;
111-
112-
for(i = 0; i < PBUF_POOL_SIZE; ++i) {
113-
p->next = (struct pbuf *)((u8_t *)p + PBUF_POOL_BUFSIZE + sizeof(struct pbuf));
114-
p->len = p->tot_len = PBUF_POOL_BUFSIZE;
115-
p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf)));
116-
p->flags = PBUF_FLAG_POOL;
117-
q = p;
118-
p = p->next;
119-
}
120-
121-
/* The ->next pointer of last pbuf is NULL to indicate that there
122-
are no more pbufs in the pool */
123-
q->next = NULL;
124-
125-
#if !SYS_LIGHTWEIGHT_PROT
126-
pbuf_pool_alloc_lock = 0;
127-
pbuf_pool_free_lock = 0;
128-
LWP_SemInit(&pbuf_pool_free_sem,1,1);
129-
#endif
130-
}
131-
132-
/**
133-
* @internal only called from pbuf_alloc()
134-
*/
135-
static struct pbuf *
136-
pbuf_pool_alloc(void)
137-
{
138-
struct pbuf *p = NULL;
139-
140-
SYS_ARCH_DECL_PROTECT(old_level);
141-
SYS_ARCH_PROTECT(old_level);
142-
143-
#if !SYS_LIGHTWEIGHT_PROT
144-
/* Next, check the actual pbuf pool, but if the pool is locked, we
145-
pretend to be out of buffers and return NULL. */
146-
if (pbuf_pool_free_lock) {
147-
#if PBUF_STATS
148-
++lwip_stats.pbuf.alloc_locked;
149-
#endif /* PBUF_STATS */
150-
return NULL;
151-
}
152-
pbuf_pool_alloc_lock = 1;
153-
if (!pbuf_pool_free_lock) {
154-
#endif /* SYS_LIGHTWEIGHT_PROT */
155-
p = pbuf_pool;
156-
if (p) {
157-
pbuf_pool = p->next;
158-
}
159-
#if !SYS_LIGHTWEIGHT_PROT
160-
#if PBUF_STATS
161-
} else {
162-
++lwip_stats.pbuf.alloc_locked;
163-
#endif /* PBUF_STATS */
164-
}
165-
pbuf_pool_alloc_lock = 0;
166-
#endif /* SYS_LIGHTWEIGHT_PROT */
167-
168-
#if PBUF_STATS
169-
if (p != NULL) {
170-
++lwip_stats.pbuf.used;
171-
if (lwip_stats.pbuf.used > lwip_stats.pbuf.max) {
172-
lwip_stats.pbuf.max = lwip_stats.pbuf.used;
173-
}
174-
}
175-
#endif /* PBUF_STATS */
176-
177-
SYS_ARCH_UNPROTECT(old_level);
178-
return p;
17991
}
18092

18193

@@ -241,14 +153,15 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
241153
switch (flag) {
242154
case PBUF_POOL:
243155
/* allocate head of pbuf chain into p */
244-
p = pbuf_pool_alloc();
156+
p = memp_malloc(MEMP_PBUF_POOL);
245157
LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
246158
if (p == NULL) {
247159
#if PBUF_STATS
248160
++lwip_stats.pbuf.err;
249161
#endif /* PBUF_STATS */
250162
return NULL;
251163
}
164+
p->flags = PBUF_FLAG_POOL;
252165
p->next = NULL;
253166

254167
/* make the payload pointer point 'offset' bytes into pbuf data memory */
@@ -270,7 +183,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
270183
rem_len = length - p->len;
271184
/* any remaining pbufs to be allocated? */
272185
while (rem_len > 0) {
273-
q = pbuf_pool_alloc();
186+
q = memp_malloc(MEMP_PBUF_POOL);
274187
if (q == NULL) {
275188
LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_alloc: Out of pbufs in pool.\n"));
276189
#if PBUF_STATS
@@ -281,6 +194,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
281194
/* bail out unsuccesfully */
282195
return NULL;
283196
}
197+
q->flags = PBUF_FLAG_POOL;
284198
q->next = NULL;
285199
/* make previous pbuf point to this pbuf */
286200
r->next = q;
@@ -343,33 +257,6 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
343257
}
344258

345259

346-
#if PBUF_STATS
347-
#define DEC_PBUF_STATS do { --lwip_stats.pbuf.used; } while (0)
348-
#else /* PBUF_STATS */
349-
#define DEC_PBUF_STATS
350-
#endif /* PBUF_STATS */
351-
352-
#define PBUF_POOL_FAST_FREE(p) do { \
353-
p->next = pbuf_pool; \
354-
pbuf_pool = p; \
355-
DEC_PBUF_STATS; \
356-
} while (0)
357-
358-
#if SYS_LIGHTWEIGHT_PROT
359-
#define PBUF_POOL_FREE(p) do { \
360-
SYS_ARCH_DECL_PROTECT(old_level); \
361-
SYS_ARCH_PROTECT(old_level); \
362-
PBUF_POOL_FAST_FREE(p); \
363-
SYS_ARCH_UNPROTECT(old_level); \
364-
} while (0)
365-
#else /* SYS_LIGHTWEIGHT_PROT */
366-
#define PBUF_POOL_FREE(p) do { \
367-
LWP_SemWait(pbuf_pool_free_sem); \
368-
PBUF_POOL_FAST_FREE(p); \
369-
LWP_SemPost(pbuf_pool_free_sem); \
370-
} while (0)
371-
#endif /* SYS_LIGHTWEIGHT_PROT */
372-
373260
/**
374261
* Shrink a pbuf chain to a desired length.
375262
*
@@ -581,9 +468,7 @@ pbuf_free(struct pbuf *p)
581468
LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p));
582469
/* is this a pbuf from the pool? */
583470
if (p->flags == PBUF_FLAG_POOL) {
584-
p->len = p->tot_len = PBUF_POOL_BUFSIZE;
585-
p->payload = (void *)((u8_t *)p + sizeof(struct pbuf));
586-
PBUF_POOL_FREE(p);
471+
memp_free(MEMP_PBUF_POOL, p);
587472
/* is this a ROM or RAM referencing pbuf? */
588473
} else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) {
589474
memp_free(MEMP_PBUF, p);

0 commit comments

Comments
 (0)