3 * Network buffer management
\r
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
\r
9 * All rights reserved.
\r
11 * Redistribution and use in source and binary forms, with or without modification,
\r
12 * are permitted provided that the following conditions are met:
\r
14 * 1. Redistributions of source code must retain the above copyright notice,
\r
15 * this list of conditions and the following disclaimer.
\r
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
17 * this list of conditions and the following disclaimer in the documentation
\r
18 * and/or other materials provided with the distribution.
\r
19 * 3. The name of the author may not be used to endorse or promote products
\r
20 * derived from this software without specific prior written permission.
\r
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
33 * This file is part of the lwIP TCP/IP stack.
\r
35 * Author: Adam Dunkels <adam@sics.se>
\r
39 #include "lwip/opt.h"
\r
41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
\r
43 #include "lwip/netbuf.h"
\r
44 #include "lwip/memp.h"
\r
49 * Create (allocate) and initialize a new netbuf.
\r
50 * The netbuf doesn't yet contain a packet buffer!
\r
52 * @return a pointer to a new netbuf
\r
53 * NULL on lack of memory
\r
56 netbuf *netbuf_new(void)
\r
60 buf = memp_malloc(MEMP_NETBUF);
\r
72 * Deallocate a netbuf allocated by netbuf_new().
\r
74 * @param buf pointer to a netbuf allocated by netbuf_new()
\r
77 netbuf_delete(struct netbuf *buf)
\r
80 if (buf->p != NULL) {
\r
82 buf->p = buf->ptr = NULL;
\r
84 memp_free(MEMP_NETBUF, buf);
\r
89 * Allocate memory for a packet buffer for a given netbuf.
\r
91 * @param buf the netbuf for which to allocate a packet buffer
\r
92 * @param size the size of the packet buffer to allocate
\r
93 * @return pointer to the allocated memory
\r
94 * NULL if no memory could be allocated
\r
97 netbuf_alloc(struct netbuf *buf, u16_t size)
\r
99 LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
\r
101 /* Deallocate any previously allocated memory. */
\r
102 if (buf->p != NULL) {
\r
105 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
\r
106 if (buf->p == NULL) {
\r
109 LWIP_ASSERT("check that first pbuf can hold size",
\r
110 (buf->p->len >= size));
\r
112 return buf->p->payload;
\r
116 * Free the packet buffer included in a netbuf
\r
118 * @param buf pointer to the netbuf which contains the packet buffer to free
\r
121 netbuf_free(struct netbuf *buf)
\r
123 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
\r
124 if (buf->p != NULL) {
\r
127 buf->p = buf->ptr = NULL;
\r
131 * Let a netbuf reference existing (non-volatile) data.
\r
133 * @param buf netbuf which should reference the data
\r
134 * @param dataptr pointer to the data to reference
\r
135 * @param size size of the data
\r
136 * @return ERR_OK if data is referenced
\r
137 * ERR_MEM if data couldn't be referenced due to lack of memory
\r
140 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
\r
142 LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
\r
143 if (buf->p != NULL) {
\r
146 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
\r
147 if (buf->p == NULL) {
\r
151 buf->p->payload = (void*)dataptr;
\r
152 buf->p->len = buf->p->tot_len = size;
\r
158 * Chain one netbuf to another (@see pbuf_chain)
\r
160 * @param head the first netbuf
\r
161 * @param tail netbuf to chain after head
\r
164 netbuf_chain(struct netbuf *head, struct netbuf *tail)
\r
166 LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
\r
167 LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
\r
168 pbuf_chain(head->p, tail->p);
\r
169 head->ptr = head->p;
\r
170 memp_free(MEMP_NETBUF, tail);
\r
174 * Get the data pointer and length of the data inside a netbuf.
\r
176 * @param buf netbuf to get the data from
\r
177 * @param dataptr pointer to a void pointer where to store the data pointer
\r
178 * @param len pointer to an u16_t where the length of the data is stored
\r
179 * @return ERR_OK if the information was retreived,
\r
180 * ERR_BUF on error.
\r
183 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
\r
185 LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
\r
186 LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
\r
187 LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
\r
189 if (buf->ptr == NULL) {
\r
192 *dataptr = buf->ptr->payload;
\r
193 *len = buf->ptr->len;
\r
198 * Move the current data pointer of a packet buffer contained in a netbuf
\r
199 * to the next part.
\r
200 * The packet buffer itself is not modified.
\r
202 * @param buf the netbuf to modify
\r
203 * @return -1 if there is no next part
\r
204 * 1 if moved to the next part but now there is no next part
\r
205 * 0 if moved to the next part and there are still more parts
\r
208 netbuf_next(struct netbuf *buf)
\r
210 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);
\r
211 if (buf->ptr->next == NULL) {
\r
214 buf->ptr = buf->ptr->next;
\r
215 if (buf->ptr->next == NULL) {
\r
222 * Move the current data pointer of a packet buffer contained in a netbuf
\r
223 * to the beginning of the packet.
\r
224 * The packet buffer itself is not modified.
\r
226 * @param buf the netbuf to modify
\r
229 netbuf_first(struct netbuf *buf)
\r
231 LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
\r
235 #endif /* LWIP_NETCONN */
\r