first public release
[dpdk.git] / lib / librte_malloc / malloc_elem.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35 #include <stdint.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <sys/queue.h>
39
40 #include <rte_memory.h>
41 #include <rte_memzone.h>
42 #include <rte_tailq.h>
43 #include <rte_eal.h>
44 #include <rte_launch.h>
45 #include <rte_per_lcore.h>
46 #include <rte_lcore.h>
47 #include <rte_debug.h>
48 #include <rte_common.h>
49 #include <rte_spinlock.h>
50
51 #include "malloc_elem.h"
52 #include "malloc_heap.h"
53
54 #define MIN_DATA_SIZE (CACHE_LINE_SIZE * 2)
55
56 /*
57  * initialise a general malloc_elem header structure
58  */
59 void
60 malloc_elem_init(struct malloc_elem *elem,
61                 struct malloc_heap *heap, size_t size)
62 {
63         elem->heap = heap;
64         elem->prev = elem->next_free = NULL;
65         elem->state = ELEM_FREE;
66         elem->size = size;
67         elem->pad = 0;
68         set_header(elem);
69         set_trailer(elem);
70 }
71
72 /*
73  * initialise a dummy malloc_elem header for the end-of-memzone marker
74  */
75 void
76 malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev)
77 {
78         malloc_elem_init(elem, prev->heap, 0);
79         elem->prev = prev;
80         elem->state = ELEM_BUSY; /* mark busy so its never merged */
81 }
82
83 /*
84  * calculate the starting point of where data of the requested size
85  * and alignment would fit in the current element. If the data doesn't
86  * fit, return NULL.
87  */
88 static void *
89 elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align)
90 {
91         const uintptr_t end_pt = (uintptr_t)elem +
92                         elem->size - MALLOC_ELEM_TRAILER_LEN;
93         const uintptr_t new_data_start = rte_align_floor_int((end_pt - size),align);
94         const uintptr_t new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN;
95
96         /* if the new start point is before the exist start, it won't fit */
97         return (new_elem_start < (uintptr_t)elem) ? NULL : (void *)new_elem_start;
98 }
99
100 /*
101  * use elem_start_pt to determine if we get meet the size and
102  * alignment request from the current element
103  */
104 int
105 malloc_elem_can_hold(struct malloc_elem *elem, size_t size, unsigned align)
106 {
107         return elem_start_pt(elem, size, align) != NULL;
108 }
109
110 /*
111  * split an existing element into two smaller elements at the given
112  * split_pt parameter.
113  */
114 static void
115 split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt)
116 {
117         struct malloc_elem *next_elem = RTE_PTR_ADD(elem, elem->size);
118         const unsigned old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem;
119         const unsigned new_elem_size = elem->size - old_elem_size;
120
121         malloc_elem_init(split_pt, elem->heap, new_elem_size);
122         split_pt->prev = elem;
123         next_elem->prev = split_pt;
124         elem->size = old_elem_size;
125         set_trailer(elem);
126 }
127
128 /*
129  * reserve a block of data in an existing malloc_elem. If the malloc_elem
130  * is much larger than the data block requested, we split the element in two.
131  * This function is only called from malloc_heap_alloc so parameter checking
132  * is not done here, as it's done there previously.
133  */
134 struct malloc_elem *
135 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
136                 unsigned align, struct malloc_elem *prev_free)
137 {
138         struct malloc_elem *new_elem = elem_start_pt(elem, size, align);
139         const unsigned old_elem_size = (uintptr_t)new_elem - (uintptr_t)elem;
140
141         if (old_elem_size <= MALLOC_ELEM_OVERHEAD + MIN_DATA_SIZE){
142                 /* don't split it, pad the element instead */
143                 elem->state = ELEM_BUSY;
144                 elem->pad = old_elem_size;
145
146                 /* put a dummy header in padding, to point to real element header */
147                 if (elem->pad > 0){ /* pad will be at least 64-bytes, as everything
148                                      * is cache-line aligned */
149                         new_elem->pad = elem->pad;
150                         new_elem->state = ELEM_PAD;
151                         new_elem->size = elem->size - elem->pad;
152                         set_header(new_elem);
153                 }
154                 /* remove element from free list */
155                 if (prev_free == NULL)
156                         elem->heap->free_head = elem->next_free;
157                 else
158                         prev_free->next_free = elem->next_free;
159
160                 return new_elem;
161         }
162
163         /* we are going to split the element in two. The original element
164          * remains free, and the new element is the one allocated, so no free list
165          * changes need to be made.
166          */
167         split_elem(elem, new_elem);
168         new_elem->state = ELEM_BUSY;
169
170         return new_elem;
171 }
172
173 /*
174  * joing two struct malloc_elem together. elem1 and elem2 must
175  * be contiguous in memory.
176  */
177 static inline void
178 join_elem(struct malloc_elem *elem1, struct malloc_elem *elem2)
179 {
180         struct malloc_elem *next = RTE_PTR_ADD(elem2, elem2->size);
181         elem1->size += elem2->size;
182         next->prev = elem1;
183 }
184
185 /*
186  * scan the free list, and remove the request element from that
187  * free list. (Free list to scan is got from heap pointer in element)
188  */
189 static inline void
190 remove_from_free_list(struct malloc_elem *elem)
191 {
192         if (elem == elem->heap->free_head)
193                 elem->heap->free_head = elem->next_free;
194         else{
195                 struct malloc_elem *prev_free = elem->heap->free_head;
196                 while (prev_free && prev_free->next_free != elem)
197                         prev_free = prev_free->next_free;
198                 if (!prev_free)
199                         rte_panic("Corrupted free list\n");
200                 prev_free->next_free = elem->next_free;
201         }
202 }
203
204 /*
205  * free a malloc_elem block by adding it to the free list. If the
206  * blocks either immediately before or immediately after newly freed block
207  * are also free, the blocks are merged together.
208  */
209 int
210 malloc_elem_free(struct malloc_elem *elem)
211 {
212         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
213                 return -1;
214
215         rte_spinlock_lock(&(elem->heap->lock));
216         struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
217         if (next->state == ELEM_FREE){
218                 /* join to this one, and remove from free list */
219                 join_elem(elem, next);
220                 remove_from_free_list(next);
221         }
222
223         /* check if previous element is free, if so join with it and return,
224          * no need to update free list, as that element is already there
225          */
226         if (elem->prev != NULL && elem->prev->state == ELEM_FREE)
227                 join_elem(elem->prev, elem);
228         /* otherwise add ourselves to the free list */
229         else {
230                 elem->next_free = elem->heap->free_head;
231                 elem->heap->free_head = elem;
232                 elem->state = ELEM_FREE;
233                 elem->pad = 0;
234         }
235         rte_spinlock_unlock(&(elem->heap->lock));
236         return 0;
237 }
238
239 /*
240  * attempt to resize a malloc_elem by expanding into any free space
241  * immediately after it in memory.
242  */
243 int
244 malloc_elem_resize(struct malloc_elem *elem, size_t size)
245 {
246         const size_t new_size = size + MALLOC_ELEM_OVERHEAD;
247         /* if we request a smaller size, then always return ok */
248         const size_t current_size = elem->size - elem->pad;
249         if (current_size >= new_size)
250                 return 0;
251
252         struct malloc_elem *next = RTE_PTR_ADD(elem, elem->size);
253         rte_spinlock_lock(&elem->heap->lock);
254         if (next ->state != ELEM_FREE)
255                 goto err_return;
256         if (current_size + next->size < new_size)
257                 goto err_return;
258
259         /* we now know the element fits, so join the two, then remove from free
260          * list
261          */
262         join_elem(elem, next);
263         remove_from_free_list(next);
264
265         if (elem->size - new_size > MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD){
266                 /* now we have a big block together. Lets cut it down a bit, by splitting */
267                 struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size);
268                 split_pt = RTE_ALIGN_CEIL(split_pt, CACHE_LINE_SIZE);
269                 split_elem(elem, split_pt);
270                 split_pt->state = ELEM_FREE;
271                 split_pt->next_free = elem->heap->free_head;
272                 elem->heap->free_head = split_pt;
273         }
274         rte_spinlock_unlock(&elem->heap->lock);
275         return 0;
276
277 err_return:
278         rte_spinlock_unlock(&elem->heap->lock);
279         return -1;
280 }