]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/lwIP_MCF5235_GCC/lwip/src/core/pbuf.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / lwIP_MCF5235_GCC / lwip / src / core / pbuf.c
diff --git a/FreeRTOS/Demo/lwIP_MCF5235_GCC/lwip/src/core/pbuf.c b/FreeRTOS/Demo/lwIP_MCF5235_GCC/lwip/src/core/pbuf.c
new file mode 100644 (file)
index 0000000..c8e6e22
--- /dev/null
@@ -0,0 +1,957 @@
+/**\r
+ * @file\r
+ * Packet buffer management\r
+ *\r
+ * Packets are built from the pbuf data structure. It supports dynamic\r
+ * memory allocation for packet contents or can reference externally\r
+ * managed packet contents both in RAM and ROM. Quick allocation for\r
+ * incoming packets is provided through pools with fixed sized pbufs.\r
+ *\r
+ * A packet may span over multiple pbufs, chained as a singly linked\r
+ * list. This is called a "pbuf chain".\r
+ *\r
+ * Multiple packets may be queued, also using this singly linked list.\r
+ * This is called a "packet queue".\r
+ * \r
+ * So, a packet queue consists of one or more pbuf chains, each of\r
+ * which consist of one or more pbufs. Currently, queues are only\r
+ * supported in a limited section of lwIP, this is the etharp queueing\r
+ * code. Outside of this section no packet queues are supported yet.\r
+ * \r
+ * The differences between a pbuf chain and a packet queue are very\r
+ * precise but subtle. \r
+ *\r
+ * The last pbuf of a packet has a ->tot_len field that equals the\r
+ * ->len field. It can be found by traversing the list. If the last\r
+ * pbuf of a packet has a ->next field other than NULL, more packets\r
+ * are on the queue.\r
+ *\r
+ * Therefore, looping through a pbuf of a single packet, has an\r
+ * loop end condition (tot_len == p->len), NOT (next == NULL).\r
+ */\r
+\r
+/*\r
+ * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without modification,\r
+ * are permitted provided that the following conditions are met:\r
+ *\r
+ * 1. Redistributions of source code must retain the above copyright notice,\r
+ *    this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright notice,\r
+ *    this list of conditions and the following disclaimer in the documentation\r
+ *    and/or other materials provided with the distribution.\r
+ * 3. The name of the author may not be used to endorse or promote products\r
+ *    derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\r
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\r
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY\r
+ * OF SUCH DAMAGE.\r
+ *\r
+ * This file is part of the lwIP TCP/IP stack.\r
+ *\r
+ * Author: Adam Dunkels <adam@sics.se>\r
+ *\r
+ */\r
+\r
+#include <string.h>\r
+\r
+#include "lwip/opt.h"\r
+#include "lwip/stats.h"\r
+#include "lwip/def.h"\r
+#include "lwip/mem.h"\r
+#include "lwip/memp.h"\r
+#include "lwip/pbuf.h"\r
+#include "lwip/sys.h"\r
+#include "arch/perf.h"\r
+\r
+static u8_t pbuf_pool_memory[MEM_ALIGNMENT - 1 + PBUF_POOL_SIZE * MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf))];\r
+\r
+#if !SYS_LIGHTWEIGHT_PROT\r
+static volatile u8_t pbuf_pool_free_lock, pbuf_pool_alloc_lock;\r
+static sys_sem_t pbuf_pool_free_sem;\r
+#endif\r
+\r
+static struct pbuf *pbuf_pool = NULL;\r
+\r
+/**\r
+ * Initializes the pbuf module.\r
+ *\r
+ * A large part of memory is allocated for holding the pool of pbufs.\r
+ * The size of the individual pbufs in the pool is given by the size\r
+ * parameter, and the number of pbufs in the pool by the num parameter.\r
+ *\r
+ * After the memory has been allocated, the pbufs are set up. The\r
+ * ->next pointer in each pbuf is set up to point to the next pbuf in\r
+ * the pool.\r
+ *\r
+ */\r
+void\r
+pbuf_init(void)\r
+{\r
+  struct pbuf *p, *q = NULL;\r
+  u16_t i;\r
+\r
+  pbuf_pool = (struct pbuf *)MEM_ALIGN(pbuf_pool_memory);\r
+\r
+#if PBUF_STATS\r
+  lwip_stats.pbuf.avail = PBUF_POOL_SIZE;\r
+#endif /* PBUF_STATS */\r
+\r
+  /* Set up ->next pointers to link the pbufs of the pool together */\r
+  p = pbuf_pool;\r
+\r
+  for(i = 0; i < PBUF_POOL_SIZE; ++i) {\r
+    p->next = (struct pbuf *)((u8_t *)p + PBUF_POOL_BUFSIZE + sizeof(struct pbuf));\r
+    p->len = p->tot_len = PBUF_POOL_BUFSIZE;\r
+    p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf)));\r
+    p->flags = PBUF_FLAG_POOL;\r
+    q = p;\r
+    p = p->next;\r
+  }\r
+\r
+  /* The ->next pointer of last pbuf is NULL to indicate that there\r
+     are no more pbufs in the pool */\r
+  q->next = NULL;\r
+\r
+#if !SYS_LIGHTWEIGHT_PROT\r
+  pbuf_pool_alloc_lock = 0;\r
+  pbuf_pool_free_lock = 0;\r
+  pbuf_pool_free_sem = sys_sem_new(1);\r
+#endif\r
+}\r
+\r
+/**\r
+ * @internal only called from pbuf_alloc()\r
+ */\r
+static struct pbuf *\r
+pbuf_pool_alloc(void)\r
+{\r
+  struct pbuf *p = NULL;\r
+\r
+  SYS_ARCH_DECL_PROTECT(old_level);\r
+  SYS_ARCH_PROTECT(old_level);\r
+\r
+#if !SYS_LIGHTWEIGHT_PROT\r
+  /* Next, check the actual pbuf pool, but if the pool is locked, we\r
+     pretend to be out of buffers and return NULL. */\r
+  if (pbuf_pool_free_lock) {\r
+#if PBUF_STATS\r
+    ++lwip_stats.pbuf.alloc_locked;\r
+#endif /* PBUF_STATS */\r
+    return NULL;\r
+  }\r
+  pbuf_pool_alloc_lock = 1;\r
+  if (!pbuf_pool_free_lock) {\r
+#endif /* SYS_LIGHTWEIGHT_PROT */\r
+    p = pbuf_pool;\r
+    if (p) {\r
+      pbuf_pool = p->next;\r
+    }\r
+#if !SYS_LIGHTWEIGHT_PROT\r
+#if PBUF_STATS\r
+  } else {\r
+    ++lwip_stats.pbuf.alloc_locked;\r
+#endif /* PBUF_STATS */\r
+  }\r
+  pbuf_pool_alloc_lock = 0;\r
+#endif /* SYS_LIGHTWEIGHT_PROT */\r
+\r
+#if PBUF_STATS\r
+  if (p != NULL) {\r
+    ++lwip_stats.pbuf.used;\r
+    if (lwip_stats.pbuf.used > lwip_stats.pbuf.max) {\r
+      lwip_stats.pbuf.max = lwip_stats.pbuf.used;\r
+    }\r
+  }\r
+#endif /* PBUF_STATS */\r
+\r
+  SYS_ARCH_UNPROTECT(old_level);\r
+  return p;\r
+}\r
+\r
+\r
+/**\r
+ * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).\r
+ *\r
+ * The actual memory allocated for the pbuf is determined by the\r
+ * layer at which the pbuf is allocated and the requested size\r
+ * (from the size parameter).\r
+ *\r
+ * @param flag this parameter decides how and where the pbuf\r
+ * should be allocated as follows:\r
+ *\r
+ * - PBUF_RAM: buffer memory for pbuf is allocated as one large\r
+ *             chunk. This includes protocol headers as well.\r
+ * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for\r
+ *             protocol headers. Additional headers must be prepended\r
+ *             by allocating another pbuf and chain in to the front of\r
+ *             the ROM pbuf. It is assumed that the memory used is really\r
+ *             similar to ROM in that it is immutable and will not be\r
+ *             changed. Memory which is dynamic should generally not\r
+ *             be attached to PBUF_ROM pbufs. Use PBUF_REF instead.\r
+ * - PBUF_REF: no buffer memory is allocated for the pbuf, even for\r
+ *             protocol headers. It is assumed that the pbuf is only\r
+ *             being used in a single thread. If the pbuf gets queued,\r
+ *             then pbuf_take should be called to copy the buffer.\r
+ * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from\r
+ *              the pbuf pool that is allocated during pbuf_init().\r
+ *\r
+ * @return the allocated pbuf. If multiple pbufs where allocated, this\r
+ * is the first pbuf of a pbuf chain.\r
+ */\r
+struct pbuf *\r
+pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)\r
+{\r
+  struct pbuf *p, *q, *r;\r
+  u16_t offset;\r
+  s32_t rem_len; /* remaining length */\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F")\n", length));\r
+\r
+  /* determine header offset */\r
+  offset = 0;\r
+  switch (l) {\r
+  case PBUF_TRANSPORT:\r
+    /* add room for transport (often TCP) layer header */\r
+    offset += PBUF_TRANSPORT_HLEN;\r
+    /* FALLTHROUGH */\r
+  case PBUF_IP:\r
+    /* add room for IP layer header */\r
+    offset += PBUF_IP_HLEN;\r
+    /* FALLTHROUGH */\r
+  case PBUF_LINK:\r
+    /* add room for link layer header */\r
+    offset += PBUF_LINK_HLEN;\r
+    break;\r
+  case PBUF_RAW:\r
+    break;\r
+  default:\r
+    LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);\r
+    return NULL;\r
+  }\r
+\r
+  switch (flag) {\r
+  case PBUF_POOL:\r
+    /* allocate head of pbuf chain into p */\r
+    p = pbuf_pool_alloc();\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));\r
+    if (p == NULL) {\r
+#if PBUF_STATS\r
+      ++lwip_stats.pbuf.err;\r
+#endif /* PBUF_STATS */\r
+      return NULL;\r
+    }\r
+    p->next = NULL;\r
+\r
+    /* make the payload pointer point 'offset' bytes into pbuf data memory */\r
+    p->payload = MEM_ALIGN((void *)((u8_t *)p + (sizeof(struct pbuf) + offset)));\r
+    LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",\r
+            ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);\r
+    /* the total length of the pbuf chain is the requested size */\r
+    p->tot_len = length;\r
+    /* set the length of the first pbuf in the chain */\r
+    p->len = length > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: length;\r
+    /* set reference count (needed here in case we fail) */\r
+    p->ref = 1;\r
+\r
+    /* now allocate the tail of the pbuf chain */\r
+\r
+    /* remember first pbuf for linkage in next iteration */\r
+    r = p;\r
+    /* remaining length to be allocated */\r
+    rem_len = length - p->len;\r
+    /* any remaining pbufs to be allocated? */\r
+    while (rem_len > 0) {\r
+      q = pbuf_pool_alloc();\r
+      if (q == NULL) {\r
+       LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_alloc: Out of pbufs in pool.\n"));\r
+#if PBUF_STATS\r
+        ++lwip_stats.pbuf.err;\r
+#endif /* PBUF_STATS */\r
+        /* free chain so far allocated */\r
+        pbuf_free(p);\r
+        /* bail out unsuccesfully */\r
+        return NULL;\r
+      }\r
+      q->next = NULL;\r
+      /* make previous pbuf point to this pbuf */\r
+      r->next = q;\r
+      /* set total length of this pbuf and next in chain */\r
+      q->tot_len = rem_len;\r
+      /* this pbuf length is pool size, unless smaller sized tail */\r
+      q->len = rem_len > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rem_len;\r
+      q->payload = (void *)((u8_t *)q + sizeof(struct pbuf));\r
+      LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",\r
+              ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);\r
+      q->ref = 1;\r
+      /* calculate remaining length to be allocated */\r
+      rem_len -= q->len;\r
+      /* remember this pbuf for linkage in next iteration */\r
+      r = q;\r
+    }\r
+    /* end of chain */\r
+    /*r->next = NULL;*/\r
+\r
+    break;\r
+  case PBUF_RAM:\r
+    /* If pbuf is to be allocated in RAM, allocate memory for it. */\r
+    p = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + offset) + MEM_ALIGN_SIZE(length));\r
+    if (p == NULL) {\r
+      return NULL;\r
+    }\r
+    /* Set up internal structure of the pbuf. */\r
+    p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf) + offset));\r
+    p->len = p->tot_len = length;\r
+    p->next = NULL;\r
+    p->flags = PBUF_FLAG_RAM;\r
+\r
+    LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",\r
+           ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);\r
+    break;\r
+  /* pbuf references existing (non-volatile static constant) ROM payload? */\r
+  case PBUF_ROM:\r
+  /* pbuf references existing (externally allocated) RAM payload? */\r
+  case PBUF_REF:\r
+    /* only allocate memory for the pbuf structure */\r
+    p = memp_malloc(MEMP_PBUF);\r
+    if (p == NULL) {\r
+      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == PBUF_ROM?"ROM":"REF"));\r
+      return NULL;\r
+    }\r
+    /* caller must set this field properly, afterwards */\r
+    p->payload = NULL;\r
+    p->len = p->tot_len = length;\r
+    p->next = NULL;\r
+    p->flags = (flag == PBUF_ROM? PBUF_FLAG_ROM: PBUF_FLAG_REF);\r
+    break;\r
+  default:\r
+    LWIP_ASSERT("pbuf_alloc: erroneous flag", 0);\r
+    return NULL;\r
+  }\r
+  /* set reference count */\r
+  p->ref = 1;\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));\r
+  return p;\r
+}\r
+\r
+\r
+#if PBUF_STATS\r
+#define DEC_PBUF_STATS do { --lwip_stats.pbuf.used; } while (0)\r
+#else /* PBUF_STATS */\r
+#define DEC_PBUF_STATS\r
+#endif /* PBUF_STATS */\r
+\r
+#define PBUF_POOL_FAST_FREE(p)  do {                                    \\r
+                                  p->next = pbuf_pool;                  \\r
+                                  pbuf_pool = p;                        \\r
+                                  DEC_PBUF_STATS;                       \\r
+                                } while (0)\r
+\r
+#if SYS_LIGHTWEIGHT_PROT\r
+#define PBUF_POOL_FREE(p)  do {                                         \\r
+                                SYS_ARCH_DECL_PROTECT(old_level);       \\r
+                                SYS_ARCH_PROTECT(old_level);            \\r
+                                PBUF_POOL_FAST_FREE(p);                 \\r
+                                SYS_ARCH_UNPROTECT(old_level);          \\r
+                               } while (0)\r
+#else /* SYS_LIGHTWEIGHT_PROT */\r
+#define PBUF_POOL_FREE(p)  do {                                         \\r
+                             sys_sem_wait(pbuf_pool_free_sem);          \\r
+                             PBUF_POOL_FAST_FREE(p);                    \\r
+                             sys_sem_signal(pbuf_pool_free_sem);        \\r
+                           } while (0)\r
+#endif /* SYS_LIGHTWEIGHT_PROT */\r
+\r
+/**\r
+ * Shrink a pbuf chain to a desired length.\r
+ *\r
+ * @param p pbuf to shrink.\r
+ * @param new_len desired new length of pbuf chain\r
+ *\r
+ * Depending on the desired length, the first few pbufs in a chain might\r
+ * be skipped and left unchanged. The new last pbuf in the chain will be\r
+ * resized, and any remaining pbufs will be freed.\r
+ *\r
+ * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.\r
+ * @note May not be called on a packet queue.\r
+ *\r
+ * @bug Cannot grow the size of a pbuf (chain) (yet).\r
+ */\r
+void\r
+pbuf_realloc(struct pbuf *p, u16_t new_len)\r
+{\r
+  struct pbuf *q;\r
+  u16_t rem_len; /* remaining length */\r
+  s16_t grow;\r
+\r
+  LWIP_ASSERT("pbuf_realloc: sane p->flags", p->flags == PBUF_FLAG_POOL ||\r
+              p->flags == PBUF_FLAG_ROM ||\r
+              p->flags == PBUF_FLAG_RAM ||\r
+              p->flags == PBUF_FLAG_REF);\r
+\r
+  /* desired length larger than current length? */\r
+  if (new_len >= p->tot_len) {\r
+    /* enlarging not yet supported */\r
+    return;\r
+  }\r
+\r
+  /* the pbuf chain grows by (new_len - p->tot_len) bytes\r
+   * (which may be negative in case of shrinking) */\r
+  grow = new_len - p->tot_len;\r
+\r
+  /* first, step over any pbufs that should remain in the chain */\r
+  rem_len = new_len;\r
+  q = p;\r
+  /* should this pbuf be kept? */\r
+  while (rem_len > q->len) {\r
+    /* decrease remaining length by pbuf length */\r
+    rem_len -= q->len;\r
+    /* decrease total length indicator */\r
+    q->tot_len += grow;\r
+    /* proceed to next pbuf in chain */\r
+    q = q->next;\r
+  }\r
+  /* we have now reached the new last pbuf (in q) */\r
+  /* rem_len == desired length for pbuf q */\r
+\r
+  /* shrink allocated memory for PBUF_RAM */\r
+  /* (other types merely adjust their length fields */\r
+  if ((q->flags == PBUF_FLAG_RAM) && (rem_len != q->len)) {\r
+    /* reallocate and adjust the length of the pbuf that will be split */\r
+    mem_realloc(q, (u8_t *)q->payload - (u8_t *)q + rem_len);\r
+  }\r
+  /* adjust length fields for new last pbuf */\r
+  q->len = rem_len;\r
+  q->tot_len = q->len;\r
+\r
+  /* any remaining pbufs in chain? */\r
+  if (q->next != NULL) {\r
+    /* free remaining pbufs in chain */\r
+    pbuf_free(q->next);\r
+  }\r
+  /* q is last packet in chain */\r
+  q->next = NULL;\r
+\r
+}\r
+\r
+/**\r
+ * Adjusts the payload pointer to hide or reveal headers in the payload.\r
+ *\r
+ * Adjusts the ->payload pointer so that space for a header\r
+ * (dis)appears in the pbuf payload.\r
+ *\r
+ * The ->payload, ->tot_len and ->len fields are adjusted.\r
+ *\r
+ * @param hdr_size_inc Number of bytes to increment header size which\r
+ * increases the size of the pbuf. New space is on the front.\r
+ * (Using a negative value decreases the header size.)\r
+ * If hdr_size_inc is 0, this function does nothing and returns succesful.\r
+ *\r
+ * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so\r
+ * the call will fail. A check is made that the increase in header size does\r
+ * not move the payload pointer in front of the start of the buffer.\r
+ * @return non-zero on failure, zero on success.\r
+ *\r
+ */\r
+u8_t\r
+pbuf_header(struct pbuf *p, s16_t header_size_increment)\r
+{\r
+  void *payload;\r
+\r
+  LWIP_ASSERT("p != NULL", p != NULL);\r
+  if ((header_size_increment == 0) || (p == NULL)) return 0;\r
\r
+  /* remember current payload pointer */\r
+  payload = p->payload;\r
+\r
+  /* pbuf types containing payloads? */\r
+  if (p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_POOL) {\r
+    /* set new payload pointer */\r
+    p->payload = (u8_t *)p->payload - header_size_increment;\r
+    /* boundary check fails? */\r
+    if ((u8_t *)p->payload < (u8_t *)p + sizeof(struct pbuf)) {\r
+      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",\r
+        (void *)p->payload,\r
+        (void *)(p + 1)));\\r
+      /* restore old payload pointer */\r
+      p->payload = payload;\r
+      /* bail out unsuccesfully */\r
+      return 1;\r
+    }\r
+  /* pbuf types refering to external payloads? */\r
+  } else if (p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_ROM) {\r
+    /* hide a header in the payload? */\r
+    if ((header_size_increment < 0) && (header_size_increment - p->len <= 0)) {\r
+      /* increase payload pointer */\r
+      p->payload = (u8_t *)p->payload - header_size_increment;\r
+    } else {\r
+      /* cannot expand payload to front (yet!)\r
+       * bail out unsuccesfully */\r
+      return 1;\r
+    }\r
+  }\r
+  /* modify pbuf length fields */\r
+  p->len += header_size_increment;\r
+  p->tot_len += header_size_increment;\r
+\r
+  LWIP_DEBUGF( PBUF_DEBUG, ("pbuf_header: old %p new %p (%"S16_F")\n",\r
+    (void *)payload, (void *)p->payload, header_size_increment));\r
+\r
+  return 0;\r
+}\r
+\r
+/**\r
+ * Dereference a pbuf chain or queue and deallocate any no-longer-used\r
+ * pbufs at the head of this chain or queue.\r
+ *\r
+ * Decrements the pbuf reference count. If it reaches zero, the pbuf is\r
+ * deallocated.\r
+ *\r
+ * For a pbuf chain, this is repeated for each pbuf in the chain,\r
+ * up to the first pbuf which has a non-zero reference count after\r
+ * decrementing. So, when all reference counts are one, the whole\r
+ * chain is free'd.\r
+ *\r
+ * @param pbuf The pbuf (chain) to be dereferenced.\r
+ *\r
+ * @return the number of pbufs that were de-allocated\r
+ * from the head of the chain.\r
+ *\r
+ * @note MUST NOT be called on a packet queue (Not verified to work yet).\r
+ * @note the reference counter of a pbuf equals the number of pointers\r
+ * that refer to the pbuf (or into the pbuf).\r
+ *\r
+ * @internal examples:\r
+ *\r
+ * Assuming existing chains a->b->c with the following reference\r
+ * counts, calling pbuf_free(a) results in:\r
+ * \r
+ * 1->2->3 becomes ...1->3\r
+ * 3->3->3 becomes 2->3->3\r
+ * 1->1->2 becomes ......1\r
+ * 2->1->1 becomes 1->1->1\r
+ * 1->1->1 becomes .......\r
+ *\r
+ */\r
+u8_t\r
+pbuf_free(struct pbuf *p)\r
+{\r
+  struct pbuf *q;\r
+  u8_t count;\r
+  SYS_ARCH_DECL_PROTECT(old_level);\r
+\r
+  LWIP_ASSERT("p != NULL", p != NULL);\r
+  /* if assertions are disabled, proceed with debug output */\r
+  if (p == NULL) {\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n"));\r
+    return 0;\r
+  }\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_free(%p)\n", (void *)p));\r
+\r
+  PERF_START;\r
+\r
+  LWIP_ASSERT("pbuf_free: sane flags",\r
+    p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_ROM ||\r
+    p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_POOL);\r
+\r
+  count = 0;\r
+  /* Since decrementing ref cannot be guaranteed to be a single machine operation\r
+   * we must protect it. Also, the later test of ref must be protected.\r
+   */\r
+  SYS_ARCH_PROTECT(old_level);\r
+  /* de-allocate all consecutive pbufs from the head of the chain that\r
+   * obtain a zero reference count after decrementing*/\r
+  while (p != NULL) {\r
+    /* all pbufs in a chain are referenced at least once */\r
+    LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);\r
+    /* decrease reference count (number of pointers to pbuf) */\r
+    p->ref--;\r
+    /* this pbuf is no longer referenced to? */\r
+    if (p->ref == 0) {\r
+      /* remember next pbuf in chain for next iteration */\r
+      q = p->next;\r
+      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p));\r
+      /* is this a pbuf from the pool? */\r
+      if (p->flags == PBUF_FLAG_POOL) {\r
+        p->len = p->tot_len = PBUF_POOL_BUFSIZE;\r
+        p->payload = (void *)((u8_t *)p + sizeof(struct pbuf));\r
+        PBUF_POOL_FREE(p);\r
+      /* is this a ROM or RAM referencing pbuf? */\r
+      } else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) {\r
+        memp_free(MEMP_PBUF, p);\r
+      /* p->flags == PBUF_FLAG_RAM */\r
+      } else {\r
+        mem_free(p);\r
+      }\r
+      count++;\r
+      /* proceed to next pbuf */\r
+      p = q;\r
+    /* p->ref > 0, this pbuf is still referenced to */\r
+    /* (and so the remaining pbufs in chain as well) */\r
+    } else {\r
+      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, (u16_t)p->ref));\r
+      /* stop walking through the chain */\r
+      p = NULL;\r
+    }\r
+  }\r
+  SYS_ARCH_UNPROTECT(old_level);\r
+  PERF_STOP("pbuf_free");\r
+  /* return number of de-allocated pbufs */\r
+  return count;\r
+}\r
+\r
+/**\r
+ * Count number of pbufs in a chain\r
+ *\r
+ * @param p first pbuf of chain\r
+ * @return the number of pbufs in a chain\r
+ */\r
+\r
+u8_t\r
+pbuf_clen(struct pbuf *p)\r
+{\r
+  u8_t len;\r
+\r
+  len = 0;\r
+  while (p != NULL) {\r
+    ++len;\r
+    p = p->next;\r
+  }\r
+  return len;\r
+}\r
+\r
+/**\r
+ * Increment the reference count of the pbuf.\r
+ *\r
+ * @param p pbuf to increase reference counter of\r
+ *\r
+ */\r
+void\r
+pbuf_ref(struct pbuf *p)\r
+{\r
+  SYS_ARCH_DECL_PROTECT(old_level);\r
+  /* pbuf given? */\r
+  if (p != NULL) {\r
+    SYS_ARCH_PROTECT(old_level);\r
+    ++(p->ref);\r
+    SYS_ARCH_UNPROTECT(old_level);\r
+  }\r
+}\r
+\r
+/**\r
+ * Concatenate two pbufs (each may be a pbuf chain) and take over\r
+ * the caller's reference of the tail pbuf.\r
+ * \r
+ * @note The caller MAY NOT reference the tail pbuf afterwards.\r
+ * Use pbuf_chain() for that purpose.\r
+ * \r
+ * @see pbuf_chain()\r
+ */\r
+\r
+void\r
+pbuf_cat(struct pbuf *h, struct pbuf *t)\r
+{\r
+  struct pbuf *p;\r
+\r
+  LWIP_ASSERT("h != NULL (programmer violates API)", h != NULL);\r
+  LWIP_ASSERT("t != NULL (programmer violates API)", t != NULL);\r
+  if ((h == NULL) || (t == NULL)) return;\r
+\r
+  /* proceed to last pbuf of chain */\r
+  for (p = h; p->next != NULL; p = p->next) {\r
+    /* add total length of second chain to all totals of first chain */\r
+    p->tot_len += t->tot_len;\r
+  }\r
+  /* { p is last pbuf of first h chain, p->next == NULL } */\r
+  LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);\r
+  LWIP_ASSERT("p->next == NULL", p->next == NULL);\r
+  /* add total length of second chain to last pbuf total of first chain */\r
+  p->tot_len += t->tot_len;\r
+  /* chain last pbuf of head (p) with first of tail (t) */\r
+  p->next = t;\r
+  /* p->next now references t, but the caller will drop its reference to t,\r
+   * so netto there is no change to the reference count of t.\r
+   */\r
+}\r
+\r
+/**\r
+ * Chain two pbufs (or pbuf chains) together.\r
+ * \r
+ * The caller MUST call pbuf_free(t) once it has stopped\r
+ * using it. Use pbuf_cat() instead if you no longer use t.\r
+ * \r
+ * @param h head pbuf (chain)\r
+ * @param t tail pbuf (chain)\r
+ * @note The pbufs MUST belong to the same packet.\r
+ * @note MAY NOT be called on a packet queue.\r
+ *\r
+ * The ->tot_len fields of all pbufs of the head chain are adjusted.\r
+ * The ->next field of the last pbuf of the head chain is adjusted.\r
+ * The ->ref field of the first pbuf of the tail chain is adjusted.\r
+ *\r
+ */\r
+void\r
+pbuf_chain(struct pbuf *h, struct pbuf *t)\r
+{\r
+  pbuf_cat(h, t);\r
+  /* t is now referenced by h */\r
+  pbuf_ref(t);\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));\r
+}\r
+\r
+/* For packet queueing. Note that queued packets MUST be dequeued first\r
+ * using pbuf_dequeue() before calling other pbuf_() functions. */\r
+#if ARP_QUEUEING\r
+/**\r
+ * Add a packet to the end of a queue.\r
+ *\r
+ * @param q pointer to first packet on the queue\r
+ * @param n packet to be queued\r
+ *\r
+ * Both packets MUST be given, and must be different.\r
+ */\r
+void\r
+pbuf_queue(struct pbuf *p, struct pbuf *n)\r
+{\r
+#if PBUF_DEBUG /* remember head of queue */\r
+  struct pbuf *q = p;\r
+#endif\r
+  /* programmer stupidity checks */\r
+  LWIP_ASSERT("p == NULL in pbuf_queue: this indicates a programmer error\n", p != NULL);\r
+  LWIP_ASSERT("n == NULL in pbuf_queue: this indicates a programmer error\n", n != NULL);\r
+  LWIP_ASSERT("p == n in pbuf_queue: this indicates a programmer error\n", p != n);\r
+  if ((p == NULL) || (n == NULL) || (p == n)){\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_HALT | 3, ("pbuf_queue: programmer argument error\n"));\r
+    return;\r
+  }\r
+\r
+  /* iterate through all packets on queue */\r
+  while (p->next != NULL) {\r
+/* be very picky about pbuf chain correctness */\r
+#if PBUF_DEBUG\r
+    /* iterate through all pbufs in packet */\r
+    while (p->tot_len != p->len) {\r
+      /* make sure invariant condition holds */\r
+      LWIP_ASSERT("p->len < p->tot_len", p->len < p->tot_len);\r
+      /* make sure each packet is complete */\r
+      LWIP_ASSERT("p->next != NULL", p->next != NULL);\r
+      p = p->next;\r
+      /* { p->tot_len == p->len => p is last pbuf of a packet } */\r
+    }\r
+    /* { p is last pbuf of a packet } */\r
+    /* proceed to next packet on queue */\r
+#endif\r
+    /* proceed to next pbuf */\r
+    if (p->next != NULL) p = p->next;\r
+  }\r
+  /* { p->tot_len == p->len and p->next == NULL } ==>\r
+   * { p is last pbuf of last packet on queue } */\r
+  /* chain last pbuf of queue with n */\r
+  p->next = n;\r
+  /* n is now referenced to by the (packet p in the) queue */\r
+  pbuf_ref(n);\r
+#if PBUF_DEBUG\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2,\r
+    ("pbuf_queue: newly queued packet %p sits after packet %p in queue %p\n",\r
+    (void *)n, (void *)p, (void *)q));\r
+#endif\r
+}\r
+\r
+/**\r
+ * Remove a packet from the head of a queue.\r
+ *\r
+ * The caller MUST reference the remainder of the queue (as returned). The\r
+ * caller MUST NOT call pbuf_ref() as it implicitly takes over the reference\r
+ * from p.\r
+ * \r
+ * @param p pointer to first packet on the queue which will be dequeued.\r
+ * @return first packet on the remaining queue (NULL if no further packets).\r
+ *\r
+ */\r
+struct pbuf *\r
+pbuf_dequeue(struct pbuf *p)\r
+{\r
+  struct pbuf *q;\r
+  LWIP_ASSERT("p != NULL", p != NULL);\r
+\r
+  /* iterate through all pbufs in packet p */\r
+  while (p->tot_len != p->len) {\r
+    /* make sure invariant condition holds */\r
+    LWIP_ASSERT("p->len < p->tot_len", p->len < p->tot_len);\r
+    /* make sure each packet is complete */\r
+    LWIP_ASSERT("p->next != NULL", p->next != NULL);\r
+    p = p->next;\r
+  }\r
+  /* { p->tot_len == p->len } => p is the last pbuf of the first packet */\r
+  /* remember next packet on queue in q */\r
+  q = p->next;\r
+  /* dequeue packet p from queue */\r
+  p->next = NULL;\r
+  /* any next packet on queue? */\r
+  if (q != NULL) {\r
+    /* although q is no longer referenced by p, it MUST be referenced by\r
+     * the caller, who is maintaining this packet queue. So, we do not call\r
+     * pbuf_free(q) here, resulting in an implicit pbuf_ref(q) for the caller. */\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_dequeue: first remaining packet on queue is %p\n", (void *)q));\r
+  } else {\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_dequeue: no further packets on queue\n"));\r
+  }\r
+  return q;\r
+}\r
+#endif\r
+\r
+/**\r
+ *\r
+ * Create PBUF_POOL (or PBUF_RAM) copies of PBUF_REF pbufs.\r
+ *\r
+ * Used to queue packets on behalf of the lwIP stack, such as\r
+ * ARP based queueing.\r
+ *\r
+ * Go through a pbuf chain and replace any PBUF_REF buffers\r
+ * with PBUF_POOL (or PBUF_RAM) pbufs, each taking a copy of\r
+ * the referenced data.\r
+ *\r
+ * @note You MUST explicitly use p = pbuf_take(p);\r
+ * The pbuf you give as argument, may have been replaced\r
+ * by a (differently located) copy through pbuf_take()!\r
+ *\r
+ * @note Any replaced pbufs will be freed through pbuf_free().\r
+ * This may deallocate them if they become no longer referenced.\r
+ *\r
+ * @param p Head of pbuf chain to process\r
+ *\r
+ * @return Pointer to head of pbuf chain\r
+ */\r
+struct pbuf *\r
+pbuf_take(struct pbuf *p)\r
+{\r
+  struct pbuf *q , *prev, *head;\r
+  LWIP_ASSERT("pbuf_take: p != NULL\n", p != NULL);\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_take(%p)\n", (void*)p));\r
+\r
+  prev = NULL;\r
+  head = p;\r
+  /* iterate through pbuf chain */\r
+  do\r
+  {\r
+    /* pbuf is of type PBUF_REF? */\r
+    if (p->flags == PBUF_FLAG_REF) {\r
+      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE, ("pbuf_take: encountered PBUF_REF %p\n", (void *)p));\r
+      /* allocate a pbuf (w/ payload) fully in RAM */\r
+      /* PBUF_POOL buffers are faster if we can use them */\r
+      if (p->len <= PBUF_POOL_BUFSIZE) {\r
+        q = pbuf_alloc(PBUF_RAW, p->len, PBUF_POOL);\r
+        if (q == NULL) {\r
+          LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_POOL\n"));\r
+        }\r
+      } else {\r
+        /* no replacement pbuf yet */\r
+        q = NULL;\r
+        LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: PBUF_POOL too small to replace PBUF_REF\n"));\r
+      }\r
+      /* no (large enough) PBUF_POOL was available? retry with PBUF_RAM */\r
+      if (q == NULL) {\r
+        q = pbuf_alloc(PBUF_RAW, p->len, PBUF_RAM);\r
+        if (q == NULL) {\r
+          LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_RAM\n"));\r
+        }\r
+      }\r
+      /* replacement pbuf could be allocated? */\r
+      if (q != NULL)\r
+      {\r
+        /* copy p to q */\r
+        /* copy successor */\r
+        q->next = p->next;\r
+        /* remove linkage from original pbuf */\r
+        p->next = NULL;\r
+        /* remove linkage to original pbuf */\r
+        if (prev != NULL) {\r
+          /* prev->next == p at this point */\r
+          LWIP_ASSERT("prev->next == p", prev->next == p);\r
+          /* break chain and insert new pbuf instead */\r
+          prev->next = q;\r
+        /* prev == NULL, so we replaced the head pbuf of the chain */\r
+        } else {\r
+          head = q;\r
+        }\r
+        /* copy pbuf payload */\r
+        memcpy(q->payload, p->payload, p->len);\r
+        q->tot_len = p->tot_len;\r
+        q->len = p->len;\r
+        /* in case p was the first pbuf, it is no longer refered to by\r
+         * our caller, as the caller MUST do p = pbuf_take(p);\r
+         * in case p was not the first pbuf, it is no longer refered to\r
+         * by prev. we can safely free the pbuf here.\r
+         * (note that we have set p->next to NULL already so that\r
+         * we will not free the rest of the chain by accident.)\r
+         */\r
+        pbuf_free(p);\r
+        /* do not copy ref, since someone else might be using the old buffer */\r
+        LWIP_DEBUGF(PBUF_DEBUG, ("pbuf_take: replaced PBUF_REF %p with %p\n", (void *)p, (void *)q));\r
+        p = q;\r
+      } else {\r
+        /* deallocate chain */\r
+        pbuf_free(head);\r
+        LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_take: failed to allocate replacement pbuf for %p\n", (void *)p));\r
+        return NULL;\r
+      }\r
+    /* p->flags != PBUF_FLAG_REF */\r
+    } else {\r
+      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: skipping pbuf not of type PBUF_REF\n"));\r
+    }\r
+    /* remember this pbuf */\r
+    prev = p;\r
+    /* proceed to next pbuf in original chain */\r
+    p = p->next;\r
+  } while (p);\r
+  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: end of chain reached.\n"));\r
+\r
+  return head;\r
+}\r
+\r
+/**\r
+ * Dechains the first pbuf from its succeeding pbufs in the chain.\r
+ *\r
+ * Makes p->tot_len field equal to p->len.\r
+ * @param p pbuf to dechain\r
+ * @return remainder of the pbuf chain, or NULL if it was de-allocated.\r
+ * @note May not be called on a packet queue.\r
+ */\r
+struct pbuf *\r
+pbuf_dechain(struct pbuf *p)\r
+{\r
+  struct pbuf *q;\r
+  u8_t tail_gone = 1;\r
+  /* tail */\r
+  q = p->next;\r
+  /* pbuf has successor in chain? */\r
+  if (q != NULL) {\r
+    /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */\r
+    LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);\r
+    /* enforce invariant if assertion is disabled */\r
+    q->tot_len = p->tot_len - p->len;\r
+    /* decouple pbuf from remainder */\r
+    p->next = NULL;\r
+    /* total length of pbuf p is its own length only */\r
+    p->tot_len = p->len;\r
+    /* q is no longer referenced by p, free it */\r
+    LWIP_DEBUGF(PBUF_DEBUG | DBG_STATE, ("pbuf_dechain: unreferencing %p\n", (void *)q));\r
+    tail_gone = pbuf_free(q);\r
+    if (tail_gone > 0) {\r
+      LWIP_DEBUGF(PBUF_DEBUG | DBG_STATE,\r
+                  ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));\r
+    }\r
+    /* return remaining tail or NULL if deallocated */\r
+  }\r
+  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */\r
+  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);\r
+  return (tail_gone > 0? NULL: q);\r
+}\r