vppinfra: don't use dlmalloc apis outside of mem_dlmalloc.c 76/43876/2
authorDamjan Marion <[email protected]>
Fri, 10 Oct 2025 21:07:31 +0000 (23:07 +0200)
committerFlorin Coras <[email protected]>
Sat, 11 Oct 2025 18:22:43 +0000 (18:22 +0000)
Type: improvement
Change-Id: Iecc397b639352129ba4b908736210eeaf60e5c14
Signed-off-by: Damjan Marion <[email protected]>
src/vppinfra/mem_bulk.c

index 4dd6a16..cfc75ca 100644 (file)
 #include <vppinfra/format.h>
 #include <vppinfra/clib_error.h>
 
-/* while usage of dlmalloc APIs is genrally discouraged, in this particular
- * case there is significant benefit of calling them directly due to
- * smaller memory consuption (no wwp and headroom space) */
-#include <vppinfra/dlmalloc.h>
-
 #define CLIB_MEM_BULK_DEFAULT_MIN_ELTS_PER_CHUNK 32
 
 typedef struct clib_mem_bulk_chunk_hdr
@@ -40,7 +35,7 @@ typedef struct
   u32 elts_per_chunk;
   u32 align;
   u32 chunk_align;
-  void *mspace;
+  clib_mem_heap_t *heap;
   clib_mem_bulk_chunk_hdr_t *full_chunks, *avail_chunks;
 } clib_mem_bulk_t;
 
@@ -57,7 +52,8 @@ clib_mem_bulk_init (u32 elt_sz, u32 align, u32 min_elts_per_chunk)
   clib_mem_bulk_t *b;
   uword sz;
 
-  if ((b = mspace_memalign (heap->mspace, 16, sizeof (clib_mem_bulk_t))) == 0)
+  b = clib_mem_heap_alloc_aligned_or_null (heap, sizeof (clib_mem_bulk_t), 16);
+  if (b == 0)
     return 0;
 
   if (align < 16)
@@ -68,7 +64,7 @@ clib_mem_bulk_init (u32 elt_sz, u32 align, u32 min_elts_per_chunk)
 
   clib_mem_unpoison (b, sizeof (clib_mem_bulk_t));
   clib_memset (b, 0, sizeof (clib_mem_bulk_t));
-  b->mspace = heap->mspace;
+  b->heap = heap;
   b->align = align;
   b->elt_sz = round_pow2 (elt_sz, align);
   b->chunk_hdr_sz = round_pow2 (sizeof (clib_mem_bulk_chunk_hdr_t), align);
@@ -84,7 +80,7 @@ clib_mem_bulk_destroy (clib_mem_bulk_handle_t h)
 {
   clib_mem_bulk_t *b = h;
   clib_mem_bulk_chunk_hdr_t *c, *next;
-  void *ms = b->mspace;
+  clib_mem_heap_t *heap = b->heap;
 
   c = b->full_chunks;
 
@@ -92,8 +88,7 @@ again:
   while (c)
     {
       next = c->next;
-      clib_mem_poison (c, bulk_chunk_size (b));
-      mspace_free (ms, c);
+      clib_mem_heap_free (heap, c);
       c = next;
     }
 
@@ -105,7 +100,7 @@ again:
     }
 
   clib_mem_poison (b, sizeof (clib_mem_bulk_t));
-  mspace_free (ms, b);
+  clib_mem_heap_free (heap, b);
 }
 
 static inline void *
@@ -147,7 +142,9 @@ clib_mem_bulk_alloc (clib_mem_bulk_handle_t h)
   if (b->avail_chunks == 0)
     {
       u32 i, sz = bulk_chunk_size (b);
-      c = mspace_memalign (b->mspace, b->chunk_align, sz);
+      c = clib_mem_heap_alloc_aligned_or_null (b->heap, sz, b->chunk_align);
+      if (c == 0)
+       return 0;
       clib_mem_unpoison (c, sz);
       clib_memset (c, 0, sizeof (clib_mem_bulk_chunk_hdr_t));
       b->avail_chunks = c;
@@ -192,8 +189,7 @@ clib_mem_bulk_free (clib_mem_bulk_handle_t h, void *p)
     {
       /* chunk is empty - give it back */
       remove_from_chunk_list (&b->avail_chunks, c);
-      clib_mem_poison (c, bulk_chunk_size (b));
-      mspace_free (b->mspace, c);
+      clib_mem_heap_free (b->heap, c);
       return;
     }