1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2019 Intel Corporation
10 * RTE Malloc. This library provides methods for dynamically allocating memory
16 #include <rte_compat.h>
17 #include <rte_memory.h>
24 * Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
26 struct rte_malloc_socket_stats {
27 size_t heap_totalsz_bytes; /**< Total bytes on heap */
28 size_t heap_freesz_bytes; /**< Total free bytes on heap */
29 size_t greatest_free_size; /**< Size in bytes of largest free block */
30 unsigned free_count; /**< Number of free elements on heap */
31 unsigned alloc_count; /**< Number of allocated elements on heap */
32 size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
36 * This function allocates memory from the huge-page area of memory. The memory
37 * is not cleared. In NUMA systems, the memory allocated resides on the same
38 * NUMA socket as the core that calls this function.
41 * A string identifying the type of allocated objects (useful for debug
42 * purposes, such as identifying the cause of a memory leak). Can be NULL.
44 * Size (in bytes) to be allocated.
46 * If 0, the return is a pointer that is suitably aligned for any kind of
47 * variable (in the same manner as malloc()).
48 * Otherwise, the return is a pointer that is a multiple of *align*. In
49 * this case, it must be a power of two. (Minimum alignment is the
50 * cacheline size, i.e. 64-bytes)
52 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
53 * align is not a power of two).
54 * - Otherwise, the pointer to the allocated object.
57 rte_malloc(const char *type, size_t size, unsigned align)
61 * Allocate zero'ed memory from the heap.
63 * Equivalent to rte_malloc() except that the memory zone is
64 * initialised with zeros. In NUMA systems, the memory allocated resides on the
65 * same NUMA socket as the core that calls this function.
68 * A string identifying the type of allocated objects (useful for debug
69 * purposes, such as identifying the cause of a memory leak). Can be NULL.
71 * Size (in bytes) to be allocated.
73 * If 0, the return is a pointer that is suitably aligned for any kind of
74 * variable (in the same manner as malloc()).
75 * Otherwise, the return is a pointer that is a multiple of *align*. In
76 * this case, it must obviously be a power of two. (Minimum alignment is the
77 * cacheline size, i.e. 64-bytes)
79 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
80 * align is not a power of two).
81 * - Otherwise, the pointer to the allocated object.
84 rte_zmalloc(const char *type, size_t size, unsigned align)
88 * Replacement function for calloc(), using huge-page memory. Memory area is
89 * initialised with zeros. In NUMA systems, the memory allocated resides on the
90 * same NUMA socket as the core that calls this function.
93 * A string identifying the type of allocated objects (useful for debug
94 * purposes, such as identifying the cause of a memory leak). Can be NULL.
96 * Number of elements to be allocated.
98 * Size (in bytes) of a single element.
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_calloc(const char *type, size_t num, size_t size, unsigned align)
112 __rte_alloc_size(2, 3);
115 * Replacement function for realloc(), using huge-page memory. Reserved area
116 * memory is resized, preserving contents. In NUMA systems, the new area
117 * may not reside on the same NUMA node as the old one.
120 * Pointer to already allocated memory
122 * Size (in bytes) of new area. If this is 0, memory is freed.
124 * If 0, the return is a pointer that is suitably aligned for any kind of
125 * variable (in the same manner as malloc()).
126 * Otherwise, the return is a pointer that is a multiple of *align*. In
127 * this case, it must obviously be a power of two. (Minimum alignment is the
128 * cacheline size, i.e. 64-bytes)
130 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
131 * align is not a power of two).
132 * - Otherwise, the pointer to the reallocated memory.
135 rte_realloc(void *ptr, size_t size, unsigned int align)
139 * Replacement function for realloc(), using huge-page memory. Reserved area
140 * memory is resized, preserving contents. In NUMA systems, the new area
141 * resides on requested NUMA socket.
144 * Pointer to already allocated memory
146 * Size (in bytes) of new area. If this is 0, memory is freed.
148 * If 0, the return is a pointer that is suitably aligned for any kind of
149 * variable (in the same manner as malloc()).
150 * Otherwise, the return is a pointer that is a multiple of *align*. In
151 * this case, it must obviously be a power of two. (Minimum alignment is the
152 * cacheline size, i.e. 64-bytes)
154 * NUMA socket to allocate memory on.
156 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
157 * align is not a power of two).
158 * - Otherwise, the pointer to the reallocated memory.
161 rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
165 * This function allocates memory from the huge-page area of memory. The memory
169 * A string identifying the type of allocated objects (useful for debug
170 * purposes, such as identifying the cause of a memory leak). Can be NULL.
172 * Size (in bytes) to be allocated.
174 * If 0, the return is a pointer that is suitably aligned for any kind of
175 * variable (in the same manner as malloc()).
176 * Otherwise, the return is a pointer that is a multiple of *align*. In
177 * this case, it must be a power of two. (Minimum alignment is the
178 * cacheline size, i.e. 64-bytes)
180 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
181 * will behave the same as rte_malloc().
183 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
184 * align is not a power of two).
185 * - Otherwise, the pointer to the allocated object.
188 rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)
192 * Allocate zero'ed memory from the heap.
194 * Equivalent to rte_malloc() except that the memory zone is
195 * initialised with zeros.
198 * A string identifying the type of allocated objects (useful for debug
199 * purposes, such as identifying the cause of a memory leak). Can be NULL.
201 * Size (in bytes) to be allocated.
203 * If 0, the return is a pointer that is suitably aligned for any kind of
204 * variable (in the same manner as malloc()).
205 * Otherwise, the return is a pointer that is a multiple of *align*. In
206 * this case, it must obviously be a power of two. (Minimum alignment is the
207 * cacheline size, i.e. 64-bytes)
209 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
210 * will behave the same as rte_zmalloc().
212 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
213 * align is not a power of two).
214 * - Otherwise, the pointer to the allocated object.
217 rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
221 * Replacement function for calloc(), using huge-page memory. Memory area is
222 * initialised with zeros.
225 * A string identifying the type of allocated objects (useful for debug
226 * purposes, such as identifying the cause of a memory leak). Can be NULL.
228 * Number of elements to be allocated.
230 * Size (in bytes) of a single element.
232 * If 0, the return is a pointer that is suitably aligned for any kind of
233 * variable (in the same manner as malloc()).
234 * Otherwise, the return is a pointer that is a multiple of *align*. In
235 * this case, it must obviously be a power of two. (Minimum alignment is the
236 * cacheline size, i.e. 64-bytes)
238 * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
239 * will behave the same as rte_calloc().
241 * - NULL on error. Not enough memory, or invalid arguments (size is 0,
242 * align is not a power of two).
243 * - Otherwise, the pointer to the allocated object.
246 rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
247 __rte_alloc_size(2, 3);
250 * Frees the memory space pointed to by the provided pointer.
252 * This pointer must have been returned by a previous call to
253 * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
254 * rte_free() is undefined if the pointer does not match this requirement.
256 * If the pointer is NULL, the function does nothing.
259 * The pointer to memory to be freed.
265 * If malloc debug is enabled, check a memory block for header
266 * and trailer markers to indicate that all is well with the block.
267 * If size is non-null, also return the size of the block.
270 * pointer to the start of a data block, must have been returned
271 * by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
274 * if non-null, and memory block pointer is valid, returns the size
275 * of the memory block
277 * -1 on error, invalid pointer passed or header and trailer markers
278 * are missing or corrupted
282 rte_malloc_validate(const void *ptr, size_t *size);
285 * Get heap statistics for the specified heap.
287 * @note This function is not thread-safe with respect to
288 * ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
291 * An unsigned integer specifying the socket to get heap statistics for
292 * @param socket_stats
293 * A structure which provides memory to store statistics
296 * Pointer to structure storing statistics on success
299 rte_malloc_get_socket_stats(int socket,
300 struct rte_malloc_socket_stats *socket_stats);
303 * Add memory chunk to a heap with specified name.
305 * @note Multiple memory chunks can be added to the same heap
307 * @note Before accessing this memory in other processes, it needs to be
308 * attached in each of those processes by calling
309 * ``rte_malloc_heap_memory_attach`` in each other process.
311 * @note Memory must be previously allocated for DPDK to be able to use it as a
312 * malloc heap. Failing to do so will result in undefined behavior, up to and
313 * including segmentation faults.
315 * @note Calling this function will erase any contents already present at the
316 * supplied memory address.
319 * Name of the heap to add memory chunk to
321 * Start of virtual area to add to the heap. Must be aligned by ``page_sz``.
323 * Length of virtual area to add to the heap. Must be aligned by ``page_sz``.
325 * Array of page IOVA addresses corresponding to each page in this memory
326 * area. Can be NULL, in which case page IOVA addresses will be set to
329 * Number of elements in the iova_addrs array. Ignored if ``iova_addrs``
332 * Page size of the underlying memory
336 * - -1 in case of error, with rte_errno set to one of the following:
337 * EINVAL - one of the parameters was invalid
338 * EPERM - attempted to add memory to a reserved heap
339 * ENOSPC - no more space in internal config to store a new memory chunk
342 rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
343 rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz);
346 * Remove memory chunk from heap with specified name.
348 * @note Memory chunk being removed must be the same as one that was added;
349 * partially removing memory chunks is not supported
351 * @note Memory area must not contain any allocated elements to allow its
352 * removal from the heap
354 * @note All other processes must detach from the memory chunk prior to it being
355 * removed from the heap.
358 * Name of the heap to remove memory from
360 * Virtual address to remove from the heap
362 * Length of virtual area to remove from the heap
366 * - -1 in case of error, with rte_errno set to one of the following:
367 * EINVAL - one of the parameters was invalid
368 * EPERM - attempted to remove memory from a reserved heap
369 * ENOENT - heap or memory chunk was not found
370 * EBUSY - memory chunk still contains data
373 rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len);
376 * Attach to an already existing chunk of external memory in another process.
378 * @note This function must be called before any attempt is made to use an
379 * already existing external memory chunk. This function does *not* need to
380 * be called if a call to ``rte_malloc_heap_memory_add`` was made in the
384 * Heap name to which this chunk of memory belongs
386 * Start address of memory chunk to attach to
388 * Length of memory chunk to attach to
390 * 0 on successful attach
391 * -1 on unsuccessful attach, with rte_errno set to indicate cause for error:
392 * EINVAL - one of the parameters was invalid
393 * EPERM - attempted to attach memory to a reserved heap
394 * ENOENT - heap or memory chunk was not found
397 rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len);
400 * Detach from a chunk of external memory in secondary process.
402 * @note This function must be called in before any attempt is made to remove
403 * external memory from the heap in another process. This function does *not*
404 * need to be called if a call to ``rte_malloc_heap_memory_remove`` will be
405 * called in current process.
408 * Heap name to which this chunk of memory belongs
410 * Start address of memory chunk to attach to
412 * Length of memory chunk to attach to
414 * 0 on successful detach
415 * -1 on unsuccessful detach, with rte_errno set to indicate cause for error:
416 * EINVAL - one of the parameters was invalid
417 * EPERM - attempted to detach memory from a reserved heap
418 * ENOENT - heap or memory chunk was not found
421 rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len);
424 * Creates a new empty malloc heap with a specified name.
426 * @note Heaps created via this call will automatically get assigned a unique
427 * socket ID, which can be found using ``rte_malloc_heap_get_socket()``
430 * Name of the heap to create.
433 * - 0 on successful creation
434 * - -1 in case of error, with rte_errno set to one of the following:
435 * EINVAL - ``heap_name`` was NULL, empty or too long
436 * EEXIST - heap by name of ``heap_name`` already exists
437 * ENOSPC - no more space in internal config to store a new heap
440 rte_malloc_heap_create(const char *heap_name);
443 * Destroys a previously created malloc heap with specified name.
445 * @note This function will return a failure result if not all memory allocated
446 * from the heap has been freed back to the heap
448 * @note This function will return a failure result if not all memory segments
449 * were removed from the heap prior to its destruction
452 * Name of the heap to create.
456 * - -1 in case of error, with rte_errno set to one of the following:
457 * EINVAL - ``heap_name`` was NULL, empty or too long
458 * ENOENT - heap by the name of ``heap_name`` was not found
459 * EPERM - attempting to destroy reserved heap
460 * EBUSY - heap still contains data
463 rte_malloc_heap_destroy(const char *heap_name);
466 * Find socket ID corresponding to a named heap.
469 * Heap name to find socket ID for
471 * Socket ID in case of success (a non-negative number)
472 * -1 in case of error, with rte_errno set to one of the following:
473 * EINVAL - ``name`` was NULL
474 * ENOENT - heap identified by the name ``name`` was not found
477 rte_malloc_heap_get_socket(const char *name);
480 * Check if a given socket ID refers to externally allocated memory.
482 * @note Passing SOCKET_ID_ANY will return 0.
487 * 1 if socket ID refers to externally allocated memory
488 * 0 if socket ID refers to internal DPDK memory
489 * -1 if socket ID is invalid
492 rte_malloc_heap_socket_is_external(int socket_id);
497 * Dump for the specified type to a file. If the type argument is
498 * NULL, all memory types will be dumped.
500 * @note This function is not thread-safe with respect to
501 * ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
504 * A pointer to a file for output
506 * A string identifying the type of objects to dump, or NULL
507 * to dump all objects.
510 rte_malloc_dump_stats(FILE *f, const char *type);
513 * Dump contents of all malloc heaps to a file.
515 * @note This function is not thread-safe with respect to
516 * ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
519 * A pointer to a file for output
522 rte_malloc_dump_heaps(FILE *f);
525 * Set the maximum amount of allocated memory for this type.
527 * This is not yet implemented
530 * A string identifying the type of allocated objects.
532 * The maximum amount of allocated bytes for this type.
539 rte_malloc_set_limit(const char *type, size_t max);
542 * Return the IO address of a virtual address obtained through
546 * Address obtained from a previous rte_malloc call
548 * RTE_BAD_IOVA on error
549 * otherwise return an address suitable for IO
552 rte_malloc_virt2iova(const void *addr);
558 #endif /* _RTE_MALLOC_H_ */