📄 pbuf.c
字号:
* * @bug Cannot grow the size of a pbuf (chain) (yet). */voidpbuf_realloc(struct pbuf *p, u16_t new_len){ struct pbuf *q; u16_t rem_len; /* remaining length */ s16_t grow; LWIP_ASSERT("pbuf_realloc: sane p->flags", p->flags == PBUF_FLAG_POOL || p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_RAM || p->flags == PBUF_FLAG_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; /* this pbuf should be kept? */ while (rem_len > q->len) { /* decrease remaining length by pbuf length */ rem_len -= q->len; /* decrease total length indicator */ q->tot_len += grow; /* proceed to next pbuf in chain */ q = q->next; } /* 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->flags == PBUF_FLAG_RAM) && (rem_len != q->len)) { /* reallocate and adjust the length of the pbuf that will be split */ mem_realloc(q, (u8_t *)q->payload - (u8_t *)q + rem_len); } /* 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; pbuf_refresh();}/** * 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 hdr_size 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.) * * 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. */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)) { 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; } } DEBUGF( PBUF_DEBUG, ("pbuf_header: old %p new %p (%d)\n", payload, p->payload, header_size) ); /* modify pbuf length fields */ p->len += header_size; p->tot_len += header_size; return 0;}/** * Free a pbuf (chain) from usage, de-allocate non-used head of 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, until * a non-zero reference count is encountered, or the end of the chain is * reached. * * @param pbuf pbuf (chain) to be freed from one user. * * @return the number of unreferenced pbufs that were de-allocated * from the head of the chain. * * @note the reference counter of a pbuf equals the number of pointers * that refer to the pbuf (or into the pbuf). * * @internal examples: * * 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; unsigned int state; if (p == NULL) { DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_free(p == NULL) was called.\n")); return 0; }// 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_stop_interrupts(&state); /* de-allocate all consecutive pbufs from the head of the chain that * obtain a zero reference count */ while (p != NULL) { /* all pbufs in a chain are referenced at least once */ LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0); p->ref--; /* this pbuf is no longer referenced to? */ if (p->ref == 0) { /* remember next pbuf in chain for next iteration */ q = p->next; /* 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 RAM/ROM referencing pbuf */ } else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) { memp_freep(MEMP_PBUF, p); /* pbuf with data */ } else { mem_free(p); } count++; /* proceed to next pbuf */ p = q; /* p->ref > 0, this pbuf is still referenced to */ /* (so the remaining pbufs in chain as well) */ } else { /* stop walking through chain */ p = NULL; } } sys_allow_interrupts(&state); pbuf_refresh();// 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){ unsigned int state; /* pbuf given? */ if (p != NULL) { sys_stop_interrupts(&state); ++(p->ref); sys_allow_interrupts(&state); }}/** * * Increment the reference count of all pbufs in a chain. * * @param p first pbuf of chain * */voidpbuf_ref_chain(struct pbuf *p){ unsigned int state; sys_stop_interrupts(&state); while (p != NULL) { ++p->ref; p = p->next; } sys_allow_interrupts(&state);}/** * * Link two pbufs (or chains) together. * * @param h head pbuf (chain) * @param t tail pbuf (chain) * * The ->tot_len field of the first pbuf (h) is adjusted. */voidpbuf_chain(struct pbuf *h, struct pbuf *t){ struct pbuf *p; LWIP_ASSERT("h != NULL", h != NULL); LWIP_ASSERT("t != NULL", t != NULL); /* 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 */ /* add total length of second chain to last pbuf total of first chain */ p->tot_len += t->tot_len; /* chain last pbuf of h chain (p) with first of tail (t) */ p->next = t; /* t is now referenced to one more time */ pbuf_ref(t); DEBUGF(PBUF_DEBUG | DBG_FRESH | 2, ("pbuf_chain: referencing tail %p\n", (void *) t));}/** * 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. */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 */ DEBUGF(PBUF_DEBUG | DBG_STATE, ("pbuf_dechain: unreferencing %p\n", (void *) q)); tail_gone = pbuf_free(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);}/** * * Create PBUF_POOL (or PBUF_RAM) copies of PBUF_REF pbufs. * * 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(). * * Used to queue packets on behalf of the lwIP stack, such as * ARP based queueing. * * @param p Head of pbuf chain to process * * @return Pointer to new 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); 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) { 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) DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_take: Could not allocate PBUF_POOL\n")); } else { /* no replacement pbuf yet */ q = NULL; 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) 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 */ DEBUGF(PBUF_DEBUG, ("pbuf_take: replaced PBUF_REF %p with %p\n", (void *)p, (void *)q)); p = q; } else { /* deallocate chain */ pbuf_free(head); DEBUGF(PBUF_DEBUG | 2, ("pbuf_take: failed to allocate replacement pbuf for %p\n", (void *)p)); return NULL; } /* p->flags != PBUF_FLAG_REF */ } else { 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); DEBUGF(PBUF_DEBUG | DBG_TRACE | 1, ("pbuf_take: end of chain reached.\n")); return head;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -