4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MALLOC_H_
35 #define _RTE_MALLOC_H_
39 * RTE Malloc. This library provides methods for dynamically allocating memory
45 #include <rte_memory.h>
52 * Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
54 struct rte_malloc_socket_stats {
55 size_t heap_totalsz_bytes; /**< Total bytes on heap */
56 size_t heap_freesz_bytes; /**< Total free bytes on heap */
57 size_t greatest_free_size; /**< Size in bytes of largest free block */
58 unsigned free_count; /**< Number of free elements on heap */
59 unsigned alloc_count; /**< Number of allocated elements on heap */
60 size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
64 * This function allocates memory from the huge-page area of memory. The memory
65 * is not cleared. In NUMA systems, the memory allocated resides on the same
66 * NUMA socket as the core that calls this function.
69 * A string identifying the type of allocated objects (useful for debug
70 * purposes, such as identifying the cause of a memory leak). Can be NULL.
72 * Size (in bytes) to be allocated.
74 * If 0, the return is a pointer that is suitably aligned for any kind of
75 * variable (in the same manner as malloc()).
76 * Otherwise, the return is a pointer that is a multiple of *align*. In
77 * this case, it must be a power of two. (Minimum alignment is the
78 * cacheline size, i.e. 64-bytes)
80 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
81 * align is not a power of two).
82 * - Otherwise, the pointer to the allocated object.
85 rte_malloc(const char *type, size_t size, unsigned align);
88 * Allocate zero'ed memory from the heap.
90 * Equivalent to rte_malloc() except that the memory zone is
91 * initialised with zeros. In NUMA systems, the memory allocated resides on the
92 * same NUMA socket as the core that calls this function.
95 * A string identifying the type of allocated objects (useful for debug
96 * purposes, such as identifying the cause of a memory leak). Can be NULL.
98 * Size (in bytes) to be allocated.
100 * If 0, the return is a pointer that is suitably aligned for any kind of
101 * variable (in the same manner as malloc()).
102 * Otherwise, the return is a pointer that is a multiple of *align*. In
103 * this case, it must obviously be a power of two. (Minimum alignment is the
104 * cacheline size, i.e. 64-bytes)
106 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
107 * align is not a power of two).
108 * - Otherwise, the pointer to the allocated object.
111 rte_zmalloc(const char *type, size_t size, unsigned align);
114 * Replacement function for calloc(), using huge-page memory. Memory area is
115 * initialised with zeros. In NUMA systems, the memory allocated resides on the
116 * same NUMA socket as the core that calls this function.
119 * A string identifying the type of allocated objects (useful for debug
120 * purposes, such as identifying the cause of a memory leak). Can be NULL.
122 * Number of elements to be allocated.
124 * Size (in bytes) of a single element.
126 * If 0, the return is a pointer that is suitably aligned for any kind of
127 * variable (in the same manner as malloc()).
128 * Otherwise, the return is a pointer that is a multiple of *align*. In
129 * this case, it must obviously be a power of two. (Minimum alignment is the
130 * cacheline size, i.e. 64-bytes)
132 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
133 * align is not a power of two).
134 * - Otherwise, the pointer to the allocated object.
137 rte_calloc(const char *type, size_t num, size_t size, unsigned align);
140 * Replacement function for realloc(), using huge-page memory. Reserved area
141 * memory is resized, preserving contents. In NUMA systems, the new area
142 * resides on the same NUMA socket as the old area.
145 * Pointer to already allocated memory
147 * Size (in bytes) of new area. If this is 0, memory is freed.
149 * If 0, the return is a pointer that is suitably aligned for any kind of
150 * variable (in the same manner as malloc()).
151 * Otherwise, the return is a pointer that is a multiple of *align*. In
152 * this case, it must obviously be a power of two. (Minimum alignment is the
153 * cacheline size, i.e. 64-bytes)
155 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
156 * align is not a power of two).
157 * - Otherwise, the pointer to the reallocated memory.
160 rte_realloc(void *ptr, size_t size, unsigned align);
163 * This function allocates memory from the huge-page area of memory. The memory
167 * A string identifying the type of allocated objects (useful for debug
168 * purposes, such as identifying the cause of a memory leak). Can be NULL.
170 * Size (in bytes) to be allocated.
172 * If 0, the return is a pointer that is suitably aligned for any kind of
173 * variable (in the same manner as malloc()).
174 * Otherwise, the return is a pointer that is a multiple of *align*. In
175 * this case, it must be a power of two. (Minimum alignment is the
176 * cacheline size, i.e. 64-bytes)
178 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
179 * will behave the same as rte_malloc().
181 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
182 * align is not a power of two).
183 * - Otherwise, the pointer to the allocated object.
186 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket);
189 * Allocate zero'ed memory from the heap.
191 * Equivalent to rte_malloc() except that the memory zone is
192 * initialised with zeros.
195 * A string identifying the type of allocated objects (useful for debug
196 * purposes, such as identifying the cause of a memory leak). Can be NULL.
198 * Size (in bytes) to be allocated.
200 * If 0, the return is a pointer that is suitably aligned for any kind of
201 * variable (in the same manner as malloc()).
202 * Otherwise, the return is a pointer that is a multiple of *align*. In
203 * this case, it must obviously be a power of two. (Minimum alignment is the
204 * cacheline size, i.e. 64-bytes)
206 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
207 * will behave the same as rte_zmalloc().
209 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
210 * align is not a power of two).
211 * - Otherwise, the pointer to the allocated object.
214 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket);
217 * Replacement function for calloc(), using huge-page memory. Memory area is
218 * initialised with zeros.
221 * A string identifying the type of allocated objects (useful for debug
222 * purposes, such as identifying the cause of a memory leak). Can be NULL.
224 * Number of elements to be allocated.
226 * Size (in bytes) of a single element.
228 * If 0, the return is a pointer that is suitably aligned for any kind of
229 * variable (in the same manner as malloc()).
230 * Otherwise, the return is a pointer that is a multiple of *align*. In
231 * this case, it must obviously be a power of two. (Minimum alignment is the
232 * cacheline size, i.e. 64-bytes)
234 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
235 * will behave the same as rte_calloc().
237 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
238 * align is not a power of two).
239 * - Otherwise, the pointer to the allocated object.
242 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket);
245 * Frees the memory space pointed to by the provided pointer.
247 * This pointer must have been returned by a previous call to
248 * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
249 * rte_free() is undefined if the pointer does not match this requirement.
251 * If the pointer is NULL, the function does nothing.
254 * The pointer to memory to be freed.
260 * If malloc debug is enabled, check a memory block for header
261 * and trailer markers to indicate that all is well with the block.
262 * If size is non-null, also return the size of the block.
265 * pointer to the start of a data block, must have been returned
266 * by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
269 * if non-null, and memory block pointer is valid, returns the size
270 * of the memory block
272 * -1 on error, invalid pointer passed or header and trailer markers
273 * are missing or corrupted
277 rte_malloc_validate(const void *ptr, size_t *size);
280 * Get heap statistics for the specified heap.
283 * An unsigned integer specifying the socket to get heap statistics for
284 * @param socket_stats
285 * A structure which provides memory to store statistics
288 * Pointer to structure storing statistics on success
291 rte_malloc_get_socket_stats(int socket,
292 struct rte_malloc_socket_stats *socket_stats);
297 * Dump for the specified type to a file. If the type argument is
298 * NULL, all memory types will be dumped.
301 * A pointer to a file for output
303 * A string identifying the type of objects to dump, or NULL
304 * to dump all objects.
307 rte_malloc_dump_stats(FILE *f, const char *type);
310 * Set the maximum amount of allocated memory for this type.
312 * This is not yet implemented
315 * A string identifying the type of allocated objects.
317 * The maximum amount of allocated bytes for this type.
323 rte_malloc_set_limit(const char *type, size_t max);
326 * Return the physical address of a virtual address obtained through
330 * Address obtained from a previous rte_malloc call
332 * RTE_BAD_PHYS_ADDR on error
333 * otherwise return physical address of the buffer
336 rte_malloc_virt2phy(const void *addr);
342 #endif /* _RTE_MALLOC_H_ */