first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdint.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <sys/queue.h>
41
42 #include <rte_memcpy.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_tailq.h>
46 #include <rte_eal.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_debug.h>
49 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_common.h>
53 #include <rte_spinlock.h>
54
55 #include <rte_malloc.h>
56 #include "malloc_elem.h"
57 #include "malloc_heap.h"
58
59 static struct malloc_heap malloc_heap[RTE_MAX_NUMA_NODES] = {
60                 { .initialised = NOT_INITIALISED }
61 };
62
63 /* Free the memory space back to heap */
64 void rte_free(void *addr)
65 {
66         if (addr == NULL) return;
67         if (malloc_elem_free(malloc_elem_from_data(addr)) < 0)
68                 rte_panic("Fatal error: Invalid memory\n");
69 }
70
71 /*
72  * Allocate memory on default heap.
73  */
74 void *
75 rte_malloc(const char *type, size_t size, unsigned align)
76 {
77         unsigned malloc_socket = malloc_get_numa_socket();
78         /* return NULL if size is 0 or alignment is not power-of-2 */
79         if (size == 0 || !rte_is_power_of_2(align))
80                 return NULL;
81         return malloc_heap_alloc(&malloc_heap[malloc_socket], type,
82                         size, align == 0 ? 1 : align);
83 }
84
85 /*
86  * Allocate zero'd memory on default heap.
87  */
88 void *
89 rte_zmalloc(const char *type, size_t size, unsigned align)
90 {
91         void *ptr = rte_malloc(type, size, align);
92
93         if (ptr != NULL)
94                 memset(ptr, 0, size);
95         return ptr;
96 }
97
98 /*
99  * Allocate zero'd memory on default heap.
100  */
101 void *
102 rte_calloc(const char *type, size_t num, size_t size, unsigned align)
103 {
104         return rte_zmalloc(type, num * size, align);
105 }
106
107 /*
108  * Resize allocated memory.
109  */
110 void *
111 rte_realloc(void *ptr, size_t size, unsigned align)
112 {
113         if (ptr == NULL)
114                 return rte_malloc(NULL, size, align);
115
116         struct malloc_elem *elem = malloc_elem_from_data(ptr);
117         if (elem == NULL)
118                 rte_panic("Fatal error: memory corruption detected\n");
119
120         size = CACHE_LINE_ROUNDUP(size), align = CACHE_LINE_ROUNDUP(align);
121         /* check alignment matches first, and if ok, see if we can resize block */
122         if (RTE_ALIGN(ptr,align) == ptr &&
123                         malloc_elem_resize(elem, size) == 0)
124                 return ptr;
125
126         /* either alignment is off, or we have no room to expand,
127          * so move data. */
128         void *new_ptr = rte_malloc(NULL, size, align);
129         if (new_ptr == NULL)
130                 return NULL;
131         const unsigned old_size = elem->size - MALLOC_ELEM_OVERHEAD;
132         rte_memcpy(new_ptr, ptr, old_size < size ? old_size : size);
133         rte_free(ptr);
134
135         return new_ptr;
136 }
137
138 int
139 rte_malloc_validate(void *ptr, size_t *size)
140 {
141         struct malloc_elem *elem = malloc_elem_from_data(ptr);
142         if (!malloc_elem_cookies_ok(elem))
143                 return -1;
144         if (size != NULL)
145                 *size = elem->size - elem->pad - MALLOC_ELEM_OVERHEAD;
146         return 0;
147 }
148 /*
149  * TODO: Print stats on memory type. If type is NULL, info on all types is printed
150  */
151 void
152 rte_malloc_dump_stats(__rte_unused const char *type)
153 {
154         return;
155 }
156
157 /*
158  * TODO: Set limit to memory that can be allocated to memory type
159  */
160 int
161 rte_malloc_set_limit(__rte_unused const char *type,
162                 __rte_unused size_t max)
163 {
164         return 0;
165 }
166