memory: malloc now supports multi process
[dpdk.git] / lib / librte_malloc / rte_malloc.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdint.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/queue.h>
40
41 #include <rte_memcpy.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_tailq.h>
45 #include <rte_eal.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 #include "malloc_heap.c"
58
59
60 /* Free the memory space back to heap */
61 void rte_free(void *addr)
62 {
63         if (addr == NULL) return;
64         if (malloc_elem_free(malloc_elem_from_data(addr)) < 0)
65                 rte_panic("Fatal error: Invalid memory\n");
66 }
67
68 /*
69  * Allocate memory on specified heap.
70  */
71 void *
72 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)
73 {
74         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
75
76         /* return NULL if size is 0 or alignment is not power-of-2 */
77         if (size == 0 || !rte_is_power_of_2(align))
78                 return NULL;
79
80         if (socket == SOCKET_ID_ANY)
81                 socket = malloc_get_numa_socket();
82
83         /* Check socket parameter */
84         if (socket >= RTE_MAX_NUMA_NODES)
85                 return NULL;
86
87         return malloc_heap_alloc(&mcfg->malloc_heaps[socket], type,
88                         size, align == 0 ? 1 : align);
89 }
90
91 /*
92  * Allocate memory on default heap.
93  */
94 void *
95 rte_malloc(const char *type, size_t size, unsigned align)
96 {
97         return rte_malloc_socket(type, size, align, SOCKET_ID_ANY);
98 }
99
100 /*
101  * Allocate zero'd memory on specified heap.
102  */
103 void *
104 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
105 {
106         void *ptr = rte_malloc_socket(type, size, align, socket);
107
108         if (ptr != NULL)
109                 memset(ptr, 0, size);
110         return ptr;
111 }
112
113 /*
114  * Allocate zero'd memory on default heap.
115  */
116 void *
117 rte_zmalloc(const char *type, size_t size, unsigned align)
118 {
119         return rte_zmalloc_socket(type, size, align, SOCKET_ID_ANY);
120 }
121
122 /*
123  * Allocate zero'd memory on specified heap.
124  */
125 void *
126 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
127 {
128         return rte_zmalloc_socket(type, num * size, align, socket);
129 }
130
131 /*
132  * Allocate zero'd memory on default heap.
133  */
134 void *
135 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
136 {
137         return rte_zmalloc(type, num * size, align);
138 }
139
140 /*
141  * Resize allocated memory.
142  */
143 void *
144 rte_realloc(void *ptr, size_t size, unsigned align)
145 {
146         if (ptr == NULL)
147                 return rte_malloc(NULL, size, align);
148
149         struct malloc_elem *elem = malloc_elem_from_data(ptr);
150         if (elem == NULL)
151                 rte_panic("Fatal error: memory corruption detected\n");
152
153         size = CACHE_LINE_ROUNDUP(size), align = CACHE_LINE_ROUNDUP(align);
154         /* check alignment matches first, and if ok, see if we can resize block */
155         if (RTE_PTR_ALIGN(ptr,align) == ptr &&
156                         malloc_elem_resize(elem, size) == 0)
157                 return ptr;
158
159         /* either alignment is off, or we have no room to expand,
160          * so move data. */
161         void *new_ptr = rte_malloc(NULL, size, align);
162         if (new_ptr == NULL)
163                 return NULL;
164         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
165         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
166         rte_free(ptr);
167
168         return new_ptr;
169 }
170
171 int
172 rte_malloc_validate(void *ptr, size_t *size)
173 {
174         struct malloc_elem *elem = malloc_elem_from_data(ptr);
175         if (!malloc_elem_cookies_ok(elem))
176                 return -1;
177         if (size != NULL)
178                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
179         return 0;
180 }
181 /*
182  * TODO: Print stats on memory type. If type is NULL, info on all types is printed
183  */
184 void
185 rte_malloc_dump_stats(__rte_unused const char *type)
186 {
187         return;
188 }
189
190 /*
191  * TODO: Set limit to memory that can be allocated to memory type
192  */
193 int
194 rte_malloc_set_limit(__rte_unused const char *type,
195                 __rte_unused size_t max)
196 {
197         return 0;
198 }
199