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