add prefix to cache line macros
[dpdk.git] / lib / librte_malloc / malloc_elem.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 #include <stdint.h>
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/queue.h>
38
39 #include <rte_memory.h>
40 #include <rte_memzone.h>
41 #include <rte_tailq.h>
42 #include <rte_eal.h>
43 #include <rte_launch.h>
44 #include <rte_per_lcore.h>
45 #include <rte_lcore.h>
46 #include <rte_debug.h>
47 #include <rte_common.h>
48 #include <rte_spinlock.h>
49
50 #include "malloc_elem.h"
51 #include "malloc_heap.h"
52
53 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
54
55 /*
56  * initialise a general malloc_elem header structure
57  */
58 void
59 malloc_elem_init(struct malloc_elem *elem,
60                 struct malloc_heap *heap, const struct rte_memzone *mz, size_t size)
61 {
62         elem->heap = heap;
63         elem->mz = mz;
64         elem->prev = NULL;
65         memset(&elem->free_list, 0, sizeof(elem->free_list));
66         elem->state = ELEM_FREE;
67         elem->size = size;
68         elem->pad = 0;
69         set_header(elem);
70         set_trailer(elem);
71 }
72
73 /*
74  * initialise a dummy malloc_elem header for the end-of-memzone marker
75  */
76 void
77 malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev)
78 {
79         malloc_elem_init(elem, prev->heap, prev->mz, 0);
80         elem->prev = prev;
81         elem->state = ELEM_BUSY; /* mark busy so its never merged */
82 }
83
84 /*
85  * calculate the starting point of where data of the requested size
86  * and alignment would fit in the current element. If the data doesn't
87  * fit, return NULL.
88  */
89 static void *
90 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align)
91 {
92         const uintptr_t end_pt = (uintptr_t)elem +
93                         elem->size - MALLOC_ELEM_TRAILER_LEN;
94         const uintptr_t new_data_start = rte_align_floor_int((end_pt - size),align);
95         const uintptr_t new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
96
97         /* if the new start point is before the exist start, it won't fit */
98         return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
99 }
100
101 /*
102  * use elem_start_pt to determine if we get meet the size and
103  * alignment request from the current element
104  */
105 int
106 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align)
107 {
108         return elem_start_pt(elem, size, align) != NULL;
109 }
110
111 /*
112  * split an existing element into two smaller elements at the given
113  * split_pt parameter.
114  */
115 static void
116 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
117 {
118         struct malloc_elem *next_elem = RTE_PTR_ADD(elem, elem->size);
119         const unsigned old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
120         const unsigned new_elem_size = elem->size - old_elem_size;
121
122         malloc_elem_init(split_pt, elem->heap, elem->mz, new_elem_size);
123         split_pt->prev = elem;
124         next_elem->prev = split_pt;
125         elem->size = old_elem_size;
126         set_trailer(elem);
127 }
128
129 /*
130  * Given an element size, compute its freelist index.
131  * We free an element into the freelist containing similarly-sized elements.
132  * We try to allocate elements starting with the freelist containing
133  * similarly-sized elements, and if necessary, we search freelists
134  * containing larger elements.
135  *
136  * Example element size ranges for a heap with five free lists:
137  *   heap->free_head[0] - (0   , 2^8]
138  *   heap->free_head[1] - (2^8 , 2^10]
139  *   heap->free_head[2] - (2^10 ,2^12]
140  *   heap->free_head[3] - (2^12, 2^14]
141  *   heap->free_head[4] - (2^14, MAX_SIZE]
142  */
143 size_t
144 malloc_elem_free_list_index(size_t size)
145 {
146 #define MALLOC_MINSIZE_LOG2   8
147 #define MALLOC_LOG2_INCREMENT 2
148
149         size_t log2;
150         size_t index;
151
152         if (size <= (1UL << MALLOC_MINSIZE_LOG2))
153                 return 0;
154
155         /* Find next power of 2 >= size. */
156         log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
157
158         /* Compute freelist index, based on log2(size). */
159         index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
160                 MALLOC_LOG2_INCREMENT;
161
162         return (index <= RTE_HEAP_NUM_FREELISTS-1?
163                 index: RTE_HEAP_NUM_FREELISTS-1);
164 }
165
166 /*
167  * Add the specified element to its heap's free list.
168  */
169 void
170 malloc_elem_free_list_insert(struct malloc_elem *elem)
171 {
172         size_t idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
173
174         elem->state = ELEM_FREE;
175         LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
176 }
177
178 /*
179  * Remove the specified element from its heap's free list.
180  */
181 static void
182 elem_free_list_remove(struct malloc_elem *elem)
183 {
184         LIST_REMOVE(elem, free_list);
185 }
186
187 /*
188  * reserve a block of data in an existing malloc_elem. If the malloc_elem
189  * is much larger than the data block requested, we split the element in two.
190  * This function is only called from malloc_heap_alloc so parameter checking
191  * is not done here, as it's done there previously.
192  */
193 struct malloc_elem *
194 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align)
195 {
196         struct malloc_elem *new_elem = elem_start_pt(elem, size, align);
197         const unsigned old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
198
199         if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE){
200                 /* don't split it, pad the element instead */
201                 elem->state = ELEM_BUSY;
202                 elem->pad = old_elem_size;
203
204                 /* put a dummy header in padding, to point to real element header */
205                 if (elem->pad > 0){ /* pad will be at least 64-bytes, as everything
206                                      * is cache-line aligned */
207                         new_elem->pad = elem->pad;
208                         new_elem->state = ELEM_PAD;
209                         new_elem->size = elem->size - elem->pad;
210                         set_header(new_elem);
211                 }
212                 /* remove element from free list */
213                 elem_free_list_remove(elem);
214
215                 return new_elem;
216         }
217
218         /* we are going to split the element in two. The original element
219          * remains free, and the new element is the one allocated.
220          * Re-insert original element, in case its new size makes it
221          * belong on a different list.
222          */
223         elem_free_list_remove(elem);
224         split_elem(elem, new_elem);
225         new_elem->state = ELEM_BUSY;
226         malloc_elem_free_list_insert(elem);
227
228         return new_elem;
229 }
230
231 /*
232  * joing two struct malloc_elem together. elem1 and elem2 must
233  * be contiguous in memory.
234  */
235 static inline void
236 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
237 {
238         struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
239         elem1->size += elem2->size;
240         next->prev = elem1;
241 }
242
243 /*
244  * free a malloc_elem block by adding it to the free list. If the
245  * blocks either immediately before or immediately after newly freed block
246  * are also free, the blocks are merged together.
247  */
248 int
249 malloc_elem_free(struct malloc_elem *elem)
250 {
251         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
252                 return -1;
253
254         rte_spinlock_lock(&(elem->heap->lock));
255         struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
256         if (next->state == ELEM_FREE){
257                 /* remove from free list, join to this one */
258                 elem_free_list_remove(next);
259                 join_elem(elem, next);
260         }
261
262         /* check if previous element is free, if so join with it and return,
263          * need to re-insert in free list, as that element's size is changing
264          */
265         if (elem->prev != NULL && elem->prev->state == ELEM_FREE) {
266                 elem_free_list_remove(elem->prev);
267                 join_elem(elem->prev, elem);
268                 malloc_elem_free_list_insert(elem->prev);
269         }
270         /* otherwise add ourselves to the free list */
271         else {
272                 malloc_elem_free_list_insert(elem);
273                 elem->pad = 0;
274         }
275         /* decrease heap's count of allocated elements */
276         elem->heap->alloc_count--;
277         rte_spinlock_unlock(&(elem->heap->lock));
278
279         return 0;
280 }
281
282 /*
283  * attempt to resize a malloc_elem by expanding into any free space
284  * immediately after it in memory.
285  */
286 int
287 malloc_elem_resize(struct malloc_elem *elem, size_t size)
288 {
289         const size_t new_size = size + MALLOC_ELEM_OVERHEAD;
290         /* if we request a smaller size, then always return ok */
291         const size_t current_size = elem->size - elem->pad;
292         if (current_size >= new_size)
293                 return 0;
294
295         struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
296         rte_spinlock_lock(&elem->heap->lock);
297         if (next ->state != ELEM_FREE)
298                 goto err_return;
299         if (current_size + next->size < new_size)
300                 goto err_return;
301
302         /* we now know the element fits, so remove from free list,
303          * join the two
304          */
305         elem_free_list_remove(next);
306         join_elem(elem, next);
307
308         if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD){
309                 /* now we have a big block together. Lets cut it down a bit, by splitting */
310                 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
311                 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
312                 split_elem(elem, split_pt);
313                 malloc_elem_free_list_insert(split_pt);
314         }
315         rte_spinlock_unlock(&elem->heap->lock);
316         return 0;
317
318 err_return:
319         rte_spinlock_unlock(&elem->heap->lock);
320         return -1;
321 }