On 09/20/15 07:04, rujiacs wrote:
Signed-off-by: rujiacs <ruji...@163.com>
---
  helper/include/odp/helper/ring.h | 16 ++++++++--
  helper/ring.c                    | 63 ++++++++++++++++++++++++++--------------
  2 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/helper/include/odp/helper/ring.h b/helper/include/odp/helper/ring.h
index 65c32ad..62c1e5b 100644
--- a/helper/include/odp/helper/ring.h
+++ b/helper/include/odp/helper/ring.h
@@ -103,6 +103,8 @@ extern "C" {
  #include <errno.h>
  #include <sys/queue.h>
+#include <odp/shared_memory.h>
+
  enum odph_ring_queue_behavior {
        ODPH_RING_QUEUE_FIXED = 0, /**< Enq/Deq a fixed number
                                of items from a ring */
@@ -132,6 +134,8 @@ typedef struct odph_ring {
        /** @private Flags supplied at creation. */
        int flags;
+ odp_shm_t shm;
+
        /** @private Producer */
        struct prod {
                uint32_t watermark;      /* Maximum items */
@@ -187,6 +191,10 @@ typedef struct odph_ring {
   *   On success, the pointer to the new allocated ring. NULL on error with
   *    odp_errno set appropriately. Possible errno values include:
   *    - EINVAL - count provided is not a power of 2
+ * @return
+ *   On success, the pointer to the new allocated ring. NULL on error with
+ *    odp_errno set appropriately. Possible errno values include:
+ *    - EINVAL - count provided is not a power of 2
   *    - ENOSPC - the maximum number of memzones has already been allocated
   *    - EEXIST - a memzone with the same name already exists
   *    - ENOMEM - no appropriate memory area found in which to create memzone
@@ -194,6 +202,7 @@ typedef struct odph_ring {
  odph_ring_t *odph_ring_create(const char *name, unsigned count,
                            unsigned flags);
+void odph_ring_free(odph_ring_t *r); /**
   * Change the high water mark.
@@ -371,6 +380,8 @@ int odph_ring_mp_enqueue_bulk(odph_ring_t *r, void * const 
*obj_table,
  int odph_ring_sp_enqueue_bulk(odph_ring_t *r, void * const *obj_table,
                              unsigned n);
+int odph_ring_sp_enqueue(odph_ring_t *r, void *obj);
+
  /**
   * Dequeue several objects from a ring (multi-consumers safe).
   *
@@ -407,6 +418,8 @@ int odph_ring_mc_dequeue_bulk(odph_ring_t *r, void 
**obj_table, unsigned n);
   */
  int odph_ring_sc_dequeue_bulk(odph_ring_t *r, void **obj_table, unsigned n);
+int odph_ring_sc_dequeue(odph_ring_t *r, void *obj);
+
  /**
   * Test if a ring is full.
   *
@@ -520,9 +533,6 @@ int odph_ring_enqueue_burst(odph_ring_t *r, void * const 
*obj_table,
   * @param obj_table
   *   A pointer to a table of void * pointers (objects) that will be filled.
   * @param n
- *   The number of objects to dequeue from the ring to the obj_table.
- * @return
- *   - n: Actual number of objects dequeued, 0 if ring is empty
   */
  int odph_ring_mc_dequeue_burst(odph_ring_t *r, void **obj_table, unsigned n);
diff --git a/helper/ring.c b/helper/ring.c
index 3122173..5120a56 100644
--- a/helper/ring.c
+++ b/helper/ring.c
@@ -173,34 +173,45 @@ odph_ring_create(const char *name, unsigned count, 
unsigned flags)
        odp_rwlock_write_lock(&qlock);
        /* reserve a memory zone for this ring.*/
        shm = odp_shm_reserve(ring_name, ring_size, ODP_CACHE_LINE_SIZE, 0);
-
+    if (shm == ODP_SHM_INVALID) {
+        ODPH_ERR("shm allocation failed for ring %s\n", ring_name);

ODPH_ERR just prints error message. Return is missed here.
+    }
        r = odp_shm_addr(shm);
- if (r != NULL) {
-               /* init the ring structure */
-               snprintf(r->name, sizeof(r->name), "%s", name);
-               r->flags = flags;
-               r->prod.watermark = count;
-               r->prod.sp_enqueue = !!(flags & ODPH_RING_F_SP_ENQ);
-               r->cons.sc_dequeue = !!(flags & ODPH_RING_F_SC_DEQ);
-               r->prod.size = count;
-               r->cons.size = count;
-               r->prod.mask = count-1;
-               r->cons.mask = count-1;
-               r->prod.head = 0;
-               r->cons.head = 0;
-               r->prod.tail = 0;
-               r->cons.tail = 0;
-
-               TAILQ_INSERT_TAIL(&odp_ring_list, r, next);
-       } else {
-               ODPH_ERR("Cannot reserve memory\n");
-       }
+       /* init the ring structure */
+       snprintf(r->name, sizeof(r->name), "%s", name);
+       r->flags = flags;
+    r->shm = shm;
+       r->prod.watermark = count;
+       r->prod.sp_enqueue = !!(flags & ODPH_RING_F_SP_ENQ);
+       r->cons.sc_dequeue = !!(flags & ODPH_RING_F_SC_DEQ);
+       r->prod.size = count;
+       r->cons.size = count;
+       r->prod.mask = count-1;
+       r->cons.mask = count-1;
+       r->prod.head = 0;
+       r->cons.head = 0;
+       r->prod.tail = 0;
+       r->cons.tail = 0;
+       TAILQ_INSERT_TAIL(&odp_ring_list, r, next);
odp_rwlock_write_unlock(&qlock);
        return r;
  }
+/* free a ring */
+void
+odph_ring_free(odph_ring_t *r)
+{
+    odp_rwlock_write_lock(&qlock);
+    TAILQ_REMOVE(&odp_ring_list, r, next);
+    odp_rwlock_write_unlock(&qlock);
+
+    if (r->shm != ODP_SHM_INVALID) {
+        odp_shm_free(r->shm);
+    }
+}
+
  /*
   * change the high water mark. If *count* is 0, water marking is
   * disabled
@@ -471,6 +482,11 @@ int odph_ring_sp_enqueue_bulk(odph_ring_t *r, void * const 
*obj_table,
                                         ODPH_RING_QUEUE_FIXED);
  }
+int odph_ring_sp_enqueue(odph_ring_t *r, void *obj)
+{
+    return odph_ring_sp_enqueue_bulk(r, &obj, 1);
+}
+
  /**
   * Dequeue several objects from a ring (multi-consumers safe).
   */
@@ -489,6 +505,11 @@ int odph_ring_sc_dequeue_bulk(odph_ring_t *r, void 
**obj_table, unsigned n)
                                         ODPH_RING_QUEUE_FIXED);
  }
+int odph_ring_sc_dequeue(odph_ring_t *r, void *obj)
+{
+    return odph_ring_sc_dequeue_bulk(r, &obj, 1);
+}
+
  /**
   * Test if a ring is full.
   */

_______________________________________________
lng-odp mailing list
lng-odp@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to