From: Olivier Matz Date: Fri, 19 Jun 2015 16:16:38 +0000 (+0200) Subject: mempool: add structure for object trailers X-Git-Tag: spdx-start~9035 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=97e7e685bfcd;p=dpdk.git mempool: add structure for object trailers Each object stored in mempools are suffixed by a trailer, storing a cookie in debug mode which help to detect memory corruptions. Like for headers, introduce a structure that materializes the content of this trailer. Signed-off-by: Olivier Matz --- diff --git a/lib/librte_mempool/rte_mempool.c b/lib/librte_mempool/rte_mempool.c index b2d8700cf2..60369cf9d1 100644 --- a/lib/librte_mempool/rte_mempool.c +++ b/lib/librte_mempool/rte_mempool.c @@ -131,6 +131,7 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx, rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg) { struct rte_mempool_objhdr *hdr; + struct rte_mempool_objtlr *tlr __rte_unused; obj = (char *)obj + mp->header_size; @@ -140,7 +141,8 @@ mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx, #ifdef RTE_LIBRTE_MEMPOOL_DEBUG hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2; - __mempool_write_trailer_cookie(obj); + tlr = __mempool_get_trailer(obj); + tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE; #endif /* call the initializer */ if (obj_init) diff --git a/lib/librte_mempool/rte_mempool.h b/lib/librte_mempool/rte_mempool.h index b047810b8b..ec1bb2e23c 100644 --- a/lib/librte_mempool/rte_mempool.h +++ b/lib/librte_mempool/rte_mempool.h @@ -154,6 +154,18 @@ struct rte_mempool_objhdr { #endif }; +/** + * Mempool object trailer structure + * + * In debug mode, each object stored in mempools are suffixed by this + * trailer structure containing a cookie preventing memory corruptions. + */ +struct rte_mempool_objtlr { +#ifdef RTE_LIBRTE_MEMPOOL_DEBUG + uint64_t cookie; /**< Debug cookie. */ +#endif +}; + /** * The RTE mempool structure. */ @@ -249,6 +261,13 @@ static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj) sizeof(struct rte_mempool_objhdr)); } +/* return the trailer of a mempool object (internal) */ +static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj) +{ + return (struct rte_mempool_objtlr *)((char *)obj - + sizeof(struct rte_mempool_objtlr)); +} + /** * Return a pointer to the mempool owning this object. * @@ -264,25 +283,6 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj) return hdr->mp; } -#ifdef RTE_LIBRTE_MEMPOOL_DEBUG -/* get trailer cookie value */ -static inline uint64_t __mempool_read_trailer_cookie(void *obj) -{ - struct rte_mempool *mp = rte_mempool_from_obj(obj); - return *(uint64_t *)((char *)obj + mp->elt_size); - -} - -/* write trailer cookie value */ -static inline void __mempool_write_trailer_cookie(void *obj) -{ - uint64_t *cookie_p; - struct rte_mempool *mp = rte_mempool_from_obj(obj); - cookie_p = (uint64_t *)((char *)obj + mp->elt_size); - *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE; -} -#endif /* RTE_LIBRTE_MEMPOOL_DEBUG */ - /** * @internal Check and update cookies or panic. * @@ -306,6 +306,7 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp, unsigned n, int free) { struct rte_mempool_objhdr *hdr; + struct rte_mempool_objtlr *tlr; uint64_t cookie; void *tmp; void *obj; @@ -356,7 +357,8 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp, rte_panic("MEMPOOL: bad header cookie (audit)\n"); } } - cookie = __mempool_read_trailer_cookie(obj); + tlr = __mempool_get_trailer(obj); + cookie = tlr->cookie; if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) { rte_log_set_history(0); RTE_LOG(CRIT, MEMPOOL,