OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP_130/] [src/] [api/] [netbuf.c] - Blame information for rev 606

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/**
2
 * @file
3
 * Network buffer management
4
 *
5
 */
6
 
7
/*
8
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without modification,
12
 * are permitted provided that the following conditions are met:
13
 *
14
 * 1. Redistributions of source code must retain the above copyright notice,
15
 *    this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
17
 *    this list of conditions and the following disclaimer in the documentation
18
 *    and/or other materials provided with the distribution.
19
 * 3. The name of the author may not be used to endorse or promote products
20
 *    derived from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31
 * OF SUCH DAMAGE.
32
 *
33
 * This file is part of the lwIP TCP/IP stack.
34
 *
35
 * Author: Adam Dunkels <adam@sics.se>
36
 *
37
 */
38
 
39
#include "lwip/opt.h"
40
 
41
#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
42
 
43
#include "lwip/netbuf.h"
44
#include "lwip/memp.h"
45
 
46
#include <string.h>
47
 
48
/**
49
 * Create (allocate) and initialize a new netbuf.
50
 * The netbuf doesn't yet contain a packet buffer!
51
 *
52
 * @return a pointer to a new netbuf
53
 *         NULL on lack of memory
54
 */
55
struct
56
netbuf *netbuf_new(void)
57
{
58
  struct netbuf *buf;
59
 
60
  buf = memp_malloc(MEMP_NETBUF);
61
  if (buf != NULL) {
62
    buf->p = NULL;
63
    buf->ptr = NULL;
64
    buf->addr = NULL;
65
    return buf;
66
  } else {
67
    return NULL;
68
  }
69
}
70
 
71
/**
72
 * Deallocate a netbuf allocated by netbuf_new().
73
 *
74
 * @param buf pointer to a netbuf allocated by netbuf_new()
75
 */
76
void
77
netbuf_delete(struct netbuf *buf)
78
{
79
  if (buf != NULL) {
80
    if (buf->p != NULL) {
81
      pbuf_free(buf->p);
82
      buf->p = buf->ptr = NULL;
83
    }
84
    memp_free(MEMP_NETBUF, buf);
85
  }
86
}
87
 
88
/**
89
 * Allocate memory for a packet buffer for a given netbuf.
90
 *
91
 * @param buf the netbuf for which to allocate a packet buffer
92
 * @param size the size of the packet buffer to allocate
93
 * @return pointer to the allocated memory
94
 *         NULL if no memory could be allocated
95
 */
96
void *
97
netbuf_alloc(struct netbuf *buf, u16_t size)
98
{
99
  LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);
100
 
101
  /* Deallocate any previously allocated memory. */
102
  if (buf->p != NULL) {
103
    pbuf_free(buf->p);
104
  }
105
  buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
106
  if (buf->p == NULL) {
107
     return NULL;
108
  }
109
  LWIP_ASSERT("check that first pbuf can hold size",
110
             (buf->p->len >= size));
111
  buf->ptr = buf->p;
112
  return buf->p->payload;
113
}
114
 
115
/**
116
 * Free the packet buffer included in a netbuf
117
 *
118
 * @param buf pointer to the netbuf which contains the packet buffer to free
119
 */
120
void
121
netbuf_free(struct netbuf *buf)
122
{
123
  LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
124
  if (buf->p != NULL) {
125
    pbuf_free(buf->p);
126
  }
127
  buf->p = buf->ptr = NULL;
128
}
129
 
130
/**
131
 * Let a netbuf reference existing (non-volatile) data.
132
 *
133
 * @param buf netbuf which should reference the data
134
 * @param dataptr pointer to the data to reference
135
 * @param size size of the data
136
 * @return ERR_OK if data is referenced
137
 *         ERR_MEM if data couldn't be referenced due to lack of memory
138
 */
139
err_t
140
netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
141
{
142
  LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);
143
  if (buf->p != NULL) {
144
    pbuf_free(buf->p);
145
  }
146
  buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
147
  if (buf->p == NULL) {
148
    buf->ptr = NULL;
149
    return ERR_MEM;
150
  }
151
  buf->p->payload = (void*)dataptr;
152
  buf->p->len = buf->p->tot_len = size;
153
  buf->ptr = buf->p;
154
  return ERR_OK;
155
}
156
 
157
/**
158
 * Chain one netbuf to another (@see pbuf_chain)
159
 *
160
 * @param head the first netbuf
161
 * @param tail netbuf to chain after head
162
 */
163
void
164
netbuf_chain(struct netbuf *head, struct netbuf *tail)
165
{
166
  LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;);
167
  LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);
168
  pbuf_chain(head->p, tail->p);
169
  head->ptr = head->p;
170
  memp_free(MEMP_NETBUF, tail);
171
}
172
 
173
/**
174
 * Get the data pointer and length of the data inside a netbuf.
175
 *
176
 * @param buf netbuf to get the data from
177
 * @param dataptr pointer to a void pointer where to store the data pointer
178
 * @param len pointer to an u16_t where the length of the data is stored
179
 * @return ERR_OK if the information was retreived,
180
 *         ERR_BUF on error.
181
 */
182
err_t
183
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
184
{
185
  LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);
186
  LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
187
  LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);
188
 
189
  if (buf->ptr == NULL) {
190
    return ERR_BUF;
191
  }
192
  *dataptr = buf->ptr->payload;
193
  *len = buf->ptr->len;
194
  return ERR_OK;
195
}
196
 
197
/**
198
 * Move the current data pointer of a packet buffer contained in a netbuf
199
 * to the next part.
200
 * The packet buffer itself is not modified.
201
 *
202
 * @param buf the netbuf to modify
203
 * @return -1 if there is no next part
204
 *         1  if moved to the next part but now there is no next part
205
 *         0  if moved to the next part and there are still more parts
206
 */
207
s8_t
208
netbuf_next(struct netbuf *buf)
209
{
210
  LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;);
211
  if (buf->ptr->next == NULL) {
212
    return -1;
213
  }
214
  buf->ptr = buf->ptr->next;
215
  if (buf->ptr->next == NULL) {
216
    return 1;
217
  }
218
  return 0;
219
}
220
 
221
/**
222
 * Move the current data pointer of a packet buffer contained in a netbuf
223
 * to the beginning of the packet.
224
 * The packet buffer itself is not modified.
225
 *
226
 * @param buf the netbuf to modify
227
 */
228
void
229
netbuf_first(struct netbuf *buf)
230
{
231
  LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);
232
  buf->ptr = buf->p;
233
}
234
 
235
#endif /* LWIP_NETCONN */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.