4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/queue.h>
40 #include <rte_memcpy.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_tailq.h>
45 #include <rte_eal_memconfig.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_debug.h>
48 #include <rte_launch.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_common.h>
52 #include <rte_spinlock.h>
54 #include <rte_malloc.h>
55 #include "malloc_elem.h"
56 #include "malloc_heap.h"
59 /* Free the memory space back to heap */
60 void rte_free(void *addr)
62 if (addr == NULL) return;
63 if (malloc_elem_free(malloc_elem_from_data(addr)) < 0)
64 rte_panic("Fatal error: Invalid memory\n");
68 * Allocate memory on specified heap.
71 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)
73 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
75 /* return NULL if size is 0 or alignment is not power-of-2 */
76 if (size == 0 || !rte_is_power_of_2(align))
79 if (socket == SOCKET_ID_ANY)
80 socket = malloc_get_numa_socket();
82 /* Check socket parameter */
83 if (socket >= RTE_MAX_NUMA_NODES)
86 return malloc_heap_alloc(&mcfg->malloc_heaps[socket], type,
87 size, align == 0 ? 1 : align);
91 * Allocate memory on default heap.
94 rte_malloc(const char *type, size_t size, unsigned align)
96 return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
100 * Allocate zero'd memory on specified heap.
103 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
105 void *ptr = rte_malloc_socket(type, size, align, socket);
108 memset(ptr, 0, size);
113 * Allocate zero'd memory on default heap.
116 rte_zmalloc(const char *type, size_t size, unsigned align)
118 return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
122 * Allocate zero'd memory on specified heap.
125 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
127 return rte_zmalloc_socket(type, num * size, align, socket);
131 * Allocate zero'd memory on default heap.
134 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
136 return rte_zmalloc(type, num * size, align);
140 * Resize allocated memory.
143 rte_realloc(void *ptr, size_t size, unsigned align)
146 return rte_malloc(NULL, size, align);
148 struct malloc_elem *elem = malloc_elem_from_data(ptr);
150 rte_panic("Fatal error: memory corruption detected\n");
152 size = CACHE_LINE_ROUNDUP(size), align = CACHE_LINE_ROUNDUP(align);
153 /* check alignment matches first, and if ok, see if we can resize block */
154 if (RTE_PTR_ALIGN(ptr,align) == ptr &&
155 malloc_elem_resize(elem, size) == 0)
158 /* either alignment is off, or we have no room to expand,
160 void *new_ptr = rte_malloc(NULL, size, align);
163 const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
164 rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
171 rte_malloc_validate(void *ptr, size_t *size)
173 struct malloc_elem *elem = malloc_elem_from_data(ptr);
174 if (!malloc_elem_cookies_ok(elem))
177 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
182 * Function to retrieve data for heap on given socket
185 rte_malloc_get_socket_stats(int socket,
186 struct rte_malloc_socket_stats *socket_stats)
188 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
190 if (socket >= RTE_MAX_NUMA_NODES || socket < 0)
193 return malloc_heap_get_stats(&mcfg->malloc_heaps[socket], socket_stats);
197 * Print stats on memory type. If type is NULL, info on all types is printed
200 rte_malloc_dump_stats(__rte_unused const char *type)
203 struct rte_malloc_socket_stats sock_stats;
204 /* Iterate through all initialised heaps */
205 for (socket=0; socket< RTE_MAX_NUMA_NODES; socket++) {
206 if ((rte_malloc_get_socket_stats(socket, &sock_stats) < 0))
209 printf("Socket:%u\n", socket);
210 printf("\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
211 printf("\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
212 printf("\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
213 printf("\tGreatest_free_size:%zu,\n",
214 sock_stats.greatest_free_size);
215 printf("\tAlloc_count:%u,\n",sock_stats.alloc_count);
216 printf("\tFree_count:%u,\n", sock_stats.free_count);
222 * TODO: Set limit to memory that can be allocated to memory type
225 rte_malloc_set_limit(__rte_unused const char *type,
226 __rte_unused size_t max)