From dcb2cb99a1c5bbd9decaab1186f14e5278d316f8 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Tue, 25 Apr 2017 10:07:57 +0200 Subject: [PATCH] Added 'pbuf_alloc_copy' e.g. as a single function for use with non scatter-gather drivers --- src/core/pbuf.c | 27 +++++++++++++++++++++++++++ src/include/lwip/pbuf.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/core/pbuf.c b/src/core/pbuf.c index b75747a7..202d7de7 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -1319,6 +1319,33 @@ pbuf_coalesce(struct pbuf *p, pbuf_layer layer) return q; } +/** + * @ingroup pbuf + * Allocates a new pbuf of same length (via @pbuf_alloc) and copies the source + * pbuf into this new pbuf (using @pbuf_copy). + * + * @param layer pbuf_layer of the new pbuf + * @param type this parameter decides how and where the pbuf should be allocated + * (@see pbuf_alloc) + * @param p the source pbuf + * + * @return a new pbuf or NULL if allocation fails + */ +struct pbuf * +pbuf_alloc_copy(pbuf_layer layer, pbuf_type type, struct pbuf *p) +{ + struct pbuf *q; + err_t err; + q = pbuf_alloc(layer, p->tot_len, type); + if (q == NULL) { + return NULL; + } + err = pbuf_copy(q, p); + LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */ + LWIP_ASSERT("pbuf_copy failed", err == ERR_OK); + return q; +} + #if LWIP_CHECKSUM_ON_COPY /** * Copies data into a single pbuf (*not* into a pbuf queue!) and updates diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h index 667c2383..52a4f1fe 100644 --- a/src/include/lwip/pbuf.h +++ b/src/include/lwip/pbuf.h @@ -262,6 +262,7 @@ err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len); err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset); struct pbuf *pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset); struct pbuf *pbuf_coalesce(struct pbuf *p, pbuf_layer layer); +struct pbuf *pbuf_alloc_copy(pbuf_layer l, pbuf_type type, struct pbuf *p); #if LWIP_CHECKSUM_ON_COPY err_t pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr, u16_t len, u16_t *chksum);