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