1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_eal_memconfig.h>
12 #define MIN_DATA_SIZE (RTE_CACHE_LINE_SIZE)
14 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
20 ELEM_PAD /* element is a padding-only header */
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;
35 struct malloc_elem *orig_elem;
37 #ifdef RTE_MALLOC_DEBUG
38 uint64_t header_cookie; /* Cookie marking start of data */
39 /* trailer cookie at start + size */
41 } __rte_cache_aligned;
43 #ifndef RTE_MALLOC_DEBUG
44 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
46 /* dummy function - just check if pointer is non-null */
48 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
50 /* dummy function - no header if malloc_debug is not enabled */
52 set_header(struct malloc_elem *elem __rte_unused){ }
54 /* dummy function - no trailer if malloc_debug is not enabled */
56 set_trailer(struct malloc_elem *elem __rte_unused){ }
60 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
62 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
63 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
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)
71 set_header(struct malloc_elem *elem)
74 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
78 set_trailer(struct malloc_elem *elem)
81 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
84 /* check that the header and trailer cookies are set correctly */
86 malloc_elem_cookies_ok(const struct malloc_elem *elem)
88 return elem != NULL &&
89 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
90 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
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)
99 * Given a pointer to the start of a memory block returned by malloc, get
100 * the actual malloc_elem header for that block.
102 static inline struct malloc_elem *
103 malloc_elem_from_data(const void *data)
108 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
109 if (!malloc_elem_cookies_ok(elem))
111 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
115 * initialise a malloc_elem header
118 malloc_elem_init(struct malloc_elem *elem,
119 struct malloc_heap *heap,
120 struct rte_memseg_list *msl,
122 struct malloc_elem *orig_elem,
126 malloc_elem_insert(struct malloc_elem *elem);
129 * return true if the current malloc_elem can hold a block of data
130 * of the requested size and with the requested alignment
133 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
134 unsigned int align, size_t bound, bool contig);
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.
141 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
142 unsigned int align, size_t bound, bool contig);
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.
150 malloc_elem_free(struct malloc_elem *elem);
153 malloc_elem_join_adjacent_free(struct malloc_elem *elem);
156 * attempt to resize a malloc_elem by expanding into any free space
157 * immediately after it in memory.
160 malloc_elem_resize(struct malloc_elem *elem, size_t size);
163 malloc_elem_hide_region(struct malloc_elem *elem, void *start, size_t len);
166 malloc_elem_free_list_remove(struct malloc_elem *elem);
169 * dump contents of malloc elem to a file.
172 malloc_elem_dump(const struct malloc_elem *elem, FILE *f);
175 * Given an element size, compute its freelist index.
178 malloc_elem_free_list_index(size_t size);
181 * Add element to its heap's free list.
184 malloc_elem_free_list_insert(struct malloc_elem *elem);
187 * Find biggest IOVA-contiguous zone within an element with specified alignment.
190 malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align);
192 #endif /* MALLOC_ELEM_H_ */