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