From b2906abb0dd7207c237c81540c1d8e59e151a5bc Mon Sep 17 00:00:00 2001 From: Cyril Chemparathy Date: Mon, 22 Jun 2015 11:34:21 -0700 Subject: [PATCH] mbuf: add macro for offset arithmetic There are a number of instances in the code where rte_pktmbuf_mtod() is used to get the mbuf data pointer, only to add an offset before casting the result to some other header type. This patch adds a new rte_pktmbuf_mtod_offset() macro to eliminate these awful double cast situations. Signed-off-by: Cyril Chemparathy Acked-by: Olivier Matz --- lib/librte_mbuf/rte_mbuf.h | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h index 70da79707e..8a2cae1cbc 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -54,6 +54,7 @@ */ #include +#include #include #include #include @@ -1079,19 +1080,36 @@ static inline struct rte_mbuf *rte_pktmbuf_lastseg(struct rte_mbuf *m) return m2; } +/** + * A macro that points to an offset into the data in the mbuf. + * + * The returned pointer is cast to type t. Before using this + * function, the user must ensure that the first segment is large + * enough to accommodate its data. + * + * @param m + * The packet mbuf. + * @param o + * The offset into the mbuf data. + * @param t + * The type to cast the result into. + */ +#define rte_pktmbuf_mtod_offset(m, t, o) \ + ((t)((char *)(m)->buf_addr + (m)->data_off + (o))) + /** * A macro that points to the start of the data in the mbuf. * * The returned pointer is cast to type t. Before using this - * function, the user must ensure that m_headlen(m) is large enough to - * read its data. + * function, the user must ensure that the first segment is large + * enough to accommodate its data. * * @param m * The packet mbuf. * @param t * The type to cast the result into. */ -#define rte_pktmbuf_mtod(m, t) ((t)((char *)(m)->buf_addr + (m)->data_off)) +#define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0) /** * A macro that returns the length of the packet. -- 2.20.1