From 2d65283c259616a5643599ce32d7a62d174ec49e Mon Sep 17 00:00:00 2001 From: Cyril Chemparathy Date: Thu, 9 Jul 2015 16:25:14 +0800 Subject: [PATCH] mem: refactor memzone reserve functions The definitions of rte_memzone_reserve_aligned() and rte_memzone_reserve_bounded() were identical with the exception of the bound argument passed into rte_memzone_reserve_thread_safe(). This patch removes this replication of code by unifying it into rte_memzone_reserve_thread_safe(), which is then called by all three variants of rte_memzone_reserve(). Signed-off-by: Cyril Chemparathy Signed-off-by: Zhigang Lu --- lib/librte_eal/common/eal_common_memzone.c | 77 ++++++++++------------ 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/lib/librte_eal/common/eal_common_memzone.c b/lib/librte_eal/common/eal_common_memzone.c index aee184ac7e..1ea502b618 100644 --- a/lib/librte_eal/common/eal_common_memzone.c +++ b/lib/librte_eal/common/eal_common_memzone.c @@ -76,18 +76,6 @@ memzone_lookup_thread_unsafe(const char *name) return NULL; } -/* - * Return a pointer to a correctly filled memzone descriptor. If the - * allocation cannot be done, return NULL. - */ -const struct rte_memzone * -rte_memzone_reserve(const char *name, size_t len, int socket_id, - unsigned flags) -{ - return rte_memzone_reserve_aligned(name, - len, socket_id, flags, RTE_CACHE_LINE_SIZE); -} - /* * Helper function for memzone_reserve_aligned_thread_unsafe(). * Calculate address offset from the start of the segment. @@ -307,13 +295,10 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len, return mz; } -/* - * Return a pointer to a correctly filled memzone descriptor (with a - * specified alignment). If the allocation cannot be done, return NULL. - */ -const struct rte_memzone * -rte_memzone_reserve_aligned(const char *name, size_t len, - int socket_id, unsigned flags, unsigned align) +static const struct rte_memzone * +rte_memzone_reserve_thread_safe(const char *name, size_t len, + int socket_id, unsigned flags, unsigned align, + unsigned bound) { struct rte_mem_config *mcfg; const struct rte_memzone *mz = NULL; @@ -331,7 +316,7 @@ rte_memzone_reserve_aligned(const char *name, size_t len, rte_rwlock_write_lock(&mcfg->mlock); mz = memzone_reserve_aligned_thread_unsafe( - name, len, socket_id, flags, align, 0); + name, len, socket_id, flags, align, bound); rte_rwlock_write_unlock(&mcfg->mlock); @@ -340,36 +325,40 @@ rte_memzone_reserve_aligned(const char *name, size_t len, /* * Return a pointer to a correctly filled memzone descriptor (with a - * specified alignment and boundary). - * If the allocation cannot be done, return NULL. + * specified alignment and boundary). If the allocation cannot be done, + * return NULL. */ const struct rte_memzone * -rte_memzone_reserve_bounded(const char *name, size_t len, - int socket_id, unsigned flags, unsigned align, unsigned bound) +rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id, + unsigned flags, unsigned align, unsigned bound) { - struct rte_mem_config *mcfg; - const struct rte_memzone *mz = NULL; - - /* both sizes cannot be explicitly called for */ - if (((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) - || ((flags & RTE_MEMZONE_16MB) && (flags & RTE_MEMZONE_16GB))) { - rte_errno = EINVAL; - return NULL; - } - - /* get pointer to global configuration */ - mcfg = rte_eal_get_configuration()->mem_config; - - rte_rwlock_write_lock(&mcfg->mlock); - - mz = memzone_reserve_aligned_thread_unsafe( - name, len, socket_id, flags, align, bound); - - rte_rwlock_write_unlock(&mcfg->mlock); + return rte_memzone_reserve_thread_safe(name, len, socket_id, flags, + align, bound); +} - return mz; +/* + * Return a pointer to a correctly filled memzone descriptor (with a + * specified alignment). If the allocation cannot be done, return NULL. + */ +const struct rte_memzone * +rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id, + unsigned flags, unsigned align) +{ + return rte_memzone_reserve_thread_safe(name, len, socket_id, flags, + align, 0); } +/* + * Return a pointer to a correctly filled memzone descriptor. If the + * allocation cannot be done, return NULL. + */ +const struct rte_memzone * +rte_memzone_reserve(const char *name, size_t len, int socket_id, + unsigned flags) +{ + return rte_memzone_reserve_thread_safe(name, len, socket_id, + flags, RTE_CACHE_LINE_SIZE, 0); +} /* * Lookup for the memzone identified by the given name -- 2.20.1