From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v2 1/2] ring: introduce rte_ring_get_memsize()
Date: Fri, 9 May 2014 12:14:52 +0200 [thread overview]
Message-ID: <1399630493-26739-2-git-send-email-olivier.matz@6wind.com> (raw)
In-Reply-To: <1399630493-26739-1-git-send-email-olivier.matz@6wind.com>
Add a function that returns the amount of memory occupied by a rte_ring
structure and its object table. This commit prepares the next one that
will allow to allocate a ring dynamically.
Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
lib/librte_ring/rte_ring.c | 30 +++++++++++++++++++++++-------
lib/librte_ring/rte_ring.h | 16 ++++++++++++++++
2 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c
index 0d43a55..156fe49 100644
--- a/lib/librte_ring/rte_ring.c
+++ b/lib/librte_ring/rte_ring.c
@@ -94,6 +94,25 @@ TAILQ_HEAD(rte_ring_list, rte_ring);
/* true if x is a power of 2 */
#define POWEROF2(x) ((((x)-1) & (x)) == 0)
+/* return the size of memory occupied by a ring */
+ssize_t
+rte_ring_get_memsize(unsigned count)
+{
+ ssize_t sz;
+
+ /* count must be a power of 2 */
+ if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
+ RTE_LOG(ERR, RING,
+ "Requested size is invalid, must be power of 2, and "
+ "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
+ return -EINVAL;
+ }
+
+ sz = sizeof(struct rte_ring) + count * sizeof(void *);
+ sz = RTE_ALIGN(sz, CACHE_LINE_SIZE);
+ return sz;
+}
+
/* create the ring */
struct rte_ring *
rte_ring_create(const char *name, unsigned count, int socket_id,
@@ -102,7 +121,7 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
char mz_name[RTE_MEMZONE_NAMESIZE];
struct rte_ring *r;
const struct rte_memzone *mz;
- size_t ring_size;
+ ssize_t ring_size;
int mz_flags = 0;
struct rte_ring_list* ring_list = NULL;
@@ -129,16 +148,13 @@ rte_ring_create(const char *name, unsigned count, int socket_id,
return NULL;
}
- /* count must be a power of 2 */
- if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
- rte_errno = EINVAL;
- RTE_LOG(ERR, RING, "Requested size is invalid, must be power of 2, and "
- "do not exceed the size limit %u\n", RTE_RING_SZ_MASK);
+ ring_size = rte_ring_get_memsize(count);
+ if (ring_size < 0) {
+ rte_errno = ring_size;
return NULL;
}
rte_snprintf(mz_name, sizeof(mz_name), "%s%s", RTE_RING_MZ_PREFIX, name);
- ring_size = count * sizeof(void *) + sizeof(struct rte_ring);
rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
diff --git a/lib/librte_ring/rte_ring.h b/lib/librte_ring/rte_ring.h
index 775ea79..e8493f2 100644
--- a/lib/librte_ring/rte_ring.h
+++ b/lib/librte_ring/rte_ring.h
@@ -199,6 +199,22 @@ struct rte_ring {
#endif
/**
+ * Calculate the memory size needed for a ring
+ *
+ * This function returns the number of bytes needed for a ring, given
+ * the number of elements in it. This value is the sum of the size of
+ * the structure rte_ring and the size of the memory needed by the
+ * objects pointers. The value is aligned to a cache line size.
+ *
+ * @param count
+ * The number of elements in the ring (must be a power of 2).
+ * @return
+ * - The memory size needed for the ring on success.
+ * - -EINVAL if count is not a power of 2.
+ */
+ssize_t rte_ring_get_memsize(unsigned count);
+
+/**
* Create a new ring named *name* in memory.
*
* This function uses ``memzone_reserve()`` to allocate memory. Its size is
--
1.9.2
next prev parent reply other threads:[~2014-05-09 10:15 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-09 10:14 [dpdk-dev] [PATCH v2 0/2] ring: allow to init a rte_ring outside of an rte_memzone Olivier Matz
2014-05-09 10:14 ` Olivier Matz [this message]
2014-05-09 10:14 ` [dpdk-dev] [PATCH v2 2/2] ring: introduce rte_ring_init() Olivier Matz
2014-05-09 12:17 ` [dpdk-dev] [PATCH v2 0/2] ring: allow to init a rte_ring outside of an rte_memzone Ananyev, Konstantin
2014-05-13 14:32 ` 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=1399630493-26739-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).