mempool: use the list to audit all elements
[dpdk.git] / lib / librte_mempool / rte_mempool.h
index a6b82cf..bdb217b 100644 (file)
@@ -2,6 +2,7 @@
  *   BSD LICENSE
  *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2016 6WIND S.A.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
@@ -150,17 +151,24 @@ struct rte_mempool_objsz {
  * 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.
+ * it allows to retrieve the mempool pointer from the object and to
+ * iterate on all objects attached to a mempool. When debug is enabled,
+ * a cookie is also added in this structure preventing corruptions and
+ * double-frees.
  */
 struct rte_mempool_objhdr {
+       STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
        struct rte_mempool *mp;          /**< The mempool owning the object. */
 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
        uint64_t cookie;                 /**< Debug cookie. */
 #endif
 };
 
+/**
+ * A list of object headers type
+ */
+STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
+
 /**
  * Mempool object trailer structure
  *
@@ -194,6 +202,8 @@ struct rte_mempool {
 
        struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
 
+       struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
+
 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
        /** Per-lcore statistics. */
        struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
@@ -323,62 +333,13 @@ void rte_mempool_check_cookies(const struct rte_mempool *mp,
 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
 
 /**
- * A mempool object iterator callback function.
- */
-typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
-       void * /*obj_start*/,
-       void * /*obj_end*/,
-       uint32_t /*obj_index */);
-
-/**
- * Call a function for each mempool object in a memory chunk
- *
- * Iterate across objects of the given size and alignment in the
- * provided chunk of memory. The given memory buffer can consist of
- * disjointed physical pages.
+ * An object callback function for mempool.
  *
- * For each object, call the provided callback (if any). This function
- * is used to populate a mempool, or walk through all the elements of a
- * mempool, or estimate how many elements of the given size could be
- * created in the given memory buffer.
- *
- * @param vaddr
- *   Virtual address of the memory buffer.
- * @param elt_num
- *   Maximum number of objects to iterate through.
- * @param elt_sz
- *   Size of each object.
- * @param align
- *   Alignment of each object.
- * @param paddr
- *   Array of physical addresses of the pages that comprises given memory
- *   buffer.
- * @param pg_num
- *   Number of elements in the paddr array.
- * @param pg_shift
- *   LOG2 of the physical pages size.
- * @param obj_iter
- *   Object iterator callback function (could be NULL).
- * @param obj_iter_arg
- *   User defined parameter for the object iterator callback function.
- *
- * @return
- *   Number of objects iterated through.
- */
-uint32_t rte_mempool_obj_iter(void *vaddr,
-       uint32_t elt_num, size_t elt_sz, size_t align,
-       const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
-       rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
-
-/**
- * An object constructor callback function for mempool.
- *
- * Arguments are the mempool, the opaque pointer given by the user in
- * rte_mempool_create(), the pointer to the element and the index of
- * the element in the pool.
+ * Used by rte_mempool_create() and rte_mempool_obj_iter().
  */
-typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
-                                     void *, unsigned);
+typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
+               void *opaque, void *obj, unsigned obj_idx);
+typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
 
 /**
  * A mempool constructor callback function.
@@ -473,7 +434,7 @@ struct rte_mempool *
 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
                   unsigned cache_size, unsigned private_data_size,
                   rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-                  rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+                  rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
                   int socket_id, unsigned flags);
 
 /**
@@ -539,7 +500,7 @@ struct rte_mempool *
 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
                unsigned cache_size, unsigned private_data_size,
                rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-               rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+               rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
                int socket_id, unsigned flags, void *vaddr,
                const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
 
@@ -628,10 +589,28 @@ struct rte_mempool *
 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
                unsigned cache_size, unsigned private_data_size,
                rte_mempool_ctor_t *mp_init, void *mp_init_arg,
-               rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
+               rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
                int socket_id, unsigned flags);
 
 
+/**
+ * Call a function for each mempool element
+ *
+ * Iterate across all objects attached to a rte_mempool and call the
+ * callback function on it.
+ *
+ * @param mp
+ *   A pointer to an initialized mempool.
+ * @param obj_cb
+ *   A function pointer that is called for each object.
+ * @param obj_cb_arg
+ *   An opaque pointer passed to the callback function.
+ * @return
+ *   Number of objects iterated.
+ */
+uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
+       rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
+
 /**
  * Dump the status of the mempool to the console.
  *
@@ -640,7 +619,7 @@ rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
  * @param mp
  *   A pointer to the mempool structure.
  */
-void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
+void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
 
 /**
  * @internal Put several objects back in the mempool; used internally.
@@ -1178,7 +1157,7 @@ rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
  * @param mp
  *   A pointer to the mempool structure.
  */
-void rte_mempool_audit(const struct rte_mempool *mp);
+void rte_mempool_audit(struct rte_mempool *mp);
 
 /**
  * Return a pointer to the private data in an mempool structure.
@@ -1299,7 +1278,7 @@ ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
  * @param arg
  *   Argument passed to iterator
  */
-void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
+void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
                      void *arg);
 
 #ifdef __cplusplus