remove extra parentheses in return statement
[dpdk.git] / lib / librte_eal / common / malloc_elem.h
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
34 #ifndef MALLOC_ELEM_H_
35 #define MALLOC_ELEM_H_
36
37 #include <rte_memory.h>
38
39 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
40 struct malloc_heap;
41
42 enum elem_state {
43         ELEM_FREE = 0,
44         ELEM_BUSY,
45         ELEM_PAD  /* element is a padding-only header */
46 };
47
48 struct malloc_elem {
49         struct malloc_heap *heap;
50         struct malloc_elem *volatile prev;      /* points to prev elem in memseg */
51         LIST_ENTRY(malloc_elem) free_list;      /* list of free elements in heap */
52         const struct rte_memseg *ms;
53         volatile enum elem_state state;
54         uint32_t pad;
55         size_t size;
56 #ifdef RTE_LIBRTE_MALLOC_DEBUG
57         uint64_t header_cookie;         /* Cookie marking start of data */
58                                         /* trailer cookie at start + size */
59 #endif
60 } __rte_cache_aligned;
61
62 #ifndef RTE_LIBRTE_MALLOC_DEBUG
63 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
64
65 /* dummy function - just check if pointer is non-null */
66 static inline int
67 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
68
69 /* dummy function - no header if malloc_debug is not enabled */
70 static inline void
71 set_header(struct malloc_elem *elem __rte_unused){ }
72
73 /* dummy function - no trailer if malloc_debug is not enabled */
74 static inline void
75 set_trailer(struct malloc_elem *elem __rte_unused){ }
76
77
78 #else
79 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
80
81 #define MALLOC_HEADER_COOKIE   0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
83
84 /* define macros to make referencing the header and trailer cookies easier */
85 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
86                 elem->size - MALLOC_ELEM_TRAILER_LEN)))
87 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
88
89 static inline void
90 set_header(struct malloc_elem *elem)
91 {
92         if (elem != NULL)
93                 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
94 }
95
96 static inline void
97 set_trailer(struct malloc_elem *elem)
98 {
99         if (elem != NULL)
100                 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
101 }
102
103 /* check that the header and trailer cookies are set correctly */
104 static inline int
105 malloc_elem_cookies_ok(const struct malloc_elem *elem)
106 {
107         return elem != NULL &&
108                         MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
109                         MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
110 }
111
112 #endif
113
114 static const unsigned MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
115 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
116
117 /*
118  * Given a pointer to the start of a memory block returned by malloc, get
119  * the actual malloc_elem header for that block.
120  */
121 static inline struct malloc_elem *
122 malloc_elem_from_data(const void *data)
123 {
124         if (data == NULL)
125                 return NULL;
126
127         struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
128         if (!malloc_elem_cookies_ok(elem))
129                 return NULL;
130         return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
131 }
132
133 /*
134  * initialise a malloc_elem header
135  */
136 void
137 malloc_elem_init(struct malloc_elem *elem,
138                 struct malloc_heap *heap,
139                 const struct rte_memseg *ms,
140                 size_t size);
141
142 /*
143  * initialise a dummy malloc_elem header for the end-of-memseg marker
144  */
145 void
146 malloc_elem_mkend(struct malloc_elem *elem,
147                 struct malloc_elem *prev_free);
148
149 /*
150  * return true if the current malloc_elem can hold a block of data
151  * of the requested size and with the requested alignment
152  */
153 int
154 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
155                 unsigned align, size_t bound);
156
157 /*
158  * reserve a block of data in an existing malloc_elem. If the malloc_elem
159  * is much larger than the data block requested, we split the element in two.
160  */
161 struct malloc_elem *
162 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
163                 unsigned align, size_t bound);
164
165 /*
166  * free a malloc_elem block by adding it to the free list. If the
167  * blocks either immediately before or immediately after newly freed block
168  * are also free, the blocks are merged together.
169  */
170 int
171 malloc_elem_free(struct malloc_elem *elem);
172
173 /*
174  * attempt to resize a malloc_elem by expanding into any free space
175  * immediately after it in memory.
176  */
177 int
178 malloc_elem_resize(struct malloc_elem *elem, size_t size);
179
180 /*
181  * Given an element size, compute its freelist index.
182  */
183 size_t
184 malloc_elem_free_list_index(size_t size);
185
186 /*
187  * Add element to its heap's free list.
188  */
189 void
190 malloc_elem_free_list_insert(struct malloc_elem *elem);
191
192 #endif /* MALLOC_ELEM_H_ */