All of lore.kernel.org
 help / color / mirror / Atom feed
From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org
Subject: [PATCH v2 1/3] mempool: introduce objhdr structure for object headers
Date: Fri, 19 Jun 2015 18:16:37 +0200	[thread overview]
Message-ID: <1434730599-24339-2-git-send-email-olivier.matz@6wind.com> (raw)
In-Reply-To: <1434730599-24339-1-git-send-email-olivier.matz@6wind.com>

Each object stored in mempools are prefixed by a header, allowing for
instance to retrieve the mempool pointer from the object. When debug is
enabled, a cookie is also added in this header that helps to detect
corruptions and double-frees.

Introduce a structure that materializes the content of this header,
and will simplify future patches adding things in this header.

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
 lib/librte_mempool/rte_mempool.c |  8 ++---
 lib/librte_mempool/rte_mempool.h | 76 ++++++++++++++++------------------------
 2 files changed, 35 insertions(+), 49 deletions(-)

diff --git a/lib/librte_mempool/rte_mempool.c b/lib/librte_mempool/rte_mempool.c
index 190cfd9..b2d8700 100644
--- a/lib/librte_mempool/rte_mempool.c
+++ b/lib/librte_mempool/rte_mempool.c
@@ -130,16 +130,16 @@ static void
 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 **mpp;
+	struct rte_mempool_objhdr *hdr;
 
 	obj = (char *)obj + mp->header_size;
 
 	/* set mempool ptr in header */
-	mpp = __mempool_from_obj(obj);
-	*mpp = mp;
+	hdr = (struct rte_mempool_objhdr *)((char *)obj - sizeof(*hdr));
+	hdr->mp = mp;
 
 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	__mempool_write_header_cookie(obj, 1);
+	hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
 	__mempool_write_trailer_cookie(obj);
 #endif
 	/* call the initializer */
diff --git a/lib/librte_mempool/rte_mempool.h b/lib/librte_mempool/rte_mempool.h
index bc2bae0..b047810 100644
--- a/lib/librte_mempool/rte_mempool.h
+++ b/lib/librte_mempool/rte_mempool.h
@@ -140,6 +140,21 @@ struct rte_mempool_objsz {
 #define	MEMPOOL_PG_NUM_DEFAULT	1
 
 /**
+ * Mempool object header structure
+ *
+ * Each object stored in mempools are prefixed by this header structure,
+ * it allows to retrieve the mempool pointer from the object. When debug
+ * is enabled, a cookie is also added in this structure preventing
+ * corruptions and double-frees.
+ */
+struct rte_mempool_objhdr {
+	struct rte_mempool *mp;          /**< The mempool owning the object. */
+#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
+	uint64_t cookie;                 /**< Debug cookie. */
+#endif
+};
+
+/**
  * The RTE mempool structure.
  */
 struct rte_mempool {
@@ -227,24 +242,11 @@ struct rte_mempool {
 	((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
 	(mp)->phys_addr == (mp)->elt_pa[0])
 
-/**
- * @internal Get a pointer to a mempool pointer in the object header.
- * @param obj
- *   Pointer to object.
- * @return
- *   The pointer to the mempool from which the object was allocated.
- */
-static inline struct rte_mempool **__mempool_from_obj(void *obj)
+/* return the header of a mempool object (internal) */
+static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
 {
-	struct rte_mempool **mpp;
-	unsigned off;
-
-	off = sizeof(struct rte_mempool *);
-#ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-	off += sizeof(uint64_t);
-#endif
-	mpp = (struct rte_mempool **)((char *)obj - off);
-	return mpp;
+	return (struct rte_mempool_objhdr *)((char *)obj -
+		sizeof(struct rte_mempool_objhdr));
 }
 
 /**
@@ -256,36 +258,18 @@ static inline struct rte_mempool **__mempool_from_obj(void *obj)
  * @return
  *   A pointer to the mempool structure.
  */
-static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
+static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 {
-	struct rte_mempool * const *mpp;
-	mpp = __mempool_from_obj(obj);
-	return *mpp;
+	struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
+	return hdr->mp;
 }
 
 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
-/* get header cookie value */
-static inline uint64_t __mempool_read_header_cookie(const void *obj)
-{
-	return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
-}
-
 /* get trailer cookie value */
 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
 {
-	struct rte_mempool **mpp = __mempool_from_obj(obj);
-	return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
-}
-
-/* write header cookie value */
-static inline void __mempool_write_header_cookie(void *obj, int free)
-{
-	uint64_t *cookie_p;
-	cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
-	if (free == 0)
-		*cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
-	else
-		*cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
+	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	return *(uint64_t *)((char *)obj + mp->elt_size);
 
 }
 
@@ -293,8 +277,8 @@ static inline void __mempool_write_header_cookie(void *obj, int free)
 static inline void __mempool_write_trailer_cookie(void *obj)
 {
 	uint64_t *cookie_p;
-	struct rte_mempool **mpp = __mempool_from_obj(obj);
-	cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
+	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 */
@@ -321,6 +305,7 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp,
 					   void * const *obj_table_const,
 					   unsigned n, int free)
 {
+	struct rte_mempool_objhdr *hdr;
 	uint64_t cookie;
 	void *tmp;
 	void *obj;
@@ -338,7 +323,8 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp,
 			rte_panic("MEMPOOL: object is owned by another "
 				  "mempool\n");
 
-		cookie = __mempool_read_header_cookie(obj);
+		hdr = __mempool_get_header(obj);
+		cookie = hdr->cookie;
 
 		if (free == 0) {
 			if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
@@ -348,7 +334,7 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp,
 					obj, (const void *) mp, cookie);
 				rte_panic("MEMPOOL: bad header cookie (put)\n");
 			}
-			__mempool_write_header_cookie(obj, 1);
+			hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
 		}
 		else if (free == 1) {
 			if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
@@ -358,7 +344,7 @@ static inline void __mempool_check_cookies(const struct rte_mempool *mp,
 					obj, (const void *) mp, cookie);
 				rte_panic("MEMPOOL: bad header cookie (get)\n");
 			}
-			__mempool_write_header_cookie(obj, 0);
+			hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
 		}
 		else if (free == 2) {
 			if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
-- 
2.1.4

  reply	other threads:[~2015-06-19 16:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-01  9:15 [PATCH 0/3] mempool: clean-up Olivier Matz
2015-06-01  9:15 ` [PATCH 1/3] mempool: introduce objhdr structure for object headers Olivier Matz
2015-06-01  9:15 ` [PATCH 2/3] mempool: introduce objtlr structure for object trailers Olivier Matz
2015-06-01  9:15 ` [PATCH 3/3] mempool: fix typos, indentation, and doxygen style Olivier Matz
2015-06-19 16:16 ` [PATCH v2 0/3] mempool: clean-up Olivier Matz
2015-06-19 16:16   ` Olivier Matz [this message]
2015-06-19 16:16   ` [PATCH v2 2/3] mempool: introduce objtlr structure for object trailers Olivier Matz
2015-06-19 16:16   ` [PATCH v2 3/3] mempool: fix typos, indentation, and doxygen style Olivier Matz
2015-06-19 21:40   ` [PATCH v2 0/3] mempool: clean-up Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1434730599-24339-2-git-send-email-olivier.matz@6wind.com \
    --to=olivier.matz@6wind.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.