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