doc: add Meson coding style to contributors guide
[dpdk.git] / lib / librte_eal / common / rte_malloc.c
index eebd067..9d39e58 100644 (file)
 #include <rte_common.h>
 #include <rte_spinlock.h>
 
+#include <rte_eal_trace.h>
+
 #include <rte_malloc.h>
 #include "malloc_elem.h"
 #include "malloc_heap.h"
 #include "eal_memalloc.h"
+#include "eal_memcfg.h"
+#include "eal_private.h"
 
 
 /* Free the memory space back to heap */
-void rte_free(void *addr)
+static void
+mem_free(void *addr, const bool trace_ena)
 {
+       if (trace_ena)
+               rte_eal_trace_mem_free(addr);
+
        if (addr == NULL) return;
        if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
                RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
 }
 
-/*
- * Allocate memory on specified heap.
- */
-void *
-rte_malloc_socket(const char *type, size_t size, unsigned int align,
-               int socket_arg)
+void
+rte_free(void *addr)
+{
+       return mem_free(addr, true);
+}
+
+void
+eal_free_no_trace(void *addr)
+{
+       return mem_free(addr, false);
+}
+
+static void *
+malloc_socket(const char *type, size_t size, unsigned int align,
+               int socket_arg, const bool trace_ena)
 {
+       void *ptr;
+
        /* return NULL if size is 0 or alignment is not power-of-2 */
        if (size == 0 || (align && !rte_is_power_of_2(align)))
                return NULL;
@@ -55,8 +74,28 @@ rte_malloc_socket(const char *type, size_t size, unsigned int align,
                                !rte_eal_has_hugepages())
                socket_arg = SOCKET_ID_ANY;
 
-       return malloc_heap_alloc(type, size, socket_arg, 0,
+       ptr = malloc_heap_alloc(type, size, socket_arg, 0,
                        align == 0 ? 1 : align, 0, false);
+
+       if (trace_ena)
+               rte_eal_trace_mem_malloc(type, size, align, socket_arg, ptr);
+       return ptr;
+}
+
+/*
+ * Allocate memory on specified heap.
+ */
+void *
+rte_malloc_socket(const char *type, size_t size, unsigned int align,
+               int socket_arg)
+{
+       return malloc_socket(type, size, align, socket_arg, true);
+}
+
+void *
+eal_malloc_no_trace(const char *type, size_t size, unsigned int align)
+{
+       return malloc_socket(type, size, align, SOCKET_ID_ANY, false);
 }
 
 /*
@@ -74,7 +113,20 @@ rte_malloc(const char *type, size_t size, unsigned align)
 void *
 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
 {
-       return rte_malloc_socket(type, size, align, socket);
+       void *ptr = rte_malloc_socket(type, size, align, socket);
+
+#ifdef RTE_MALLOC_DEBUG
+       /*
+        * If DEBUG is enabled, then freed memory is marked with poison
+        * value and set to zero on allocation.
+        * If DEBUG is not enabled then  memory is already zeroed.
+        */
+       if (ptr != NULL)
+               memset(ptr, 0, size);
+#endif
+
+       rte_eal_trace_mem_zmalloc(type, size, align, socket, ptr);
+       return ptr;
 }
 
 /*
@@ -127,8 +179,10 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
        if ((socket == SOCKET_ID_ANY ||
             (unsigned int)socket == elem->heap->socket_id) &&
                        RTE_PTR_ALIGN(ptr, align) == ptr &&
-                       malloc_heap_resize(elem, size) == 0)
+                       malloc_heap_resize(elem, size) == 0) {
+               rte_eal_trace_mem_realloc(size, align, socket, ptr);
                return ptr;
+       }
 
        /* either requested socket id doesn't match, alignment is off
         * or we have no room to expand,
@@ -137,10 +191,12 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
        void *new_ptr = rte_malloc_socket(NULL, size, align, socket);
        if (new_ptr == NULL)
                return NULL;
-       const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
+       /* elem: |pad|data_elem|data|trailer| */
+       const size_t old_size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
        rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
        rte_free(ptr);
 
+       rte_eal_trace_mem_realloc(size, align, socket, new_ptr);
        return new_ptr;
 }
 
