gnrc_pktbuf: add gnrc_pktbuf_merge()

Co-Authored-By: Martine Lenders <m.lenders@fu-berlin.de>
This commit is contained in:
Tobias Heider 2017-01-26 14:29:39 +01:00 committed by Martine Lenders
parent 455cdcd00c
commit f03247e752
2 changed files with 77 additions and 0 deletions

View File

@ -298,6 +298,56 @@ gnrc_pktsnip_t *gnrc_pktbuf_reverse_snips(gnrc_pktsnip_t *pkt);
*/
gnrc_pktsnip_t *gnrc_pktbuf_duplicate_upto(gnrc_pktsnip_t *pkt, gnrc_nettype_t type);
/**
* @brief Merge pktsnip chain to single pktsnip.
*
* Specifically it calls @ref gnrc_pktbuf_realloc_data() on @p pkt, then copies
* the data of all following packet snips into that reallocated space, and
* removes the packet snip the data was copied from afterwards.
*
* Example:
* Input:
* buffer
* +---------------------------+ +------+
* | size = 8 | data +-------->| |
* | type = NETTYPE_IPV6 |------------+ +------+
* +---------------------------+ . .
* | next . .
* v . .
* +---------------------------+ +------+
* | size = 40 | data +----------->| |
* | type = NETTYPE_UDP |---------+ +------+
* +---------------------------+ . .
* | next . .
* v
* +---------------------------+ +------+
* | size = 14 | data +-------------->| |
* | type = NETTYPE_UNDEF |------+ +------+
* +---------------------------+ . .
*
*
* Output:
* buffer
* +---------------------------+ +------+
* | size = 62 | data +-------->| |
* | type = NETTYPE_IPV6 |------------+ | |
* +---------------------------+ | |
* | |
* +------+
* . .
*
* @warning @p pkt needs to write protected before calling this function.
* @note Packets in receive order need to call
* @ref gnrc_pktbuf_reverse_snips() first to get the data in the
* correct order.
*
* @param[in,out] pkt The snip to merge.
*
* @return 0, on success
* @return ENOMEM, if no space is left in the packet buffer.
*/
int gnrc_pktbuf_merge(gnrc_pktsnip_t *pkt);
#ifdef DEVELHELP
/**
* @brief Prints some statistics about the packet buffer to stdout.

View File

@ -109,5 +109,32 @@ gnrc_pktsnip_t *gnrc_pktbuf_reverse_snips(gnrc_pktsnip_t *pkt)
return reversed;
}
int gnrc_pktbuf_merge(gnrc_pktsnip_t *pkt)
{
size_t offset = pkt->size;
size_t size = gnrc_pkt_len(pkt);
int res = 0;
if (pkt->size == size) {
return res;
}
/* Re-allocate data */
res = gnrc_pktbuf_realloc_data(pkt, size);
if (res != 0) {
return res;
}
/* Copy data to new buffer */
for (gnrc_pktsnip_t *ptr = pkt->next; ptr != NULL; ptr = ptr->next) {
memcpy(((uint8_t *)pkt->data) + offset, ptr->data, ptr->size);
offset += ptr->size;
}
/* Release old pktsnips and data*/
gnrc_pktbuf_release(pkt->next);
pkt->next = NULL;
return res;
}
/** @} */