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