⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pbuf.c

📁 lwip-1.4.0
💻 C
📖 第 1 页 / 共 3 页
字号:
  return &p->pbuf;}#endif /* LWIP_SUPPORT_CUSTOM_PBUF *//** * Shrink a pbuf chain to a desired length. * * @param p pbuf to shrink. * @param new_len desired new length of pbuf chain * * Depending on the desired length, the first few pbufs in a chain might * be skipped and left unchanged. The new last pbuf in the chain will be * resized, and any remaining pbufs will be freed. * * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted. * @note May not be called on a packet queue. * * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain). */voidpbuf_realloc(struct pbuf *p, u16_t new_len){  struct pbuf *q;  u16_t rem_len; /* remaining length */  s32_t grow;  LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);  LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||              p->type == PBUF_ROM ||              p->type == PBUF_RAM ||              p->type == PBUF_REF);  /* desired length larger than current length? */  if (new_len >= p->tot_len) {    /* enlarging not yet supported */    return;  }  /* the pbuf chain grows by (new_len - p->tot_len) bytes   * (which may be negative in case of shrinking) */  grow = new_len - p->tot_len;  /* first, step over any pbufs that should remain in the chain */  rem_len = new_len;  q = p;  /* should this pbuf be kept? */  while (rem_len > q->len) {    /* decrease remaining length by pbuf length */    rem_len -= q->len;    /* decrease total length indicator */    LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);    q->tot_len += (u16_t)grow;    /* proceed to next pbuf in chain */    q = q->next;    LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);  }  /* we have now reached the new last pbuf (in q) */  /* rem_len == desired length for pbuf q */  /* shrink allocated memory for PBUF_RAM */  /* (other types merely adjust their length fields */  if ((q->type == PBUF_RAM) && (rem_len != q->len)) {    /* reallocate and adjust the length of the pbuf that will be split */    q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);    LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);  }  /* adjust length fields for new last pbuf */  q->len = rem_len;  q->tot_len = q->len;  /* any remaining pbufs in chain? */  if (q->next != NULL) {    /* free remaining pbufs in chain */    pbuf_free(q->next);  }  /* q is last packet in chain */  q->next = NULL;}/** * Adjusts the payload pointer to hide or reveal headers in the payload. * * Adjusts the ->payload pointer so that space for a header * (dis)appears in the pbuf payload. * * The ->payload, ->tot_len and ->len fields are adjusted. * * @param p pbuf to change the header size. * @param header_size_increment Number of bytes to increment header size which * increases the size of the pbuf. New space is on the front. * (Using a negative value decreases the header size.) * If hdr_size_inc is 0, this function does nothing and returns succesful. * * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so * the call will fail. A check is made that the increase in header size does * not move the payload pointer in front of the start of the buffer. * @return non-zero on failure, zero on success. * */u8_tpbuf_header(struct pbuf *p, s16_t header_size_increment){  u16_t type;  void *payload;  u16_t increment_magnitude;  LWIP_ASSERT("p != NULL", p != NULL);  if ((header_size_increment == 0) || (p == NULL)) {    return 0;  }   if (header_size_increment < 0){    increment_magnitude = -header_size_increment;    /* Check that we aren't going to move off the end of the pbuf */    LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);  } else {    increment_magnitude = header_size_increment;#if 0    /* Can't assert these as some callers speculatively call         pbuf_header() to see if it's OK.  Will return 1 below instead. */    /* Check that we've got the correct type of pbuf to work with */    LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",                 p->type == PBUF_RAM || p->type == PBUF_POOL);    /* Check that we aren't going to move off the beginning of the pbuf */    LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",                (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);#endif  }  type = p->type;  /* remember current payload pointer */  payload = p->payload;  /* pbuf types containing payloads? */  if (type == PBUF_RAM || type == PBUF_POOL) {    /* set new payload pointer */    p->payload = (u8_t *)p->payload - header_size_increment;    /* boundary check fails? */    if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {      LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,        ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",        (void *)p->payload, (void *)(p + 1)));      /* restore old payload pointer */      p->payload = payload;      /* bail out unsuccesfully */      return 1;    }  /* pbuf types refering to external payloads? */  } else if (type == PBUF_REF || type == PBUF_ROM) {    /* hide a header in the payload? */    if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {      /* increase payload pointer */      p->payload = (u8_t *)p->payload - header_size_increment;    } else {      /* cannot expand payload to front (yet!)       * bail out unsuccesfully */      return 1;    }  } else {    /* Unknown type */    LWIP_ASSERT("bad pbuf type", 0);    return 1;  }  /* modify pbuf length fields */  p->len += header_size_increment;  p->tot_len += header_size_increment;  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",    (void *)payload, (void *)p->payload, header_size_increment));  return 0;}/** * Dereference a pbuf chain or queue and deallocate any no-longer-used * pbufs at the head of this chain or queue. * * Decrements the pbuf reference count. If it reaches zero, the pbuf is * deallocated. * * For a pbuf chain, this is repeated for each pbuf in the chain, * up to the first pbuf which has a non-zero reference count after * decrementing. So, when all reference counts are one, the whole * chain is free'd. * * @param p The pbuf (chain) to be dereferenced. * * @return the number of pbufs that were de-allocated * from the head of the chain. * * @note MUST NOT be called on a packet queue (Not verified to work yet). * @note the reference counter of a pbuf equals the number of pointers * that refer to the pbuf (or into the pbuf). * * @internal examples: * * Assuming existing chains a->b->c with the following reference * counts, calling pbuf_free(a) results in: *  * 1->2->3 becomes ...1->3 * 3->3->3 becomes 2->3->3 * 1->1->2 becomes ......1 * 2->1->1 becomes 1->1->1 * 1->1->1 becomes ....... * */u8_tpbuf_free(struct pbuf *p){  u16_t type;  struct pbuf *q;  u8_t count;  if (p == NULL) {    LWIP_ASSERT("p != NULL", p != NULL);    /* if assertions are disabled, proceed with debug output */    LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,      ("pbuf_free(p == NULL) was called.\n"));    return 0;  }  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));  PERF_START;  LWIP_ASSERT("pbuf_free: sane type",    p->type == PBUF_RAM || p->type == PBUF_ROM ||    p->type == PBUF_REF || p->type == PBUF_POOL);  count = 0;  /* de-allocate all consecutive pbufs from the head of the chain that   * obtain a zero reference count after decrementing*/  while (p != NULL) {    u16_t ref;    SYS_ARCH_DECL_PROTECT(old_level);    /* Since decrementing ref cannot be guaranteed to be a single machine operation     * we must protect it. We put the new ref into a local variable to prevent     * further protection. */    SYS_ARCH_PROTECT(old_level);    /* all pbufs in a chain are referenced at least once */    LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);    /* decrease reference count (number of pointers to pbuf) */    ref = --(p->ref);    SYS_ARCH_UNPROTECT(old_level);    /* this pbuf is no longer referenced to? */    if (ref == 0) {      /* remember next pbuf in chain for next iteration */      q = p->next;      LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));      type = p->type;#if LWIP_SUPPORT_CUSTOM_PBUF      /* is this a custom pbuf? */      if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {        struct pbuf_custom *pc = (struct pbuf_custom*)p;        LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);        pc->custom_free_function(p);      } else#endif /* LWIP_SUPPORT_CUSTOM_PBUF */      {        /* is this a pbuf from the pool? */        if (type == PBUF_POOL) {          memp_free(MEMP_PBUF_POOL, p);        /* is this a ROM or RAM referencing pbuf? */        } else if (type == PBUF_ROM || type == PBUF_REF) {          memp_free(MEMP_PBUF, p);        /* type == PBUF_RAM */        } else {          mem_free(p);        }      }      count++;      /* proceed to next pbuf */      p = q;    /* p->ref > 0, this pbuf is still referenced to */    /* (and so the remaining pbufs in chain as well) */    } else {      LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));      /* stop walking through the chain */      p = NULL;    }  }  PERF_STOP("pbuf_free");  /* return number of de-allocated pbufs */  return count;}/** * Count number of pbufs in a chain * * @param p first pbuf of chain * @return the number of pbufs in a chain */u8_tpbuf_clen(struct pbuf *p){  u8_t len;  len = 0;  while (p != NULL) {    ++len;    p = p->next;  }  return len;}/** * Increment the reference count of the pbuf. * * @param p pbuf to increase reference counter of * */voidpbuf_ref(struct pbuf *p){  SYS_ARCH_DECL_PROTECT(old_level);  /* pbuf given? */  if (p != NULL) {    SYS_ARCH_PROTECT(old_level);    ++(p->ref);    SYS_ARCH_UNPROTECT(old_level);  }}/** * Concatenate two pbufs (each may be a pbuf chain) and take over * the caller's reference of the tail pbuf. *  * @note The caller MAY NOT reference the tail pbuf afterwards. * Use pbuf_chain() for that purpose. *  * @see pbuf_chain() */voidpbuf_cat(struct pbuf *h, struct pbuf *t){  struct pbuf *p;  LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",             ((h != NULL) && (t != NULL)), return;);  /* proceed to last pbuf of chain */  for (p = h; p->next != NULL; p = p->next) {    /* add total length of second chain to all totals of first chain */    p->tot_len += t->tot_len;  }  /* { p is last pbuf of first h chain, p->next == NULL } */  LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);  LWIP_ASSERT("p->next == NULL", p->next == NULL);  /* add total length of second chain to last pbuf total of first chain */  p->tot_len += t->tot_len;  /* chain last pbuf of head (p) with first of tail (t) */  p->next = t;  /* p->next now references t, but the caller will drop its reference to t,   * so netto there is no change to the reference count of t.   */}/** * Chain two pbufs (or pbuf chains) together. *  * The caller MUST call pbuf_free(t) once it has stopped * using it. Use pbuf_cat() instead if you no longer use t. *  * @param h head pbuf (chain) * @param t tail pbuf (chain) * @note The pbufs MUST belong to the same packet. * @note MAY NOT be called on a packet queue. * * The ->tot_len fields of all pbufs of the head chain are adjusted. * The ->next field of the last pbuf of the head chain is adjusted. * The ->ref field of the first pbuf of the tail chain is adjusted. * */voidpbuf_chain(struct pbuf *h, struct pbuf *t){  pbuf_cat(h, t);  /* t is now referenced by h */  pbuf_ref(t);  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));}/** * Dechains the first pbuf from its succeeding pbufs in the chain.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -