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