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