73
73
#include "lwip/sys.h"
74
74
#include "arch/perf.h"
75
75
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
-
85
76
/**
86
77
* Initializes the pbuf module.
87
78
*
@@ -97,85 +88,6 @@ static struct pbuf *pbuf_pool = NULL;
97
88
void
98
89
pbuf_init (void )
99
90
{
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 ;
179
91
}
180
92
181
93
@@ -241,14 +153,15 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
241
153
switch (flag ) {
242
154
case PBUF_POOL :
243
155
/* allocate head of pbuf chain into p */
244
- p = pbuf_pool_alloc ( );
156
+ p = memp_malloc ( MEMP_PBUF_POOL );
245
157
LWIP_DEBUGF (PBUF_DEBUG | DBG_TRACE | 3 , ("pbuf_alloc: allocated pbuf %p\n" , (void * )p ));
246
158
if (p == NULL ) {
247
159
#if PBUF_STATS
248
160
++ lwip_stats .pbuf .err ;
249
161
#endif /* PBUF_STATS */
250
162
return NULL ;
251
163
}
164
+ p -> flags = PBUF_FLAG_POOL ;
252
165
p -> next = NULL ;
253
166
254
167
/* 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)
270
183
rem_len = length - p -> len ;
271
184
/* any remaining pbufs to be allocated? */
272
185
while (rem_len > 0 ) {
273
- q = pbuf_pool_alloc ( );
186
+ q = memp_malloc ( MEMP_PBUF_POOL );
274
187
if (q == NULL ) {
275
188
LWIP_DEBUGF (PBUF_DEBUG | 2 , ("pbuf_alloc: Out of pbufs in pool.\n" ));
276
189
#if PBUF_STATS
@@ -281,6 +194,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
281
194
/* bail out unsuccesfully */
282
195
return NULL ;
283
196
}
197
+ q -> flags = PBUF_FLAG_POOL ;
284
198
q -> next = NULL ;
285
199
/* make previous pbuf point to this pbuf */
286
200
r -> next = q ;
@@ -343,33 +257,6 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
343
257
}
344
258
345
259
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
-
373
260
/**
374
261
* Shrink a pbuf chain to a desired length.
375
262
*
@@ -581,9 +468,7 @@ pbuf_free(struct pbuf *p)
581
468
LWIP_DEBUGF ( PBUF_DEBUG | 2 , ("pbuf_free: deallocating %p\n" , (void * )p ));
582
469
/* is this a pbuf from the pool? */
583
470
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 );
587
472
/* is this a ROM or RAM referencing pbuf? */
588
473
} else if (p -> flags == PBUF_FLAG_ROM || p -> flags == PBUF_FLAG_REF ) {
589
474
memp_free (MEMP_PBUF , p );
0 commit comments