mem: replace memseg with memseg lists
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
index 44538d7..0ef2c45 100644 (file)
@@ -21,6 +21,7 @@
 #include <rte_memcpy.h>
 #include <rte_atomic.h>
 
+#include "eal_internal_cfg.h"
 #include "malloc_elem.h"
 #include "malloc_heap.h"
 
@@ -62,22 +63,50 @@ check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
 }
 
 /*
- * Expand the heap with a memseg.
- * This reserves the zone and sets a dummy malloc_elem header at the end
- * to prevent overflow. The rest of the zone is added to free list as a single
- * large free block
+ * Expand the heap with a memory area.
  */
-static void
-malloc_heap_add_memseg(struct malloc_heap *heap, struct rte_memseg *ms)
+static struct malloc_elem *
+malloc_heap_add_memory(struct malloc_heap *heap, struct rte_memseg_list *msl,
+               void *start, size_t len)
 {
-       struct malloc_elem *start_elem = (struct malloc_elem *)ms->addr;
-       const size_t elem_size = ms->len - MALLOC_ELEM_OVERHEAD;
+       struct malloc_elem *elem = start;
+
+       malloc_elem_init(elem, heap, msl, len);
+
+       malloc_elem_insert(elem);
+
+       elem = malloc_elem_join_adjacent_free(elem);
+
+       malloc_elem_free_list_insert(elem);
+
+       heap->total_size += len;
+
+       return elem;
+}
+
+static int
+malloc_add_seg(const struct rte_memseg_list *msl,
+               const struct rte_memseg *ms, size_t len, void *arg __rte_unused)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct rte_memseg_list *found_msl;
+       struct malloc_heap *heap;
+       int msl_idx;
+
+       heap = &mcfg->malloc_heaps[msl->socket_id];
+
+       /* msl is const, so find it */
+       msl_idx = msl - mcfg->memsegs;
+       found_msl = &mcfg->memsegs[msl_idx];
 
-       malloc_elem_init(start_elem, heap, ms, elem_size);
-       malloc_elem_insert(start_elem);
-       malloc_elem_free_list_insert(start_elem);
+       if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
+               return -1;
+
+       malloc_heap_add_memory(heap, found_msl, ms->addr, len);
 
-       heap->total_size += elem_size;
+       RTE_LOG(DEBUG, EAL, "Added %zuM to heap on socket %i\n", len >> 20,
+                       msl->socket_id);
+       return 0;
 }
 
 /*
@@ -88,7 +117,7 @@ malloc_heap_add_memseg(struct malloc_heap *heap, struct rte_memseg *ms)
  */
 static struct malloc_elem *
 find_suitable_element(struct malloc_heap *heap, size_t size,
-               unsigned flags, size_t align, size_t bound)
+               unsigned int flags, size_t align, size_t bound, bool contig)
 {
        size_t idx;
        struct malloc_elem *elem, *alt_elem = NULL;
@@ -97,8 +126,10 @@ find_suitable_element(struct malloc_heap *heap, size_t size,
                        idx < RTE_HEAP_NUM_FREELISTS; idx++) {
                for (elem = LIST_FIRST(&heap->free_head[idx]);
                                !!elem; elem = LIST_NEXT(elem, free_list)) {
-                       if (malloc_elem_can_hold(elem, size, align, bound)) {
-                               if (check_hugepage_sz(flags, elem->ms->hugepage_sz))
+                       if (malloc_elem_can_hold(elem, size, align, bound,
+                                       contig)) {
+                               if (check_hugepage_sz(flags,
+                                               elem->msl->page_sz))
                                        return elem;
                                if (alt_elem == NULL)
                                        alt_elem = elem;
@@ -121,7 +152,7 @@ find_suitable_element(struct malloc_heap *heap, size_t size,
 void *
 malloc_heap_alloc(struct malloc_heap *heap,
                const char *type __attribute__((unused)), size_t size, unsigned flags,
-               size_t align, size_t bound)
+               size_t align, size_t bound, bool contig)
 {
        struct malloc_elem *elem;
 
@@ -130,9 +161,9 @@ malloc_heap_alloc(struct malloc_heap *heap,
 
        rte_spinlock_lock(&heap->lock);
 
-       elem = find_suitable_element(heap, size, flags, align, bound);
+       elem = find_suitable_element(heap, size, flags, align, bound, contig);
        if (elem != NULL) {
-               elem = malloc_elem_alloc(elem, size, align, bound);
+               elem = malloc_elem_alloc(elem, size, align, bound, contig);
                /* increase heap's count of allocated elements */
                heap->alloc_count++;
        }
@@ -145,7 +176,7 @@ int
 malloc_heap_free(struct malloc_elem *elem)
 {
        struct malloc_heap *heap;
-       int ret;
+       struct malloc_elem *ret;
 
        if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
                return -1;
@@ -159,7 +190,7 @@ malloc_heap_free(struct malloc_elem *elem)
 
        rte_spinlock_unlock(&(heap->lock));
 
-       return ret;
+       return ret != NULL ? 0 : -1;
 }
 
 int
@@ -243,17 +274,10 @@ int
 rte_eal_malloc_heap_init(void)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
-       unsigned ms_cnt;
-       struct rte_memseg *ms;
 
        if (mcfg == NULL)
                return -1;
 
-       for (ms = &mcfg->memseg[0], ms_cnt = 0;
-                       (ms_cnt < RTE_MAX_MEMSEG) && (ms->len > 0);
-                       ms_cnt++, ms++) {
-               malloc_heap_add_memseg(&mcfg->malloc_heaps[ms->socket_id], ms);
-       }
-
-       return 0;
+       /* add all IOVA-contiguous areas to the heap */
+       return rte_memseg_contig_walk(malloc_add_seg, NULL);
 }