@@ -185,7 +241,7 @@ rte_malloc_get_socket_stats(int socket,
 /*
  * Function to dump contents of all heaps
  */
-void __rte_experimental
+void
 rte_malloc_dump_heaps(FILE *f)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
@@ -212,7 +268,7 @@ rte_malloc_heap_get_socket(const char *name)
                rte_errno = EINVAL;
                return -1;
        }
-       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_lock();
        for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
                struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
 
@@ -228,7 +284,7 @@ rte_malloc_heap_get_socket(const char *name)
                rte_errno = ENOENT;
                ret = -1;
        }
-       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_unlock();
 
        return ret;
 }
@@ -243,7 +299,7 @@ rte_malloc_heap_socket_is_external(int socket_id)
        if (socket_id == SOCKET_ID_ANY)
                return 0;
 
-       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_lock();
        for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
                struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
 
@@ -253,7 +309,7 @@ rte_malloc_heap_socket_is_external(int socket_id)
                        break;
                }
        }
-       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_unlock();
 
        return ret;
 }
@@ -341,7 +397,6 @@ int
 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
                rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz)
 {
-       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        struct malloc_heap *heap = NULL;
        struct rte_memseg_list *msl;
        unsigned int n;
@@ -358,7 +413,7 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
                rte_errno = EINVAL;
                return -1;
        }
-       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_lock();
 
        /* find our heap */
        heap = find_named_heap(heap_name);
@@ -384,10 +439,11 @@ rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
 
        rte_spinlock_lock(&heap->lock);
        ret = malloc_heap_add_external_memory(heap, msl);
+       msl->heap = 1; /* mark it as heap segment */
        rte_spinlock_unlock(&heap->lock);
 
 unlock:
-       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_unlock();
 
        return ret;
 }
@@ -395,7 +451,6 @@ unlock:
 int
 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
 {
-       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        struct malloc_heap *heap = NULL;
        struct rte_memseg_list *msl;
        int ret;
@@ -407,7 +462,7 @@ rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
                rte_errno = EINVAL;
                return -1;
        }
-       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_lock();
        /* find our heap */
        heap = find_named_heap(heap_name);
        if (heap == NULL) {
@@ -437,7 +492,7 @@ rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len)
        ret = malloc_heap_destroy_external_seg(msl);
 
 unlock:
-       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_unlock();
 
        return ret;
 }
@@ -445,7 +500,6 @@ unlock:
 static int
 sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
 {
-       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        struct malloc_heap *heap = NULL;
        struct rte_memseg_list *msl;
        int ret;
@@ -457,7 +511,7 @@ sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
                rte_errno = EINVAL;
                return -1;
        }
-       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_lock();
 
        /* find our heap */
        heap = find_named_heap(heap_name);
@@ -505,7 +559,7 @@ sync_memory(const char *heap_name, void *va_addr, size_t len, bool attach)
                }
        }
 unlock:
-       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_read_unlock();
        return ret;
 }
 
@@ -538,7 +592,7 @@ rte_malloc_heap_create(const char *heap_name)
        /* check if there is space in the heap list, or if heap with this name
         * already exists.
         */
-       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_lock();
 
        for (i = 0; i < RTE_MAX_HEAPS; i++) {
                struct malloc_heap *tmp = &mcfg->malloc_heaps[i];
@@ -567,7 +621,7 @@ rte_malloc_heap_create(const char *heap_name)
        /* we're sure that we can create a new heap, so do it */
        ret = malloc_heap_create(heap, heap_name);
 unlock:
-       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_unlock();
 
        return ret;
 }
@@ -575,7 +629,6 @@ unlock:
 int
 rte_malloc_heap_destroy(const char *heap_name)
 {
-       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        struct malloc_heap *heap = NULL;
        int ret;
 
@@ -586,7 +639,7 @@ rte_malloc_heap_destroy(const char *heap_name)
                rte_errno = EINVAL;
                return -1;
        }
-       rte_rwlock_write_lock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_lock();
 
        /* start from non-socket heaps */
        heap = find_named_heap(heap_name);
@@ -610,7 +663,7 @@ rte_malloc_heap_destroy(const char *heap_name)
        if (ret < 0)
                rte_spinlock_unlock(&heap->lock);
 unlock:
-       rte_rwlock_write_unlock(&mcfg->memory_hotplug_lock);
+       rte_mcfg_mem_write_unlock();
 
        return ret;
 }