mem: refactor memzone reserve functions
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
index f1fc4a7..1ea502b 100644 (file)
@@ -43,7 +43,6 @@
 #include <rte_log.h>
 #include <rte_memory.h>
 #include <rte_memzone.h>
-#include <rte_tailq.h>
 #include <rte_eal.h>
 #include <rte_eal_memconfig.h>
 #include <rte_per_lcore.h>
@@ -77,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, CACHE_LINE_SIZE);
-}
-
 /*
  * Helper function for memzone_reserve_aligned_thread_unsafe().
  * Calculate address offset from the start of the segment.
@@ -121,7 +108,7 @@ align_phys_boundary(const struct rte_memseg *ms, size_t len, size_t align,
                addr_offset = start - ms->phys_addr;
        }
 
-       return (addr_offset);
+       return addr_offset;
 }
 
 static const struct rte_memzone *
@@ -156,7 +143,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
        }
 
        /* if alignment is not a power of two */
-       if (!rte_is_power_of_2(align)) {
+       if (align && !rte_is_power_of_2(align)) {
                RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
                                align);
                rte_errno = EINVAL;
@@ -164,21 +151,21 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
        }
 
        /* alignment less than cache size is not allowed */
-       if (align < CACHE_LINE_SIZE)
-               align = CACHE_LINE_SIZE;
+       if (align < RTE_CACHE_LINE_SIZE)
+               align = RTE_CACHE_LINE_SIZE;
 
 
        /* align length on cache boundary. Check for overflow before doing so */
-       if (len > SIZE_MAX - CACHE_LINE_MASK) {
+       if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
                rte_errno = EINVAL; /* requested size too big */
                return NULL;
        }
 
-       len += CACHE_LINE_MASK;
-       len &= ~((size_t) CACHE_LINE_MASK);
+       len += RTE_CACHE_LINE_MASK;
+       len &= ~((size_t) RTE_CACHE_LINE_MASK);
 
        /* save minimal requested  length */
-       requested_len = RTE_MAX((size_t)CACHE_LINE_SIZE,  len);
+       requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
 
        /* check that boundary condition is valid */
        if (bound != 0 &&
@@ -308,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;
@@ -332,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);
 
@@ -341,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
@@ -430,8 +418,8 @@ memseg_sanitize(struct rte_memseg *memseg)
        unsigned virt_align;
        unsigned off;
 
-       phys_align = memseg->phys_addr & CACHE_LINE_MASK;
-       virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
+       phys_align = memseg->phys_addr & RTE_CACHE_LINE_MASK;
+       virt_align = (unsigned long)memseg->addr & RTE_CACHE_LINE_MASK;
 
        /*
         * sanity check: phys_addr and addr must have the same
@@ -441,19 +429,19 @@ memseg_sanitize(struct rte_memseg *memseg)
                return -1;
 
        /* memseg is really too small, don't bother with it */
-       if (memseg->len < (2 * CACHE_LINE_SIZE)) {
+       if (memseg->len < (2 * RTE_CACHE_LINE_SIZE)) {
                memseg->len = 0;
                return 0;
        }
 
        /* align start address */
-       off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
+       off = (RTE_CACHE_LINE_SIZE - phys_align) & RTE_CACHE_LINE_MASK;
        memseg->phys_addr += off;
        memseg->addr = (char *)memseg->addr + off;
        memseg->len -= off;
 
        /* align end address */
-       memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
+       memseg->len &= ~((uint64_t)RTE_CACHE_LINE_MASK);
 
        return 0;
 }