pbuf: added new function pbuf_free_header() to gradually hide bytes and free pbufs from the front of a pbuf chain

This commit is contained in:
goldsimon 2017-03-06 21:53:48 +01:00
parent c295717ce7
commit 49414826af
2 changed files with 31 additions and 0 deletions

View File

@ -683,6 +683,36 @@ pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
return pbuf_header_impl(p, header_size_increment, 1);
}
/** Similar to pbuf_header(-size) but de-refs header pbufs for (size >= p->len)
*
* @param q pbufs to operate on
* @param size The number of bytes to remove from the beginning of the pbuf list.
* While size >= p->len, pbufs are freed.
* ATTENTION: this is the opposite direction as @ref pbuf_header, but
* takes an u16_t not s16_t!
* @return the new head pbuf
*/
struct pbuf*
pbuf_free_header(struct pbuf *q, u16_t size)
{
struct pbuf *p = q;
u16_t free_left = size;
while (free_left && p) {
s16_t free_len = (free_left > INT16_MAX ? INT16_MAX : (s16_t)free_left);
if (free_len >= p->len) {
struct pbuf *f = p;
free_left -= p->len;
p = p->next;
f->next = 0;
pbuf_free(f);
} else {
pbuf_header(p, -free_len);
free_left -= free_len;
}
}
return p;
}
/**
* @ingroup pbuf
* Dereference a pbuf chain or queue and deallocate any no-longer-used

View File

@ -248,6 +248,7 @@ struct pbuf *pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type,
void pbuf_realloc(struct pbuf *p, u16_t size);
u8_t pbuf_header(struct pbuf *p, s16_t header_size);
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size);
struct pbuf *pbuf_free_header(struct pbuf *q, u16_t size);
void pbuf_ref(struct pbuf *p);
u8_t pbuf_free(struct pbuf *p);
u16_t pbuf_clen(const struct pbuf *p);