bitrate: use common macro RTE_DIM
[dpdk.git] / lib / librte_ring / rte_ring.c
index 036467f..d9b3080 100644 (file)
@@ -1,67 +1,11 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2015 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
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * 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
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * Derived from FreeBSD's bufring.c
- *
- **************************************************************************
+/* SPDX-License-Identifier: BSD-3-Clause
  *
+ * Copyright (c) 2010-2015 Intel Corporation
  * Copyright (c) 2007,2008 Kip Macy kmacy@freebsd.org
  * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- *
- * 2. The name of Kip Macy nor the names of other
- *    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 A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- ***************************************************************************/
+ * Derived from FreeBSD's bufring.h
+ * Used as BSD-3 Licensed with permission from Kip Macy.
+ */
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -86,6 +30,7 @@
 #include <rte_errno.h>
 #include <rte_string_fns.h>
 #include <rte_spinlock.h>
+#include <rte_tailq.h>
 
 #include "rte_ring.h"
 
@@ -118,6 +63,13 @@ rte_ring_get_memsize(unsigned count)
        return sz;
 }
 
+void
+rte_ring_reset(struct rte_ring *r)
+{
+       r->prod.head = r->cons.head = 0;
+       r->prod.tail = r->cons.tail = 0;
+}
+
 int
 rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
        unsigned flags)
@@ -134,7 +86,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
 
        /* init the ring structure */
        memset(r, 0, sizeof(*r));
-       ret = snprintf(r->name, sizeof(r->name), "%s", name);
+       ret = strlcpy(r->name, name, sizeof(r->name));
        if (ret < 0 || ret >= (int)sizeof(r->name))
                return -ENAMETOOLONG;
        r->flags = flags;
@@ -203,7 +155,7 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
                return NULL;
        }
 
-       rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_write_lock();
 
        /* reserve a memory zone for this ring. If we can't get rte_config or
         * we are secondary process, the memzone_reserve function will set
@@ -225,7 +177,7 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
                RTE_LOG(ERR, RING, "Cannot reserve memory\n");
                rte_free(te);
        }
-       rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_write_unlock();
 
        return r;
 }
@@ -245,7 +197,8 @@ rte_ring_free(struct rte_ring *r)
         * therefore, there is no memzone to free.
         */
        if (r->memzone == NULL) {
-               RTE_LOG(ERR, RING, "Cannot free ring (not created with rte_ring_create()");
+               RTE_LOG(ERR, RING,
+                       "Cannot free ring, not created with rte_ring_create()\n");
                return;
        }
 
@@ -255,7 +208,7 @@ rte_ring_free(struct rte_ring *r)
        }
 
        ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
-       rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_write_lock();
 
        /* find out tailq entry */
        TAILQ_FOREACH(te, ring_list, next) {
@@ -264,13 +217,13 @@ rte_ring_free(struct rte_ring *r)
        }
 
        if (te == NULL) {
-               rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+               rte_mcfg_tailq_write_unlock();
                return;
        }
 
        TAILQ_REMOVE(ring_list, te, next);
 
-       rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_write_unlock();
 
        rte_free(te);
 }
@@ -300,13 +253,13 @@ rte_ring_list_dump(FILE *f)
 
        ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
 
-       rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_read_lock();
 
        TAILQ_FOREACH(te, ring_list, next) {
                rte_ring_dump(f, (struct rte_ring *) te->data);
        }
 
-       rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_read_unlock();
 }
 
 /* search a ring from its name */
@@ -319,7 +272,7 @@ rte_ring_lookup(const char *name)
 
        ring_list = RTE_TAILQ_CAST(rte_ring_tailq.head, rte_ring_list);
 
-       rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_read_lock();
 
        TAILQ_FOREACH(te, ring_list, next) {
                r = (struct rte_ring *) te->data;
@@ -327,7 +280,7 @@ rte_ring_lookup(const char *name)
                        break;
        }
 
-       rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
+       rte_mcfg_tailq_read_unlock();
 
        if (te == NULL) {
                rte_errno = ENOENT;