The Pedigree Project 0.1
netbuf.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
32/*
33 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without modification,
37 * are permitted provided that the following conditions are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
50 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
52 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
56 * OF SUCH DAMAGE.
57 *
58 * This file is part of the lwIP TCP/IP stack.
59 *
60 * Author: Adam Dunkels <adam@sics.se>
61 *
62 */
63
64#include "lwip/opt.h"
65
66#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
67
68#include "lwip/netbuf.h"
69#include "lwip/memp.h"
70
71#include <string.h>
72
81struct
82netbuf *netbuf_new(void)
83{
84 struct netbuf *buf;
85
86 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
87 if (buf != NULL) {
88 memset(buf, 0, sizeof(struct netbuf));
89 }
90 return buf;
91}
92
99void
100netbuf_delete(struct netbuf *buf)
101{
102 if (buf != NULL) {
103 if (buf->p != NULL) {
104 pbuf_free(buf->p);
105 buf->p = buf->ptr = NULL;
106 }
107 memp_free(MEMP_NETBUF, buf);
108 }
109}
110
120void *
121netbuf_alloc(struct netbuf *buf, u16_t size)
122{
123 LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
124
125 /* Deallocate any previously allocated memory. */
126 if (buf->p != NULL) {
127 pbuf_free(buf->p);
128 }
129 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
130 if (buf->p == NULL) {
131 return NULL;
132 }
133 LWIP_ASSERT("check that first pbuf can hold size",
134 (buf->p->len >= size));
135 buf->ptr = buf->p;
136 return buf->p->payload;
137}
138
145void
146netbuf_free(struct netbuf *buf)
147{
148 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
149 if (buf->p != NULL) {
150 pbuf_free(buf->p);
151 }
152 buf->p = buf->ptr = NULL;
153}
154
165err_t
166netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
167{
168 LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
169 if (buf->p != NULL) {
170 pbuf_free(buf->p);
171 }
172 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
173 if (buf->p == NULL) {
174 buf->ptr = NULL;
175 return ERR_MEM;
176 }
177 ((struct pbuf_rom*)buf->p)->payload = dataptr;
178 buf->p->len = buf->p->tot_len = size;
179 buf->ptr = buf->p;
180 return ERR_OK;
181}
182
190void
191netbuf_chain(struct netbuf *head, struct netbuf *tail)
192{
193 LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
194 LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
195 pbuf_cat(head->p, tail->p);
196 head->ptr = head->p;
197 memp_free(MEMP_NETBUF, tail);
198}
199
210err_t
211netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
212{
213 LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
214 LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
215 LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
216
217 if (buf->ptr == NULL) {
218 return ERR_BUF;
219 }
220 *dataptr = buf->ptr->payload;
221 *len = buf->ptr->len;
222 return ERR_OK;
223}
224
236s8_t
237netbuf_next(struct netbuf *buf)
238{
239 LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
240 if (buf->ptr->next == NULL) {
241 return -1;
242 }
243 buf->ptr = buf->ptr->next;
244 if (buf->ptr->next == NULL) {
245 return 1;
246 }
247 return 0;
248}
249
258void
259netbuf_first(struct netbuf *buf)
260{
261 LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
262 buf->ptr = buf->p;
263}
264
265#endif /* LWIP_NETCONN */
s8_t err_t
Definition err.h:76
@ ERR_BUF
Definition err.h:86
@ ERR_OK
Definition err.h:82
@ ERR_MEM
Definition err.h:84
@ ERR_ARG
Definition err.h:115
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition pbuf.c:859
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition pbuf.c:267
u8_t pbuf_free(struct pbuf *p)
Definition pbuf.c:734
@ PBUF_RAM
Definition pbuf.h:127
@ PBUF_REF
Definition pbuf.h:135
@ PBUF_TRANSPORT
Definition pbuf.h:95
void * memp_malloc(memp_t type)
Definition memp.c:404
void memp_free(memp_t type, void *mem)
Definition memp.c:488