Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / common / rte_malloc.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/queue.h>
39
40 #include <rte_memcpy.h>
41 #include <rte_memory.h>
42 #include <rte_eal.h>
43 #include <rte_eal_memconfig.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_debug.h>
46 #include <rte_launch.h>
47 #include <rte_per_lcore.h>
48 #include <rte_lcore.h>
49 #include <rte_common.h>
50 #include <rte_spinlock.h>
51
52 #include <rte_malloc.h>
53 #include "malloc_elem.h"
54 #include "malloc_heap.h"
55
56
57 /* Free the memory space back to heap */
58 void rte_free(void *addr)
59 {
60         if (addr == NULL) return;
61         if (malloc_elem_free(malloc_elem_from_data(addr)) < 0)
62                 rte_panic("Fatal error: Invalid memory\n");
63 }
64
65 /*
66  * Allocate memory on specified heap.
67  */
68 void *
69 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket_arg)
70 {
71         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
72         int socket, i;
73         void *ret;
74
75         /* return NULL if size is 0 or alignment is not power-of-2 */
76         if (size == 0 || (align && !rte_is_power_of_2(align)))
77                 return NULL;
78
79         if (!rte_eal_has_hugepages())
80                 socket_arg = SOCKET_ID_ANY;
81
82         if (socket_arg == SOCKET_ID_ANY)
83                 socket = malloc_get_numa_socket();
84         else
85                 socket = socket_arg;
86
87         /* Check socket parameter */
88         if (socket >= RTE_MAX_NUMA_NODES)
89                 return NULL;
90
91         ret = malloc_heap_alloc(&mcfg->malloc_heaps[socket], type,
92                                 size, 0, align == 0 ? 1 : align, 0);
93         if (ret != NULL || socket_arg != SOCKET_ID_ANY)
94                 return ret;
95
96         /* try other heaps */
97         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
98                 /* we already tried this one */
99                 if (i == socket)
100                         continue;
101
102                 ret = malloc_heap_alloc(&mcfg->malloc_heaps[i], type,
103                                         size, 0, align == 0 ? 1 : align, 0);
104                 if (ret != NULL)
105                         return ret;
106         }
107
108         return NULL;
109 }
110
111 /*
112  * Allocate memory on default heap.
113  */
114 void *
115 rte_malloc(const char *type, size_t size, unsigned align)
116 {
117         return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
118 }
119
120 /*
121  * Allocate zero'd memory on specified heap.
122  */
123 void *
124 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
125 {
126         void *ptr = rte_malloc_socket(type, size, align, socket);
127
128         if (ptr != NULL)
129                 memset(ptr, 0, size);
130         return ptr;
131 }
132
133 /*
134  * Allocate zero'd memory on default heap.
135  */
136 void *
137 rte_zmalloc(const char *type, size_t size, unsigned align)
138 {
139         return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
140 }
141
142 /*
143  * Allocate zero'd memory on specified heap.
144  */
145 void *
146 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
147 {
148         return rte_zmalloc_socket(type, num * size, align, socket);
149 }
150
151 /*
152  * Allocate zero'd memory on default heap.
153  */
154 void *
155 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
156 {
157         return rte_zmalloc(type, num * size, align);
158 }
159
160 /*
161  * Resize allocated memory.
162  */
163 void *
164 rte_realloc(void *ptr, size_t size, unsigned align)
165 {
166         if (ptr == NULL)
167                 return rte_malloc(NULL, size, align);
168
169         struct malloc_elem *elem = malloc_elem_from_data(ptr);
170         if (elem == NULL)
171                 rte_panic("Fatal error: memory corruption detected\n");
172
173         size = RTE_CACHE_LINE_ROUNDUP(size), align = RTE_CACHE_LINE_ROUNDUP(align);
174         /* check alignment matches first, and if ok, see if we can resize block */
175         if (RTE_PTR_ALIGN(ptr,align) == ptr &&
176                         malloc_elem_resize(elem, size) == 0)
177                 return ptr;
178
179         /* either alignment is off, or we have no room to expand,
180          * so move data. */
181         void *new_ptr = rte_malloc(NULL, size, align);
182         if (new_ptr == NULL)
183                 return NULL;
184         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
185         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
186         rte_free(ptr);
187
188         return new_ptr;
189 }
190
191 int
192 rte_malloc_validate(const void *ptr, size_t *size)
193 {
194         const struct malloc_elem *elem = malloc_elem_from_data(ptr);
195         if (!malloc_elem_cookies_ok(elem))
196                 return -1;
197         if (size != NULL)
198                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
199         return 0;
200 }
201
202 /*
203  * Function to retrieve data for heap on given socket
204  */
205 int
206 rte_malloc_get_socket_stats(int socket,
207                 struct rte_malloc_socket_stats *socket_stats)
208 {
209         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
210
211         if (socket >= RTE_MAX_NUMA_NODES || socket < 0)
212                 return -1;
213
214         return malloc_heap_get_stats(&mcfg->malloc_heaps[socket], socket_stats);
215 }
216
217 /*
218  * Print stats on memory type. If type is NULL, info on all types is printed
219  */
220 void
221 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
222 {
223         unsigned int socket;
224         struct rte_malloc_socket_stats sock_stats;
225         /* Iterate through all initialised heaps */
226         for (socket=0; socket< RTE_MAX_NUMA_NODES; socket++) {
227                 if ((rte_malloc_get_socket_stats(socket, &sock_stats) < 0))
228                         continue;
229
230                 fprintf(f, "Socket:%u\n", socket);
231                 fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
232                 fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
233                 fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
234                 fprintf(f, "\tGreatest_free_size:%zu,\n",
235                                 sock_stats.greatest_free_size);
236                 fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
237                 fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
238         }
239         return;
240 }
241
242 /*
243  * TODO: Set limit to memory that can be allocated to memory type
244  */
245 int
246 rte_malloc_set_limit(__rte_unused const char *type,
247                 __rte_unused size_t max)
248 {
249         return 0;
250 }
251
252 /*
253  * Return the physical address of a virtual address obtained through rte_malloc
254  */
255 phys_addr_t
256 rte_malloc_virt2phy(const void *addr)
257 {
258         const struct malloc_elem *elem = malloc_elem_from_data(addr);
259         if (elem == NULL)
260                 return 0;
261         return elem->ms->phys_addr + ((uintptr_t)addr - (uintptr_t)elem->ms->addr);
262 }