1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_memory.h>
14 #include <rte_launch.h>
15 #include <rte_per_lcore.h>
16 #include <rte_lcore.h>
17 #include <rte_debug.h>
18 #include <rte_common.h>
19 #include <rte_spinlock.h>
21 #include "eal_memalloc.h"
22 #include "malloc_elem.h"
23 #include "malloc_heap.h"
26 * Initialize a general malloc_elem header structure
29 malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap,
30 struct rte_memseg_list *msl, 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 /* first and last elements must be both NULL or both non-NULL */
51 if ((heap->first == NULL) != (heap->last == NULL)) {
52 RTE_LOG(ERR, EAL, "Heap is probably corrupt\n");
56 if (heap->first == NULL && heap->last == NULL) {
62 } else if (elem < heap->first) {
63 /* if lower than start */
65 next_elem = heap->first;
67 } else if (elem > heap->last) {
68 /* if higher than end */
69 prev_elem = heap->last;
73 /* the new memory is somewhere inbetween start and end */
74 uint64_t dist_from_start, dist_from_end;
76 dist_from_end = RTE_PTR_DIFF(heap->last, elem);
77 dist_from_start = RTE_PTR_DIFF(elem, heap->first);
79 /* check which is closer, and find closest list entries */
80 if (dist_from_start < dist_from_end) {
81 prev_elem = heap->first;
82 while (prev_elem->next < elem)
83 prev_elem = prev_elem->next;
84 next_elem = prev_elem->next;
86 next_elem = heap->last;
87 while (next_elem->prev > elem)
88 next_elem = next_elem->prev;
89 prev_elem = next_elem->prev;
93 /* insert new element */
94 elem->prev = prev_elem;
95 elem->next = next_elem;
97 prev_elem->next = elem;
99 next_elem->prev = elem;
103 * Attempt to find enough physically contiguous memory in this block to store
104 * our data. Assume that element has at least enough space to fit in the data,
105 * so we just check the page addresses.
108 elem_check_phys_contig(const struct rte_memseg_list *msl,
109 void *start, size_t size)
111 return eal_memalloc_is_contig(msl, start, size);
115 * calculate the starting point of where data of the requested size
116 * and alignment would fit in the current element. If the data doesn't
120 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
121 size_t bound, bool contig)
123 size_t elem_size = elem->size;
126 * we're allocating from the end, so adjust the size of element by
129 while (elem_size >= size) {
130 const size_t bmask = ~(bound - 1);
131 uintptr_t end_pt = (uintptr_t)elem +
132 elem_size - MALLOC_ELEM_TRAILER_LEN;
133 uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
135 uintptr_t new_elem_start;
138 if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) {
139 end_pt = RTE_ALIGN_FLOOR(end_pt, bound);
140 new_data_start = RTE_ALIGN_FLOOR((end_pt - size),
142 end_pt = new_data_start + size;
144 if (((end_pt - 1) & bmask) != (new_data_start & bmask))
148 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
150 /* if the new start point is before the exist start,
153 if (new_elem_start < (uintptr_t)elem)
157 size_t new_data_size = end_pt - new_data_start;
160 * if physical contiguousness was requested and we
161 * couldn't fit all data into one physically contiguous
162 * block, try again with lower addresses.
164 if (!elem_check_phys_contig(elem->msl,
165 (void *)new_data_start,
171 return (void *)new_elem_start;
177 * use elem_start_pt to determine if we get meet the size and
178 * alignment request from the current element
181 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
182 size_t bound, bool contig)
184 return elem_start_pt(elem, size, align, bound, contig) != NULL;
188 * split an existing element into two smaller elements at the given
189 * split_pt parameter.
192 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
194 struct malloc_elem *next_elem = elem->next;
195 const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
196 const size_t new_elem_size = elem->size - old_elem_size;
198 malloc_elem_init(split_pt, elem->heap, elem->msl, new_elem_size);
199 split_pt->prev = elem;
200 split_pt->next = next_elem;
202 next_elem->prev = split_pt;
204 elem->heap->last = split_pt;
205 elem->next = split_pt;
206 elem->size = old_elem_size;
211 * our malloc heap is a doubly linked list, so doubly remove our element.
213 static void __rte_unused
214 remove_elem(struct malloc_elem *elem)
216 struct malloc_elem *next, *prev;
223 elem->heap->last = prev;
227 elem->heap->first = next;
234 next_elem_is_adjacent(struct malloc_elem *elem)
236 return elem->next == RTE_PTR_ADD(elem, elem->size);
240 prev_elem_is_adjacent(struct malloc_elem *elem)
242 return elem == RTE_PTR_ADD(elem->prev, elem->prev->size);
246 * Given an element size, compute its freelist index.
247 * We free an element into the freelist containing similarly-sized elements.
248 * We try to allocate elements starting with the freelist containing
249 * similarly-sized elements, and if necessary, we search freelists
250 * containing larger elements.
252 * Example element size ranges for a heap with five free lists:
253 * heap->free_head[0] - (0 , 2^8]
254 * heap->free_head[1] - (2^8 , 2^10]
255 * heap->free_head[2] - (2^10 ,2^12]
256 * heap->free_head[3] - (2^12, 2^14]
257 * heap->free_head[4] - (2^14, MAX_SIZE]
260 malloc_elem_free_list_index(size_t size)
262 #define MALLOC_MINSIZE_LOG2 8
263 #define MALLOC_LOG2_INCREMENT 2
268 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
271 /* Find next power of 2 >= size. */
272 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
274 /* Compute freelist index, based on log2(size). */
275 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
276 MALLOC_LOG2_INCREMENT;
278 return index <= RTE_HEAP_NUM_FREELISTS-1?
279 index: RTE_HEAP_NUM_FREELISTS-1;
283 * Add the specified element to its heap's free list.
286 malloc_elem_free_list_insert(struct malloc_elem *elem)
290 idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
291 elem->state = ELEM_FREE;
292 LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
296 * Remove the specified element from its heap's free list.
299 malloc_elem_free_list_remove(struct malloc_elem *elem)
301 LIST_REMOVE(elem, free_list);
305 * reserve a block of data in an existing malloc_elem. If the malloc_elem
306 * is much larger than the data block requested, we split the element in two.
307 * This function is only called from malloc_heap_alloc so parameter checking
308 * is not done here, as it's done there previously.
311 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
312 size_t bound, bool contig)
314 struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound,
316 const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
317 const size_t trailer_size = elem->size - old_elem_size - size -
318 MALLOC_ELEM_OVERHEAD;
320 malloc_elem_free_list_remove(elem);
322 if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
323 /* split it, too much free space after elem */
324 struct malloc_elem *new_free_elem =
325 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
327 split_elem(elem, new_free_elem);
328 malloc_elem_free_list_insert(new_free_elem);
330 if (elem == elem->heap->last)
331 elem->heap->last = new_free_elem;
334 if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
335 /* don't split it, pad the element instead */
336 elem->state = ELEM_BUSY;
337 elem->pad = old_elem_size;
339 /* put a dummy header in padding, to point to real element header */
340 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
341 * is cache-line aligned */
342 new_elem->pad = elem->pad;
343 new_elem->state = ELEM_PAD;
344 new_elem->size = elem->size - elem->pad;
345 set_header(new_elem);
351 /* we are going to split the element in two. The original element
352 * remains free, and the new element is the one allocated.
353 * Re-insert original element, in case its new size makes it
354 * belong on a different list.
356 split_elem(elem, new_elem);
357 new_elem->state = ELEM_BUSY;
358 malloc_elem_free_list_insert(elem);
364 * join two struct malloc_elem together. elem1 and elem2 must
365 * be contiguous in memory.
368 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
370 struct malloc_elem *next = elem2->next;
371 elem1->size += elem2->size;
375 elem1->heap->last = elem1;
380 malloc_elem_join_adjacent_free(struct malloc_elem *elem)
383 * check if next element exists, is adjacent and is free, if so join
384 * with it, need to remove from free list.
386 if (elem->next != NULL && elem->next->state == ELEM_FREE &&
387 next_elem_is_adjacent(elem)) {
390 /* we will want to erase the trailer and header */
391 erase = RTE_PTR_SUB(elem->next, MALLOC_ELEM_TRAILER_LEN);
393 /* remove from free list, join to this one */
394 malloc_elem_free_list_remove(elem->next);
395 join_elem(elem, elem->next);
397 /* erase header and trailer */
398 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
402 * check if prev element exists, is adjacent and is free, if so join
403 * with it, need to remove from free list.
405 if (elem->prev != NULL && elem->prev->state == ELEM_FREE &&
406 prev_elem_is_adjacent(elem)) {
407 struct malloc_elem *new_elem;
410 /* we will want to erase trailer and header */
411 erase = RTE_PTR_SUB(elem, MALLOC_ELEM_TRAILER_LEN);
413 /* remove from free list, join to this one */
414 malloc_elem_free_list_remove(elem->prev);
416 new_elem = elem->prev;
417 join_elem(new_elem, elem);
419 /* erase header and trailer */
420 memset(erase, 0, MALLOC_ELEM_OVERHEAD);
429 * free a malloc_elem block by adding it to the free list. If the
430 * blocks either immediately before or immediately after newly freed block
431 * are also free, the blocks are merged together.
434 malloc_elem_free(struct malloc_elem *elem)
439 ptr = RTE_PTR_ADD(elem, sizeof(*elem));
440 data_len = elem->size - MALLOC_ELEM_OVERHEAD;
442 elem = malloc_elem_join_adjacent_free(elem);
444 malloc_elem_free_list_insert(elem);
448 /* decrease heap's count of allocated elements */
449 elem->heap->alloc_count--;
451 memset(ptr, 0, data_len);
456 /* assume all checks were already done */
458 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len)
460 struct malloc_elem *hide_start, *hide_end, *prev, *next;
461 size_t len_before, len_after;
464 hide_end = RTE_PTR_ADD(start, len);
469 /* we cannot do anything with non-adjacent elements */
470 if (next && next_elem_is_adjacent(elem)) {
471 len_after = RTE_PTR_DIFF(next, hide_end);
472 if (len_after >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
474 split_elem(elem, hide_end);
476 malloc_elem_free_list_insert(hide_end);
477 } else if (len_after > 0) {
478 RTE_LOG(ERR, EAL, "Unaligned element, heap is probably corrupt\n");
483 /* we cannot do anything with non-adjacent elements */
484 if (prev && prev_elem_is_adjacent(elem)) {
485 len_before = RTE_PTR_DIFF(hide_start, elem);
486 if (len_before >= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
488 split_elem(elem, hide_start);
493 malloc_elem_free_list_insert(prev);
494 } else if (len_before > 0) {
495 RTE_LOG(ERR, EAL, "Unaligned element, heap is probably corrupt\n");
504 * attempt to resize a malloc_elem by expanding into any free space
505 * immediately after it in memory.
508 malloc_elem_resize(struct malloc_elem *elem, size_t size)
510 const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
512 /* if we request a smaller size, then always return ok */
513 if (elem->size >= new_size)
516 /* check if there is a next element, it's free and adjacent */
517 if (!elem->next || elem->next->state != ELEM_FREE ||
518 !next_elem_is_adjacent(elem))
520 if (elem->size + elem->next->size < new_size)
523 /* we now know the element fits, so remove from free list,
526 malloc_elem_free_list_remove(elem->next);
527 join_elem(elem, elem->next);
529 if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
530 /* now we have a big block together. Lets cut it down a bit, by splitting */
531 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
532 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
533 split_elem(elem, split_pt);
534 malloc_elem_free_list_insert(split_pt);
539 static inline const char *
540 elem_state_to_str(enum elem_state state)
554 malloc_elem_dump(const struct malloc_elem *elem, FILE *f)
556 fprintf(f, "Malloc element at %p (%s)\n", elem,
557 elem_state_to_str(elem->state));
558 fprintf(f, " len: 0x%zx pad: 0x%" PRIx32 "\n", elem->size, elem->pad);
559 fprintf(f, " prev: %p next: %p\n", elem->prev, elem->next);