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