malloc: make free list removal function public
[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 <inttypes.h>
5 #include <stdint.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/queue.h>
10
11 #include <rte_memory.h>
12 #include <rte_eal.h>
13 #include <rte_launch.h>
14 #include <rte_per_lcore.h>
15 #include <rte_lcore.h>
16 #include <rte_debug.h>
17 #include <rte_common.h>
18 #include <rte_spinlock.h>
19
20 #include "malloc_elem.h"
21 #include "malloc_heap.h"
22
23 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
24
25 /*
26  * Initialize a general malloc_elem header structure
27  */
28 void
29 malloc_elem_init(struct malloc_elem *elem,
30                 struct malloc_heap *heap, const struct rte_memseg *ms, size_t size)
31 {
32         elem->heap = heap;
33         elem->ms = ms;
34         elem->prev = NULL;
35         elem->next = NULL;
36         memset(&elem->free_list, 0, sizeof(elem->free_list));
37         elem->state = ELEM_FREE;
38         elem->size = size;
39         elem->pad = 0;
40         set_header(elem);
41         set_trailer(elem);
42 }
43
44 void
45 malloc_elem_insert(struct malloc_elem *elem)
46 {
47         struct malloc_elem *prev_elem, *next_elem;
48         struct malloc_heap *heap = elem->heap;
49
50         if (heap->first == NULL && heap->last == NULL) {
51                 /* if empty heap */
52                 heap->first = elem;
53                 heap->last = elem;
54                 prev_elem = NULL;
55                 next_elem = NULL;
56         } else if (elem < heap->first) {
57                 /* if lower than start */
58                 prev_elem = NULL;
59                 next_elem = heap->first;
60                 heap->first = elem;
61         } else if (elem > heap->last) {
62                 /* if higher than end */
63                 prev_elem = heap->last;
64                 next_elem = NULL;
65                 heap->last = elem;
66         } else {
67                 /* the new memory is somewhere inbetween start and end */
68                 uint64_t dist_from_start, dist_from_end;
69
70                 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
71                 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
72
73                 /* check which is closer, and find closest list entries */
74                 if (dist_from_start < dist_from_end) {
75                         prev_elem = heap->first;
76                         while (prev_elem->next < elem)
77                                 prev_elem = prev_elem->next;
78                         next_elem = prev_elem->next;
79                 } else {
80                         next_elem = heap->last;
81                         while (next_elem->prev > elem)
82                                 next_elem = next_elem->prev;
83                         prev_elem = next_elem->prev;
84                 }
85         }
86
87         /* insert new element */
88         elem->prev = prev_elem;
89         elem->next = next_elem;
90         if (prev_elem)
91                 prev_elem->next = elem;
92         if (next_elem)
93                 next_elem->prev = elem;
94 }
95
96 /*
97  * calculate the starting point of where data of the requested size
98  * and alignment would fit in the current element. If the data doesn't
99  * fit, return NULL.
100  */
101 static void *
102 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
103                 size_t bound)
104 {
105         const size_t bmask = ~(bound - 1);
106         uintptr_t end_pt = (uintptr_t)elem +
107                         elem->size - MALLOC_ELEM_TRAILER_LEN;
108         uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size), align);
109         uintptr_t new_elem_start;
110
111         /* check boundary */
112         if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
113                 end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
114                 new_data_start = RTE_ALIGN_FLOOR((end_pt - size), align);
115                 end_pt = new_data_start + size;
116                 if (((end_pt - 1) & bmask) != (new_data_start & bmask))
117                         return NULL;
118         }
119
120         new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
121
122         /* if the new start point is before the exist start, it won't fit */
123         return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
124 }
125
126 /*
127  * use elem_start_pt to determine if we get meet the size and
128  * alignment request from the current element
129  */
130 int
131 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,     unsigned align,
132                 size_t bound)
133 {
134         return elem_start_pt(elem, size, align, bound) != NULL;
135 }
136
137 /*
138  * split an existing element into two smaller elements at the given
139  * split_pt parameter.
140  */
141 static void
142 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
143 {
144         struct malloc_elem *next_elem = elem->next;
145         const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
146         const size_t new_elem_size = elem->size - old_elem_size;
147
148         malloc_elem_init(split_pt, elem->heap, elem->ms, new_elem_size);
149         split_pt->prev = elem;
150         split_pt->next = next_elem;
151         if (next_elem)
152                 next_elem->prev = split_pt;
153         else
154                 elem->heap->last = split_pt;
155         elem->next = split_pt;
156         elem->size = old_elem_size;
157         set_trailer(elem);
158 }
159
160 /*
161  * our malloc heap is a doubly linked list, so doubly remove our element.
162  */
163 static void __rte_unused
164 remove_elem(struct malloc_elem *elem)
165 {
166         struct malloc_elem *next, *prev;
167         next = elem->next;
168         prev = elem->prev;
169
170         if (next)
171                 next->prev = prev;
172         else
173                 elem->heap->last = prev;
174         if (prev)
175                 prev->next = next;
176         else
177                 elem->heap->first = next;
178
179         elem->prev = NULL;
180         elem->next = NULL;
181 }
182
183 static int
184 next_elem_is_adjacent(struct malloc_elem *elem)
185 {
186         return elem->next == RTE_PTR_ADD(elem, elem->size);
187 }
188
189 static int
190 prev_elem_is_adjacent(struct malloc_elem *elem)
191 {
192         return elem == RTE_PTR_ADD(elem->prev, elem->prev->size);
193 }
194
195 /*
196  * Given an element size, compute its freelist index.
197  * We free an element into the freelist containing similarly-sized elements.
198  * We try to allocate elements starting with the freelist containing
199  * similarly-sized elements, and if necessary, we search freelists
200  * containing larger elements.
201  *
202  * Example element size ranges for a heap with five free lists:
203  *   heap->free_head[0] - (0   , 2^8]
204  *   heap->free_head[1] - (2^8 , 2^10]
205  *   heap->free_head[2] - (2^10 ,2^12]
206  *   heap->free_head[3] - (2^12, 2^14]
207  *   heap->free_head[4] - (2^14, MAX_SIZE]
208  */
209 size_t
210 malloc_elem_free_list_index(size_t size)
211 {
212 #define MALLOC_MINSIZE_LOG2   8
213 #define MALLOC_LOG2_INCREMENT 2
214
215         size_t log2;
216         size_t index;
217
218         if (size <= (1UL << MALLOC_MINSIZE_LOG2))
219                 return 0;
220
221         /* Find next power of 2 >= size. */
222         log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
223
224         /* Compute freelist index, based on log2(size). */
225         index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
226                 MALLOC_LOG2_INCREMENT;
227
228         return index <= RTE_HEAP_NUM_FREELISTS-1?
229                 index: RTE_HEAP_NUM_FREELISTS-1;
230 }
231
232 /*
233  * Add the specified element to its heap's free list.
234  */
235 void
236 malloc_elem_free_list_insert(struct malloc_elem *elem)
237 {
238         size_t idx;
239
240         idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
241         elem->state = ELEM_FREE;
242         LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
243 }
244
245 /*
246  * Remove the specified element from its heap's free list.
247  */
248 void
249 malloc_elem_free_list_remove(struct malloc_elem *elem)
250 {
251         LIST_REMOVE(elem, free_list);
252 }
253
254 /*
255  * reserve a block of data in an existing malloc_elem. If the malloc_elem
256  * is much larger than the data block requested, we split the element in two.
257  * This function is only called from malloc_heap_alloc so parameter checking
258  * is not done here, as it's done there previously.
259  */
260 struct malloc_elem *
261 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
262                 size_t bound)
263 {
264         struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound);
265         const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
266         const size_t trailer_size = elem->size - old_elem_size - size -
267                 MALLOC_ELEM_OVERHEAD;
268
269         malloc_elem_free_list_remove(elem);
270
271         if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
272                 /* split it, too much free space after elem */
273                 struct malloc_elem *new_free_elem =
274                                 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
275
276                 split_elem(elem, new_free_elem);
277                 malloc_elem_free_list_insert(new_free_elem);
278
279                 if (elem == elem->heap->last)
280                         elem->heap->last = new_free_elem;
281         }
282
283         if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
284                 /* don't split it, pad the element instead */
285                 elem->state = ELEM_BUSY;
286                 elem->pad = old_elem_size;
287
288                 /* put a dummy header in padding, to point to real element header */
289                 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
290                                      * is cache-line aligned */
291                         new_elem->pad = elem->pad;
292                         new_elem->state = ELEM_PAD;
293                         new_elem->size = elem->size - elem->pad;
294                         set_header(new_elem);
295                 }
296
297                 return new_elem;
298         }
299
300         /* we are going to split the element in two. The original element
301          * remains free, and the new element is the one allocated.
302          * Re-insert original element, in case its new size makes it
303          * belong on a different list.
304          */
305         split_elem(elem, new_elem);
306         new_elem->state = ELEM_BUSY;
307         malloc_elem_free_list_insert(elem);
308
309         return new_elem;
310 }
311
312 /*
313  * join two struct malloc_elem together. elem1 and elem2 must
314  * be contiguous in memory.
315  */
316 static inline void
317 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
318 {
319         struct malloc_elem *next = elem2->next;
320         elem1->size += elem2->size;
321         if (next)
322                 next->prev = elem1;
323         else
324                 elem1->heap->last = elem1;
325         elem1->next = next;
326 }
327
328 struct malloc_elem *
329 malloc_elem_join_adjacent_free(struct malloc_elem *elem)
330 {
331         /*
332          * check if next element exists, is adjacent and is free, if so join
333          * with it, need to remove from free list.
334          */
335         if (elem->next != NULL && elem->next->state == ELEM_FREE &&
336                         next_elem_is_adjacent(elem)) {
337                 void *erase;
338
339                 /* we will want to erase the trailer and header */
340                 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
341
342                 /* remove from free list, join to this one */
343                 malloc_elem_free_list_remove(elem->next);
344                 join_elem(elem, elem->next);
345
346                 /* erase header and trailer */
347                 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
348         }
349
350         /*
351          * check if prev element exists, is adjacent and is free, if so join
352          * with it, need to remove from free list.
353          */
354         if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
355                         prev_elem_is_adjacent(elem)) {
356                 struct malloc_elem *new_elem;
357                 void *erase;
358
359                 /* we will want to erase trailer and header */
360                 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
361
362                 /* remove from free list, join to this one */
363                 malloc_elem_free_list_remove(elem->prev);
364
365                 new_elem = elem->prev;
366                 join_elem(new_elem, elem);
367
368                 /* erase header and trailer */
369                 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
370
371                 elem = new_elem;
372         }
373
374         return elem;
375 }
376
377 /*
378  * free a malloc_elem block by adding it to the free list. If the
379  * blocks either immediately before or immediately after newly freed block
380  * are also free, the blocks are merged together.
381  */
382 int
383 malloc_elem_free(struct malloc_elem *elem)
384 {
385         void *ptr;
386         size_t data_len;
387
388         ptr = RTE_PTR_ADD(elem, sizeof(*elem));
389         data_len = elem->size - MALLOC_ELEM_OVERHEAD;
390
391         elem = malloc_elem_join_adjacent_free(elem);
392
393         malloc_elem_free_list_insert(elem);
394
395         /* decrease heap's count of allocated elements */
396         elem->heap->alloc_count--;
397
398         memset(ptr, 0, data_len);
399
400         return 0;
401 }
402
403 /*
404  * attempt to resize a malloc_elem by expanding into any free space
405  * immediately after it in memory.
406  */
407 int
408 malloc_elem_resize(struct malloc_elem *elem, size_t size)
409 {
410         const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
411
412         /* if we request a smaller size, then always return ok */
413         if (elem->size >= new_size)
414                 return 0;
415
416         /* check if there is a next element, it's free and adjacent */
417         if (!elem->next || elem->next->state != ELEM_FREE ||
418                         !next_elem_is_adjacent(elem))
419                 return -1;
420         if (elem->size + elem->next->size < new_size)
421                 return -1;
422
423         /* we now know the element fits, so remove from free list,
424          * join the two
425          */
426         malloc_elem_free_list_remove(elem->next);
427         join_elem(elem, elem->next);
428
429         if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
430                 /* now we have a big block together. Lets cut it down a bit, by splitting */
431                 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
432                 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
433                 split_elem(elem, split_pt);
434                 malloc_elem_free_list_insert(split_pt);
435         }
436         return 0;
437 }
438
439 static inline const char *
440 elem_state_to_str(enum elem_state state)
441 {
442         switch (state) {
443         case ELEM_PAD:
444                 return "PAD";
445         case ELEM_BUSY:
446                 return "BUSY";
447         case ELEM_FREE:
448                 return "FREE";
449         }
450         return "ERROR";
451 }
452
453 void
454 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
455 {
456         fprintf(f, "Malloc element at %p (%s)\n", elem,
457                         elem_state_to_str(elem->state));
458         fprintf(f, "  len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
459         fprintf(f, "  prev: %p next: %p\n", elem->prev, elem->next);
460 }