1
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
92
93
94
95
96
97
98
99
100
101
102
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
142
143
144
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
235
236
237
238
239
240
241
242
243
244
253
254
255
256
257
258
259
260
267
268
269
270
271
272
273
274
275
276
277
278
282
283
284
285
288
289
290
291
292
293
294
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
374
375
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
415
416
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
483
486
487
488
489
492
493
494
495
496
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
555
556
560
561
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
591
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
650
651
655
656
660
661
662
663
664
665
666
667
668
669
670
671
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
738
739
740
741
742
743
745
746
747
748
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
786
787
788
789
790
791
796
797
798
799
800
801
802
808
809
810
811
812
813
814
818
819
820
821
828
829
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
865
866
867
868
869
870
871
872
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
982
983
984
985
986
987
992
998
999
1004
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1043
1044
1045
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1127
1129
1130
1131
1132
1133
1138
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1162
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1210
1211
1212
1213
1214
1215
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1284
1285
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1348
1349
1350
1351
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1444
1445
1446
1450
1451
1452
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1509
1510
1513
1514
/* ... */
/* ... */
/* ... */
#include "lwip/opt.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/sys.h"
#include "lwip/netif.h"
8 includes#if LWIP_TCP && TCP_QUEUE_OOSEQ
#include "lwip/priv/tcp_priv.h"
#endif
#if LWIP_CHECKSUM_ON_COPY
#include "lwip/inet_chksum.h"
#endif
#include <string.h>
#define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
/* ... */
#define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
static const struct pbuf *
pbuf_skip_const(const struct pbuf *in, u16_t in_offset, u16_t *out_offset);
#if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ
#define PBUF_POOL_IS_EMPTY()
#else
#if !NO_SYS
#ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
#include "lwip/tcpip.h"
#define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \
if (tcpip_try_callback(pbuf_free_ooseq_callback, NULL) != ERR_OK) { \
SYS_ARCH_PROTECT(old_level); \
pbuf_free_ooseq_pending = 0; \
SYS_ARCH_UNPROTECT(old_level); \
}if (tcpip_try_callback(pbuf_free_ooseq_callback, NULL) != ERR_OK) { ... } ...} while(0)...
/* ... */#endif /* ... */
#endif
volatile u8_t pbuf_free_ooseq_pending;
#define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
/* ... */
#if !NO_SYS
static
#endif
void
pbuf_free_ooseq(void)
{
struct tcp_pcb *pcb;
SYS_ARCH_SET(pbuf_free_ooseq_pending, 0);
for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
if (pcb->ooseq != NULL) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
tcp_free_ooseq(pcb);
return;
}if (pcb->ooseq != NULL) { ... }
}for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) { ... }
}pbuf_free_ooseq (void) { ... }
#if !NO_SYS
/* ... */
static void
pbuf_free_ooseq_callback(void *arg)
{
LWIP_UNUSED_ARG(arg);
pbuf_free_ooseq();
}pbuf_free_ooseq_callback (void *arg) { ... }
/* ... */#endif
static void
pbuf_pool_is_empty(void)
{
#ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
SYS_ARCH_SET(pbuf_free_ooseq_pending, 1);
#else
u8_t queued;
SYS_ARCH_DECL_PROTECT(old_level);
SYS_ARCH_PROTECT(old_level);
queued = pbuf_free_ooseq_pending;
pbuf_free_ooseq_pending = 1;
SYS_ARCH_UNPROTECT(old_level);
if (!queued) {
PBUF_POOL_FREE_OOSEQ_QUEUE_CALL();
}if (!queued) { ... }
/* ... */#endif
}pbuf_pool_is_empty (void) { ... }
/* ... */#endif
static void
pbuf_init_alloced_pbuf(struct pbuf *p, void *payload, u16_t tot_len, u16_t len, pbuf_type type, u8_t flags)
{
p->next = NULL;
p->payload = payload;
p->tot_len = tot_len;
p->len = len;
p->type_internal = (u8_t)type;
p->flags = flags;
p->ref = 1;
p->if_idx = NETIF_NO_INDEX;
}{ ... }
/* ... */
struct pbuf *
pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
{
struct pbuf *p;
u16_t offset = (u16_t)layer;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
switch (type) {
case PBUF_REF:
case PBUF_ROM:
p = pbuf_alloc_reference(NULL, length, type);
break;case PBUF_ROM:
case PBUF_POOL: {
struct pbuf *q, *last;
u16_t rem_len;
p = NULL;
last = NULL;
rem_len = length;
do {
u16_t qlen;
q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
if (q == NULL) {
PBUF_POOL_IS_EMPTY();
if (p) {
pbuf_free(p);
}if (p) { ... }
return NULL;
}if (q == NULL) { ... }
qlen = LWIP_MIN(rem_len, (u16_t)(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)));
pbuf_init_alloced_pbuf(q, LWIP_MEM_ALIGN((void *)((u8_t *)q + SIZEOF_STRUCT_PBUF + offset)),
rem_len, qlen, type, 0);
LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
(PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
if (p == NULL) {
p = q;
}if (p == NULL) { ... } else {
last->next = q;
}else { ... }
last = q;
rem_len = (u16_t)(rem_len - qlen);
offset = 0;
...} while (rem_len > 0);
break;
...}case PBUF_POOL:
case PBUF_RAM: {
u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len);
if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) ||
(alloc_len < LWIP_MEM_ALIGN_SIZE(length))) {
return NULL;
}if ((payload_len < LWIP_MEM_ALIGN_SIZE(length)) || (alloc_len < LWIP_MEM_ALIGN_SIZE(length))) { ... }
p = (struct pbuf *)mem_malloc(alloc_len);
if (p == NULL) {
return NULL;
}if (p == NULL) { ... }
pbuf_init_alloced_pbuf(p, LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)),
length, length, type, 0);
LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
break;
...}case PBUF_RAM:
default:
LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
return NULL;default
}switch (type) { ... }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
return p;
}{ ... }
/* ... */
struct pbuf *
pbuf_alloc_reference(void *payload, u16_t length, pbuf_type type)
{
struct pbuf *p;
LWIP_ASSERT("invalid pbuf_type", (type == PBUF_REF) || (type == PBUF_ROM));
p = (struct pbuf *)memp_malloc(MEMP_PBUF);
if (p == NULL) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("pbuf_alloc_reference: Could not allocate MEMP_PBUF for PBUF_%s.\n",
(type == PBUF_ROM) ? "ROM" : "REF"));
return NULL;
}if (p == NULL) { ... }
pbuf_init_alloced_pbuf(p, payload, length, length, type, 0);
return p;
}{ ... }
#if LWIP_SUPPORT_CUSTOM_PBUF
/* ... */
struct pbuf *
pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
void *payload_mem, u16_t payload_mem_len)
{
u16_t offset = (u16_t)l;
void *payload;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
return NULL;
}if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) { ... }
if (payload_mem != NULL) {
payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset);
}if (payload_mem != NULL) { ... } else {
payload = NULL;
}else { ... }
pbuf_init_alloced_pbuf(&p->pbuf, payload, length, length, type, PBUF_FLAG_IS_CUSTOM);
return &p->pbuf;
}{ ... }
#endif/* ... */
/* ... */
void
pbuf_realloc(struct pbuf *p, u16_t new_len)
{
struct pbuf *q;
u16_t rem_len;
u16_t shrink;
LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
if (new_len >= p->tot_len) {
return;
}if (new_len >= p->tot_len) { ... }
/* ... */
shrink = (u16_t)(p->tot_len - new_len);
rem_len = new_len;
q = p;
while (rem_len > q->len) {
rem_len = (u16_t)(rem_len - q->len);
q->tot_len = (u16_t)(q->tot_len - shrink);
q = q->next;
LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
}while (rem_len > q->len) { ... }
if (pbuf_match_allocsrc(q, PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP) && (rem_len != q->len)
#if LWIP_SUPPORT_CUSTOM_PBUF
&& ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
#endif
) {
q = (struct pbuf *)mem_trim(q, (mem_size_t)(((u8_t *)q->payload - (u8_t *)q) + rem_len));
LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
}if (pbuf_match_allocsrc(q, PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP) && (rem_len != q->len) #if LWIP_SUPPORT_CUSTOM_PBUF && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0) #endif /* LWIP_SUPPORT_CUSTOM_PBUF */) { ... }
q->len = rem_len;
q->tot_len = q->len;
if (q->next != NULL) {
pbuf_free(q->next);
}if (q->next != NULL) { ... }
q->next = NULL;
}{ ... }
/* ... */
static u8_t
pbuf_add_header_impl(struct pbuf *p, size_t header_size_increment, u8_t force)
{
u16_t type_internal;
void *payload;
u16_t increment_magnitude;
LWIP_ASSERT("p != NULL", p != NULL);
if ((p == NULL) || (header_size_increment > 0xFFFF)) {
return 1;
}if ((p == NULL) || (header_size_increment > 0xFFFF)) { ... }
if (header_size_increment == 0) {
return 0;
}if (header_size_increment == 0) { ... }
increment_magnitude = (u16_t)header_size_increment;
if ((u16_t)(increment_magnitude + p->tot_len) < increment_magnitude) {
return 1;
}if ((u16_t)(increment_magnitude + p->tot_len) < increment_magnitude) { ... }
type_internal = p->type_internal;
if (type_internal & PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS) {
payload = (u8_t *)p->payload - header_size_increment;
if ((u8_t *)payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_add_header: failed as %p < %p (not enough space for new header size)\n",
(void *)payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
return 1;
}if ((u8_t *)payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) { ... }
}if (type_internal & PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS) { ... } else {
if (force) {
payload = (u8_t *)p->payload - header_size_increment;
}if (force) { ... } else {
/* ... */
return 1;
}else { ... }
}else { ... }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_add_header: old %p new %p (%"U16_F")\n",
(void *)p->payload, (void *)payload, increment_magnitude));
p->payload = payload;
p->len = (u16_t)(p->len + increment_magnitude);
p->tot_len = (u16_t)(p->tot_len + increment_magnitude);
return 0;
}{ ... }
/* ... */
u8_t
pbuf_add_header(struct pbuf *p, size_t header_size_increment)
{
return pbuf_add_header_impl(p, header_size_increment, 0);
}{ ... }
/* ... */
u8_t
pbuf_add_header_force(struct pbuf *p, size_t header_size_increment)
{
return pbuf_add_header_impl(p, header_size_increment, 1);
}{ ... }
/* ... */
u8_t
pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
{
void *payload;
u16_t increment_magnitude;
LWIP_ASSERT("p != NULL", p != NULL);
if ((p == NULL) || (header_size_decrement > 0xFFFF)) {
return 1;
}if ((p == NULL) || (header_size_decrement > 0xFFFF)) { ... }
if (header_size_decrement == 0) {
return 0;
}if (header_size_decrement == 0) { ... }
increment_magnitude = (u16_t)header_size_decrement;
LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
payload = p->payload;
LWIP_UNUSED_ARG(payload);
p->payload = (u8_t *)p->payload + header_size_decrement;
p->len = (u16_t)(p->len - increment_magnitude);
p->tot_len = (u16_t)(p->tot_len - increment_magnitude);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_remove_header: old %p new %p (%"U16_F")\n",
(void *)payload, (void *)p->payload, increment_magnitude));
return 0;
}{ ... }
static u8_t
pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
{
if (header_size_increment < 0) {
return pbuf_remove_header(p, (size_t) - header_size_increment);
}if (header_size_increment < 0) { ... } else {
return pbuf_add_header_impl(p, (size_t)header_size_increment, force);
}else { ... }
}{ ... }
/* ... */
u8_t
pbuf_header(struct pbuf *p, s16_t header_size_increment)
{
return pbuf_header_impl(p, header_size_increment, 0);
}{ ... }
/* ... */
u8_t
pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
{
return pbuf_header_impl(p, header_size_increment, 1);
}{ ... }
/* ... */
struct pbuf *
pbuf_free_header(struct pbuf *q, u16_t size)
{
struct pbuf *p = q;
u16_t free_left = size;
while (free_left && p) {
if (free_left >= p->len) {
struct pbuf *f = p;
free_left = (u16_t)(free_left - p->len);
p = p->next;
f->next = 0;
pbuf_free(f);
}if (free_left >= p->len) { ... } else {
pbuf_remove_header(p, free_left);
free_left = 0;
}else { ... }
}while (free_left && p) { ... }
return p;
}{ ... }
/* ... */
u8_t
pbuf_free(struct pbuf *p)
{
u8_t alloc_src;
struct pbuf *q;
u8_t count;
if (p == NULL) {
LWIP_ASSERT("p != NULL", p != NULL);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
("pbuf_free(p == NULL) was called.\n"));
return 0;
}if (p == NULL) { ... }
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
PERF_START;
count = 0;
/* ... */
while (p != NULL) {
LWIP_PBUF_REF_T ref;
SYS_ARCH_DECL_PROTECT(old_level);
/* ... */
SYS_ARCH_PROTECT(old_level);
LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
ref = --(p->ref);
SYS_ARCH_UNPROTECT(old_level);
if (ref == 0) {
q = p->next;
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
alloc_src = pbuf_get_allocsrc(p);
#if LWIP_SUPPORT_CUSTOM_PBUF
if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
struct pbuf_custom *pc = (struct pbuf_custom *)p;
LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
pc->custom_free_function(p);
}if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) { ... } else
#endif
{
if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL) {
memp_free(MEMP_PBUF_POOL, p);
}if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF_POOL) { ... } else if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF) {
memp_free(MEMP_PBUF, p);
}else if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_MEMP_PBUF) { ... } else if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP) {
mem_free(p);
}else if (alloc_src == PBUF_TYPE_ALLOC_SRC_MASK_STD_HEAP) { ... } else {
LWIP_ASSERT("invalid pbuf type", 0);
}else { ... }
}else { ... }
count++;
p = q;
}if (ref == 0) { ... } else {
LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, (u16_t)ref));
p = NULL;
}else { ... }
}while (p != NULL) { ... }
PERF_STOP("pbuf_free");
return count;
}{ ... }
/* ... */
u16_t
pbuf_clen(const struct pbuf *p)
{
u16_t len;
len = 0;
while (p != NULL) {
++len;
p = p->next;
}while (p != NULL) { ... }
return len;
}{ ... }
/* ... */
void
pbuf_ref(struct pbuf *p)
{
if (p != NULL) {
SYS_ARCH_SET(p->ref, (LWIP_PBUF_REF_T)(p->ref + 1));
LWIP_ASSERT("pbuf ref overflow", p->ref > 0);
}if (p != NULL) { ... }
}{ ... }
/* ... */
void
pbuf_cat(struct pbuf *h, struct pbuf *t)
{
struct pbuf *p;
LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
((h != NULL) && (t != NULL)), return;);
for (p = h; p->next != NULL; p = p->next) {
p->tot_len = (u16_t)(p->tot_len + t->tot_len);
}for (p = h; p->next != NULL; p = p->next) { ... }
LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
LWIP_ASSERT("p->next == NULL", p->next == NULL);
p->tot_len = (u16_t)(p->tot_len + t->tot_len);
p->next = t;
/* ... */
}{ ... }
/* ... */
void
pbuf_chain(struct pbuf *h, struct pbuf *t)
{
pbuf_cat(h, t);
pbuf_ref(t);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
}{ ... }
/* ... */
struct pbuf *
pbuf_dechain(struct pbuf *p)
{
struct pbuf *q;
u8_t tail_gone = 1;
q = p->next;
if (q != NULL) {
LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
q->tot_len = (u16_t)(p->tot_len - p->len);
p->next = NULL;
p->tot_len = p->len;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
tail_gone = pbuf_free(q);
if (tail_gone > 0) {
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
}if (tail_gone > 0) { ... }
}if (q != NULL) { ... }
LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
return ((tail_gone > 0) ? NULL : q);
}{ ... }
/* ... */
err_t
pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
{
size_t offset_to = 0, offset_from = 0, len;
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
(const void *)p_to, (const void *)p_from));
LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
(p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
do {
if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
len = p_from->len - offset_from;
}if ((p_to->len - offset_to) >= (p_from->len - offset_from)) { ... } else {
len = p_to->len - offset_to;
}else { ... }
MEMCPY((u8_t *)p_to->payload + offset_to, (u8_t *)p_from->payload + offset_from, len);
offset_to += len;
offset_from += len;
LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
if (offset_from >= p_from->len) {
offset_from = 0;
p_from = p_from->next;
}if (offset_from >= p_from->len) { ... }
if (offset_to == p_to->len) {
offset_to = 0;
p_to = p_to->next;
LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL), return ERR_ARG;);
}if (offset_to == p_to->len) { ... }
if ((p_from != NULL) && (p_from->len == p_from->tot_len)) {
LWIP_ERROR("pbuf_copy() does not allow packet queues!",
(p_from->next == NULL), return ERR_VAL;);
}if ((p_from != NULL) && (p_from->len == p_from->tot_len)) { ... }
if ((p_to != NULL) && (p_to->len == p_to->tot_len)) {
LWIP_ERROR("pbuf_copy() does not allow packet queues!",
(p_to->next == NULL), return ERR_VAL;);
}if ((p_to != NULL) && (p_to->len == p_to->tot_len)) { ... }
...} while (p_from);
LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
return ERR_OK;
}{ ... }
/* ... */
u16_t
pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
{
const struct pbuf *p;
u16_t left = 0;
u16_t buf_copy_len;
u16_t copied_total = 0;
LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
for (p = buf; len != 0 && p != NULL; p = p->next) {
if ((offset != 0) && (offset >= p->len)) {
offset = (u16_t)(offset - p->len);
}if ((offset != 0) && (offset >= p->len)) { ... } else {
buf_copy_len = (u16_t)(p->len - offset);
if (buf_copy_len > len) {
buf_copy_len = len;
}if (buf_copy_len > len) { ... }
MEMCPY(&((char *)dataptr)[left], &((char *)p->payload)[offset], buf_copy_len);
copied_total = (u16_t)(copied_total + buf_copy_len);
left = (u16_t)(left + buf_copy_len);
len = (u16_t)(len - buf_copy_len);
offset = 0;
}else { ... }
}for (p = buf; len != 0 && p != NULL; p = p->next) { ... }
return copied_total;
}{ ... }
/* ... */
void *
pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset)
{
const struct pbuf *q;
u16_t out_offset;
LWIP_ERROR("pbuf_get_contiguous: invalid buf", (p != NULL), return NULL;);
LWIP_ERROR("pbuf_get_contiguous: invalid dataptr", (buffer != NULL), return NULL;);
LWIP_ERROR("pbuf_get_contiguous: invalid dataptr", (bufsize >= len), return NULL;);
q = pbuf_skip_const(p, offset, &out_offset);
if (q != NULL) {
if (q->len >= (out_offset + len)) {
return (u8_t *)q->payload + out_offset;
}if (q->len >= (out_offset + len)) { ... }
if (pbuf_copy_partial(q, buffer, len, out_offset) != len) {
return NULL;
}if (pbuf_copy_partial(q, buffer, len, out_offset) != len) { ... }
return buffer;
}if (q != NULL) { ... }
return NULL;
}{ ... }
#if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
/* ... */
void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
{
*rest = NULL;
if ((p != NULL) && (p->next != NULL)) {
u16_t tot_len_front = p->len;
struct pbuf *i = p;
struct pbuf *r = p->next;
while ((r != NULL) && ((u16_t)(tot_len_front + r->len) >= tot_len_front)) {
tot_len_front = (u16_t)(tot_len_front + r->len);
i = r;
r = r->next;
}while ((r != NULL) && ((u16_t)(tot_len_front + r->len) >= tot_len_front)) { ... }
/* ... */
i->next = NULL;
if (r != NULL) {
for (i = p; i != NULL; i = i->next) {
i->tot_len = (u16_t)(i->tot_len - r->tot_len);
LWIP_ASSERT("tot_len/len mismatch in last pbuf",
(i->next != NULL) || (i->tot_len == i->len));
}for (i = p; i != NULL; i = i->next) { ... }
if (p->flags & PBUF_FLAG_TCP_FIN) {
r->flags |= PBUF_FLAG_TCP_FIN;
}if (p->flags & PBUF_FLAG_TCP_FIN) { ... }
*rest = r;
}if (r != NULL) { ... }
}if ((p != NULL) && (p->next != NULL)) { ... }
}pbuf_split_64k (struct pbuf *p, struct pbuf **rest) { ... }
/* ... */#endif
static const struct pbuf *
pbuf_skip_const(const struct pbuf *in, u16_t in_offset, u16_t *out_offset)
{
u16_t offset_left = in_offset;
const struct pbuf *q = in;
while ((q != NULL) && (q->len <= offset_left)) {
offset_left = (u16_t)(offset_left - q->len);
q = q->next;
}while ((q != NULL) && (q->len <= offset_left)) { ... }
if (out_offset != NULL) {
*out_offset = offset_left;
}if (out_offset != NULL) { ... }
return q;
}{ ... }
/* ... */
struct pbuf *
pbuf_skip(struct pbuf *in, u16_t in_offset, u16_t *out_offset)
{
const struct pbuf *out = pbuf_skip_const(in, in_offset, out_offset);
return LWIP_CONST_CAST(struct pbuf *, out);
}{ ... }
/* ... */
err_t
pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
{
struct pbuf *p;
size_t buf_copy_len;
size_t total_copy_len = len;
size_t copied_total = 0;
LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;);
LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;);
if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
return ERR_ARG;
}if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) { ... }
for (p = buf; total_copy_len != 0; p = p->next) {
LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
buf_copy_len = total_copy_len;
if (buf_copy_len > p->len) {
buf_copy_len = p->len;
}if (buf_copy_len > p->len) { ... }
MEMCPY(p->payload, &((const char *)dataptr)[copied_total], buf_copy_len);
total_copy_len -= buf_copy_len;
copied_total += buf_copy_len;
}for (p = buf; total_copy_len != 0; p = p->next) { ... }
LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
return ERR_OK;
}{ ... }
/* ... */
err_t
pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
{
u16_t target_offset;
struct pbuf *q = pbuf_skip(buf, offset, &target_offset);
if ((q != NULL) && (q->tot_len >= target_offset + len)) {
u16_t remaining_len = len;
const u8_t *src_ptr = (const u8_t *)dataptr;
u16_t first_copy_len;
LWIP_ASSERT("check pbuf_skip result", target_offset < q->len);
first_copy_len = (u16_t)LWIP_MIN(q->len - target_offset, len);
MEMCPY(((u8_t *)q->payload) + target_offset, dataptr, first_copy_len);
remaining_len = (u16_t)(remaining_len - first_copy_len);
src_ptr += first_copy_len;
if (remaining_len > 0) {
return pbuf_take(q->next, src_ptr, remaining_len);
}if (remaining_len > 0) { ... }
return ERR_OK;
}if ((q != NULL) && (q->tot_len >= target_offset + len)) { ... }
return ERR_MEM;
}{ ... }
/* ... */
struct pbuf *
pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
{
struct pbuf *q;
if (p->next == NULL) {
return p;
}if (p->next == NULL) { ... }
q = pbuf_clone(layer, PBUF_RAM, p);
if (q == NULL) {
return p;
}if (q == NULL) { ... }
pbuf_free(p);
return q;
}{ ... }
/* ... */
struct pbuf *
pbuf_clone(pbuf_layer layer, pbuf_type type, struct pbuf *p)
{
struct pbuf *q;
err_t err;
q = pbuf_alloc(layer, p->tot_len, type);
if (q == NULL) {
return NULL;
}if (q == NULL) { ... }
err = pbuf_copy(q, p);
LWIP_UNUSED_ARG(err);
LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
return q;
}{ ... }
#if LWIP_CHECKSUM_ON_COPY
/* ... */
err_t
pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
u16_t len, u16_t *chksum)
{
u32_t acc;
u16_t copy_chksum;
char *dst_ptr;
LWIP_ASSERT("p != NULL", p != NULL);
LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
LWIP_ASSERT("chksum != NULL", chksum != NULL);
LWIP_ASSERT("len != 0", len != 0);
if ((start_offset >= p->len) || (start_offset + len > p->len)) {
return ERR_ARG;
}if ((start_offset >= p->len) || (start_offset + len > p->len)) { ... }
dst_ptr = ((char *)p->payload) + start_offset;
copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
if ((start_offset & 1) != 0) {
copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
}if ((start_offset & 1) != 0) { ... }
acc = *chksum;
acc += copy_chksum;
*chksum = FOLD_U32T(acc);
return ERR_OK;
}pbuf_fill_chksum (struct pbuf *p, u16_t start_offset, const void *dataptr, u16_t len, u16_t *chksum) { ... }
/* ... */#endif
/* ... */
u8_t
pbuf_get_at(const struct pbuf *p, u16_t offset)
{
int ret = pbuf_try_get_at(p, offset);
if (ret >= 0) {
return (u8_t)ret;
}if (ret >= 0) { ... }
return 0;
}{ ... }
/* ... */
int
pbuf_try_get_at(const struct pbuf *p, u16_t offset)
{
u16_t q_idx;
const struct pbuf *q = pbuf_skip_const(p, offset, &q_idx);
if ((q != NULL) && (q->len > q_idx)) {
return ((u8_t *)q->payload)[q_idx];
}if ((q != NULL) && (q->len > q_idx)) { ... }
return -1;
}{ ... }
/* ... */
void
pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
{
u16_t q_idx;
struct pbuf *q = pbuf_skip(p, offset, &q_idx);
if ((q != NULL) && (q->len > q_idx)) {
((u8_t *)q->payload)[q_idx] = data;
}if ((q != NULL) && (q->len > q_idx)) { ... }
}{ ... }
/* ... */
u16_t
pbuf_memcmp(const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
{
u16_t start = offset;
const struct pbuf *q = p;
u16_t i;
if (p->tot_len < (offset + n)) {
return 0xffff;
}if (p->tot_len < (offset + n)) { ... }
while ((q != NULL) && (q->len <= start)) {
start = (u16_t)(start - q->len);
q = q->next;
}while ((q != NULL) && (q->len <= start)) { ... }
for (i = 0; i < n; i++) {
u8_t a = pbuf_get_at(q, (u16_t)(start + i));
u8_t b = ((const u8_t *)s2)[i];
if (a != b) {
return (u16_t)LWIP_MIN(i + 1, 0xFFFF);
}if (a != b) { ... }
}for (i = 0; i < n; i++) { ... }
return 0;
}{ ... }
/* ... */
u16_t
pbuf_memfind(const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
{
u16_t i;
u16_t max_cmp_start = (u16_t)(p->tot_len - mem_len);
if (p->tot_len >= mem_len + start_offset) {
for (i = start_offset; i <= max_cmp_start; i++) {
u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
if (plus == 0) {
return i;
}if (plus == 0) { ... }
}for (i = start_offset; i <= max_cmp_start; i++) { ... }
}if (p->tot_len >= mem_len + start_offset) { ... }
return 0xFFFF;
}{ ... }
/* ... */
u16_t
pbuf_strstr(const struct pbuf *p, const char *substr)
{
size_t substr_len;
if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
return 0xFFFF;
}if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) { ... }
substr_len = strlen(substr);
if (substr_len >= 0xFFFF) {
return 0xFFFF;
}if (substr_len >= 0xFFFF) { ... }
return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
}{ ... }