UbixOS V2  2.0
netbuf.c
Go to the documentation of this file.
1 
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 "net/opt.h"
46 
47 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
48 
49 #include "net/netbuf.h"
50 #include "net/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 }
135 
146 err_t
147 netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
148 {
149  LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
150  if (buf->p != NULL) {
151  pbuf_free(buf->p);
152  }
154  if (buf->p == NULL) {
155  buf->ptr = NULL;
156  return ERR_MEM;
157  }
158  ((struct pbuf_rom*)buf->p)->payload = dataptr;
159  buf->p->len = buf->p->tot_len = size;
160  buf->ptr = buf->p;
161  return ERR_OK;
162 }
163 
171 void
172 netbuf_chain(struct netbuf *head, struct netbuf *tail)
173 {
174  LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);
175  LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
176  pbuf_cat(head->p, tail->p);
177  head->ptr = head->p;
178  memp_free(MEMP_NETBUF, tail);
179 }
180 
191 err_t
192 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
193 {
194  LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
195  LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
196  LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
197 
198  if (buf->ptr == NULL) {
199  return ERR_BUF;
200  }
201  *dataptr = buf->ptr->payload;
202  *len = buf->ptr->len;
203  return ERR_OK;
204 }
205 
217 s8_t
218 netbuf_next(struct netbuf *buf)
219 {
220  LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);
221  if (buf->ptr->next == NULL) {
222  return -1;
223  }
224  buf->ptr = buf->ptr->next;
225  if (buf->ptr->next == NULL) {
226  return 1;
227  }
228  return 0;
229 }
230 
239 void
240 netbuf_first(struct netbuf *buf)
241 {
242  LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);
243  buf->ptr = buf->p;
244 }
245 
246 #endif /* LWIP_NETCONN */
opt.h
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
ERR_BUF
Definition: err.h:67
u16_t
uint16_t u16_t
Definition: arch.h:124
string.h
netbuf.h
pbuf_rom
Definition: pbuf.h:180
PBUF_TRANSPORT
Definition: pbuf.h:76
pbuf_free
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
memp_malloc
void * memp_malloc(memp_t type)
Definition: memp.c:385
memp_free
void memp_free(memp_t type, void *mem)
Definition: memp.c:469
ERR_MEM
Definition: err.h:65
LWIP_ERROR
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:135
pbuf_alloc
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type)
Definition: pbuf.c:248
buf
Definition: buf.h:35
ERR_ARG
Definition: err.h:96
s8_t
int8_t s8_t
Definition: arch.h:123
pbuf_cat
void pbuf_cat(struct pbuf *head, struct pbuf *tail)
Definition: pbuf.c:841
PBUF_RAM
Definition: pbuf.h:108
ERR_OK
Definition: err.h:63
err_t
s8_t err_t
Definition: err.h:57
memset
void * memset(void *dst, int c, size_t length)
memp.h
PBUF_REF
Definition: pbuf.h:116
NULL
#define NULL
Definition: fat_string.h:17