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

📄 pbuf.c

📁 一个基于ucos-ii和lwip的简单telent服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
 * * 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 1 on failure, 0 on success. * * @note May not be called on a packet queue. */u8_tpbuf_header(struct pbuf *p, s16_t header_size){  void *payload;  /* remember current payload pointer */  payload = p->payload;  /* pbuf types containing payloads? */  if (p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_POOL) {    /* set new payload pointer */    p->payload = (u8_t *)p->payload - header_size;    /* boundary check fails? */    if ((u8_t *)p->payload < (u8_t *)p + sizeof(struct pbuf)) {      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_header: failed as %p < %p\n",        (u8_t *)p->payload,        (u8_t *)p + sizeof(struct pbuf)) );\      /* restore old payload pointer */      p->payload = payload;      /* bail out unsuccesfully */      return 1;    }  /* pbuf types refering to payloads? */  } else if (p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_ROM) {    /* hide a header in the payload? */    if ((header_size < 0) && (header_size - p->len <= 0)) {      /* increase payload pointer */      p->payload = (u8_t *)p->payload - header_size;    } else {      /* cannot expand payload to front (yet!)       * bail out unsuccesfully */      return 1;    }  }  LWIP_DEBUGF( PBUF_DEBUG, ("pbuf_header: old %p new %p (%d)\n", (void *)payload, (void *)p->payload, header_size) );  /* modify pbuf length fields */  p->len += header_size;  p->tot_len += header_size;  return 0;}/** * Dereference a pbuf (chain) and deallocate any no-longer-used * pbufs at the head of this chain. * * 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 a pbuf which has a non-zero reference count after * decrementing. (This might de-allocate the whole chain.) * * @param pbuf 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. * @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){  struct pbuf *q;  u8_t count;  SYS_ARCH_DECL_PROTECT(old_level);  if (p == NULL) {    LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n"));    return 0;  }  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_free(%p)\n", (void *)p));  PERF_START;  LWIP_ASSERT("pbuf_free: sane flags",    p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_ROM ||    p->flags == PBUF_FLAG_REF || p->flags == PBUF_FLAG_POOL);  count = 0;  /* Since decrementing ref cannot be guaranteed to be a single machine operation   * we must protect it. Also, the later test of ref must be protected.   */  SYS_ARCH_PROTECT(old_level);  /* de-allocate all consecutive pbufs from the head of the chain that   * obtain a zero reference count after decrementing*/  while (p != NULL) {    /* 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) */    p->ref--;    /* this pbuf is no longer referenced to? */    if (p->ref == 0) {      /* remember next pbuf in chain for next iteration */      q = p->next;      LWIP_DEBUGF( PBUF_DEBUG | 2, ("pbuf_free: deallocating %p\n", (void *)p));      /* is this a pbuf from the pool? */      if (p->flags == PBUF_FLAG_POOL) {        p->len = p->tot_len = PBUF_POOL_BUFSIZE;        p->payload = (void *)((u8_t *)p + sizeof(struct pbuf));        PBUF_POOL_FREE(p);      /* a ROM or RAM referencing pbuf */      } else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) {        memp_free(MEMP_PBUF, p);      /* p->flags == PBUF_FLAG_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 | 2, ("pbuf_free: %p has ref %u, ending here.\n", (void *)p, (unsigned int)p->ref));      /* stop walking through chain */      p = NULL;    }  }  SYS_ARCH_UNPROTECT(old_level);  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_ASSERT("h != NULL", h != NULL);  LWIP_ASSERT("t != NULL", t != NULL);  if ((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);  /* 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;}/** * 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 | DBG_FRESH | 2, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));}/* For packet queueing. Note that queued packets must be dequeued first * before calling any pbuf functions. */#if ARP_QUEUEING/** * Add a packet to the end of a queue. * * @param q pointer to first packet on the queue * @param n packet to be queued * */voidpbuf_queue(struct pbuf *p, struct pbuf *n){  LWIP_ASSERT("p != NULL", p != NULL);  LWIP_ASSERT("n != NULL", n != NULL);  if ((p == NULL) || (n == NULL))    return;  /* iterate through all packets on queue */  while (p->next != NULL) {/* be very picky about pbuf chain correctness */#if PBUF_DEBUG    /* iterate through all pbufs in packet */    while (p->tot_len != p->len) {      /* make sure each packet is complete */      LWIP_ASSERT("p->next != NULL", p->next != NULL);      p = p->next;    }#endif    /* now p->tot_len == p->len */    /* proceed to next packet on queue */    p = p->next;  }  /* chain last pbuf of queue with n */  p->next = n;  /* n is now referenced to one more time */  pbuf_ref(n);  LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_queue: referencing queued packet %p\n", (void *)n));}/** * Remove a packet from the head of a queue. * * @param p pointer to first packet on the queue which will be dequeued. * @return first packet on the remaining queue (NULL if no further packets). * */struct pbuf *pbuf_dequeue(struct pbuf *p){  struct pbuf *q;  LWIP_ASSERT("p != NULL", p != NULL);  /* iterate through all pbufs in packet */  while (p->tot_len != p->len) {    /* make sure each packet is complete */    LWIP_ASSERT("p->next != NULL", p->next != NULL);    p = p->next;  }  /* remember next packet on queue */  q = p->next;  /* dequeue p from queue */  p->next = NULL;  /* q is now referenced to one less time */  pbuf_free(q);  LWIP_DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_dequeue: dereferencing remaining queue %p\n", (void *)q));  return q;}#endif/** * * Create PBUF_POOL (or PBUF_RAM) copies of PBUF_REF pbufs. * * Used to queue packets on behalf of the lwIP stack, such as * ARP based queueing. * * Go through a pbuf chain and replace any PBUF_REF buffers * with PBUF_POOL (or PBUF_RAM) pbufs, each taking a copy of * the referenced data. * * @note You MUST explicitly use p = pbuf_take(p); * The pbuf you give as argument, may have been replaced * by pbuf_take()! * * @note Any replaced pbufs will be freed through pbuf_free(). * This may deallocate them if they become no longer referenced. * * @param p Head of pbuf chain to process * * @return Pointer to head of pbuf chain */struct pbuf *pbuf_take(struct pbuf *p){  struct pbuf *q , *prev, *head;  LWIP_ASSERT("pbuf_take: p != NULL\n", p != NULL);  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_take(%p)\n", (void*)p));  prev = NULL;  head = p;  /* iterate through pbuf chain */  do  {    /* pbuf is of type PBUF_REF? */    if (p->flags == PBUF_FLAG_REF) {      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE, ("pbuf_take: encountered PBUF_REF %p\n", (void *)p));      /* allocate a pbuf (w/ payload) fully in RAM */      /* PBUF_POOL buffers are faster if we can use them */      if (p->len <= PBUF_POOL_BUFSIZE) {        q = pbuf_alloc(PBUF_RAW, p->len, PBUF_POOL);        if (q == NULL) LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_POOL\n"));      } else {        /* no replacement pbuf yet */        q = NULL;        LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: PBUF_POOL too small to replace PBUF_REF\n"));      }      /* no (large enough) PBUF_POOL was available? retry with PBUF_RAM */      if (q == NULL) {        q = pbuf_alloc(PBUF_RAW, p->len, PBUF_RAM);        if (q == NULL) LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_RAM\n"));      }      /* replacement pbuf could be allocated? */      if (q != NULL)      {        /* copy p to q */        /* copy successor */        q->next = p->next;        /* remove linkage from original pbuf */        p->next = NULL;        /* remove linkage to original pbuf */        if (prev != NULL) {          /* prev->next == p at this point */          LWIP_ASSERT("prev->next == p", prev->next == p);          /* break chain and insert new pbuf instead */          prev->next = q;        /* prev == NULL, so we replaced the head pbuf of the chain */        } else {          head = q;        }        /* copy pbuf payload */        memcpy(q->payload, p->payload, p->len);        q->tot_len = p->tot_len;        q->len = p->len;        /* in case p was the first pbuf, it is no longer refered to by         * our caller, as the caller MUST do p = pbuf_take(p);         * in case p was not the first pbuf, it is no longer refered to         * by prev. we can safely free the pbuf here.         * (note that we have set p->next to NULL already so that         * we will not free the rest of the chain by accident.)         */        pbuf_free(p);        /* do not copy ref, since someone else might be using the old buffer */        LWIP_DEBUGF(PBUF_DEBUG, ("pbuf_take: replaced PBUF_REF %p with %p\n", (void *)p, (void *)q));        p = q;      } else {        /* deallocate chain */        pbuf_free(head);        LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_take: failed to allocate replacement pbuf for %p\n", (void *)p));        return NULL;      }    /* p->flags != PBUF_FLAG_REF */    } else {      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: skipping pbuf not of type PBUF_REF\n"));    }    /* remember this pbuf */    prev = p;    /* proceed to next pbuf in original chain */    p = p->next;  } while (p);  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: end of chain reached.\n"));  return head;}/** * Dechains the first pbuf from its succeeding pbufs in the chain. * * Makes p->tot_len field equal to p->len. * @param p pbuf to dechain * @return remainder of the pbuf chain, or NULL if it was de-allocated. * @note May not be called on a packet queue. */struct pbuf *pbuf_dechain(struct pbuf *p){  struct pbuf *q;  u8_t tail_gone = 1;  /* tail */  q = p->next;  /* pbuf has successor in chain? */  if (q != NULL) {    /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */    LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);    /* enforce invariant if assertion is disabled */    q->tot_len = p->tot_len - p->len;    /* decouple pbuf from remainder */    p->next = NULL;    /* total length of pbuf p is its own length only */    p->tot_len = p->len;    /* q is no longer referenced by p, free it */    LWIP_DEBUGF(PBUF_DEBUG | DBG_STATE, ("pbuf_dechain: unreferencing %p\n", (void *)q));    tail_gone = pbuf_free(q);    if (tail_gone > 0) LWIP_DEBUGF(PBUF_DEBUG | DBG_STATE,      ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));    /* return remaining tail or NULL if deallocated */  }  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);  return (tail_gone > 0? NULL: q);}

⌨️ 快捷键说明

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