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;
22 /**< points to prev elem in memseg */
23 struct malloc_elem *volatile next;
24 /**< points to next elem in memseg */
25 LIST_ENTRY(malloc_elem) free_list;
26 /**< list of free elements in heap */
27 const struct rte_memseg *ms;
28 volatile enum elem_state state;
31 #ifdef RTE_MALLOC_DEBUG
32 uint64_t header_cookie; /* Cookie marking start of data */
33 /* trailer cookie at start + size */
35 } __rte_cache_aligned;
37 #ifndef RTE_MALLOC_DEBUG
38 static const unsigned MALLOC_ELEM_TRAILER_LEN = 0;
40 /* dummy function - just check if pointer is non-null */
42 malloc_elem_cookies_ok(const struct malloc_elem *elem){ return elem != NULL; }
44 /* dummy function - no header if malloc_debug is not enabled */
46 set_header(struct malloc_elem *elem __rte_unused){ }
48 /* dummy function - no trailer if malloc_debug is not enabled */
50 set_trailer(struct malloc_elem *elem __rte_unused){ }
54 static const unsigned MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
56 #define MALLOC_HEADER_COOKIE 0xbadbadbadadd2e55ULL /**< Header cookie. */
57 #define MALLOC_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
59 /* define macros to make referencing the header and trailer cookies easier */
60 #define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
61 elem->size - MALLOC_ELEM_TRAILER_LEN)))
62 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
65 set_header(struct malloc_elem *elem)
68 MALLOC_ELEM_HEADER(elem) = MALLOC_HEADER_COOKIE;
72 set_trailer(struct malloc_elem *elem)
75 MALLOC_ELEM_TRAILER(elem) = MALLOC_TRAILER_COOKIE;
78 /* check that the header and trailer cookies are set correctly */
80 malloc_elem_cookies_ok(const struct malloc_elem *elem)
82 return elem != NULL &&
83 MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
84 MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
89 static const unsigned MALLOC_ELEM_HEADER_LEN = sizeof(struct malloc_elem);
90 #define MALLOC_ELEM_OVERHEAD (MALLOC_ELEM_HEADER_LEN + MALLOC_ELEM_TRAILER_LEN)
93 * Given a pointer to the start of a memory block returned by malloc, get
94 * the actual malloc_elem header for that block.
96 static inline struct malloc_elem *
97 malloc_elem_from_data(const void *data)
102 struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
103 if (!malloc_elem_cookies_ok(elem))
105 return elem->state != ELEM_PAD ? elem: RTE_PTR_SUB(elem, elem->pad);
109 * initialise a malloc_elem header
112 malloc_elem_init(struct malloc_elem *elem,
113 struct malloc_heap *heap,
114 const struct rte_memseg *ms,
118 malloc_elem_insert(struct malloc_elem *elem);
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_ */