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