From 16e10b46cf85f23a333f742831409333e45866a0 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 13 Jul 2015 14:56:25 +0200 Subject: [PATCH] ng_pktbuf: handle size == pkt->size case for ng_pktbuf_add() --- sys/net/crosslayer/ng_pktbuf/ng_pktbuf.c | 36 +++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/sys/net/crosslayer/ng_pktbuf/ng_pktbuf.c b/sys/net/crosslayer/ng_pktbuf/ng_pktbuf.c index 2e55541dc9..a67f954f75 100644 --- a/sys/net/crosslayer/ng_pktbuf/ng_pktbuf.c +++ b/sys/net/crosslayer/ng_pktbuf/ng_pktbuf.c @@ -199,17 +199,17 @@ static ng_pktsnip_t *_pktbuf_add_unsafe(ng_pktsnip_t *pkt, void *data, { ng_pktsnip_t *new_pktsnip; - new_pktsnip = (ng_pktsnip_t *)_pktbuf_internal_alloc(sizeof(ng_pktsnip_t)); - DEBUG("pktbuf: allocated (new_pktsnip = %p) ", (void *)new_pktsnip); - - if (new_pktsnip == NULL) { - DEBUG("=> failed\n"); - return NULL; - } - - DEBUG("of size %u\n", (unsigned)sizeof(ng_pktsnip_t)); - if (pkt == NULL || pkt->data != data) { + new_pktsnip = (ng_pktsnip_t *)_pktbuf_internal_alloc(sizeof(ng_pktsnip_t)); + DEBUG("pktbuf: allocated (new_pktsnip = %p) ", (void *)new_pktsnip); + + if (new_pktsnip == NULL) { + DEBUG("=> failed\n"); + return NULL; + } + + DEBUG("of size %u\n", (unsigned)sizeof(ng_pktsnip_t)); + if ((size != 0) && (!_pktbuf_internal_contains(data))) { new_pktsnip->data = _pktbuf_internal_alloc(size); DEBUG("pktbuf: allocated (new_pktsnip->data = %p) ", new_pktsnip->data); @@ -258,6 +258,22 @@ static ng_pktsnip_t *_pktbuf_add_unsafe(ng_pktsnip_t *pkt, void *data, return NULL; } + if (size == pkt->size) { + DEBUG("pktbuf: size (%u) == pkt->size (%u) => just set new packet " + "type and return\n", (unsigned)size, (unsigned)pkt->size); + pkt->type = type; + return pkt; + } + + new_pktsnip = (ng_pktsnip_t *)_pktbuf_internal_alloc(sizeof(ng_pktsnip_t)); + DEBUG("pktbuf: allocated (new_pktsnip = %p) ", (void *)new_pktsnip); + + if (new_pktsnip == NULL) { + DEBUG("=> failed\n"); + return NULL; + } + + DEBUG("of size %u\n", (unsigned)sizeof(ng_pktsnip_t)); DEBUG("pktbuf: Adding chunk to %p ", pkt->data); if (!_pktbuf_internal_add_pkt(pkt->data)) {