1
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
62
63
73
74
80
81
91
92
101
102
103
104
105
106
107
110
111
114
115
116
117
118
119
120
126
127
128
129
130
133
134
135
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
157
158
162
163
164
165
166
167
168
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
228
229
232
233
234
235
236
237
238
239
240
241
242
243
244
249
250
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_NETCONN
#include "lwip/netbuf.h"
#include "lwip/memp.h"
#include <string.h>
/* ... */
struct
netbuf *netbuf_new(void)
{
struct netbuf *buf;
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
if (buf != NULL) {
memset(buf, 0, sizeof(struct netbuf));
}if (buf != NULL) { ... }
return buf;
}{ ... }
/* ... */
void
netbuf_delete(struct netbuf *buf)
{
if (buf != NULL) {
if (buf->p != NULL) {
pbuf_free(buf->p);
buf->p = buf->ptr = NULL;
}if (buf->p != NULL) { ... }
memp_free(MEMP_NETBUF, buf);
}if (buf != NULL) { ... }
}{ ... }
/* ... */
void *
netbuf_alloc(struct netbuf *buf, u16_t size)
{
LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
if (buf->p != NULL) {
pbuf_free(buf->p);
}if (buf->p != NULL) { ... }
buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
if (buf->p == NULL) {
return NULL;
}if (buf->p == NULL) { ... }
LWIP_ASSERT("check that first pbuf can hold size",
(buf->p->len >= size));
buf->ptr = buf->p;
return buf->p->payload;
}{ ... }
/* ... */
void
netbuf_free(struct netbuf *buf)
{
LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
if (buf->p != NULL) {
pbuf_free(buf->p);
}if (buf->p != NULL) { ... }
buf->p = buf->ptr = NULL;
#if LWIP_CHECKSUM_ON_COPY
buf->flags = 0;
buf->toport_chksum = 0;/* ... */
#endif
}{ ... }
/* ... */
err_t
netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
{
LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
if (buf->p != NULL) {
pbuf_free(buf->p);
}if (buf->p != NULL) { ... }
buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
if (buf->p == NULL) {
buf->ptr = NULL;
return ERR_MEM;
}if (buf->p == NULL) { ... }
((struct pbuf_rom *)buf->p)->payload = dataptr;
buf->p->len = buf->p->tot_len = size;
buf->ptr = buf->p;
return ERR_OK;
}{ ... }
/* ... */
void
netbuf_chain(struct netbuf *head, struct netbuf *tail)
{
LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
pbuf_cat(head->p, tail->p);
head->ptr = head->p;
memp_free(MEMP_NETBUF, tail);
}{ ... }
/* ... */
err_t
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
{
LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
if (buf->ptr == NULL) {
return ERR_BUF;
}if (buf->ptr == NULL) { ... }
*dataptr = buf->ptr->payload;
*len = buf->ptr->len;
return ERR_OK;
}{ ... }
/* ... */
s8_t
netbuf_next(struct netbuf *buf)
{
LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
if (buf->ptr->next == NULL) {
return -1;
}if (buf->ptr->next == NULL) { ... }
buf->ptr = buf->ptr->next;
if (buf->ptr->next == NULL) {
return 1;
}if (buf->ptr->next == NULL) { ... }
return 0;
}{ ... }
/* ... */
void
netbuf_first(struct netbuf *buf)
{
LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
buf->ptr = buf->p;
}{ ... }
/* ... */#endif