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