sdk-core
v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Toggle main menu visibility
Loading...
Searching...
No Matches
netbuf.c
Go to the documentation of this file.
1
12
13
/*
14
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
15
* All rights reserved.
16
*
17
* Redistribution and use in source and binary forms, with or without modification,
18
* are permitted provided that the following conditions are met:
19
*
20
* 1. Redistributions of source code must retain the above copyright notice,
21
* this list of conditions and the following disclaimer.
22
* 2. Redistributions in binary form must reproduce the above copyright notice,
23
* this list of conditions and the following disclaimer in the documentation
24
* and/or other materials provided with the distribution.
25
* 3. The name of the author may not be used to endorse or promote products
26
* derived from this software without specific prior written permission.
27
*
28
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
29
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
31
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37
* OF SUCH DAMAGE.
38
*
39
* This file is part of the lwIP TCP/IP stack.
40
*
41
* Author: Adam Dunkels <adam@sics.se>
42
*
43
*/
44
45
#include "
lwip/opt.h
"
46
47
#if LWIP_NETCONN
/* don't build if not configured for use in lwipopts.h */
48
49
#include "
lwip/netbuf.h
"
50
#include "
lwip/memp.h
"
51
52
#include <
string.h
>
53
62
struct
63
netbuf *netbuf_new(
void
)
64
{
65
struct
netbuf *buf;
66
67
buf = (
struct
netbuf *)
memp_malloc
(MEMP_NETBUF);
68
if
(buf != NULL) {
69
memset(buf, 0,
sizeof
(
struct
netbuf));
70
}
71
return
buf;
72
}
73
80
void
81
netbuf_delete(
struct
netbuf *buf)
82
{
83
if
(buf != NULL) {
84
if
(buf->p != NULL) {
85
pbuf_free
(buf->p);
86
buf->p = buf->ptr = NULL;
87
}
88
memp_free
(MEMP_NETBUF, buf);
89
}
90
}
91
101
void
*
102
netbuf_alloc(
struct
netbuf *buf, u16_t size)
103
{
104
LWIP_ERROR(
"netbuf_alloc: invalid buf"
, (buf != NULL),
return
NULL;);
105
106
/* Deallocate any previously allocated memory. */
107
if
(buf->p != NULL) {
108
pbuf_free
(buf->p);
109
}
110
buf->p = pbuf_alloc(
PBUF_TRANSPORT
, size,
PBUF_RAM
);
111
if
(buf->p == NULL) {
112
return
NULL;
113
}
114
LWIP_ASSERT(
"check that first pbuf can hold size"
,
115
(buf->p->len >= size));
116
buf->ptr = buf->p;
117
return
buf->p->payload;
118
}
119
126
void
127
netbuf_free(
struct
netbuf *buf)
128
{
129
LWIP_ERROR(
"netbuf_free: invalid buf"
, (buf != NULL),
return
;);
130
if
(buf->p != NULL) {
131
pbuf_free
(buf->p);
132
}
133
buf->p = buf->ptr = NULL;
134
#if LWIP_CHECKSUM_ON_COPY
135
buf->flags = 0;
136
buf->toport_chksum = 0;
137
#endif
/* LWIP_CHECKSUM_ON_COPY */
138
}
139
150
err_t
151
netbuf_ref(
struct
netbuf *buf,
const
void
*dataptr, u16_t size)
152
{
153
LWIP_ERROR(
"netbuf_ref: invalid buf"
, (buf != NULL),
return
ERR_ARG
;);
154
if
(buf->p != NULL) {
155
pbuf_free
(buf->p);
156
}
157
buf->p = pbuf_alloc(
PBUF_TRANSPORT
, 0,
PBUF_REF
);
158
if
(buf->p == NULL) {
159
buf->ptr = NULL;
160
return
ERR_MEM
;
161
}
162
((
struct
pbuf_rom
*)buf->p)->
payload
= dataptr;
163
buf->p->len = buf->p->tot_len = size;
164
buf->ptr = buf->p;
165
return
ERR_OK
;
166
}
167
175
void
176
netbuf_chain(
struct
netbuf *head,
struct
netbuf *tail)
177
{
178
LWIP_ERROR(
"netbuf_chain: invalid head"
, (head != NULL),
return
;);
179
LWIP_ERROR(
"netbuf_chain: invalid tail"
, (tail != NULL),
return
;);
180
pbuf_cat
(head->p, tail->p);
181
head->ptr = head->p;
182
memp_free
(MEMP_NETBUF, tail);
183
}
184
195
err_t
196
netbuf_data(
struct
netbuf *buf,
void
**dataptr, u16_t *len)
197
{
198
LWIP_ERROR(
"netbuf_data: invalid buf"
, (buf != NULL),
return
ERR_ARG
;);
199
LWIP_ERROR(
"netbuf_data: invalid dataptr"
, (dataptr != NULL),
return
ERR_ARG
;);
200
LWIP_ERROR(
"netbuf_data: invalid len"
, (len != NULL),
return
ERR_ARG
;);
201
202
if
(buf->ptr == NULL) {
203
return
ERR_BUF
;
204
}
205
*dataptr = buf->ptr->payload;
206
*len = buf->ptr->len;
207
return
ERR_OK
;
208
}
209
221
s8_t
222
netbuf_next(
struct
netbuf *buf)
223
{
224
LWIP_ERROR(
"netbuf_next: invalid buf"
, (buf != NULL),
return
-1;);
225
if
(buf->ptr->next == NULL) {
226
return
-1;
227
}
228
buf->ptr = buf->ptr->next;
229
if
(buf->ptr->next == NULL) {
230
return
1;
231
}
232
return
0;
233
}
234
243
void
244
netbuf_first(
struct
netbuf *buf)
245
{
246
LWIP_ERROR(
"netbuf_first: invalid buf"
, (buf != NULL),
return
;);
247
buf->ptr = buf->p;
248
}
249
250
#endif
/* LWIP_NETCONN */
ERR_BUF
@ ERR_BUF
Definition
err.h:59
ERR_OK
@ ERR_OK
Definition
err.h:55
ERR_MEM
@ ERR_MEM
Definition
err.h:57
ERR_ARG
@ ERR_ARG
Definition
err.h:88
pbuf_cat
void pbuf_cat(struct pbuf *head, struct pbuf *tail)
Definition
pbuf.c:857
pbuf_free
u8 pbuf_free(struct pbuf *p)
Definition
pbuf.c:729
PBUF_RAM
@ PBUF_RAM
Definition
tcpip.h:214
PBUF_REF
@ PBUF_REF
Definition
tcpip.h:222
PBUF_TRANSPORT
@ PBUF_TRANSPORT
Definition
tcpip.h:182
memp.h
memp_malloc
void * memp_malloc(memp_t type)
Definition
memp.c:337
memp_free
void memp_free(memp_t type, void *mem)
Definition
memp.c:420
netbuf.h
opt.h
string.h
pbuf_rom
Definition
pbuf.h:232
pbuf_rom::payload
const void * payload
Definition
pbuf.h:237
core
ps2ip
src
lwip
api
netbuf.c
Generated by
1.18.0