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 end_pt = new_data_start + size;
73 if (((end_pt - 1) & bmask) != (new_data_start & bmask))
77 new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
79 /* if the new start point is before the exist start, it won't fit */
80 return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
84 * use elem_start_pt to determine if we get meet the size and
85 * alignment request from the current element
88 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align,
91 return elem_start_pt(elem, size, align, bound) != NULL;
95 * split an existing element into two smaller elements at the given
99 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
101 struct malloc_elem *next_elem = RTE_PTR_ADD(elem, elem->size);
102 const size_t old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
103 const size_t new_elem_size = elem->size - old_elem_size;
105 malloc_elem_init(split_pt, elem->heap, elem->ms, new_elem_size);
106 split_pt->prev = elem;
107 next_elem->prev = split_pt;
108 elem->size = old_elem_size;
113 * Given an element size, compute its freelist index.
114 * We free an element into the freelist containing similarly-sized elements.
115 * We try to allocate elements starting with the freelist containing
116 * similarly-sized elements, and if necessary, we search freelists
117 * containing larger elements.
119 * Example element size ranges for a heap with five free lists:
120 * heap->free_head[0] - (0 , 2^8]
121 * heap->free_head[1] - (2^8 , 2^10]
122 * heap->free_head[2] - (2^10 ,2^12]
123 * heap->free_head[3] - (2^12, 2^14]
124 * heap->free_head[4] - (2^14, MAX_SIZE]
127 malloc_elem_free_list_index(size_t size)
129 #define MALLOC_MINSIZE_LOG2 8
130 #define MALLOC_LOG2_INCREMENT 2
135 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
138 /* Find next power of 2 >= size. */
139 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
141 /* Compute freelist index, based on log2(size). */
142 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
143 MALLOC_LOG2_INCREMENT;
145 return index <= RTE_HEAP_NUM_FREELISTS-1?
146 index: RTE_HEAP_NUM_FREELISTS-1;
150 * Add the specified element to its heap's free list.
153 malloc_elem_free_list_insert(struct malloc_elem *elem)
157 idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
158 elem->state = ELEM_FREE;
159 LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
163 * Remove the specified element from its heap's free list.
166 elem_free_list_remove(struct malloc_elem *elem)
168 LIST_REMOVE(elem, free_list);
172 * reserve a block of data in an existing malloc_elem. If the malloc_elem
173 * is much larger than the data block requested, we split the element in two.
174 * This function is only called from malloc_heap_alloc so parameter checking
175 * is not done here, as it's done there previously.
178 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align,
181 struct malloc_elem *new_elem = elem_start_pt(elem, size, align, bound);
182 const size_t old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
183 const size_t trailer_size = elem->size - old_elem_size - size -
184 MALLOC_ELEM_OVERHEAD;
186 elem_free_list_remove(elem);
188 if (trailer_size > MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
189 /* split it, too much free space after elem */
190 struct malloc_elem *new_free_elem =
191 RTE_PTR_ADD(new_elem, size + MALLOC_ELEM_OVERHEAD);
193 split_elem(elem, new_free_elem);
194 malloc_elem_free_list_insert(new_free_elem);
197 if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE) {
198 /* don't split it, pad the element instead */
199 elem->state = ELEM_BUSY;
200 elem->pad = old_elem_size;
202 /* put a dummy header in padding, to point to real element header */
203 if (elem->pad > 0) { /* pad will be at least 64-bytes, as everything
204 * is cache-line aligned */
205 new_elem->pad = elem->pad;
206 new_elem->state = ELEM_PAD;
207 new_elem->size = elem->size - elem->pad;
208 set_header(new_elem);
214 /* we are going to split the element in two. The original element
215 * remains free, and the new element is the one allocated.
216 * Re-insert original element, in case its new size makes it
217 * belong on a different list.
219 split_elem(elem, new_elem);
220 new_elem->state = ELEM_BUSY;
221 malloc_elem_free_list_insert(elem);
227 * join two struct malloc_elem together. elem1 and elem2 must
228 * be contiguous in memory.
231 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
233 struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
234 elem1->size += elem2->size;
239 * free a malloc_elem block by adding it to the free list. If the
240 * blocks either immediately before or immediately after newly freed block
241 * are also free, the blocks are merged together.
244 malloc_elem_free(struct malloc_elem *elem)
246 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
249 rte_spinlock_lock(&(elem->heap->lock));
250 size_t sz = elem->size - sizeof(*elem) - MALLOC_ELEM_TRAILER_LEN;
251 uint8_t *ptr = (uint8_t *)&elem[1];
252 struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
253 if (next->state == ELEM_FREE){
254 /* remove from free list, join to this one */
255 elem_free_list_remove(next);
256 join_elem(elem, next);
257 sz += (sizeof(*elem) + MALLOC_ELEM_TRAILER_LEN);
260 /* check if previous element is free, if so join with it and return,
261 * need to re-insert in free list, as that element's size is changing
263 if (elem->prev != NULL && elem->prev->state == ELEM_FREE) {
264 elem_free_list_remove(elem->prev);
265 join_elem(elem->prev, elem);
266 sz += (sizeof(*elem) + MALLOC_ELEM_TRAILER_LEN);
267 ptr -= (sizeof(*elem) + MALLOC_ELEM_TRAILER_LEN);
270 malloc_elem_free_list_insert(elem);
272 /* decrease heap's count of allocated elements */
273 elem->heap->alloc_count--;
277 rte_spinlock_unlock(&(elem->heap->lock));
283 * attempt to resize a malloc_elem by expanding into any free space
284 * immediately after it in memory.
287 malloc_elem_resize(struct malloc_elem *elem, size_t size)
289 const size_t new_size = size + elem->pad + MALLOC_ELEM_OVERHEAD;
290 /* if we request a smaller size, then always return ok */
291 if (elem->size >= new_size)
294 struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
295 rte_spinlock_lock(&elem->heap->lock);
296 if (next ->state != ELEM_FREE)
298 if (elem->size + next->size < new_size)
301 /* we now know the element fits, so remove from free list,
304 elem_free_list_remove(next);
305 join_elem(elem, next);
307 if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD) {
308 /* now we have a big block together. Lets cut it down a bit, by splitting */
309 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
310 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
311 split_elem(elem, split_pt);
312 malloc_elem_free_list_insert(split_pt);
314 rte_spinlock_unlock(&elem->heap->lock);
318 rte_spinlock_unlock(&elem->heap->lock);