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