1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_memory.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>
20 #include "malloc_elem.h"
21 #include "malloc_heap.h"
23 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
26 * Initialize a general malloc_elem header structure
29 malloc_elem_init(struct malloc_elem *elem,
30 struct malloc_heap *heap, const struct rte_memseg *ms, size_t size)
36 memset(&elem->free_list, 0, sizeof(elem->free_list));
37 elem->state = ELEM_FREE;
45 malloc_elem_insert(struct malloc_elem *elem)
47 struct malloc_elem *prev_elem, *next_elem;
48 struct malloc_heap *heap = elem->heap;
50 if (heap->first == NULL && heap->last == NULL) {
56 } else if (elem < heap->first) {
57 /* if lower than start */
59 next_elem = heap->first;
61 } else if (elem > heap->last) {
62 /* if higher than end */
63 prev_elem = heap->last;
67 /* the new memory is somewhere inbetween start and end */
68 uint64_t dist_from_start, dist_from_end;
70 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
71 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
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;
80 next_elem = heap->last;
81 while (next_elem->prev > elem)
82 next_elem = next_elem->prev;
83 prev_elem = next_elem->prev;
87 /* insert new element */
88 elem->prev = prev_elem;
89 elem->next = next_elem;
91 prev_elem->next = elem;
93 next_elem->prev = elem;
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
102 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
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;
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))
120 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
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;
127 * use elem_start_pt to determine if we get meet the size and
128 * alignment request from the current element
131 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
134 return elem_start_pt(elem, size, align, bound) != NULL;
138 * split an existing element into two smaller elements at the given
139 * split_pt parameter.
142 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
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;
148 malloc_elem_init(split_pt, elem->heap, elem->ms, new_elem_size);
149 split_pt->prev = elem;
150 split_pt->next = next_elem;
152 next_elem->prev = split_pt;
154 elem->heap->last = split_pt;
155 elem->next = split_pt;
156 elem->size = old_elem_size;
161 * our malloc heap is a doubly linked list, so doubly remove our element.
163 static void __rte_unused
164 remove_elem(struct malloc_elem *elem)
166 struct malloc_elem *next, *prev;
173 elem->heap->last = prev;
177 elem->heap->first = next;
184 next_elem_is_adjacent(struct malloc_elem *elem)
186 return elem->next == RTE_PTR_ADD(elem, elem->size);
190 prev_elem_is_adjacent(struct malloc_elem *elem)
192 return elem == RTE_PTR_ADD(elem->prev, elem->prev->size);
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.
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]
210 malloc_elem_free_list_index(size_t size)
212 #define MALLOC_MINSIZE_LOG2 8
213 #define MALLOC_LOG2_INCREMENT 2
218 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
221 /* Find next power of 2 >= size. */
222 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
224 /* Compute freelist index, based on log2(size). */
225 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
226 MALLOC_LOG2_INCREMENT;
228 return index <= RTE_HEAP_NUM_FREELISTS-1?
229 index: RTE_HEAP_NUM_FREELISTS-1;
233 * Add the specified element to its heap's free list.
236 malloc_elem_free_list_insert(struct malloc_elem *elem)
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);
246 * Remove the specified element from its heap's free list.
249 elem_free_list_remove(struct malloc_elem *elem)
251 LIST_REMOVE(elem, free_list);
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.
261 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
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;
269 elem_free_list_remove(elem);
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);
276 split_elem(elem, new_free_elem);
277 malloc_elem_free_list_insert(new_free_elem);
279 if (elem == elem->heap->last)
280 elem->heap->last = new_free_elem;
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;
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);
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.
305 split_elem(elem, new_elem);
306 new_elem->state = ELEM_BUSY;
307 malloc_elem_free_list_insert(elem);
313 * join two struct malloc_elem together. elem1 and elem2 must
314 * be contiguous in memory.
317 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
319 struct malloc_elem *next = elem2->next;
320 elem1->size += elem2->size;
324 elem1->heap->last = elem1;
329 malloc_elem_join_adjacent_free(struct malloc_elem *elem)
332 * check if next element exists, is adjacent and is free, if so join
333 * with it, need to remove from free list.
335 if (elem->next != NULL && elem->next->state == ELEM_FREE &&
336 next_elem_is_adjacent(elem)) {
339 /* we will want to erase the trailer and header */
340 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
342 /* remove from free list, join to this one */
343 elem_free_list_remove(elem->next);
344 join_elem(elem, elem->next);
346 /* erase header and trailer */
347 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
351 * check if prev element exists, is adjacent and is free, if so join
352 * with it, need to remove from free list.
354 if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
355 prev_elem_is_adjacent(elem)) {
356 struct malloc_elem *new_elem;
359 /* we will want to erase trailer and header */
360 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
362 /* remove from free list, join to this one */
363 elem_free_list_remove(elem->prev);
365 new_elem = elem->prev;
366 join_elem(new_elem, elem);
368 /* erase header and trailer */
369 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
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.
383 malloc_elem_free(struct malloc_elem *elem)
388 ptr = RTE_PTR_ADD(elem, sizeof(*elem));
389 data_len = elem->size - MALLOC_ELEM_OVERHEAD;
391 elem = malloc_elem_join_adjacent_free(elem);
393 malloc_elem_free_list_insert(elem);
395 /* decrease heap's count of allocated elements */
396 elem->heap->alloc_count--;
398 memset(ptr, 0, data_len);
404 * attempt to resize a malloc_elem by expanding into any free space
405 * immediately after it in memory.
408 malloc_elem_resize(struct malloc_elem *elem, size_t size)
410 const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
412 /* if we request a smaller size, then always return ok */
413 if (elem->size >= new_size)
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))
420 if (elem->size + elem->next->size < new_size)
423 /* we now know the element fits, so remove from free list,
426 elem_free_list_remove(elem->next);
427 join_elem(elem, elem->next);
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);
439 static inline const char *
440 elem_state_to_str(enum elem_state state)
454 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
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);