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