Dirk-Willem van Gulik (NXP/mbed)
/
AmpereMeter
Example self-announcing webserver which controls a servo through a smallHTML userinterface.
lwip/core/pbuf.c@0:a259777c45a3, 2010-08-14 (annotated)
- Committer:
- dirkx
- Date:
- Sat Aug 14 15:56:01 2010 +0000
- Revision:
- 0:a259777c45a3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dirkx | 0:a259777c45a3 | 1 | /** |
dirkx | 0:a259777c45a3 | 2 | * @file |
dirkx | 0:a259777c45a3 | 3 | * Packet buffer management |
dirkx | 0:a259777c45a3 | 4 | * |
dirkx | 0:a259777c45a3 | 5 | * Packets are built from the pbuf data structure. It supports dynamic |
dirkx | 0:a259777c45a3 | 6 | * memory allocation for packet contents or can reference externally |
dirkx | 0:a259777c45a3 | 7 | * managed packet contents both in RAM and ROM. Quick allocation for |
dirkx | 0:a259777c45a3 | 8 | * incoming packets is provided through pools with fixed sized pbufs. |
dirkx | 0:a259777c45a3 | 9 | * |
dirkx | 0:a259777c45a3 | 10 | * A packet may span over multiple pbufs, chained as a singly linked |
dirkx | 0:a259777c45a3 | 11 | * list. This is called a "pbuf chain". |
dirkx | 0:a259777c45a3 | 12 | * |
dirkx | 0:a259777c45a3 | 13 | * Multiple packets may be queued, also using this singly linked list. |
dirkx | 0:a259777c45a3 | 14 | * This is called a "packet queue". |
dirkx | 0:a259777c45a3 | 15 | * |
dirkx | 0:a259777c45a3 | 16 | * So, a packet queue consists of one or more pbuf chains, each of |
dirkx | 0:a259777c45a3 | 17 | * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE |
dirkx | 0:a259777c45a3 | 18 | * NOT SUPPORTED!!! Use helper structs to queue multiple packets. |
dirkx | 0:a259777c45a3 | 19 | * |
dirkx | 0:a259777c45a3 | 20 | * The differences between a pbuf chain and a packet queue are very |
dirkx | 0:a259777c45a3 | 21 | * precise but subtle. |
dirkx | 0:a259777c45a3 | 22 | * |
dirkx | 0:a259777c45a3 | 23 | * The last pbuf of a packet has a ->tot_len field that equals the |
dirkx | 0:a259777c45a3 | 24 | * ->len field. It can be found by traversing the list. If the last |
dirkx | 0:a259777c45a3 | 25 | * pbuf of a packet has a ->next field other than NULL, more packets |
dirkx | 0:a259777c45a3 | 26 | * are on the queue. |
dirkx | 0:a259777c45a3 | 27 | * |
dirkx | 0:a259777c45a3 | 28 | * Therefore, looping through a pbuf of a single packet, has an |
dirkx | 0:a259777c45a3 | 29 | * loop end condition (tot_len == p->len), NOT (next == NULL). |
dirkx | 0:a259777c45a3 | 30 | */ |
dirkx | 0:a259777c45a3 | 31 | |
dirkx | 0:a259777c45a3 | 32 | /* |
dirkx | 0:a259777c45a3 | 33 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
dirkx | 0:a259777c45a3 | 34 | * All rights reserved. |
dirkx | 0:a259777c45a3 | 35 | * |
dirkx | 0:a259777c45a3 | 36 | * Redistribution and use in source and binary forms, with or without modification, |
dirkx | 0:a259777c45a3 | 37 | * are permitted provided that the following conditions are met: |
dirkx | 0:a259777c45a3 | 38 | * |
dirkx | 0:a259777c45a3 | 39 | * 1. Redistributions of source code must retain the above copyright notice, |
dirkx | 0:a259777c45a3 | 40 | * this list of conditions and the following disclaimer. |
dirkx | 0:a259777c45a3 | 41 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
dirkx | 0:a259777c45a3 | 42 | * this list of conditions and the following disclaimer in the documentation |
dirkx | 0:a259777c45a3 | 43 | * and/or other materials provided with the distribution. |
dirkx | 0:a259777c45a3 | 44 | * 3. The name of the author may not be used to endorse or promote products |
dirkx | 0:a259777c45a3 | 45 | * derived from this software without specific prior written permission. |
dirkx | 0:a259777c45a3 | 46 | * |
dirkx | 0:a259777c45a3 | 47 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
dirkx | 0:a259777c45a3 | 48 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
dirkx | 0:a259777c45a3 | 49 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
dirkx | 0:a259777c45a3 | 50 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
dirkx | 0:a259777c45a3 | 51 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
dirkx | 0:a259777c45a3 | 52 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
dirkx | 0:a259777c45a3 | 53 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
dirkx | 0:a259777c45a3 | 54 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
dirkx | 0:a259777c45a3 | 55 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
dirkx | 0:a259777c45a3 | 56 | * OF SUCH DAMAGE. |
dirkx | 0:a259777c45a3 | 57 | * |
dirkx | 0:a259777c45a3 | 58 | * This file is part of the lwIP TCP/IP stack. |
dirkx | 0:a259777c45a3 | 59 | * |
dirkx | 0:a259777c45a3 | 60 | * Author: Adam Dunkels <adam@sics.se> |
dirkx | 0:a259777c45a3 | 61 | * |
dirkx | 0:a259777c45a3 | 62 | */ |
dirkx | 0:a259777c45a3 | 63 | |
dirkx | 0:a259777c45a3 | 64 | #include "lwip/opt.h" |
dirkx | 0:a259777c45a3 | 65 | |
dirkx | 0:a259777c45a3 | 66 | #include "lwip/stats.h" |
dirkx | 0:a259777c45a3 | 67 | #include "lwip/def.h" |
dirkx | 0:a259777c45a3 | 68 | #include "lwip/mem.h" |
dirkx | 0:a259777c45a3 | 69 | #include "lwip/memp.h" |
dirkx | 0:a259777c45a3 | 70 | #include "lwip/pbuf.h" |
dirkx | 0:a259777c45a3 | 71 | #include "lwip/sys.h" |
dirkx | 0:a259777c45a3 | 72 | #include "arch/perf.h" |
dirkx | 0:a259777c45a3 | 73 | #if TCP_QUEUE_OOSEQ |
dirkx | 0:a259777c45a3 | 74 | #include "lwip/tcp_impl.h" |
dirkx | 0:a259777c45a3 | 75 | #endif |
dirkx | 0:a259777c45a3 | 76 | |
dirkx | 0:a259777c45a3 | 77 | #include <string.h> |
dirkx | 0:a259777c45a3 | 78 | |
dirkx | 0:a259777c45a3 | 79 | #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf)) |
dirkx | 0:a259777c45a3 | 80 | /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically |
dirkx | 0:a259777c45a3 | 81 | aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */ |
dirkx | 0:a259777c45a3 | 82 | #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) |
dirkx | 0:a259777c45a3 | 83 | |
dirkx | 0:a259777c45a3 | 84 | #if !TCP_QUEUE_OOSEQ || NO_SYS |
dirkx | 0:a259777c45a3 | 85 | #define PBUF_POOL_IS_EMPTY() |
dirkx | 0:a259777c45a3 | 86 | #else /* !TCP_QUEUE_OOSEQ || NO_SYS */ |
dirkx | 0:a259777c45a3 | 87 | /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */ |
dirkx | 0:a259777c45a3 | 88 | #ifndef PBUF_POOL_FREE_OOSEQ |
dirkx | 0:a259777c45a3 | 89 | #define PBUF_POOL_FREE_OOSEQ 1 |
dirkx | 0:a259777c45a3 | 90 | #endif /* PBUF_POOL_FREE_OOSEQ */ |
dirkx | 0:a259777c45a3 | 91 | |
dirkx | 0:a259777c45a3 | 92 | #if PBUF_POOL_FREE_OOSEQ |
dirkx | 0:a259777c45a3 | 93 | #include "lwip/tcpip.h" |
dirkx | 0:a259777c45a3 | 94 | #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty() |
dirkx | 0:a259777c45a3 | 95 | static u8_t pbuf_free_ooseq_queued; |
dirkx | 0:a259777c45a3 | 96 | /** |
dirkx | 0:a259777c45a3 | 97 | * Attempt to reclaim some memory from queued out-of-sequence TCP segments |
dirkx | 0:a259777c45a3 | 98 | * if we run out of pool pbufs. It's better to give priority to new packets |
dirkx | 0:a259777c45a3 | 99 | * if we're running out. |
dirkx | 0:a259777c45a3 | 100 | * |
dirkx | 0:a259777c45a3 | 101 | * This must be done in the correct thread context therefore this function |
dirkx | 0:a259777c45a3 | 102 | * can only be used with NO_SYS=0 and through tcpip_callback. |
dirkx | 0:a259777c45a3 | 103 | */ |
dirkx | 0:a259777c45a3 | 104 | static void |
dirkx | 0:a259777c45a3 | 105 | pbuf_free_ooseq(void* arg) |
dirkx | 0:a259777c45a3 | 106 | { |
dirkx | 0:a259777c45a3 | 107 | struct tcp_pcb* pcb; |
dirkx | 0:a259777c45a3 | 108 | SYS_ARCH_DECL_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 109 | LWIP_UNUSED_ARG(arg); |
dirkx | 0:a259777c45a3 | 110 | |
dirkx | 0:a259777c45a3 | 111 | SYS_ARCH_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 112 | pbuf_free_ooseq_queued = 0; |
dirkx | 0:a259777c45a3 | 113 | SYS_ARCH_UNPROTECT(old_level); |
dirkx | 0:a259777c45a3 | 114 | |
dirkx | 0:a259777c45a3 | 115 | for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) { |
dirkx | 0:a259777c45a3 | 116 | if (NULL != pcb->ooseq) { |
dirkx | 0:a259777c45a3 | 117 | /** Free the ooseq pbufs of one PCB only */ |
dirkx | 0:a259777c45a3 | 118 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n")); |
dirkx | 0:a259777c45a3 | 119 | tcp_segs_free(pcb->ooseq); |
dirkx | 0:a259777c45a3 | 120 | pcb->ooseq = NULL; |
dirkx | 0:a259777c45a3 | 121 | return; |
dirkx | 0:a259777c45a3 | 122 | } |
dirkx | 0:a259777c45a3 | 123 | } |
dirkx | 0:a259777c45a3 | 124 | } |
dirkx | 0:a259777c45a3 | 125 | |
dirkx | 0:a259777c45a3 | 126 | /** Queue a call to pbuf_free_ooseq if not already queued. */ |
dirkx | 0:a259777c45a3 | 127 | static void |
dirkx | 0:a259777c45a3 | 128 | pbuf_pool_is_empty(void) |
dirkx | 0:a259777c45a3 | 129 | { |
dirkx | 0:a259777c45a3 | 130 | u8_t queued; |
dirkx | 0:a259777c45a3 | 131 | SYS_ARCH_DECL_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 132 | |
dirkx | 0:a259777c45a3 | 133 | SYS_ARCH_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 134 | queued = pbuf_free_ooseq_queued; |
dirkx | 0:a259777c45a3 | 135 | pbuf_free_ooseq_queued = 1; |
dirkx | 0:a259777c45a3 | 136 | SYS_ARCH_UNPROTECT(old_level); |
dirkx | 0:a259777c45a3 | 137 | |
dirkx | 0:a259777c45a3 | 138 | if(!queued) { |
dirkx | 0:a259777c45a3 | 139 | /* queue a call to pbuf_free_ooseq if not already queued */ |
dirkx | 0:a259777c45a3 | 140 | if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) { |
dirkx | 0:a259777c45a3 | 141 | SYS_ARCH_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 142 | pbuf_free_ooseq_queued = 0; |
dirkx | 0:a259777c45a3 | 143 | SYS_ARCH_UNPROTECT(old_level); |
dirkx | 0:a259777c45a3 | 144 | } |
dirkx | 0:a259777c45a3 | 145 | } |
dirkx | 0:a259777c45a3 | 146 | } |
dirkx | 0:a259777c45a3 | 147 | #endif /* PBUF_POOL_FREE_OOSEQ */ |
dirkx | 0:a259777c45a3 | 148 | #endif /* !TCP_QUEUE_OOSEQ || NO_SYS */ |
dirkx | 0:a259777c45a3 | 149 | |
dirkx | 0:a259777c45a3 | 150 | /** |
dirkx | 0:a259777c45a3 | 151 | * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type). |
dirkx | 0:a259777c45a3 | 152 | * |
dirkx | 0:a259777c45a3 | 153 | * The actual memory allocated for the pbuf is determined by the |
dirkx | 0:a259777c45a3 | 154 | * layer at which the pbuf is allocated and the requested size |
dirkx | 0:a259777c45a3 | 155 | * (from the size parameter). |
dirkx | 0:a259777c45a3 | 156 | * |
dirkx | 0:a259777c45a3 | 157 | * @param layer flag to define header size |
dirkx | 0:a259777c45a3 | 158 | * @param length size of the pbuf's payload |
dirkx | 0:a259777c45a3 | 159 | * @param type this parameter decides how and where the pbuf |
dirkx | 0:a259777c45a3 | 160 | * should be allocated as follows: |
dirkx | 0:a259777c45a3 | 161 | * |
dirkx | 0:a259777c45a3 | 162 | * - PBUF_RAM: buffer memory for pbuf is allocated as one large |
dirkx | 0:a259777c45a3 | 163 | * chunk. This includes protocol headers as well. |
dirkx | 0:a259777c45a3 | 164 | * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for |
dirkx | 0:a259777c45a3 | 165 | * protocol headers. Additional headers must be prepended |
dirkx | 0:a259777c45a3 | 166 | * by allocating another pbuf and chain in to the front of |
dirkx | 0:a259777c45a3 | 167 | * the ROM pbuf. It is assumed that the memory used is really |
dirkx | 0:a259777c45a3 | 168 | * similar to ROM in that it is immutable and will not be |
dirkx | 0:a259777c45a3 | 169 | * changed. Memory which is dynamic should generally not |
dirkx | 0:a259777c45a3 | 170 | * be attached to PBUF_ROM pbufs. Use PBUF_REF instead. |
dirkx | 0:a259777c45a3 | 171 | * - PBUF_REF: no buffer memory is allocated for the pbuf, even for |
dirkx | 0:a259777c45a3 | 172 | * protocol headers. It is assumed that the pbuf is only |
dirkx | 0:a259777c45a3 | 173 | * being used in a single thread. If the pbuf gets queued, |
dirkx | 0:a259777c45a3 | 174 | * then pbuf_take should be called to copy the buffer. |
dirkx | 0:a259777c45a3 | 175 | * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from |
dirkx | 0:a259777c45a3 | 176 | * the pbuf pool that is allocated during pbuf_init(). |
dirkx | 0:a259777c45a3 | 177 | * |
dirkx | 0:a259777c45a3 | 178 | * @return the allocated pbuf. If multiple pbufs where allocated, this |
dirkx | 0:a259777c45a3 | 179 | * is the first pbuf of a pbuf chain. |
dirkx | 0:a259777c45a3 | 180 | */ |
dirkx | 0:a259777c45a3 | 181 | struct pbuf * |
dirkx | 0:a259777c45a3 | 182 | pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) |
dirkx | 0:a259777c45a3 | 183 | { |
dirkx | 0:a259777c45a3 | 184 | struct pbuf *p, *q, *r; |
dirkx | 0:a259777c45a3 | 185 | u16_t offset; |
dirkx | 0:a259777c45a3 | 186 | s32_t rem_len; /* remaining length */ |
dirkx | 0:a259777c45a3 | 187 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length)); |
dirkx | 0:a259777c45a3 | 188 | |
dirkx | 0:a259777c45a3 | 189 | /* determine header offset */ |
dirkx | 0:a259777c45a3 | 190 | offset = 0; |
dirkx | 0:a259777c45a3 | 191 | switch (layer) { |
dirkx | 0:a259777c45a3 | 192 | case PBUF_TRANSPORT: |
dirkx | 0:a259777c45a3 | 193 | /* add room for transport (often TCP) layer header */ |
dirkx | 0:a259777c45a3 | 194 | offset += PBUF_TRANSPORT_HLEN; |
dirkx | 0:a259777c45a3 | 195 | /* FALLTHROUGH */ |
dirkx | 0:a259777c45a3 | 196 | case PBUF_IP: |
dirkx | 0:a259777c45a3 | 197 | /* add room for IP layer header */ |
dirkx | 0:a259777c45a3 | 198 | offset += PBUF_IP_HLEN; |
dirkx | 0:a259777c45a3 | 199 | /* FALLTHROUGH */ |
dirkx | 0:a259777c45a3 | 200 | case PBUF_LINK: |
dirkx | 0:a259777c45a3 | 201 | /* add room for link layer header */ |
dirkx | 0:a259777c45a3 | 202 | offset += PBUF_LINK_HLEN; |
dirkx | 0:a259777c45a3 | 203 | break; |
dirkx | 0:a259777c45a3 | 204 | case PBUF_RAW: |
dirkx | 0:a259777c45a3 | 205 | break; |
dirkx | 0:a259777c45a3 | 206 | default: |
dirkx | 0:a259777c45a3 | 207 | LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0); |
dirkx | 0:a259777c45a3 | 208 | return NULL; |
dirkx | 0:a259777c45a3 | 209 | } |
dirkx | 0:a259777c45a3 | 210 | |
dirkx | 0:a259777c45a3 | 211 | switch (type) { |
dirkx | 0:a259777c45a3 | 212 | case PBUF_POOL: |
dirkx | 0:a259777c45a3 | 213 | /* allocate head of pbuf chain into p */ |
dirkx | 0:a259777c45a3 | 214 | p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); |
dirkx | 0:a259777c45a3 | 215 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p)); |
dirkx | 0:a259777c45a3 | 216 | if (p == NULL) { |
dirkx | 0:a259777c45a3 | 217 | PBUF_POOL_IS_EMPTY(); |
dirkx | 0:a259777c45a3 | 218 | return NULL; |
dirkx | 0:a259777c45a3 | 219 | } |
dirkx | 0:a259777c45a3 | 220 | p->type = type; |
dirkx | 0:a259777c45a3 | 221 | p->next = NULL; |
dirkx | 0:a259777c45a3 | 222 | |
dirkx | 0:a259777c45a3 | 223 | /* make the payload pointer point 'offset' bytes into pbuf data memory */ |
dirkx | 0:a259777c45a3 | 224 | p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset))); |
dirkx | 0:a259777c45a3 | 225 | LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned", |
dirkx | 0:a259777c45a3 | 226 | ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); |
dirkx | 0:a259777c45a3 | 227 | /* the total length of the pbuf chain is the requested size */ |
dirkx | 0:a259777c45a3 | 228 | p->tot_len = length; |
dirkx | 0:a259777c45a3 | 229 | /* set the length of the first pbuf in the chain */ |
dirkx | 0:a259777c45a3 | 230 | p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)); |
dirkx | 0:a259777c45a3 | 231 | LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", |
dirkx | 0:a259777c45a3 | 232 | ((u8_t*)p->payload + p->len <= |
dirkx | 0:a259777c45a3 | 233 | (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); |
dirkx | 0:a259777c45a3 | 234 | LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT", |
dirkx | 0:a259777c45a3 | 235 | (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 ); |
dirkx | 0:a259777c45a3 | 236 | /* set reference count (needed here in case we fail) */ |
dirkx | 0:a259777c45a3 | 237 | p->ref = 1; |
dirkx | 0:a259777c45a3 | 238 | |
dirkx | 0:a259777c45a3 | 239 | /* now allocate the tail of the pbuf chain */ |
dirkx | 0:a259777c45a3 | 240 | |
dirkx | 0:a259777c45a3 | 241 | /* remember first pbuf for linkage in next iteration */ |
dirkx | 0:a259777c45a3 | 242 | r = p; |
dirkx | 0:a259777c45a3 | 243 | /* remaining length to be allocated */ |
dirkx | 0:a259777c45a3 | 244 | rem_len = length - p->len; |
dirkx | 0:a259777c45a3 | 245 | /* any remaining pbufs to be allocated? */ |
dirkx | 0:a259777c45a3 | 246 | while (rem_len > 0) { |
dirkx | 0:a259777c45a3 | 247 | q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL); |
dirkx | 0:a259777c45a3 | 248 | if (q == NULL) { |
dirkx | 0:a259777c45a3 | 249 | PBUF_POOL_IS_EMPTY(); |
dirkx | 0:a259777c45a3 | 250 | /* free chain so far allocated */ |
dirkx | 0:a259777c45a3 | 251 | pbuf_free(p); |
dirkx | 0:a259777c45a3 | 252 | /* bail out unsuccesfully */ |
dirkx | 0:a259777c45a3 | 253 | return NULL; |
dirkx | 0:a259777c45a3 | 254 | } |
dirkx | 0:a259777c45a3 | 255 | q->type = type; |
dirkx | 0:a259777c45a3 | 256 | q->flags = 0; |
dirkx | 0:a259777c45a3 | 257 | q->next = NULL; |
dirkx | 0:a259777c45a3 | 258 | /* make previous pbuf point to this pbuf */ |
dirkx | 0:a259777c45a3 | 259 | r->next = q; |
dirkx | 0:a259777c45a3 | 260 | /* set total length of this pbuf and next in chain */ |
dirkx | 0:a259777c45a3 | 261 | LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff); |
dirkx | 0:a259777c45a3 | 262 | q->tot_len = (u16_t)rem_len; |
dirkx | 0:a259777c45a3 | 263 | /* this pbuf length is pool size, unless smaller sized tail */ |
dirkx | 0:a259777c45a3 | 264 | q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED); |
dirkx | 0:a259777c45a3 | 265 | q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF); |
dirkx | 0:a259777c45a3 | 266 | LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned", |
dirkx | 0:a259777c45a3 | 267 | ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0); |
dirkx | 0:a259777c45a3 | 268 | LWIP_ASSERT("check p->payload + p->len does not overflow pbuf", |
dirkx | 0:a259777c45a3 | 269 | ((u8_t*)p->payload + p->len <= |
dirkx | 0:a259777c45a3 | 270 | (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED)); |
dirkx | 0:a259777c45a3 | 271 | q->ref = 1; |
dirkx | 0:a259777c45a3 | 272 | /* calculate remaining length to be allocated */ |
dirkx | 0:a259777c45a3 | 273 | rem_len -= q->len; |
dirkx | 0:a259777c45a3 | 274 | /* remember this pbuf for linkage in next iteration */ |
dirkx | 0:a259777c45a3 | 275 | r = q; |
dirkx | 0:a259777c45a3 | 276 | } |
dirkx | 0:a259777c45a3 | 277 | /* end of chain */ |
dirkx | 0:a259777c45a3 | 278 | /*r->next = NULL;*/ |
dirkx | 0:a259777c45a3 | 279 | |
dirkx | 0:a259777c45a3 | 280 | break; |
dirkx | 0:a259777c45a3 | 281 | case PBUF_RAM: |
dirkx | 0:a259777c45a3 | 282 | /* If pbuf is to be allocated in RAM, allocate memory for it. */ |
dirkx | 0:a259777c45a3 | 283 | p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length)); |
dirkx | 0:a259777c45a3 | 284 | if (p == NULL) { |
dirkx | 0:a259777c45a3 | 285 | return NULL; |
dirkx | 0:a259777c45a3 | 286 | } |
dirkx | 0:a259777c45a3 | 287 | /* Set up internal structure of the pbuf. */ |
dirkx | 0:a259777c45a3 | 288 | p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset)); |
dirkx | 0:a259777c45a3 | 289 | p->len = p->tot_len = length; |
dirkx | 0:a259777c45a3 | 290 | p->next = NULL; |
dirkx | 0:a259777c45a3 | 291 | p->type = type; |
dirkx | 0:a259777c45a3 | 292 | |
dirkx | 0:a259777c45a3 | 293 | LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned", |
dirkx | 0:a259777c45a3 | 294 | ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0); |
dirkx | 0:a259777c45a3 | 295 | break; |
dirkx | 0:a259777c45a3 | 296 | /* pbuf references existing (non-volatile static constant) ROM payload? */ |
dirkx | 0:a259777c45a3 | 297 | case PBUF_ROM: |
dirkx | 0:a259777c45a3 | 298 | /* pbuf references existing (externally allocated) RAM payload? */ |
dirkx | 0:a259777c45a3 | 299 | case PBUF_REF: |
dirkx | 0:a259777c45a3 | 300 | /* only allocate memory for the pbuf structure */ |
dirkx | 0:a259777c45a3 | 301 | p = (struct pbuf *)memp_malloc(MEMP_PBUF); |
dirkx | 0:a259777c45a3 | 302 | if (p == NULL) { |
dirkx | 0:a259777c45a3 | 303 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
dirkx | 0:a259777c45a3 | 304 | ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", |
dirkx | 0:a259777c45a3 | 305 | (type == PBUF_ROM) ? "ROM" : "REF")); |
dirkx | 0:a259777c45a3 | 306 | return NULL; |
dirkx | 0:a259777c45a3 | 307 | } |
dirkx | 0:a259777c45a3 | 308 | /* caller must set this field properly, afterwards */ |
dirkx | 0:a259777c45a3 | 309 | p->payload = NULL; |
dirkx | 0:a259777c45a3 | 310 | p->len = p->tot_len = length; |
dirkx | 0:a259777c45a3 | 311 | p->next = NULL; |
dirkx | 0:a259777c45a3 | 312 | p->type = type; |
dirkx | 0:a259777c45a3 | 313 | break; |
dirkx | 0:a259777c45a3 | 314 | default: |
dirkx | 0:a259777c45a3 | 315 | LWIP_ASSERT("pbuf_alloc: erroneous type", 0); |
dirkx | 0:a259777c45a3 | 316 | return NULL; |
dirkx | 0:a259777c45a3 | 317 | } |
dirkx | 0:a259777c45a3 | 318 | /* set reference count */ |
dirkx | 0:a259777c45a3 | 319 | p->ref = 1; |
dirkx | 0:a259777c45a3 | 320 | /* set flags */ |
dirkx | 0:a259777c45a3 | 321 | p->flags = 0; |
dirkx | 0:a259777c45a3 | 322 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p)); |
dirkx | 0:a259777c45a3 | 323 | return p; |
dirkx | 0:a259777c45a3 | 324 | } |
dirkx | 0:a259777c45a3 | 325 | |
dirkx | 0:a259777c45a3 | 326 | |
dirkx | 0:a259777c45a3 | 327 | /** |
dirkx | 0:a259777c45a3 | 328 | * Shrink a pbuf chain to a desired length. |
dirkx | 0:a259777c45a3 | 329 | * |
dirkx | 0:a259777c45a3 | 330 | * @param p pbuf to shrink. |
dirkx | 0:a259777c45a3 | 331 | * @param new_len desired new length of pbuf chain |
dirkx | 0:a259777c45a3 | 332 | * |
dirkx | 0:a259777c45a3 | 333 | * Depending on the desired length, the first few pbufs in a chain might |
dirkx | 0:a259777c45a3 | 334 | * be skipped and left unchanged. The new last pbuf in the chain will be |
dirkx | 0:a259777c45a3 | 335 | * resized, and any remaining pbufs will be freed. |
dirkx | 0:a259777c45a3 | 336 | * |
dirkx | 0:a259777c45a3 | 337 | * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted. |
dirkx | 0:a259777c45a3 | 338 | * @note May not be called on a packet queue. |
dirkx | 0:a259777c45a3 | 339 | * |
dirkx | 0:a259777c45a3 | 340 | * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain). |
dirkx | 0:a259777c45a3 | 341 | */ |
dirkx | 0:a259777c45a3 | 342 | void |
dirkx | 0:a259777c45a3 | 343 | pbuf_realloc(struct pbuf *p, u16_t new_len) |
dirkx | 0:a259777c45a3 | 344 | { |
dirkx | 0:a259777c45a3 | 345 | struct pbuf *q; |
dirkx | 0:a259777c45a3 | 346 | u16_t rem_len; /* remaining length */ |
dirkx | 0:a259777c45a3 | 347 | s32_t grow; |
dirkx | 0:a259777c45a3 | 348 | |
dirkx | 0:a259777c45a3 | 349 | LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL); |
dirkx | 0:a259777c45a3 | 350 | LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL || |
dirkx | 0:a259777c45a3 | 351 | p->type == PBUF_ROM || |
dirkx | 0:a259777c45a3 | 352 | p->type == PBUF_RAM || |
dirkx | 0:a259777c45a3 | 353 | p->type == PBUF_REF); |
dirkx | 0:a259777c45a3 | 354 | |
dirkx | 0:a259777c45a3 | 355 | /* desired length larger than current length? */ |
dirkx | 0:a259777c45a3 | 356 | if (new_len >= p->tot_len) { |
dirkx | 0:a259777c45a3 | 357 | /* enlarging not yet supported */ |
dirkx | 0:a259777c45a3 | 358 | return; |
dirkx | 0:a259777c45a3 | 359 | } |
dirkx | 0:a259777c45a3 | 360 | |
dirkx | 0:a259777c45a3 | 361 | /* the pbuf chain grows by (new_len - p->tot_len) bytes |
dirkx | 0:a259777c45a3 | 362 | * (which may be negative in case of shrinking) */ |
dirkx | 0:a259777c45a3 | 363 | grow = new_len - p->tot_len; |
dirkx | 0:a259777c45a3 | 364 | |
dirkx | 0:a259777c45a3 | 365 | /* first, step over any pbufs that should remain in the chain */ |
dirkx | 0:a259777c45a3 | 366 | rem_len = new_len; |
dirkx | 0:a259777c45a3 | 367 | q = p; |
dirkx | 0:a259777c45a3 | 368 | /* should this pbuf be kept? */ |
dirkx | 0:a259777c45a3 | 369 | while (rem_len > q->len) { |
dirkx | 0:a259777c45a3 | 370 | /* decrease remaining length by pbuf length */ |
dirkx | 0:a259777c45a3 | 371 | rem_len -= q->len; |
dirkx | 0:a259777c45a3 | 372 | /* decrease total length indicator */ |
dirkx | 0:a259777c45a3 | 373 | LWIP_ASSERT("grow < max_u16_t", grow < 0xffff); |
dirkx | 0:a259777c45a3 | 374 | q->tot_len += (u16_t)grow; |
dirkx | 0:a259777c45a3 | 375 | /* proceed to next pbuf in chain */ |
dirkx | 0:a259777c45a3 | 376 | q = q->next; |
dirkx | 0:a259777c45a3 | 377 | LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL); |
dirkx | 0:a259777c45a3 | 378 | } |
dirkx | 0:a259777c45a3 | 379 | /* we have now reached the new last pbuf (in q) */ |
dirkx | 0:a259777c45a3 | 380 | /* rem_len == desired length for pbuf q */ |
dirkx | 0:a259777c45a3 | 381 | |
dirkx | 0:a259777c45a3 | 382 | /* shrink allocated memory for PBUF_RAM */ |
dirkx | 0:a259777c45a3 | 383 | /* (other types merely adjust their length fields */ |
dirkx | 0:a259777c45a3 | 384 | if ((q->type == PBUF_RAM) && (rem_len != q->len)) { |
dirkx | 0:a259777c45a3 | 385 | /* reallocate and adjust the length of the pbuf that will be split */ |
dirkx | 0:a259777c45a3 | 386 | q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len); |
dirkx | 0:a259777c45a3 | 387 | LWIP_ASSERT("mem_trim returned q == NULL", q != NULL); |
dirkx | 0:a259777c45a3 | 388 | } |
dirkx | 0:a259777c45a3 | 389 | /* adjust length fields for new last pbuf */ |
dirkx | 0:a259777c45a3 | 390 | q->len = rem_len; |
dirkx | 0:a259777c45a3 | 391 | q->tot_len = q->len; |
dirkx | 0:a259777c45a3 | 392 | |
dirkx | 0:a259777c45a3 | 393 | /* any remaining pbufs in chain? */ |
dirkx | 0:a259777c45a3 | 394 | if (q->next != NULL) { |
dirkx | 0:a259777c45a3 | 395 | /* free remaining pbufs in chain */ |
dirkx | 0:a259777c45a3 | 396 | pbuf_free(q->next); |
dirkx | 0:a259777c45a3 | 397 | } |
dirkx | 0:a259777c45a3 | 398 | /* q is last packet in chain */ |
dirkx | 0:a259777c45a3 | 399 | q->next = NULL; |
dirkx | 0:a259777c45a3 | 400 | |
dirkx | 0:a259777c45a3 | 401 | } |
dirkx | 0:a259777c45a3 | 402 | |
dirkx | 0:a259777c45a3 | 403 | /** |
dirkx | 0:a259777c45a3 | 404 | * Adjusts the payload pointer to hide or reveal headers in the payload. |
dirkx | 0:a259777c45a3 | 405 | * |
dirkx | 0:a259777c45a3 | 406 | * Adjusts the ->payload pointer so that space for a header |
dirkx | 0:a259777c45a3 | 407 | * (dis)appears in the pbuf payload. |
dirkx | 0:a259777c45a3 | 408 | * |
dirkx | 0:a259777c45a3 | 409 | * The ->payload, ->tot_len and ->len fields are adjusted. |
dirkx | 0:a259777c45a3 | 410 | * |
dirkx | 0:a259777c45a3 | 411 | * @param p pbuf to change the header size. |
dirkx | 0:a259777c45a3 | 412 | * @param header_size_increment Number of bytes to increment header size which |
dirkx | 0:a259777c45a3 | 413 | * increases the size of the pbuf. New space is on the front. |
dirkx | 0:a259777c45a3 | 414 | * (Using a negative value decreases the header size.) |
dirkx | 0:a259777c45a3 | 415 | * If hdr_size_inc is 0, this function does nothing and returns succesful. |
dirkx | 0:a259777c45a3 | 416 | * |
dirkx | 0:a259777c45a3 | 417 | * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so |
dirkx | 0:a259777c45a3 | 418 | * the call will fail. A check is made that the increase in header size does |
dirkx | 0:a259777c45a3 | 419 | * not move the payload pointer in front of the start of the buffer. |
dirkx | 0:a259777c45a3 | 420 | * @return non-zero on failure, zero on success. |
dirkx | 0:a259777c45a3 | 421 | * |
dirkx | 0:a259777c45a3 | 422 | */ |
dirkx | 0:a259777c45a3 | 423 | u8_t |
dirkx | 0:a259777c45a3 | 424 | pbuf_header(struct pbuf *p, s16_t header_size_increment) |
dirkx | 0:a259777c45a3 | 425 | { |
dirkx | 0:a259777c45a3 | 426 | u16_t type; |
dirkx | 0:a259777c45a3 | 427 | void *payload; |
dirkx | 0:a259777c45a3 | 428 | u16_t increment_magnitude; |
dirkx | 0:a259777c45a3 | 429 | |
dirkx | 0:a259777c45a3 | 430 | LWIP_ASSERT("p != NULL", p != NULL); |
dirkx | 0:a259777c45a3 | 431 | if ((header_size_increment == 0) || (p == NULL)) |
dirkx | 0:a259777c45a3 | 432 | return 0; |
dirkx | 0:a259777c45a3 | 433 | |
dirkx | 0:a259777c45a3 | 434 | if (header_size_increment < 0){ |
dirkx | 0:a259777c45a3 | 435 | increment_magnitude = -header_size_increment; |
dirkx | 0:a259777c45a3 | 436 | /* Check that we aren't going to move off the end of the pbuf */ |
dirkx | 0:a259777c45a3 | 437 | LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;); |
dirkx | 0:a259777c45a3 | 438 | } else { |
dirkx | 0:a259777c45a3 | 439 | increment_magnitude = header_size_increment; |
dirkx | 0:a259777c45a3 | 440 | #if 0 |
dirkx | 0:a259777c45a3 | 441 | /* Can't assert these as some callers speculatively call |
dirkx | 0:a259777c45a3 | 442 | pbuf_header() to see if it's OK. Will return 1 below instead. */ |
dirkx | 0:a259777c45a3 | 443 | /* Check that we've got the correct type of pbuf to work with */ |
dirkx | 0:a259777c45a3 | 444 | LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL", |
dirkx | 0:a259777c45a3 | 445 | p->type == PBUF_RAM || p->type == PBUF_POOL); |
dirkx | 0:a259777c45a3 | 446 | /* Check that we aren't going to move off the beginning of the pbuf */ |
dirkx | 0:a259777c45a3 | 447 | LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF", |
dirkx | 0:a259777c45a3 | 448 | (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF); |
dirkx | 0:a259777c45a3 | 449 | #endif |
dirkx | 0:a259777c45a3 | 450 | } |
dirkx | 0:a259777c45a3 | 451 | |
dirkx | 0:a259777c45a3 | 452 | type = p->type; |
dirkx | 0:a259777c45a3 | 453 | /* remember current payload pointer */ |
dirkx | 0:a259777c45a3 | 454 | payload = p->payload; |
dirkx | 0:a259777c45a3 | 455 | |
dirkx | 0:a259777c45a3 | 456 | /* pbuf types containing payloads? */ |
dirkx | 0:a259777c45a3 | 457 | if (type == PBUF_RAM || type == PBUF_POOL) { |
dirkx | 0:a259777c45a3 | 458 | /* set new payload pointer */ |
dirkx | 0:a259777c45a3 | 459 | p->payload = (u8_t *)p->payload - header_size_increment; |
dirkx | 0:a259777c45a3 | 460 | /* boundary check fails? */ |
dirkx | 0:a259777c45a3 | 461 | if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) { |
dirkx | 0:a259777c45a3 | 462 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
dirkx | 0:a259777c45a3 | 463 | ("pbuf_header: failed as %p < %p (not enough space for new header size)\n", |
dirkx | 0:a259777c45a3 | 464 | (void *)p->payload, (void *)(p + 1))); |
dirkx | 0:a259777c45a3 | 465 | /* restore old payload pointer */ |
dirkx | 0:a259777c45a3 | 466 | p->payload = payload; |
dirkx | 0:a259777c45a3 | 467 | /* bail out unsuccesfully */ |
dirkx | 0:a259777c45a3 | 468 | return 1; |
dirkx | 0:a259777c45a3 | 469 | } |
dirkx | 0:a259777c45a3 | 470 | /* pbuf types refering to external payloads? */ |
dirkx | 0:a259777c45a3 | 471 | } else if (type == PBUF_REF || type == PBUF_ROM) { |
dirkx | 0:a259777c45a3 | 472 | /* hide a header in the payload? */ |
dirkx | 0:a259777c45a3 | 473 | if ((header_size_increment < 0) && (increment_magnitude <= p->len)) { |
dirkx | 0:a259777c45a3 | 474 | /* increase payload pointer */ |
dirkx | 0:a259777c45a3 | 475 | p->payload = (u8_t *)p->payload - header_size_increment; |
dirkx | 0:a259777c45a3 | 476 | } else { |
dirkx | 0:a259777c45a3 | 477 | /* cannot expand payload to front (yet!) |
dirkx | 0:a259777c45a3 | 478 | * bail out unsuccesfully */ |
dirkx | 0:a259777c45a3 | 479 | return 1; |
dirkx | 0:a259777c45a3 | 480 | } |
dirkx | 0:a259777c45a3 | 481 | } |
dirkx | 0:a259777c45a3 | 482 | else { |
dirkx | 0:a259777c45a3 | 483 | /* Unknown type */ |
dirkx | 0:a259777c45a3 | 484 | LWIP_ASSERT("bad pbuf type", 0); |
dirkx | 0:a259777c45a3 | 485 | return 1; |
dirkx | 0:a259777c45a3 | 486 | } |
dirkx | 0:a259777c45a3 | 487 | /* modify pbuf length fields */ |
dirkx | 0:a259777c45a3 | 488 | p->len += header_size_increment; |
dirkx | 0:a259777c45a3 | 489 | p->tot_len += header_size_increment; |
dirkx | 0:a259777c45a3 | 490 | |
dirkx | 0:a259777c45a3 | 491 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n", |
dirkx | 0:a259777c45a3 | 492 | (void *)payload, (void *)p->payload, header_size_increment)); |
dirkx | 0:a259777c45a3 | 493 | |
dirkx | 0:a259777c45a3 | 494 | return 0; |
dirkx | 0:a259777c45a3 | 495 | } |
dirkx | 0:a259777c45a3 | 496 | |
dirkx | 0:a259777c45a3 | 497 | /** |
dirkx | 0:a259777c45a3 | 498 | * Dereference a pbuf chain or queue and deallocate any no-longer-used |
dirkx | 0:a259777c45a3 | 499 | * pbufs at the head of this chain or queue. |
dirkx | 0:a259777c45a3 | 500 | * |
dirkx | 0:a259777c45a3 | 501 | * Decrements the pbuf reference count. If it reaches zero, the pbuf is |
dirkx | 0:a259777c45a3 | 502 | * deallocated. |
dirkx | 0:a259777c45a3 | 503 | * |
dirkx | 0:a259777c45a3 | 504 | * For a pbuf chain, this is repeated for each pbuf in the chain, |
dirkx | 0:a259777c45a3 | 505 | * up to the first pbuf which has a non-zero reference count after |
dirkx | 0:a259777c45a3 | 506 | * decrementing. So, when all reference counts are one, the whole |
dirkx | 0:a259777c45a3 | 507 | * chain is free'd. |
dirkx | 0:a259777c45a3 | 508 | * |
dirkx | 0:a259777c45a3 | 509 | * @param p The pbuf (chain) to be dereferenced. |
dirkx | 0:a259777c45a3 | 510 | * |
dirkx | 0:a259777c45a3 | 511 | * @return the number of pbufs that were de-allocated |
dirkx | 0:a259777c45a3 | 512 | * from the head of the chain. |
dirkx | 0:a259777c45a3 | 513 | * |
dirkx | 0:a259777c45a3 | 514 | * @note MUST NOT be called on a packet queue (Not verified to work yet). |
dirkx | 0:a259777c45a3 | 515 | * @note the reference counter of a pbuf equals the number of pointers |
dirkx | 0:a259777c45a3 | 516 | * that refer to the pbuf (or into the pbuf). |
dirkx | 0:a259777c45a3 | 517 | * |
dirkx | 0:a259777c45a3 | 518 | * @internal examples: |
dirkx | 0:a259777c45a3 | 519 | * |
dirkx | 0:a259777c45a3 | 520 | * Assuming existing chains a->b->c with the following reference |
dirkx | 0:a259777c45a3 | 521 | * counts, calling pbuf_free(a) results in: |
dirkx | 0:a259777c45a3 | 522 | * |
dirkx | 0:a259777c45a3 | 523 | * 1->2->3 becomes ...1->3 |
dirkx | 0:a259777c45a3 | 524 | * 3->3->3 becomes 2->3->3 |
dirkx | 0:a259777c45a3 | 525 | * 1->1->2 becomes ......1 |
dirkx | 0:a259777c45a3 | 526 | * 2->1->1 becomes 1->1->1 |
dirkx | 0:a259777c45a3 | 527 | * 1->1->1 becomes ....... |
dirkx | 0:a259777c45a3 | 528 | * |
dirkx | 0:a259777c45a3 | 529 | */ |
dirkx | 0:a259777c45a3 | 530 | u8_t |
dirkx | 0:a259777c45a3 | 531 | pbuf_free(struct pbuf *p) |
dirkx | 0:a259777c45a3 | 532 | { |
dirkx | 0:a259777c45a3 | 533 | u16_t type; |
dirkx | 0:a259777c45a3 | 534 | struct pbuf *q; |
dirkx | 0:a259777c45a3 | 535 | u8_t count; |
dirkx | 0:a259777c45a3 | 536 | |
dirkx | 0:a259777c45a3 | 537 | if (p == NULL) { |
dirkx | 0:a259777c45a3 | 538 | LWIP_ASSERT("p != NULL", p != NULL); |
dirkx | 0:a259777c45a3 | 539 | /* if assertions are disabled, proceed with debug output */ |
dirkx | 0:a259777c45a3 | 540 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
dirkx | 0:a259777c45a3 | 541 | ("pbuf_free(p == NULL) was called.\n")); |
dirkx | 0:a259777c45a3 | 542 | return 0; |
dirkx | 0:a259777c45a3 | 543 | } |
dirkx | 0:a259777c45a3 | 544 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p)); |
dirkx | 0:a259777c45a3 | 545 | |
dirkx | 0:a259777c45a3 | 546 | PERF_START; |
dirkx | 0:a259777c45a3 | 547 | |
dirkx | 0:a259777c45a3 | 548 | LWIP_ASSERT("pbuf_free: sane type", |
dirkx | 0:a259777c45a3 | 549 | p->type == PBUF_RAM || p->type == PBUF_ROM || |
dirkx | 0:a259777c45a3 | 550 | p->type == PBUF_REF || p->type == PBUF_POOL); |
dirkx | 0:a259777c45a3 | 551 | |
dirkx | 0:a259777c45a3 | 552 | count = 0; |
dirkx | 0:a259777c45a3 | 553 | /* de-allocate all consecutive pbufs from the head of the chain that |
dirkx | 0:a259777c45a3 | 554 | * obtain a zero reference count after decrementing*/ |
dirkx | 0:a259777c45a3 | 555 | while (p != NULL) { |
dirkx | 0:a259777c45a3 | 556 | u16_t ref; |
dirkx | 0:a259777c45a3 | 557 | SYS_ARCH_DECL_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 558 | /* Since decrementing ref cannot be guaranteed to be a single machine operation |
dirkx | 0:a259777c45a3 | 559 | * we must protect it. We put the new ref into a local variable to prevent |
dirkx | 0:a259777c45a3 | 560 | * further protection. */ |
dirkx | 0:a259777c45a3 | 561 | SYS_ARCH_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 562 | /* all pbufs in a chain are referenced at least once */ |
dirkx | 0:a259777c45a3 | 563 | LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0); |
dirkx | 0:a259777c45a3 | 564 | /* decrease reference count (number of pointers to pbuf) */ |
dirkx | 0:a259777c45a3 | 565 | ref = --(p->ref); |
dirkx | 0:a259777c45a3 | 566 | SYS_ARCH_UNPROTECT(old_level); |
dirkx | 0:a259777c45a3 | 567 | /* this pbuf is no longer referenced to? */ |
dirkx | 0:a259777c45a3 | 568 | if (ref == 0) { |
dirkx | 0:a259777c45a3 | 569 | /* remember next pbuf in chain for next iteration */ |
dirkx | 0:a259777c45a3 | 570 | q = p->next; |
dirkx | 0:a259777c45a3 | 571 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p)); |
dirkx | 0:a259777c45a3 | 572 | type = p->type; |
dirkx | 0:a259777c45a3 | 573 | /* is this a pbuf from the pool? */ |
dirkx | 0:a259777c45a3 | 574 | if (type == PBUF_POOL) { |
dirkx | 0:a259777c45a3 | 575 | memp_free(MEMP_PBUF_POOL, p); |
dirkx | 0:a259777c45a3 | 576 | /* is this a ROM or RAM referencing pbuf? */ |
dirkx | 0:a259777c45a3 | 577 | } else if (type == PBUF_ROM || type == PBUF_REF) { |
dirkx | 0:a259777c45a3 | 578 | memp_free(MEMP_PBUF, p); |
dirkx | 0:a259777c45a3 | 579 | /* type == PBUF_RAM */ |
dirkx | 0:a259777c45a3 | 580 | } else { |
dirkx | 0:a259777c45a3 | 581 | mem_free(p); |
dirkx | 0:a259777c45a3 | 582 | } |
dirkx | 0:a259777c45a3 | 583 | count++; |
dirkx | 0:a259777c45a3 | 584 | /* proceed to next pbuf */ |
dirkx | 0:a259777c45a3 | 585 | p = q; |
dirkx | 0:a259777c45a3 | 586 | /* p->ref > 0, this pbuf is still referenced to */ |
dirkx | 0:a259777c45a3 | 587 | /* (and so the remaining pbufs in chain as well) */ |
dirkx | 0:a259777c45a3 | 588 | } else { |
dirkx | 0:a259777c45a3 | 589 | LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref)); |
dirkx | 0:a259777c45a3 | 590 | /* stop walking through the chain */ |
dirkx | 0:a259777c45a3 | 591 | p = NULL; |
dirkx | 0:a259777c45a3 | 592 | } |
dirkx | 0:a259777c45a3 | 593 | } |
dirkx | 0:a259777c45a3 | 594 | PERF_STOP("pbuf_free"); |
dirkx | 0:a259777c45a3 | 595 | /* return number of de-allocated pbufs */ |
dirkx | 0:a259777c45a3 | 596 | return count; |
dirkx | 0:a259777c45a3 | 597 | } |
dirkx | 0:a259777c45a3 | 598 | |
dirkx | 0:a259777c45a3 | 599 | /** |
dirkx | 0:a259777c45a3 | 600 | * Count number of pbufs in a chain |
dirkx | 0:a259777c45a3 | 601 | * |
dirkx | 0:a259777c45a3 | 602 | * @param p first pbuf of chain |
dirkx | 0:a259777c45a3 | 603 | * @return the number of pbufs in a chain |
dirkx | 0:a259777c45a3 | 604 | */ |
dirkx | 0:a259777c45a3 | 605 | |
dirkx | 0:a259777c45a3 | 606 | u8_t |
dirkx | 0:a259777c45a3 | 607 | pbuf_clen(struct pbuf *p) |
dirkx | 0:a259777c45a3 | 608 | { |
dirkx | 0:a259777c45a3 | 609 | u8_t len; |
dirkx | 0:a259777c45a3 | 610 | |
dirkx | 0:a259777c45a3 | 611 | len = 0; |
dirkx | 0:a259777c45a3 | 612 | while (p != NULL) { |
dirkx | 0:a259777c45a3 | 613 | ++len; |
dirkx | 0:a259777c45a3 | 614 | p = p->next; |
dirkx | 0:a259777c45a3 | 615 | } |
dirkx | 0:a259777c45a3 | 616 | return len; |
dirkx | 0:a259777c45a3 | 617 | } |
dirkx | 0:a259777c45a3 | 618 | |
dirkx | 0:a259777c45a3 | 619 | /** |
dirkx | 0:a259777c45a3 | 620 | * Increment the reference count of the pbuf. |
dirkx | 0:a259777c45a3 | 621 | * |
dirkx | 0:a259777c45a3 | 622 | * @param p pbuf to increase reference counter of |
dirkx | 0:a259777c45a3 | 623 | * |
dirkx | 0:a259777c45a3 | 624 | */ |
dirkx | 0:a259777c45a3 | 625 | void |
dirkx | 0:a259777c45a3 | 626 | pbuf_ref(struct pbuf *p) |
dirkx | 0:a259777c45a3 | 627 | { |
dirkx | 0:a259777c45a3 | 628 | SYS_ARCH_DECL_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 629 | /* pbuf given? */ |
dirkx | 0:a259777c45a3 | 630 | if (p != NULL) { |
dirkx | 0:a259777c45a3 | 631 | SYS_ARCH_PROTECT(old_level); |
dirkx | 0:a259777c45a3 | 632 | ++(p->ref); |
dirkx | 0:a259777c45a3 | 633 | SYS_ARCH_UNPROTECT(old_level); |
dirkx | 0:a259777c45a3 | 634 | } |
dirkx | 0:a259777c45a3 | 635 | } |
dirkx | 0:a259777c45a3 | 636 | |
dirkx | 0:a259777c45a3 | 637 | /** |
dirkx | 0:a259777c45a3 | 638 | * Concatenate two pbufs (each may be a pbuf chain) and take over |
dirkx | 0:a259777c45a3 | 639 | * the caller's reference of the tail pbuf. |
dirkx | 0:a259777c45a3 | 640 | * |
dirkx | 0:a259777c45a3 | 641 | * @note The caller MAY NOT reference the tail pbuf afterwards. |
dirkx | 0:a259777c45a3 | 642 | * Use pbuf_chain() for that purpose. |
dirkx | 0:a259777c45a3 | 643 | * |
dirkx | 0:a259777c45a3 | 644 | * @see pbuf_chain() |
dirkx | 0:a259777c45a3 | 645 | */ |
dirkx | 0:a259777c45a3 | 646 | |
dirkx | 0:a259777c45a3 | 647 | void |
dirkx | 0:a259777c45a3 | 648 | pbuf_cat(struct pbuf *h, struct pbuf *t) |
dirkx | 0:a259777c45a3 | 649 | { |
dirkx | 0:a259777c45a3 | 650 | struct pbuf *p; |
dirkx | 0:a259777c45a3 | 651 | |
dirkx | 0:a259777c45a3 | 652 | LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)", |
dirkx | 0:a259777c45a3 | 653 | ((h != NULL) && (t != NULL)), return;); |
dirkx | 0:a259777c45a3 | 654 | |
dirkx | 0:a259777c45a3 | 655 | /* proceed to last pbuf of chain */ |
dirkx | 0:a259777c45a3 | 656 | for (p = h; p->next != NULL; p = p->next) { |
dirkx | 0:a259777c45a3 | 657 | /* add total length of second chain to all totals of first chain */ |
dirkx | 0:a259777c45a3 | 658 | p->tot_len += t->tot_len; |
dirkx | 0:a259777c45a3 | 659 | } |
dirkx | 0:a259777c45a3 | 660 | /* { p is last pbuf of first h chain, p->next == NULL } */ |
dirkx | 0:a259777c45a3 | 661 | LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len); |
dirkx | 0:a259777c45a3 | 662 | LWIP_ASSERT("p->next == NULL", p->next == NULL); |
dirkx | 0:a259777c45a3 | 663 | /* add total length of second chain to last pbuf total of first chain */ |
dirkx | 0:a259777c45a3 | 664 | p->tot_len += t->tot_len; |
dirkx | 0:a259777c45a3 | 665 | /* chain last pbuf of head (p) with first of tail (t) */ |
dirkx | 0:a259777c45a3 | 666 | p->next = t; |
dirkx | 0:a259777c45a3 | 667 | /* p->next now references t, but the caller will drop its reference to t, |
dirkx | 0:a259777c45a3 | 668 | * so netto there is no change to the reference count of t. |
dirkx | 0:a259777c45a3 | 669 | */ |
dirkx | 0:a259777c45a3 | 670 | } |
dirkx | 0:a259777c45a3 | 671 | |
dirkx | 0:a259777c45a3 | 672 | /** |
dirkx | 0:a259777c45a3 | 673 | * Chain two pbufs (or pbuf chains) together. |
dirkx | 0:a259777c45a3 | 674 | * |
dirkx | 0:a259777c45a3 | 675 | * The caller MUST call pbuf_free(t) once it has stopped |
dirkx | 0:a259777c45a3 | 676 | * using it. Use pbuf_cat() instead if you no longer use t. |
dirkx | 0:a259777c45a3 | 677 | * |
dirkx | 0:a259777c45a3 | 678 | * @param h head pbuf (chain) |
dirkx | 0:a259777c45a3 | 679 | * @param t tail pbuf (chain) |
dirkx | 0:a259777c45a3 | 680 | * @note The pbufs MUST belong to the same packet. |
dirkx | 0:a259777c45a3 | 681 | * @note MAY NOT be called on a packet queue. |
dirkx | 0:a259777c45a3 | 682 | * |
dirkx | 0:a259777c45a3 | 683 | * The ->tot_len fields of all pbufs of the head chain are adjusted. |
dirkx | 0:a259777c45a3 | 684 | * The ->next field of the last pbuf of the head chain is adjusted. |
dirkx | 0:a259777c45a3 | 685 | * The ->ref field of the first pbuf of the tail chain is adjusted. |
dirkx | 0:a259777c45a3 | 686 | * |
dirkx | 0:a259777c45a3 | 687 | */ |
dirkx | 0:a259777c45a3 | 688 | void |
dirkx | 0:a259777c45a3 | 689 | pbuf_chain(struct pbuf *h, struct pbuf *t) |
dirkx | 0:a259777c45a3 | 690 | { |
dirkx | 0:a259777c45a3 | 691 | pbuf_cat(h, t); |
dirkx | 0:a259777c45a3 | 692 | /* t is now referenced by h */ |
dirkx | 0:a259777c45a3 | 693 | pbuf_ref(t); |
dirkx | 0:a259777c45a3 | 694 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t)); |
dirkx | 0:a259777c45a3 | 695 | } |
dirkx | 0:a259777c45a3 | 696 | |
dirkx | 0:a259777c45a3 | 697 | /** |
dirkx | 0:a259777c45a3 | 698 | * Dechains the first pbuf from its succeeding pbufs in the chain. |
dirkx | 0:a259777c45a3 | 699 | * |
dirkx | 0:a259777c45a3 | 700 | * Makes p->tot_len field equal to p->len. |
dirkx | 0:a259777c45a3 | 701 | * @param p pbuf to dechain |
dirkx | 0:a259777c45a3 | 702 | * @return remainder of the pbuf chain, or NULL if it was de-allocated. |
dirkx | 0:a259777c45a3 | 703 | * @note May not be called on a packet queue. |
dirkx | 0:a259777c45a3 | 704 | */ |
dirkx | 0:a259777c45a3 | 705 | struct pbuf * |
dirkx | 0:a259777c45a3 | 706 | pbuf_dechain(struct pbuf *p) |
dirkx | 0:a259777c45a3 | 707 | { |
dirkx | 0:a259777c45a3 | 708 | struct pbuf *q; |
dirkx | 0:a259777c45a3 | 709 | u8_t tail_gone = 1; |
dirkx | 0:a259777c45a3 | 710 | /* tail */ |
dirkx | 0:a259777c45a3 | 711 | q = p->next; |
dirkx | 0:a259777c45a3 | 712 | /* pbuf has successor in chain? */ |
dirkx | 0:a259777c45a3 | 713 | if (q != NULL) { |
dirkx | 0:a259777c45a3 | 714 | /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ |
dirkx | 0:a259777c45a3 | 715 | LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len); |
dirkx | 0:a259777c45a3 | 716 | /* enforce invariant if assertion is disabled */ |
dirkx | 0:a259777c45a3 | 717 | q->tot_len = p->tot_len - p->len; |
dirkx | 0:a259777c45a3 | 718 | /* decouple pbuf from remainder */ |
dirkx | 0:a259777c45a3 | 719 | p->next = NULL; |
dirkx | 0:a259777c45a3 | 720 | /* total length of pbuf p is its own length only */ |
dirkx | 0:a259777c45a3 | 721 | p->tot_len = p->len; |
dirkx | 0:a259777c45a3 | 722 | /* q is no longer referenced by p, free it */ |
dirkx | 0:a259777c45a3 | 723 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q)); |
dirkx | 0:a259777c45a3 | 724 | tail_gone = pbuf_free(q); |
dirkx | 0:a259777c45a3 | 725 | if (tail_gone > 0) { |
dirkx | 0:a259777c45a3 | 726 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, |
dirkx | 0:a259777c45a3 | 727 | ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q)); |
dirkx | 0:a259777c45a3 | 728 | } |
dirkx | 0:a259777c45a3 | 729 | /* return remaining tail or NULL if deallocated */ |
dirkx | 0:a259777c45a3 | 730 | } |
dirkx | 0:a259777c45a3 | 731 | /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */ |
dirkx | 0:a259777c45a3 | 732 | LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len); |
dirkx | 0:a259777c45a3 | 733 | return ((tail_gone > 0) ? NULL : q); |
dirkx | 0:a259777c45a3 | 734 | } |
dirkx | 0:a259777c45a3 | 735 | |
dirkx | 0:a259777c45a3 | 736 | /** |
dirkx | 0:a259777c45a3 | 737 | * |
dirkx | 0:a259777c45a3 | 738 | * Create PBUF_RAM copies of pbufs. |
dirkx | 0:a259777c45a3 | 739 | * |
dirkx | 0:a259777c45a3 | 740 | * Used to queue packets on behalf of the lwIP stack, such as |
dirkx | 0:a259777c45a3 | 741 | * ARP based queueing. |
dirkx | 0:a259777c45a3 | 742 | * |
dirkx | 0:a259777c45a3 | 743 | * @note You MUST explicitly use p = pbuf_take(p); |
dirkx | 0:a259777c45a3 | 744 | * |
dirkx | 0:a259777c45a3 | 745 | * @note Only one packet is copied, no packet queue! |
dirkx | 0:a259777c45a3 | 746 | * |
dirkx | 0:a259777c45a3 | 747 | * @param p_to pbuf destination of the copy |
dirkx | 0:a259777c45a3 | 748 | * @param p_from pbuf source of the copy |
dirkx | 0:a259777c45a3 | 749 | * |
dirkx | 0:a259777c45a3 | 750 | * @return ERR_OK if pbuf was copied |
dirkx | 0:a259777c45a3 | 751 | * ERR_ARG if one of the pbufs is NULL or p_to is not big |
dirkx | 0:a259777c45a3 | 752 | * enough to hold p_from |
dirkx | 0:a259777c45a3 | 753 | */ |
dirkx | 0:a259777c45a3 | 754 | err_t |
dirkx | 0:a259777c45a3 | 755 | pbuf_copy(struct pbuf *p_to, struct pbuf *p_from) |
dirkx | 0:a259777c45a3 | 756 | { |
dirkx | 0:a259777c45a3 | 757 | u16_t offset_to=0, offset_from=0, len; |
dirkx | 0:a259777c45a3 | 758 | |
dirkx | 0:a259777c45a3 | 759 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n", |
dirkx | 0:a259777c45a3 | 760 | (void*)p_to, (void*)p_from)); |
dirkx | 0:a259777c45a3 | 761 | |
dirkx | 0:a259777c45a3 | 762 | /* is the target big enough to hold the source? */ |
dirkx | 0:a259777c45a3 | 763 | LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) && |
dirkx | 0:a259777c45a3 | 764 | (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;); |
dirkx | 0:a259777c45a3 | 765 | |
dirkx | 0:a259777c45a3 | 766 | /* iterate through pbuf chain */ |
dirkx | 0:a259777c45a3 | 767 | do |
dirkx | 0:a259777c45a3 | 768 | { |
dirkx | 0:a259777c45a3 | 769 | LWIP_ASSERT("p_to != NULL", p_to != NULL); |
dirkx | 0:a259777c45a3 | 770 | /* copy one part of the original chain */ |
dirkx | 0:a259777c45a3 | 771 | if ((p_to->len - offset_to) >= (p_from->len - offset_from)) { |
dirkx | 0:a259777c45a3 | 772 | /* complete current p_from fits into current p_to */ |
dirkx | 0:a259777c45a3 | 773 | len = p_from->len - offset_from; |
dirkx | 0:a259777c45a3 | 774 | } else { |
dirkx | 0:a259777c45a3 | 775 | /* current p_from does not fit into current p_to */ |
dirkx | 0:a259777c45a3 | 776 | len = p_to->len - offset_to; |
dirkx | 0:a259777c45a3 | 777 | } |
dirkx | 0:a259777c45a3 | 778 | MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len); |
dirkx | 0:a259777c45a3 | 779 | offset_to += len; |
dirkx | 0:a259777c45a3 | 780 | offset_from += len; |
dirkx | 0:a259777c45a3 | 781 | LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len); |
dirkx | 0:a259777c45a3 | 782 | if (offset_to == p_to->len) { |
dirkx | 0:a259777c45a3 | 783 | /* on to next p_to (if any) */ |
dirkx | 0:a259777c45a3 | 784 | offset_to = 0; |
dirkx | 0:a259777c45a3 | 785 | p_to = p_to->next; |
dirkx | 0:a259777c45a3 | 786 | } |
dirkx | 0:a259777c45a3 | 787 | LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len); |
dirkx | 0:a259777c45a3 | 788 | if (offset_from >= p_from->len) { |
dirkx | 0:a259777c45a3 | 789 | /* on to next p_from (if any) */ |
dirkx | 0:a259777c45a3 | 790 | offset_from = 0; |
dirkx | 0:a259777c45a3 | 791 | p_from = p_from->next; |
dirkx | 0:a259777c45a3 | 792 | } |
dirkx | 0:a259777c45a3 | 793 | |
dirkx | 0:a259777c45a3 | 794 | if((p_from != NULL) && (p_from->len == p_from->tot_len)) { |
dirkx | 0:a259777c45a3 | 795 | /* don't copy more than one packet! */ |
dirkx | 0:a259777c45a3 | 796 | LWIP_ERROR("pbuf_copy() does not allow packet queues!\n", |
dirkx | 0:a259777c45a3 | 797 | (p_from->next == NULL), return ERR_VAL;); |
dirkx | 0:a259777c45a3 | 798 | } |
dirkx | 0:a259777c45a3 | 799 | if((p_to != NULL) && (p_to->len == p_to->tot_len)) { |
dirkx | 0:a259777c45a3 | 800 | /* don't copy more than one packet! */ |
dirkx | 0:a259777c45a3 | 801 | LWIP_ERROR("pbuf_copy() does not allow packet queues!\n", |
dirkx | 0:a259777c45a3 | 802 | (p_to->next == NULL), return ERR_VAL;); |
dirkx | 0:a259777c45a3 | 803 | } |
dirkx | 0:a259777c45a3 | 804 | } while (p_from); |
dirkx | 0:a259777c45a3 | 805 | LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n")); |
dirkx | 0:a259777c45a3 | 806 | return ERR_OK; |
dirkx | 0:a259777c45a3 | 807 | } |
dirkx | 0:a259777c45a3 | 808 | |
dirkx | 0:a259777c45a3 | 809 | /** |
dirkx | 0:a259777c45a3 | 810 | * Copy (part of) the contents of a packet buffer |
dirkx | 0:a259777c45a3 | 811 | * to an application supplied buffer. |
dirkx | 0:a259777c45a3 | 812 | * |
dirkx | 0:a259777c45a3 | 813 | * @param buf the pbuf from which to copy data |
dirkx | 0:a259777c45a3 | 814 | * @param dataptr the application supplied buffer |
dirkx | 0:a259777c45a3 | 815 | * @param len length of data to copy (dataptr must be big enough). No more |
dirkx | 0:a259777c45a3 | 816 | * than buf->tot_len will be copied, irrespective of len |
dirkx | 0:a259777c45a3 | 817 | * @param offset offset into the packet buffer from where to begin copying len bytes |
dirkx | 0:a259777c45a3 | 818 | * @return the number of bytes copied, or 0 on failure |
dirkx | 0:a259777c45a3 | 819 | */ |
dirkx | 0:a259777c45a3 | 820 | u16_t |
dirkx | 0:a259777c45a3 | 821 | pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset) |
dirkx | 0:a259777c45a3 | 822 | { |
dirkx | 0:a259777c45a3 | 823 | struct pbuf *p; |
dirkx | 0:a259777c45a3 | 824 | u16_t left; |
dirkx | 0:a259777c45a3 | 825 | u16_t buf_copy_len; |
dirkx | 0:a259777c45a3 | 826 | u16_t copied_total = 0; |
dirkx | 0:a259777c45a3 | 827 | |
dirkx | 0:a259777c45a3 | 828 | LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;); |
dirkx | 0:a259777c45a3 | 829 | LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;); |
dirkx | 0:a259777c45a3 | 830 | |
dirkx | 0:a259777c45a3 | 831 | left = 0; |
dirkx | 0:a259777c45a3 | 832 | |
dirkx | 0:a259777c45a3 | 833 | if((buf == NULL) || (dataptr == NULL)) { |
dirkx | 0:a259777c45a3 | 834 | return 0; |
dirkx | 0:a259777c45a3 | 835 | } |
dirkx | 0:a259777c45a3 | 836 | |
dirkx | 0:a259777c45a3 | 837 | /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ |
dirkx | 0:a259777c45a3 | 838 | for(p = buf; len != 0 && p != NULL; p = p->next) { |
dirkx | 0:a259777c45a3 | 839 | if ((offset != 0) && (offset >= p->len)) { |
dirkx | 0:a259777c45a3 | 840 | /* don't copy from this buffer -> on to the next */ |
dirkx | 0:a259777c45a3 | 841 | offset -= p->len; |
dirkx | 0:a259777c45a3 | 842 | } else { |
dirkx | 0:a259777c45a3 | 843 | /* copy from this buffer. maybe only partially. */ |
dirkx | 0:a259777c45a3 | 844 | buf_copy_len = p->len - offset; |
dirkx | 0:a259777c45a3 | 845 | if (buf_copy_len > len) |
dirkx | 0:a259777c45a3 | 846 | buf_copy_len = len; |
dirkx | 0:a259777c45a3 | 847 | /* copy the necessary parts of the buffer */ |
dirkx | 0:a259777c45a3 | 848 | MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); |
dirkx | 0:a259777c45a3 | 849 | copied_total += buf_copy_len; |
dirkx | 0:a259777c45a3 | 850 | left += buf_copy_len; |
dirkx | 0:a259777c45a3 | 851 | len -= buf_copy_len; |
dirkx | 0:a259777c45a3 | 852 | offset = 0; |
dirkx | 0:a259777c45a3 | 853 | } |
dirkx | 0:a259777c45a3 | 854 | } |
dirkx | 0:a259777c45a3 | 855 | return copied_total; |
dirkx | 0:a259777c45a3 | 856 | } |
dirkx | 0:a259777c45a3 | 857 | |
dirkx | 0:a259777c45a3 | 858 | /** |
dirkx | 0:a259777c45a3 | 859 | * Copy application supplied data into a pbuf. |
dirkx | 0:a259777c45a3 | 860 | * This function can only be used to copy the equivalent of buf->tot_len data. |
dirkx | 0:a259777c45a3 | 861 | * |
dirkx | 0:a259777c45a3 | 862 | * @param buf pbuf to fill with data |
dirkx | 0:a259777c45a3 | 863 | * @param dataptr application supplied data buffer |
dirkx | 0:a259777c45a3 | 864 | * @param len length of the application supplied data buffer |
dirkx | 0:a259777c45a3 | 865 | * |
dirkx | 0:a259777c45a3 | 866 | * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough |
dirkx | 0:a259777c45a3 | 867 | */ |
dirkx | 0:a259777c45a3 | 868 | err_t |
dirkx | 0:a259777c45a3 | 869 | pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) |
dirkx | 0:a259777c45a3 | 870 | { |
dirkx | 0:a259777c45a3 | 871 | struct pbuf *p; |
dirkx | 0:a259777c45a3 | 872 | u16_t buf_copy_len; |
dirkx | 0:a259777c45a3 | 873 | u16_t total_copy_len = len; |
dirkx | 0:a259777c45a3 | 874 | u16_t copied_total = 0; |
dirkx | 0:a259777c45a3 | 875 | |
dirkx | 0:a259777c45a3 | 876 | LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;); |
dirkx | 0:a259777c45a3 | 877 | LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;); |
dirkx | 0:a259777c45a3 | 878 | |
dirkx | 0:a259777c45a3 | 879 | if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) { |
dirkx | 0:a259777c45a3 | 880 | return ERR_ARG; |
dirkx | 0:a259777c45a3 | 881 | } |
dirkx | 0:a259777c45a3 | 882 | |
dirkx | 0:a259777c45a3 | 883 | /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ |
dirkx | 0:a259777c45a3 | 884 | for(p = buf; total_copy_len != 0; p = p->next) { |
dirkx | 0:a259777c45a3 | 885 | LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL); |
dirkx | 0:a259777c45a3 | 886 | buf_copy_len = total_copy_len; |
dirkx | 0:a259777c45a3 | 887 | if (buf_copy_len > p->len) { |
dirkx | 0:a259777c45a3 | 888 | /* this pbuf cannot hold all remaining data */ |
dirkx | 0:a259777c45a3 | 889 | buf_copy_len = p->len; |
dirkx | 0:a259777c45a3 | 890 | } |
dirkx | 0:a259777c45a3 | 891 | /* copy the necessary parts of the buffer */ |
dirkx | 0:a259777c45a3 | 892 | MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len); |
dirkx | 0:a259777c45a3 | 893 | total_copy_len -= buf_copy_len; |
dirkx | 0:a259777c45a3 | 894 | copied_total += buf_copy_len; |
dirkx | 0:a259777c45a3 | 895 | } |
dirkx | 0:a259777c45a3 | 896 | LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len); |
dirkx | 0:a259777c45a3 | 897 | return ERR_OK; |
dirkx | 0:a259777c45a3 | 898 | } |
dirkx | 0:a259777c45a3 | 899 | |
dirkx | 0:a259777c45a3 | 900 | /** |
dirkx | 0:a259777c45a3 | 901 | * Creates a single pbuf out of a queue of pbufs. |
dirkx | 0:a259777c45a3 | 902 | * |
dirkx | 0:a259777c45a3 | 903 | * @remark: The source pbuf 'p' is not freed by this function because that can |
dirkx | 0:a259777c45a3 | 904 | * be illegal in some places! |
dirkx | 0:a259777c45a3 | 905 | * |
dirkx | 0:a259777c45a3 | 906 | * @param p the source pbuf |
dirkx | 0:a259777c45a3 | 907 | * @param layer pbuf_layer of the new pbuf |
dirkx | 0:a259777c45a3 | 908 | * |
dirkx | 0:a259777c45a3 | 909 | * @return a new, single pbuf (p->next is NULL) |
dirkx | 0:a259777c45a3 | 910 | * or the old pbuf if allocation fails |
dirkx | 0:a259777c45a3 | 911 | */ |
dirkx | 0:a259777c45a3 | 912 | struct pbuf* |
dirkx | 0:a259777c45a3 | 913 | pbuf_coalesce(struct pbuf *p, pbuf_layer layer) |
dirkx | 0:a259777c45a3 | 914 | { |
dirkx | 0:a259777c45a3 | 915 | struct pbuf *q; |
dirkx | 0:a259777c45a3 | 916 | err_t err; |
dirkx | 0:a259777c45a3 | 917 | if (p->next == NULL) { |
dirkx | 0:a259777c45a3 | 918 | return p; |
dirkx | 0:a259777c45a3 | 919 | } |
dirkx | 0:a259777c45a3 | 920 | q = pbuf_alloc(layer, p->tot_len, PBUF_RAM); |
dirkx | 0:a259777c45a3 | 921 | if (q == NULL) { |
dirkx | 0:a259777c45a3 | 922 | /* @todo: what do we do now? */ |
dirkx | 0:a259777c45a3 | 923 | return p; |
dirkx | 0:a259777c45a3 | 924 | } |
dirkx | 0:a259777c45a3 | 925 | err = pbuf_copy(q, p); |
dirkx | 0:a259777c45a3 | 926 | LWIP_ASSERT("pbuf_copy failed", err == ERR_OK); |
dirkx | 0:a259777c45a3 | 927 | pbuf_free(p); |
dirkx | 0:a259777c45a3 | 928 | return q; |
dirkx | 0:a259777c45a3 | 929 | } |