New upstream version 18.02
[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 <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;      /* points to prev elem in memseg */
22         LIST_ENTRY(malloc_elem) free_list;      /* list of free elements in heap */
23         const struct rte_memseg *ms;
24         volatile enum elem_state state;
25         uint32_t pad;
26         size_t size;
27 #ifdef RTE_MALLOC_DEBUG
28         uint64_t header_cookie;         /* Cookie marking start of data */
29                                         /* trailer cookie at start + size */
30 #endif
31 } __rte_cache_aligned;
32
33 #ifndef RTE_MALLOC_DEBUG
34 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
35
36 /* dummy function - just check if pointer is non-null */
37 static inline int
38 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
39
40 /* dummy function - no header if malloc_debug is not enabled */
41 static inline void
42 set_header(struct malloc_elem *elem __rte_unused){ }
43
44 /* dummy function - no trailer if malloc_debug is not enabled */
45 static inline void
46 set_trailer(struct malloc_elem *elem __rte_unused){ }
47
48
49 #else
50 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
51
52 #define MALLOC_HEADER_COOKIE   0xbadbadbadadd2e55ULL /**< Header cookie. */
53 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
54
55 /* define macros to make referencing the header and trailer cookies easier */
56 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
57                 elem->size - MALLOC_ELEM_TRAILER_LEN)))
58 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
59
60 static inline void
61 set_header(struct malloc_elem *elem)
62 {
63         if (elem != NULL)
64                 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
65 }
66
67 static inline void
68 set_trailer(struct malloc_elem *elem)
69 {
70         if (elem != NULL)
71                 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
72 }
73
74 /* check that the header and trailer cookies are set correctly */
75 static inline int
76 malloc_elem_cookies_ok(const struct malloc_elem *elem)
77 {
78         return elem != NULL &&
79                         MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
80                         MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
81 }
82
83 #endif
84
85 static const unsigned MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
86 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
87
88 /*
89  * Given a pointer to the start of a memory block returned by malloc, get
90  * the actual malloc_elem header for that block.
91  */
92 static inline struct malloc_elem *
93 malloc_elem_from_data(const void *data)
94 {
95         if (data == NULL)
96                 return NULL;
97
98         struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
99         if (!malloc_elem_cookies_ok(elem))
100                 return NULL;
101         return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
102 }
103
104 /*
105  * initialise a malloc_elem header
106  */
107 void
108 malloc_elem_init(struct malloc_elem *elem,
109                 struct malloc_heap *heap,
110                 const struct rte_memseg *ms,
111                 size_t size);
112
113 /*
114  * initialise a dummy malloc_elem header for the end-of-memseg marker
115  */
116 void
117 malloc_elem_mkend(struct malloc_elem *elem,
118                 struct malloc_elem *prev_free);
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 int
142 malloc_elem_free(struct malloc_elem *elem);
143
144 /*
145  * attempt to resize a malloc_elem by expanding into any free space
146  * immediately after it in memory.
147  */
148 int
149 malloc_elem_resize(struct malloc_elem *elem, size_t size);
150
151 /*
152  * Given an element size, compute its freelist index.
153  */
154 size_t
155 malloc_elem_free_list_index(size_t size);
156
157 /*
158  * Add element to its heap's free list.
159  */
160 void
161 malloc_elem_free_list_insert(struct malloc_elem *elem);
162
163 #endif /* MALLOC_ELEM_H_ */