1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_memory.h>
10 /* dummy definition of struct so we can use pointers to it in malloc_elem struct */
16 ELEM_PAD /* element is a padding-only header */
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;
27 #ifdef RTE_MALLOC_DEBUG
28 uint64_t header_cookie; /* Cookie marking start of data */
29 /* trailer cookie at start + size */
31 } __rte_cache_aligned;
33 #ifndef RTE_MALLOC_DEBUG
34 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
36 /* dummy function - just check if pointer is non-null */
38 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
40 /* dummy function - no header if malloc_debug is not enabled */
42 set_header(struct malloc_elem *elem __rte_unused){ }
44 /* dummy function - no trailer if malloc_debug is not enabled */
46 set_trailer(struct malloc_elem *elem __rte_unused){ }
50 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
52 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
53 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
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)
61 set_header(struct malloc_elem *elem)
64 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
68 set_trailer(struct malloc_elem *elem)
71 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
74 /* check that the header and trailer cookies are set correctly */
76 malloc_elem_cookies_ok(const struct malloc_elem *elem)
78 return elem != NULL &&
79 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
80 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
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)
89 * Given a pointer to the start of a memory block returned by malloc, get
90 * the actual malloc_elem header for that block.
92 static inline struct malloc_elem *
93 malloc_elem_from_data(const void *data)
98 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
99 if (!malloc_elem_cookies_ok(elem))
101 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
105 * initialise a malloc_elem header
108 malloc_elem_init(struct malloc_elem *elem,
109 struct malloc_heap *heap,
110 const struct rte_memseg *ms,
114 * initialise a dummy malloc_elem header for the end-of-memseg marker
117 malloc_elem_mkend(struct malloc_elem *elem,
118 struct malloc_elem *prev_free);
121 * return true if the current malloc_elem can hold a block of data
122 * of the requested size and with the requested alignment
125 malloc_elem_can_hold(struct malloc_elem *elem, size_t size,
126 unsigned align, size_t bound);
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.
133 malloc_elem_alloc(struct malloc_elem *elem, size_t size,
134 unsigned align, size_t bound);
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.
142 malloc_elem_free(struct malloc_elem *elem);
145 * attempt to resize a malloc_elem by expanding into any free space
146 * immediately after it in memory.
149 malloc_elem_resize(struct malloc_elem *elem, size_t size);
152 * Given an element size, compute its freelist index.
155 malloc_elem_free_list_index(size_t size);
158 * Add element to its heap's free list.
161 malloc_elem_free_list_insert(struct malloc_elem *elem);
163 #endif /* MALLOC_ELEM_H_ */