first public release
[dpdk.git] / lib / librte_malloc / malloc_heap.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 #include <stdint.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_tailq.h>
46 #include <rte_eal.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_string_fns.h>
52 #include <rte_spinlock.h>
53
54 #include "malloc_elem.h"
55 #include "malloc_heap.h"
56
57 #define QUOTE_(x) #x
58 #define QUOTE(x) QUOTE_(x)
59 /* since the memzone size starts with a digit, it will appear unquoted in
60  * rte_config.h, so quote it so it can be passed to rte_str_to_size */
61 #define MALLOC_MEMZONE_SIZE QUOTE(RTE_MALLOC_MEMZONE_SIZE)
62
63 /*
64  * returns the configuration setting for the memzone size as a size_t value
65  */
66 static inline size_t
67 get_malloc_memzone_size(void)
68 {
69         return rte_str_to_size(MALLOC_MEMZONE_SIZE);
70 }
71
72 /*
73  * reserve an extra memory zone and make it available for use by a particular
74  * heap. This reserves the zone and sets a dummy malloc_elem header at the end
75  * to prevent overflow. The rest of the zone is added to free list as a single
76  * large free block
77  */
78 static int
79 malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align)
80 {
81         const unsigned mz_flags = 0;
82         const size_t min_size = get_malloc_memzone_size();
83         /* ensure the data we want to allocate will fit in the memzone */
84         size_t mz_size = size + align + MALLOC_ELEM_OVERHEAD * 2;
85         if (mz_size < min_size)
86                 mz_size = min_size;
87
88         char mz_name[RTE_MEMZONE_NAMESIZE];
89         rte_snprintf(mz_name, sizeof(mz_name), "MALLOC_S%u_HEAP_%u",
90                         heap->numa_socket, heap->mz_count++);
91         const struct rte_memzone *mz = rte_memzone_reserve(mz_name, mz_size,
92                         heap->numa_socket, mz_flags);
93         if (mz == NULL)
94                 return -1;
95
96         /* allocate the memory block headers, one at end, one at start */
97         struct malloc_elem *start_elem = (struct malloc_elem *)mz->addr;
98         struct malloc_elem *end_elem = RTE_PTR_ADD(mz->addr,
99                         mz_size - MALLOC_ELEM_OVERHEAD);
100         end_elem = RTE_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE);
101
102         const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem;
103         malloc_elem_init(start_elem, heap, elem_size);
104         malloc_elem_mkend(end_elem, start_elem);
105
106         start_elem->next_free = heap->free_head;
107         heap->free_head = start_elem;
108         return 0;
109 }
110
111 /*
112  * initialise a malloc heap object. The heap is locked with a private
113  * lock while being initialised. This function should only be called the
114  * first time a thread calls malloc - if even then, as heaps are per-socket
115  * not per-thread.
116  */
117 static void
118 malloc_heap_init(struct malloc_heap *heap)
119 {
120         static rte_spinlock_t init_lock = RTE_SPINLOCK_INITIALIZER;
121         rte_spinlock_lock(&init_lock);
122         if (!heap->initialised) {
123                 heap->free_head = NULL;
124                 heap->mz_count = 0;
125                 heap->numa_socket = malloc_get_numa_socket();
126                 rte_spinlock_init(&heap->lock);
127                 heap->initialised = INITIALISED;
128         }
129         rte_spinlock_unlock(&init_lock);
130 }
131
132 /*
133  * Iterates through the freelist for a heap to find a free element
134  * which can store data of the required size and with the requested alignment.
135  * Returns null on failure, or pointer to element on success, with the pointer
136  * to the previous element in the list, if any, being returned in a parameter
137  * (to make removing the element from the free list faster).
138  */
139 static struct malloc_elem *
140 find_suitable_element(struct malloc_heap *heap, size_t size,
141                 unsigned align, struct malloc_elem **prev)
142 {
143         struct malloc_elem *elem = heap->free_head;
144         *prev = NULL;
145         while(elem){
146                 if (malloc_elem_can_hold(elem, size, align))
147                         break;
148                 *prev = elem;
149                 elem = elem->next_free;
150         }
151         return elem;
152 }
153
154 /*
155  * Main function called by malloc to allocate a block of memory from the
156  * heap. It locks the free list, scans it, and adds a new memzone if the
157  * scan fails. Once the new memzone is added, it re-scans and should return
158  * the new element after releasing the lock.
159  */
160 void *
161 malloc_heap_alloc(struct malloc_heap *heap,
162                 const char *type __attribute__((unused)), size_t size, unsigned align)
163 {
164         if (!heap->initialised)
165                 malloc_heap_init(heap);
166
167         size = CACHE_LINE_ROUNDUP(size);
168         align = CACHE_LINE_ROUNDUP(align);
169         rte_spinlock_lock(&heap->lock);
170
171         struct malloc_elem *prev, *elem = find_suitable_element(heap,
172                         size, align, &prev);
173         if (elem == NULL){
174                 malloc_heap_add_memzone(heap, size, align);
175                 elem = find_suitable_element(heap, size, align, &prev);
176         }
177         if (elem != NULL)
178                 elem = malloc_elem_alloc(elem, size, align, prev);
179         rte_spinlock_unlock(&heap->lock);
180         return elem == NULL ? NULL : (void *)(&elem[1]);
181 }