4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/queue.h>
39 #include <rte_memory.h>
40 #include <rte_memzone.h>
41 #include <rte_tailq.h>
43 #include <rte_launch.h>
44 #include <rte_per_lcore.h>
45 #include <rte_lcore.h>
46 #include <rte_debug.h>
47 #include <rte_common.h>
48 #include <rte_spinlock.h>
50 #include "malloc_elem.h"
51 #include "malloc_heap.h"
53 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
56 * initialise a general malloc_elem header structure
59 malloc_elem_init(struct malloc_elem *elem,
60 struct malloc_heap *heap, const struct rte_memzone *mz, size_t size)
65 memset(&elem->free_list, 0, sizeof(elem->free_list));
66 elem->state = ELEM_FREE;
74 * initialise a dummy malloc_elem header for the end-of-memzone marker
77 malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev)
79 malloc_elem_init(elem, prev->heap, prev->mz, 0);
81 elem->state = ELEM_BUSY; /* mark busy so its never merged */
85 * calculate the starting point of where data of the requested size
86 * and alignment would fit in the current element. If the data doesn't
90 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align)
92 const uintptr_t end_pt = (uintptr_t)elem +
93 elem->size - MALLOC_ELEM_TRAILER_LEN;
94 const uintptr_t new_data_start = rte_align_floor_int((end_pt - size),align);
95 const uintptr_t new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
97 /* if the new start point is before the exist start, it won't fit */
98 return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
102 * use elem_start_pt to determine if we get meet the size and
103 * alignment request from the current element
106 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align)
108 return elem_start_pt(elem, size, align) != NULL;
112 * split an existing element into two smaller elements at the given
113 * split_pt parameter.
116 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
118 struct malloc_elem *next_elem = RTE_PTR_ADD(elem, elem->size);
119 const unsigned old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
120 const unsigned new_elem_size = elem->size - old_elem_size;
122 malloc_elem_init(split_pt, elem->heap, elem->mz, new_elem_size);
123 split_pt->prev = elem;
124 next_elem->prev = split_pt;
125 elem->size = old_elem_size;
130 * Given an element size, compute its freelist index.
131 * We free an element into the freelist containing similarly-sized elements.
132 * We try to allocate elements starting with the freelist containing
133 * similarly-sized elements, and if necessary, we search freelists
134 * containing larger elements.
136 * Example element size ranges for a heap with five free lists:
137 * heap->free_head[0] - (0 , 2^8]
138 * heap->free_head[1] - (2^8 , 2^10]
139 * heap->free_head[2] - (2^10 ,2^12]
140 * heap->free_head[3] - (2^12, 2^14]
141 * heap->free_head[4] - (2^14, MAX_SIZE]
144 malloc_elem_free_list_index(size_t size)
146 #define MALLOC_MINSIZE_LOG2 8
147 #define MALLOC_LOG2_INCREMENT 2
152 if (size <= (1UL << MALLOC_MINSIZE_LOG2))
155 /* Find next power of 2 >= size. */
156 log2 = sizeof(size) * 8 - __builtin_clzl(size-1);
158 /* Compute freelist index, based on log2(size). */
159 index = (log2 - MALLOC_MINSIZE_LOG2 + MALLOC_LOG2_INCREMENT - 1) /
160 MALLOC_LOG2_INCREMENT;
162 return (index <= RTE_HEAP_NUM_FREELISTS-1?
163 index: RTE_HEAP_NUM_FREELISTS-1);
167 * Add the specified element to its heap's free list.
170 malloc_elem_free_list_insert(struct malloc_elem *elem)
172 size_t idx = malloc_elem_free_list_index(elem->size - MALLOC_ELEM_HEADER_LEN);
174 elem->state = ELEM_FREE;
175 LIST_INSERT_HEAD(&elem->heap->free_head[idx], elem, free_list);
179 * Remove the specified element from its heap's free list.
182 elem_free_list_remove(struct malloc_elem *elem)
184 LIST_REMOVE(elem, free_list);
188 * reserve a block of data in an existing malloc_elem. If the malloc_elem
189 * is much larger than the data block requested, we split the element in two.
190 * This function is only called from malloc_heap_alloc so parameter checking
191 * is not done here, as it's done there previously.
194 malloc_elem_alloc(struct malloc_elem *elem, size_t size, unsigned align)
196 struct malloc_elem *new_elem = elem_start_pt(elem, size, align);
197 const unsigned old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
199 if (old_elem_size < MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE){
200 /* don't split it, pad the element instead */
201 elem->state = ELEM_BUSY;
202 elem->pad = old_elem_size;
204 /* put a dummy header in padding, to point to real element header */
205 if (elem->pad > 0){ /* pad will be at least 64-bytes, as everything
206 * is cache-line aligned */
207 new_elem->pad = elem->pad;
208 new_elem->state = ELEM_PAD;
209 new_elem->size = elem->size - elem->pad;
210 set_header(new_elem);
212 /* remove element from free list */
213 elem_free_list_remove(elem);
218 /* we are going to split the element in two. The original element
219 * remains free, and the new element is the one allocated.
220 * Re-insert original element, in case its new size makes it
221 * belong on a different list.
223 elem_free_list_remove(elem);
224 split_elem(elem, new_elem);
225 new_elem->state = ELEM_BUSY;
226 malloc_elem_free_list_insert(elem);
232 * joing two struct malloc_elem together. elem1 and elem2 must
233 * be contiguous in memory.
236 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
238 struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
239 elem1->size += elem2->size;
244 * free a malloc_elem block by adding it to the free list. If the
245 * blocks either immediately before or immediately after newly freed block
246 * are also free, the blocks are merged together.
249 malloc_elem_free(struct malloc_elem *elem)
251 if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
254 rte_spinlock_lock(&(elem->heap->lock));
255 struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
256 if (next->state == ELEM_FREE){
257 /* remove from free list, join to this one */
258 elem_free_list_remove(next);
259 join_elem(elem, next);
262 /* check if previous element is free, if so join with it and return,
263 * need to re-insert in free list, as that element's size is changing
265 if (elem->prev != NULL && elem->prev->state == ELEM_FREE) {
266 elem_free_list_remove(elem->prev);
267 join_elem(elem->prev, elem);
268 malloc_elem_free_list_insert(elem->prev);
270 /* otherwise add ourselves to the free list */
272 malloc_elem_free_list_insert(elem);
275 /* decrease heap's count of allocated elements */
276 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 + MALLOC_ELEM_OVERHEAD;
290 /* if we request a smaller size, then always return ok */
291 const size_t current_size = elem->size - elem->pad;
292 if (current_size >= new_size)
295 struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
296 rte_spinlock_lock(&elem->heap->lock);
297 if (next ->state != ELEM_FREE)
299 if (current_size + next->size < new_size)
302 /* we now know the element fits, so remove from free list,
305 elem_free_list_remove(next);
306 join_elem(elem, next);
308 if (elem->size - new_size >= MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD){
309 /* now we have a big block together. Lets cut it down a bit, by splitting */
310 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
311 split_pt = RTE_PTR_ALIGN_CEIL(split_pt, RTE_CACHE_LINE_SIZE);
312 split_elem(elem, split_pt);
313 malloc_elem_free_list_insert(split_pt);
315 rte_spinlock_unlock(&elem->heap->lock);
319 rte_spinlock_unlock(&elem->heap->lock);