1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_memcpy.h>
12 #include <rte_memory.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_branch_prediction.h>
16 #include <rte_debug.h>
17 #include <rte_launch.h>
18 #include <rte_per_lcore.h>
19 #include <rte_lcore.h>
20 #include <rte_common.h>
21 #include <rte_spinlock.h>
23 #include <rte_malloc.h>
24 #include "malloc_elem.h"
25 #include "malloc_heap.h"
28 /* Free the memory space back to heap */
29 void rte_free(void *addr)
31 if (addr == NULL) return;
32 if (malloc_heap_free(malloc_elem_from_data(addr)) < 0)
33 RTE_LOG(ERR, EAL, "Error: Invalid memory\n");
37 * Allocate memory on specified heap.
40 rte_malloc_socket(const char *type, size_t size, unsigned int align,
43 /* return NULL if size is 0 or alignment is not power-of-2 */
44 if (size == 0 || (align && !rte_is_power_of_2(align)))
47 if (!rte_eal_has_hugepages())
48 socket_arg = SOCKET_ID_ANY;
50 /* Check socket parameter */
51 if (socket_arg >= RTE_MAX_NUMA_NODES)
54 return malloc_heap_alloc(type, size, socket_arg, 0,
55 align == 0 ? 1 : align, 0, false);
59 * Allocate memory on default heap.
62 rte_malloc(const char *type, size_t size, unsigned align)
64 return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
68 * Allocate zero'd memory on specified heap.
71 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
73 return rte_malloc_socket(type, size, align, socket);
77 * Allocate zero'd memory on default heap.
80 rte_zmalloc(const char *type, size_t size, unsigned align)
82 return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
86 * Allocate zero'd memory on specified heap.
89 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
91 return rte_zmalloc_socket(type, num * size, align, socket);
95 * Allocate zero'd memory on default heap.
98 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
100 return rte_zmalloc(type, num * size, align);
104 * Resize allocated memory.
107 rte_realloc(void *ptr, size_t size, unsigned align)
110 return rte_malloc(NULL, size, align);
112 struct malloc_elem *elem = malloc_elem_from_data(ptr);
114 RTE_LOG(ERR, EAL, "Error: memory corruption detected\n");
118 size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
119 /* check alignment matches first, and if ok, see if we can resize block */
120 if (RTE_PTR_ALIGN(ptr,align) == ptr &&
121 malloc_heap_resize(elem, size) == 0)
124 /* either alignment is off, or we have no room to expand,
126 void *new_ptr = rte_malloc(NULL, size, align);
129 const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
130 rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
137 rte_malloc_validate(const void *ptr, size_t *size)
139 const struct malloc_elem *elem = malloc_elem_from_data(ptr);
140 if (!malloc_elem_cookies_ok(elem))
143 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
148 * Function to retrieve data for heap on given socket
151 rte_malloc_get_socket_stats(int socket,
152 struct rte_malloc_socket_stats *socket_stats)
154 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
156 if (socket >= RTE_MAX_NUMA_NODES || socket < 0)
159 return malloc_heap_get_stats(&mcfg->malloc_heaps[socket], socket_stats);
163 * Function to dump contents of all heaps
165 void __rte_experimental
166 rte_malloc_dump_heaps(FILE *f)
168 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
171 for (idx = 0; idx < rte_socket_count(); idx++) {
172 unsigned int socket = rte_socket_id_by_idx(idx);
173 fprintf(f, "Heap on socket %i:\n", socket);
174 malloc_heap_dump(&mcfg->malloc_heaps[socket], f);
180 * Print stats on memory type. If type is NULL, info on all types is printed
183 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
186 struct rte_malloc_socket_stats sock_stats;
187 /* Iterate through all initialised heaps */
188 for (socket=0; socket< RTE_MAX_NUMA_NODES; socket++) {
189 if ((rte_malloc_get_socket_stats(socket, &sock_stats) < 0))
192 fprintf(f, "Socket:%u\n", socket);
193 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
194 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
195 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
196 fprintf(f, "\tGreatest_free_size:%zu,\n",
197 sock_stats.greatest_free_size);
198 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
199 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
205 * TODO: Set limit to memory that can be allocated to memory type
208 rte_malloc_set_limit(__rte_unused const char *type,
209 __rte_unused size_t max)
215 * Return the IO address of a virtual address obtained through rte_malloc
218 rte_malloc_virt2iova(const void *addr)
220 const struct rte_memseg *ms;
221 struct malloc_elem *elem = malloc_elem_from_data(addr);
226 if (rte_eal_iova_mode() == RTE_IOVA_VA)
227 return (uintptr_t) addr;
229 ms = rte_mem_virt2memseg(addr, elem->msl);
233 if (ms->iova == RTE_BAD_IOVA)
236 return ms->iova + RTE_PTR_DIFF(addr, ms->addr);