1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_memory.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>
19 #include "malloc_elem.h"
20 #include "malloc_heap.h"
22 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
25 * Initialize a general malloc_elem header structure
28 malloc_elem_init(struct malloc_elem *elem,
29 struct malloc_heap *heap, const struct rte_memseg *ms, size_t size)
35 memset(&elem->free_list, 0, sizeof(elem->free_list));
36 elem->state = ELEM_FREE;
44 malloc_elem_insert(struct malloc_elem *elem)
46 struct malloc_elem *prev_elem, *next_elem;
47 struct malloc_heap *heap = elem->heap;
49 if (heap->first == NULL && heap->last == NULL) {
55 } else if (elem < heap->first) {
56 /* if lower than start */
58 next_elem = heap->first;
60 } else if (elem > heap->last) {
61 /* if higher than end */
62 prev_elem = heap->last;
66 /* the new memory is somewhere inbetween start and end */
67 uint64_t dist_from_start, dist_from_end;
69 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
70 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
72 /* check which is closer, and find closest list entries */
73 if (dist_from_start < dist_from_end) {
74 prev_elem = heap->first;
75 while (prev_elem->next < elem)
76 prev_elem = prev_elem->next;
77 next_elem = prev_elem->next;
79 next_elem = heap->last;
80 while (next_elem->prev > elem)
81 next_elem = next_elem->prev;
82 prev_elem = next_elem->prev;
86 /* insert new element */
87 elem->prev = prev_elem;
88 elem->next = next_elem;
90 prev_elem->next = elem;
92 next_elem->prev = elem;
96 * calculate the starting point of where data of the requested size
97 * and alignment would fit in the current element. If the data doesn't
101 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
104 const size_t bmask = ~(bound - 1);
105 uintptr_t end_pt = (uintptr_t)elem +
106 elem->size - MALLOC_ELEM_TRAILER_LEN;
107 uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size), align);
108 uintptr_t new_elem_start;
111 if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
112 end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
113 new_data_start = RTE_ALIGN_FLOOR((end_pt - size), align);
114 end_pt = new_data_start + size;
115 if (((end_pt - 1) & bmask) != (new_data_start & bmask))
119 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
121 /* if the new start point is before the exist start, it won't fit */
122 return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
126 * use elem_start_pt to determine if we get meet the size and
127 * alignment request from the current element
130 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
133 return elem_start_pt(elem, size, align, bound) != NULL;
137 * split an existing element into two smaller elements at the given
138 * split_pt parameter.
141 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
143 struct malloc_elem *next_elem = elem->next;
144 const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
145 const size_t new_elem_size = elem->size - old_elem_size;
147 malloc_elem_init(split_pt, elem->heap, elem->ms, new_elem_size);
148 split_pt->prev = elem;
149 split_pt->next = next_elem;
151 next_elem->prev = split_pt;
153 elem->heap->last = split_pt;
154 elem->next = split_pt;
155 elem->size = old_elem_size;
160 * our malloc heap is a doubly linked list, so doubly remove our element.
162 static void __rte_unused
163 remove_elem(struct malloc_elem *elem)
165 struct malloc_elem *next, *prev;
172 elem->heap->last = prev;
176 elem->heap->first = next;
183 next_elem_is_adjacent(struct malloc_elem *elem)
185 return elem->next == RTE_PTR_ADD(elem, elem->size);
189 prev_elem_is_adjacent(struct malloc_elem *elem)
191 return elem == RTE_PTR_ADD(elem->prev, elem->prev->size);
195 * Given an element size, compute its freelist index.
196 * We free an element into the freelist containing similarly-sized elements.
197 * We try to allocate elements starting with the freelist containing
198 * similarly-sized elements, and if necessary, we search freelists
199 * containing larger elements.
201 * Example element size ranges for a heap with five free lists:
202 * heap->free_head[0] - (0 , 2^8]
203 * heap->free_head[1] - (2^8 , 2^10]
204 * heap->free_head[2] - (2^10 ,2^12]
205 * heap->free_head[3] - (2^12, 2^14]
206 * heap->free_head[4] - (2^14, MAX_SIZE]
209 malloc_elem_free_list_index(size_t size)
211 #define MALLOC_MINSIZE_LOG2 8
212 #define MALLOC_LOG2_INCREMENT 2
217 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
220 /* Find next power of 2 >= size. */
221 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
223 /* Compute freelist index, based on log2(size). */
224 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
225 MALLOC_LOG2_INCREMENT;
227 return index <= RTE_HEAP_NUM_FREELISTS-1?
228 index: RTE_HEAP_NUM_FREELISTS-1;
232 * Add the specified element to its heap's free list.
235 malloc_elem_free_list_insert(struct malloc_elem *elem)
239 idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
240 elem->state = ELEM_FREE;
241 LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
245 * Remove the specified element from its heap's free list.
248 elem_free_list_remove(struct malloc_elem *elem)
250 LIST_REMOVE(elem, free_list);
254 * reserve a block of data in an existing malloc_elem. If the malloc_elem
255 * is much larger than the data block requested, we split the element in two.
256 * This function is only called from malloc_heap_alloc so parameter checking
257 * is not done here, as it's done there previously.
260 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
263 struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound);
264 const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
265 const size_t trailer_size = elem->size - old_elem_size - size -
266 MALLOC_ELEM_OVERHEAD;
268 elem_free_list_remove(elem);
270 if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
271 /* split it, too much free space after elem */
272 struct malloc_elem *new_free_elem =
273 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
275 split_elem(elem, new_free_elem);
276 malloc_elem_free_list_insert(new_free_elem);
278 if (elem == elem->heap->last)
279 elem->heap->last = new_free_elem;
282 if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
283 /* don't split it, pad the element instead */
284 elem->state = ELEM_BUSY;
285 elem->pad = old_elem_size;
287 /* put a dummy header in padding, to point to real element header */
288 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
289 * is cache-line aligned */
290 new_elem->pad = elem->pad;
291 new_elem->state = ELEM_PAD;
292 new_elem->size = elem->size - elem->pad;
293 set_header(new_elem);
299 /* we are going to split the element in two. The original element
300 * remains free, and the new element is the one allocated.
301 * Re-insert original element, in case its new size makes it
302 * belong on a different list.
304 split_elem(elem, new_elem);
305 new_elem->state = ELEM_BUSY;
306 malloc_elem_free_list_insert(elem);
312 * join two struct malloc_elem together. elem1 and elem2 must
313 * be contiguous in memory.
316 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
318 struct malloc_elem *next = elem2->next;
319 elem1->size += elem2->size;
323 elem1->heap->last = elem1;
327 static struct malloc_elem *
328 elem_join_adjacent_free(struct malloc_elem *elem)
331 * check if next element exists, is adjacent and is free, if so join
332 * with it, need to remove from free list.
334 if (elem->next != NULL && elem->next->state == ELEM_FREE &&
335 next_elem_is_adjacent(elem)) {
338 /* we will want to erase the trailer and header */
339 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
341 /* remove from free list, join to this one */
342 elem_free_list_remove(elem->next);
343 join_elem(elem, elem->next);
345 /* erase header and trailer */
346 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
350 * check if prev element exists, is adjacent and is free, if so join
351 * with it, need to remove from free list.
353 if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
354 prev_elem_is_adjacent(elem)) {
355 struct malloc_elem *new_elem;
358 /* we will want to erase trailer and header */
359 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
361 /* remove from free list, join to this one */
362 elem_free_list_remove(elem->prev);
364 new_elem = elem->prev;
365 join_elem(new_elem, elem);
367 /* erase header and trailer */
368 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
377 * free a malloc_elem block by adding it to the free list. If the
378 * blocks either immediately before or immediately after newly freed block
379 * are also free, the blocks are merged together.
382 malloc_elem_free(struct malloc_elem *elem)
387 ptr = RTE_PTR_ADD(elem, sizeof(*elem));
388 data_len = elem->size - MALLOC_ELEM_OVERHEAD;
390 elem = elem_join_adjacent_free(elem);
392 malloc_elem_free_list_insert(elem);
394 /* decrease heap's count of allocated elements */
395 elem->heap->alloc_count--;
397 memset(ptr, 0, data_len);
403 * attempt to resize a malloc_elem by expanding into any free space
404 * immediately after it in memory.
407 malloc_elem_resize(struct malloc_elem *elem, size_t size)
409 const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
411 /* if we request a smaller size, then always return ok */
412 if (elem->size >= new_size)
415 /* check if there is a next element, it's free and adjacent */
416 if (!elem->next || elem->next->state != ELEM_FREE ||
417 !next_elem_is_adjacent(elem))
419 if (elem->size + elem->next->size < new_size)
422 /* we now know the element fits, so remove from free list,
425 elem_free_list_remove(elem->next);
426 join_elem(elem, elem->next);
428 if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
429 /* now we have a big block together. Lets cut it down a bit, by splitting */
430 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
431 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
432 split_elem(elem, split_pt);
433 malloc_elem_free_list_insert(split_pt);