tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_ring / rte_ring.c
index 3a919b0..92ecf04 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -75,8 +75,8 @@
 #include <rte_log.h>
 #include <rte_memory.h>
 #include <rte_memzone.h>
+#include <rte_malloc.h>
 #include <rte_launch.h>
-#include <rte_tailq.h>
 #include <rte_eal.h>
 #include <rte_eal_memconfig.h>
 #include <rte_atomic.h>
@@ -89,7 +89,7 @@
 
 #include "rte_ring.h"
 
-TAILQ_HEAD(rte_ring_list, rte_ring);
+TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
 
 /* true if x is a power of 2 */
 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
@@ -109,7 +109,7 @@ rte_ring_get_memsize(unsigned count)
        }
 
        sz = sizeof(struct rte_ring) + count * sizeof(void *);
-       sz = RTE_ALIGN(sz, CACHE_LINE_SIZE);
+       sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
        return sz;
 }
 
@@ -119,23 +119,23 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
 {
        /* compilation-time checks */
        RTE_BUILD_BUG_ON((sizeof(struct rte_ring) &
-                         CACHE_LINE_MASK) != 0);
+                         RTE_CACHE_LINE_MASK) != 0);
 #ifdef RTE_RING_SPLIT_PROD_CONS
        RTE_BUILD_BUG_ON((offsetof(struct rte_ring, cons) &
-                         CACHE_LINE_MASK) != 0);
+                         RTE_CACHE_LINE_MASK) != 0);
 #endif
        RTE_BUILD_BUG_ON((offsetof(struct rte_ring, prod) &
-                         CACHE_LINE_MASK) != 0);
+                         RTE_CACHE_LINE_MASK) != 0);
 #ifdef RTE_LIBRTE_RING_DEBUG
        RTE_BUILD_BUG_ON((sizeof(struct rte_ring_debug_stats) &
-                         CACHE_LINE_MASK) != 0);
+                         RTE_CACHE_LINE_MASK) != 0);
        RTE_BUILD_BUG_ON((offsetof(struct rte_ring, stats) &
-                         CACHE_LINE_MASK) != 0);
+                         RTE_CACHE_LINE_MASK) != 0);
 #endif
 
        /* init the ring structure */
        memset(r, 0, sizeof(*r));
-       rte_snprintf(r->name, sizeof(r->name), "%s", name);
+       snprintf(r->name, sizeof(r->name), "%s", name);
        r->flags = flags;
        r->prod.watermark = count;
        r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ);
@@ -155,6 +155,7 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
 {
        char mz_name[RTE_MEMZONE_NAMESIZE];
        struct rte_ring *r;
+       struct rte_tailq_entry *te;
        const struct rte_memzone *mz;
        ssize_t ring_size;
        int mz_flags = 0;
@@ -173,7 +174,14 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
                return NULL;
        }
 
-       rte_snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, name);
+       te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
+       if (te == NULL) {
+               RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
+               rte_errno = ENOMEM;
+               return NULL;
+       }
+
+       snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, name);
 
        rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 
@@ -186,10 +194,14 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
                /* no need to check return value here, we already checked the
                 * arguments above */
                rte_ring_init(r, name, count, flags);
-               TAILQ_INSERT_TAIL(ring_list, r, next);
+
+               te->data = (void *) r;
+
+               TAILQ_INSERT_TAIL(ring_list, te, next);
        } else {
                r = NULL;
                RTE_LOG(ERR, RING, "Cannot reserve memory\n");
+               rte_free(te);
        }
        rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
@@ -272,20 +284,20 @@ rte_ring_dump(FILE *f, const struct rte_ring *r)
 void
 rte_ring_list_dump(FILE *f)
 {
-       const struct rte_ring *mp;
+       const struct rte_tailq_entry *te;
        struct rte_ring_list *ring_list;
 
        /* check that we have an initialised tail queue */
-       if ((ring_list = 
+       if ((ring_list =
             RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
                rte_errno = E_RTE_NO_TAILQ;
-               return; 
+               return;
        }
 
        rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
 
-       TAILQ_FOREACH(mp, ring_list, next) {
-               rte_ring_dump(f, mp);
+       TAILQ_FOREACH(te, ring_list, next) {
+               rte_ring_dump(f, (struct rte_ring *) te->data);
        }
 
        rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
@@ -295,27 +307,31 @@ rte_ring_list_dump(FILE *f)
 struct rte_ring *
 rte_ring_lookup(const char *name)
 {
-       struct rte_ring *r;
+       struct rte_tailq_entry *te;
+       struct rte_ring *r = NULL;
        struct rte_ring_list *ring_list;
 
        /* check that we have an initialized tail queue */
-       if ((ring_list = 
+       if ((ring_list =
             RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
                rte_errno = E_RTE_NO_TAILQ;
-               return NULL;    
+               return NULL;
        }
 
        rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
-       
-       TAILQ_FOREACH(r, ring_list, next) {
+
+       TAILQ_FOREACH(te, ring_list, next) {
+               r = (struct rte_ring *) te->data;
                if (strncmp(name, r->name, RTE_RING_NAMESIZE) == 0)
                        break;
        }
 
        rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
 
-       if (r == NULL)
+       if (te == NULL) {
                rte_errno = ENOENT;
+               return NULL;
+       }
 
        return r;
 }