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)
34 memset(&elem->free_list, 0, sizeof(elem->free_list));
35 elem->state = ELEM_FREE;
43 * Initialize a dummy malloc_elem header for the end-of-memseg marker
46 malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev)
48 malloc_elem_init(elem, prev->heap, prev->ms, 0);
50 elem->state = ELEM_BUSY; /* mark busy so its never merged */
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
59 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align,
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;
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))
76 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
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;
83 * use elem_start_pt to determine if we get meet the size and
84 * alignment request from the current element
87 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
90 return elem_start_pt(elem, size, align, bound) != NULL;
94 * split an existing element into two smaller elements at the given
98 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
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;
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;
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.
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]
126 malloc_elem_free_list_index(size_t size)
128 #define MALLOC_MINSIZE_LOG2 8
129 #define MALLOC_LOG2_INCREMENT 2
134 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
137 /* Find next power of 2 >= size. */
138 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
140 /* Compute freelist index, based on log2(size). */
141 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
142 MALLOC_LOG2_INCREMENT;
144 return index <= RTE_HEAP_NUM_FREELISTS-1?
145 index: RTE_HEAP_NUM_FREELISTS-1;
149 * Add the specified element to its heap's free list.
152 malloc_elem_free_list_insert(struct malloc_elem *elem)
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);
162 * Remove the specified element from its heap's free list.
165 elem_free_list_remove(struct malloc_elem *elem)
167 LIST_REMOVE(elem, free_list);
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.
177 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
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;
185 elem_free_list_remove(elem);
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);
192 split_elem(elem, new_free_elem);
193 malloc_elem_free_list_insert(new_free_elem);
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;
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);
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.
218 split_elem(elem, new_elem);
219 new_elem->state = ELEM_BUSY;
220 malloc_elem_free_list_insert(elem);
226 * join two struct malloc_elem together. elem1 and elem2 must
227 * be contiguous in memory.
230 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
232 struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
233 elem1->size += elem2->size;
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.
243 malloc_elem_free(struct malloc_elem *elem)
245 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
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);
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
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);
269 malloc_elem_free_list_insert(elem);
271 /* decrease heap's count of allocated elements */
272 elem->heap->alloc_count--;
276 rte_spinlock_unlock(&(elem->heap->lock));
282 * attempt to resize a malloc_elem by expanding into any free space
283 * immediately after it in memory.
286 malloc_elem_resize(struct malloc_elem *elem, size_t size)
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)
293 struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
294 rte_spinlock_lock(&elem->heap->lock);
295 if (next ->state != ELEM_FREE)
297 if (elem->size + next->size < new_size)
300 /* we now know the element fits, so remove from free list,
303 elem_free_list_remove(next);
304 join_elem(elem, next);
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);
313 rte_spinlock_unlock(&elem->heap->lock);
317 rte_spinlock_unlock(&elem->heap->lock);