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