1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
12 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
18 ELEM_PAD /* element is a padding-only header */
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;
33 struct malloc_elem *orig_elem;
35 #ifdef RTE_MALLOC_DEBUG
36 uint64_t header_cookie; /* Cookie marking start of data */
37 /* trailer cookie at start + size */
39 } __rte_cache_aligned;
41 #ifndef RTE_MALLOC_DEBUG
42 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
44 /* dummy function - just check if pointer is non-null */
46 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
48 /* dummy function - no header if malloc_debug is not enabled */
50 set_header(struct malloc_elem *elem __rte_unused){ }
52 /* dummy function - no trailer if malloc_debug is not enabled */
54 set_trailer(struct malloc_elem *elem __rte_unused){ }
58 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
60 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
61 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
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)
69 set_header(struct malloc_elem *elem)
72 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
76 set_trailer(struct malloc_elem *elem)
79 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
82 /* check that the header and trailer cookies are set correctly */
84 malloc_elem_cookies_ok(const struct malloc_elem *elem)
86 return elem != NULL &&
87 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
88 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
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)
97 * Given a pointer to the start of a memory block returned by malloc, get
98 * the actual malloc_elem header for that block.
100 static inline struct malloc_elem *
101 malloc_elem_from_data(const void *data)
106 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
107 if (!malloc_elem_cookies_ok(elem))
109 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
113 * initialise a malloc_elem header
116 malloc_elem_init(struct malloc_elem *elem,
117 struct malloc_heap *heap,
118 struct rte_memseg_list *msl,
120 struct malloc_elem *orig_elem,
124 malloc_elem_insert(struct malloc_elem *elem);
127 * return true if the current malloc_elem can hold a block of data
128 * of the requested size and with the requested alignment
131 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
132 unsigned int align, size_t bound, bool contig);
135 * reserve a block of data in an existing malloc_elem. If the malloc_elem
136 * is much larger than the data block requested, we split the element in two.
139 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
140 unsigned int align, size_t bound, bool contig);
143 * free a malloc_elem block by adding it to the free list. If the
144 * blocks either immediately before or immediately after newly freed block
145 * are also free, the blocks are merged together.
148 malloc_elem_free(struct malloc_elem *elem);
151 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
154 * attempt to resize a malloc_elem by expanding into any free space
155 * immediately after it in memory.
158 malloc_elem_resize(struct malloc_elem *elem, size_t size);
161 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
164 malloc_elem_free_list_remove(struct malloc_elem *elem);
167 * dump contents of malloc elem to a file.
170 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
173 * Given an element size, compute its freelist index.
176 malloc_elem_free_list_index(size_t size);
179 * Add element to its heap's free list.
182 malloc_elem_free_list_insert(struct malloc_elem *elem);
185 * Find biggest IOVA-contiguous zone within an element with specified alignment.
188 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
190 #endif /* MALLOC_ELEM_H_ */