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