From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wi0-f177.google.com (mail-wi0-f177.google.com [209.85.212.177]) by dpdk.org (Postfix) with ESMTP id 5076412A1 for ; Wed, 7 May 2014 13:38:51 +0200 (CEST) Received: by mail-wi0-f177.google.com with SMTP id f8so1140470wiw.16 for ; Wed, 07 May 2014 04:38:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vgxo4IA9gFH5VDm86JmvbD6EKn3K7XtawwsqOLpj4OM=; b=OqGBgdICWfipT1w3WSHdaFNjowaBdIeuF9pI7H4WF9r5p/OhJ++rIWTSf+X+iLyOQI QdnpTHeTnAFpmfmuXEEunWQMzqTyKCWbA6By6KZ6Nne5Gr2QFYivuEiNyQMrI0goY/76 7BQjOe2nCLtKCuVGECvpcON4oeVufGgTwp2OaNxX+dOJvsWZwtngHzIOmQTtZToofqt3 bUISuG9VC4WPhXgAMweOwP2c4HwEeY/FQx5oYXpuzEpZlZhyBsvjK5Y/sXCVJ1rqgfhY sUI+bdnOFMEOwCLCbbnQ8rESLq2vSiFjtRYaxrdbI9MSXw9M9c1lvo39CR5HMMUZDDPA xY/g== X-Gm-Message-State: ALoCoQkRCn4hszdkoHbjg7dJLPwBfuSA8Wdg+bjE2r8OC0ai3NHcZ2sci0Slv88K6ierhh4+mTDv X-Received: by 10.194.81.164 with SMTP id b4mr38368646wjy.2.1399462737086; Wed, 07 May 2014 04:38:57 -0700 (PDT) Received: from glumotte.dev.6wind.com (6wind.net2.nerim.net. [213.41.180.237]) by mx.google.com with ESMTPSA id s2sm31080513wia.7.2014.05.07.04.38.55 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 07 May 2014 04:38:56 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Date: Wed, 7 May 2014 13:38:30 +0200 Message-Id: <1399462711-12815-2-git-send-email-olivier.matz@6wind.com> X-Mailer: git-send-email 1.9.2 In-Reply-To: <1399462711-12815-1-git-send-email-olivier.matz@6wind.com> References: <1399462711-12815-1-git-send-email-olivier.matz@6wind.com> Subject: [dpdk-dev] [PATCH 1/2] ring: introduce rte_ring_get_memsize() X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 May 2014 11:38:52 -0000 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 --- lib/librte_ring/rte_ring.c | 29 ++++++++++++++++++++++------- lib/librte_ring/rte_ring.h | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c index 0d43a55..4aa500f 100644 --- a/lib/librte_ring/rte_ring.c +++ b/lib/librte_ring/rte_ring.c @@ -94,6 +94,24 @@ 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 = (sz + CACHE_LINE_MASK) & (~CACHE_LINE_MASK); + return sz; +} + /* create the ring */ struct rte_ring * rte_ring_create(const char *name, unsigned count, int socket_id, @@ -102,7 +120,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 +147,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