eal/bsd: fix build
[dpdk.git] / lib / librte_eal / common / eal_common_memory.c
index b69c829..0b69804 100644 (file)
@@ -66,14 +66,17 @@ eal_get_virtual_area(void *requested_addr, size_t *size,
                addr_is_hint = true;
        }
 
-       /* if requested address is not aligned by page size, or if requested
-        * address is NULL, add page size to requested length as we may get an
-        * address that's aligned by system page size, which can be smaller than
-        * our requested page size. additionally, we shouldn't try to align if
-        * system page size is the same as requested page size.
+       /* we don't need alignment of resulting pointer in the following cases:
+        *
+        * 1. page size is equal to system size
+        * 2. we have a requested address, and it is page-aligned, and we will
+        *    be discarding the address if we get a different one.
+        *
+        * for all other cases, alignment is potentially necessary.
         */
        no_align = (requested_addr != NULL &&
-               ((uintptr_t)requested_addr & (page_sz - 1))) ||
+               requested_addr == RTE_PTR_ALIGN(requested_addr, page_sz) &&
+               !addr_is_hint) ||
                page_sz == system_page_sz;
 
        do {
@@ -291,7 +294,7 @@ dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
                void *arg)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-       int msl_idx, ms_idx;
+       int msl_idx, ms_idx, fd;
        FILE *f = arg;
 
        msl_idx = msl - mcfg->memsegs;
@@ -302,10 +305,11 @@ dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
        if (ms_idx < 0)
                return -1;
 
+       fd = eal_memalloc_get_seg_fd(msl_idx, ms_idx);
        fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
                        "virt:%p, socket_id:%"PRId32", "
                        "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
-                       "nrank:%"PRIx32"\n",
+                       "nrank:%"PRIx32" fd:%i\n",
                        msl_idx, ms_idx,
                        ms->iova,
                        ms->len,
@@ -313,7 +317,8 @@ dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
                        ms->socket_id,
                        ms->hugepage_sz,
                        ms->nchannel,
-                       ms->nrank);
+                       ms->nrank,
+                       fd);
 
        return 0;
 }
@@ -471,14 +476,11 @@ rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
 }
 
 int __rte_experimental
-rte_memseg_walk(rte_memseg_walk_t func, void *arg)
+rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        int i, ms_idx, ret = 0;
 
-       /* do not allow allocations/frees/init while we iterate */
-       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
-
        for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
                struct rte_memseg_list *msl = &mcfg->memsegs[i];
                const struct rte_memseg *ms;
@@ -493,29 +495,33 @@ rte_memseg_walk(rte_memseg_walk_t func, void *arg)
                while (ms_idx >= 0) {
                        ms = rte_fbarray_get(arr, ms_idx);
                        ret = func(msl, ms, arg);
-                       if (ret < 0) {
-                               ret = -1;
-                               goto out;
-                       } else if (ret > 0) {
-                               ret = 1;
-                               goto out;
-                       }
+                       if (ret)
+                               return ret;
                        ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
                }
        }
-out:
-       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
-       return ret;
+       return 0;
 }
 
 int __rte_experimental
-rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
+rte_memseg_walk(rte_memseg_walk_t func, void *arg)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-       int i, ret = 0;
+       int ret = 0;
 
        /* do not allow allocations/frees/init while we iterate */
        rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       ret = rte_memseg_walk_thread_unsafe(func, arg);
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+
+       return ret;
+}
+
+int __rte_experimental
+rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       int i, ret = 0;
 
        for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
                struct rte_memseg_list *msl = &mcfg->memsegs[i];
@@ -524,17 +530,122 @@ rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
                        continue;
 
                ret = func(msl, arg);
-               if (ret < 0) {
-                       ret = -1;
-                       goto out;
-               }
-               if (ret > 0) {
-                       ret = 1;
-                       goto out;
-               }
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}
+
+int __rte_experimental
+rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       int ret = 0;
+
+       /* do not allow allocations/frees/init while we iterate */
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       ret = rte_memseg_list_walk_thread_unsafe(func, arg);
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+
+       return ret;
+}
+
+int __rte_experimental
+rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct rte_memseg_list *msl;
+       struct rte_fbarray *arr;
+       int msl_idx, seg_idx, ret;
+
+       if (ms == NULL) {
+               rte_errno = EINVAL;
+               return -1;
+       }
+
+       msl = rte_mem_virt2memseg_list(ms->addr);
+       if (msl == NULL) {
+               rte_errno = EINVAL;
+               return -1;
+       }
+       arr = &msl->memseg_arr;
+
+       msl_idx = msl - mcfg->memsegs;
+       seg_idx = rte_fbarray_find_idx(arr, ms);
+
+       if (!rte_fbarray_is_used(arr, seg_idx)) {
+               rte_errno = ENOENT;
+               return -1;
        }
-out:
+
+       ret = eal_memalloc_get_seg_fd(msl_idx, seg_idx);
+       if (ret < 0) {
+               rte_errno = -ret;
+               ret = -1;
+       }
+       return ret;
+}
+
+int __rte_experimental
+rte_memseg_get_fd(const struct rte_memseg *ms)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       int ret;
+
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       ret = rte_memseg_get_fd_thread_unsafe(ms);
        rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+
+       return ret;
+}
+
+int __rte_experimental
+rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
+               size_t *offset)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct rte_memseg_list *msl;
+       struct rte_fbarray *arr;
+       int msl_idx, seg_idx, ret;
+
+       if (ms == NULL || offset == NULL) {
+               rte_errno = EINVAL;
+               return -1;
+       }
+
+       msl = rte_mem_virt2memseg_list(ms->addr);
+       if (msl == NULL) {
+               rte_errno = EINVAL;
+               return -1;
+       }
+       arr = &msl->memseg_arr;
+
+       msl_idx = msl - mcfg->memsegs;
+       seg_idx = rte_fbarray_find_idx(arr, ms);
+
+       if (!rte_fbarray_is_used(arr, seg_idx)) {
+               rte_errno = ENOENT;
+               return -1;
+       }
+
+       ret = eal_memalloc_get_seg_fd_offset(msl_idx, seg_idx, offset);
+       if (ret < 0) {
+               rte_errno = -ret;
+               ret = -1;
+       }
+       return ret;
+}
+
+int __rte_experimental
+rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       int ret;
+
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       ret = rte_memseg_get_fd_offset_thread_unsafe(ms, offset);
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+
        return ret;
 }