xref: /dpdk/lib/eal/include/rte_malloc.h (revision 80da7efbb4c4216de93b1039b891a6f31fa06f2d)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2019 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_MALLOC_H_
699a2dd95SBruce Richardson #define _RTE_MALLOC_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  * RTE Malloc. This library provides methods for dynamically allocating memory
1199a2dd95SBruce Richardson  * from hugepages.
1299a2dd95SBruce Richardson  */
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #include <stdio.h>
1599a2dd95SBruce Richardson #include <stddef.h>
1699a2dd95SBruce Richardson #include <rte_memory.h>
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson #ifdef __cplusplus
1999a2dd95SBruce Richardson extern "C" {
2099a2dd95SBruce Richardson #endif
2199a2dd95SBruce Richardson 
2299a2dd95SBruce Richardson /**
2399a2dd95SBruce Richardson  *  Structure to hold heap statistics obtained from rte_malloc_get_socket_stats function.
2499a2dd95SBruce Richardson  */
2599a2dd95SBruce Richardson struct rte_malloc_socket_stats {
2699a2dd95SBruce Richardson 	size_t heap_totalsz_bytes; /**< Total bytes on heap */
2799a2dd95SBruce Richardson 	size_t heap_freesz_bytes;  /**< Total free bytes on heap */
2899a2dd95SBruce Richardson 	size_t greatest_free_size; /**< Size in bytes of largest free block */
2999a2dd95SBruce Richardson 	unsigned free_count;       /**< Number of free elements on heap */
3099a2dd95SBruce Richardson 	unsigned alloc_count;      /**< Number of allocated elements on heap */
3199a2dd95SBruce Richardson 	size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */
3299a2dd95SBruce Richardson };
3399a2dd95SBruce Richardson 
3499a2dd95SBruce Richardson /**
35*80da7efbSStephen Hemminger  * Functions that expect return value to be freed with rte_free()
36*80da7efbSStephen Hemminger  */
37*80da7efbSStephen Hemminger #define __rte_dealloc_free __rte_dealloc(rte_free, 1)
38*80da7efbSStephen Hemminger 
39*80da7efbSStephen Hemminger /**
40*80da7efbSStephen Hemminger  * Frees the memory space pointed to by the provided pointer.
41*80da7efbSStephen Hemminger  *
42*80da7efbSStephen Hemminger  * This pointer must have been returned by a previous call to
43*80da7efbSStephen Hemminger  * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of
44*80da7efbSStephen Hemminger  * rte_free() is undefined if the pointer does not match this requirement.
45*80da7efbSStephen Hemminger  *
46*80da7efbSStephen Hemminger  * If the pointer is NULL, the function does nothing.
47*80da7efbSStephen Hemminger  *
48*80da7efbSStephen Hemminger  * @param ptr
49*80da7efbSStephen Hemminger  *   The pointer to memory to be freed.
50*80da7efbSStephen Hemminger  */
51*80da7efbSStephen Hemminger void
52*80da7efbSStephen Hemminger rte_free(void *ptr);
53*80da7efbSStephen Hemminger 
54*80da7efbSStephen Hemminger /**
5599a2dd95SBruce Richardson  * This function allocates memory from the huge-page area of memory. The memory
5699a2dd95SBruce Richardson  * is not cleared. In NUMA systems, the memory allocated resides on the same
5799a2dd95SBruce Richardson  * NUMA socket as the core that calls this function.
5899a2dd95SBruce Richardson  *
5999a2dd95SBruce Richardson  * @param type
60ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
61ae539d90SStephen Hemminger  *   Can be NULL.
6299a2dd95SBruce Richardson  * @param size
6399a2dd95SBruce Richardson  *   Size (in bytes) to be allocated.
6499a2dd95SBruce Richardson  * @param align
6599a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
6699a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
6799a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
6899a2dd95SBruce Richardson  *   this case, it must be a power of two. (Minimum alignment is the
6999a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
7099a2dd95SBruce Richardson  * @return
7199a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
7299a2dd95SBruce Richardson  *     align is not a power of two).
7399a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
7499a2dd95SBruce Richardson  */
7599a2dd95SBruce Richardson void *
7699a2dd95SBruce Richardson rte_malloc(const char *type, size_t size, unsigned align)
77*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
78*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson /**
817be78d02SJosh Soref  * Allocate zeroed memory from the heap.
8299a2dd95SBruce Richardson  *
8399a2dd95SBruce Richardson  * Equivalent to rte_malloc() except that the memory zone is
8499a2dd95SBruce Richardson  * initialised with zeros. In NUMA systems, the memory allocated resides on the
8599a2dd95SBruce Richardson  * same NUMA socket as the core that calls this function.
8699a2dd95SBruce Richardson  *
8799a2dd95SBruce Richardson  * @param type
88ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
89ae539d90SStephen Hemminger  *   Can be NULL.
9099a2dd95SBruce Richardson  * @param size
9199a2dd95SBruce Richardson  *   Size (in bytes) to be allocated.
9299a2dd95SBruce Richardson  * @param align
9399a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
9499a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
9599a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
9699a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
9799a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
9899a2dd95SBruce Richardson  * @return
9999a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
10099a2dd95SBruce Richardson  *     align is not a power of two).
10199a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
10299a2dd95SBruce Richardson  */
10399a2dd95SBruce Richardson void *
10499a2dd95SBruce Richardson rte_zmalloc(const char *type, size_t size, unsigned align)
105*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
106*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
10799a2dd95SBruce Richardson 
10899a2dd95SBruce Richardson /**
10999a2dd95SBruce Richardson  * Replacement function for calloc(), using huge-page memory. Memory area is
11099a2dd95SBruce Richardson  * initialised with zeros. In NUMA systems, the memory allocated resides on the
11199a2dd95SBruce Richardson  * same NUMA socket as the core that calls this function.
11299a2dd95SBruce Richardson  *
11399a2dd95SBruce Richardson  * @param type
114ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
115ae539d90SStephen Hemminger  *   Can be NULL.
11699a2dd95SBruce Richardson  * @param num
11799a2dd95SBruce Richardson  *   Number of elements to be allocated.
11899a2dd95SBruce Richardson  * @param size
11999a2dd95SBruce Richardson  *   Size (in bytes) of a single element.
12099a2dd95SBruce Richardson  * @param align
12199a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
12299a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
12399a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
12499a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
12599a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
12699a2dd95SBruce Richardson  * @return
12799a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
12899a2dd95SBruce Richardson  *     align is not a power of two).
12999a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
13099a2dd95SBruce Richardson  */
13199a2dd95SBruce Richardson void *
13299a2dd95SBruce Richardson rte_calloc(const char *type, size_t num, size_t size, unsigned align)
133*80da7efbSStephen Hemminger 	__rte_alloc_size(2, 3)	__rte_alloc_align(4)
134*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
13599a2dd95SBruce Richardson 
13699a2dd95SBruce Richardson /**
13799a2dd95SBruce Richardson  * Replacement function for realloc(), using huge-page memory. Reserved area
13899a2dd95SBruce Richardson  * memory is resized, preserving contents. In NUMA systems, the new area
13999a2dd95SBruce Richardson  * may not reside on the same NUMA node as the old one.
14099a2dd95SBruce Richardson  *
14199a2dd95SBruce Richardson  * @param ptr
14299a2dd95SBruce Richardson  *   Pointer to already allocated memory
14399a2dd95SBruce Richardson  * @param size
14499a2dd95SBruce Richardson  *   Size (in bytes) of new area. If this is 0, memory is freed.
14599a2dd95SBruce Richardson  * @param align
14699a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
14799a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
14899a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
14999a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
15099a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
15199a2dd95SBruce Richardson  * @return
15299a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
15399a2dd95SBruce Richardson  *     align is not a power of two).
15499a2dd95SBruce Richardson  *   - Otherwise, the pointer to the reallocated memory.
15599a2dd95SBruce Richardson  */
15699a2dd95SBruce Richardson void *
15799a2dd95SBruce Richardson rte_realloc(void *ptr, size_t size, unsigned int align)
158*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
159*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
16099a2dd95SBruce Richardson 
16199a2dd95SBruce Richardson /**
16299a2dd95SBruce Richardson  * Replacement function for realloc(), using huge-page memory. Reserved area
16399a2dd95SBruce Richardson  * memory is resized, preserving contents. In NUMA systems, the new area
16499a2dd95SBruce Richardson  * resides on requested NUMA socket.
16599a2dd95SBruce Richardson  *
16699a2dd95SBruce Richardson  * @param ptr
16799a2dd95SBruce Richardson  *   Pointer to already allocated memory
16899a2dd95SBruce Richardson  * @param size
16999a2dd95SBruce Richardson  *   Size (in bytes) of new area. If this is 0, memory is freed.
17099a2dd95SBruce Richardson  * @param align
17199a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
17299a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
17399a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
17499a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
17599a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
17699a2dd95SBruce Richardson  * @param socket
17799a2dd95SBruce Richardson  *   NUMA socket to allocate memory on.
17899a2dd95SBruce Richardson  * @return
17999a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
18099a2dd95SBruce Richardson  *     align is not a power of two).
18199a2dd95SBruce Richardson  *   - Otherwise, the pointer to the reallocated memory.
18299a2dd95SBruce Richardson  */
18399a2dd95SBruce Richardson void *
18499a2dd95SBruce Richardson rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket)
185*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
186*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
18799a2dd95SBruce Richardson 
18899a2dd95SBruce Richardson /**
18999a2dd95SBruce Richardson  * This function allocates memory from the huge-page area of memory. The memory
19099a2dd95SBruce Richardson  * is not cleared.
19199a2dd95SBruce Richardson  *
19299a2dd95SBruce Richardson  * @param type
193ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
194ae539d90SStephen Hemminger  *   Can be NULL.
19599a2dd95SBruce Richardson  * @param size
19699a2dd95SBruce Richardson  *   Size (in bytes) to be allocated.
19799a2dd95SBruce Richardson  * @param align
19899a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
19999a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
20099a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
20199a2dd95SBruce Richardson  *   this case, it must be a power of two. (Minimum alignment is the
20299a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
20399a2dd95SBruce Richardson  * @param socket
20499a2dd95SBruce Richardson  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
20599a2dd95SBruce Richardson  *   will behave the same as rte_malloc().
20699a2dd95SBruce Richardson  * @return
20799a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
20899a2dd95SBruce Richardson  *     align is not a power of two).
20999a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
21099a2dd95SBruce Richardson  */
21199a2dd95SBruce Richardson void *
21299a2dd95SBruce Richardson rte_malloc_socket(const char *type, size_t size, unsigned align, int socket)
213*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
214*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
21599a2dd95SBruce Richardson 
21699a2dd95SBruce Richardson /**
2177be78d02SJosh Soref  * Allocate zeroed memory from the heap.
21899a2dd95SBruce Richardson  *
21999a2dd95SBruce Richardson  * Equivalent to rte_malloc() except that the memory zone is
22099a2dd95SBruce Richardson  * initialised with zeros.
22199a2dd95SBruce Richardson  *
22299a2dd95SBruce Richardson  * @param type
223ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
224ae539d90SStephen Hemminger  *   Can be NULL.
22599a2dd95SBruce Richardson  * @param size
22699a2dd95SBruce Richardson  *   Size (in bytes) to be allocated.
22799a2dd95SBruce Richardson  * @param align
22899a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
22999a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
23099a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
23199a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
23299a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
23399a2dd95SBruce Richardson  * @param socket
23499a2dd95SBruce Richardson  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
23599a2dd95SBruce Richardson  *   will behave the same as rte_zmalloc().
23699a2dd95SBruce Richardson  * @return
23799a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
23899a2dd95SBruce Richardson  *     align is not a power of two).
23999a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
24099a2dd95SBruce Richardson  */
24199a2dd95SBruce Richardson void *
24299a2dd95SBruce Richardson rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket)
243*80da7efbSStephen Hemminger 	__rte_alloc_size(2) __rte_alloc_align(3)
244*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
24599a2dd95SBruce Richardson 
24699a2dd95SBruce Richardson /**
24799a2dd95SBruce Richardson  * Replacement function for calloc(), using huge-page memory. Memory area is
24899a2dd95SBruce Richardson  * initialised with zeros.
24999a2dd95SBruce Richardson  *
25099a2dd95SBruce Richardson  * @param type
251ae539d90SStephen Hemminger  *   A string identifying the type of allocated objects (useful for tracing).
252ae539d90SStephen Hemminger  *   Can be NULL.
25399a2dd95SBruce Richardson  * @param num
25499a2dd95SBruce Richardson  *   Number of elements to be allocated.
25599a2dd95SBruce Richardson  * @param size
25699a2dd95SBruce Richardson  *   Size (in bytes) of a single element.
25799a2dd95SBruce Richardson  * @param align
25899a2dd95SBruce Richardson  *   If 0, the return is a pointer that is suitably aligned for any kind of
25999a2dd95SBruce Richardson  *   variable (in the same manner as malloc()).
26099a2dd95SBruce Richardson  *   Otherwise, the return is a pointer that is a multiple of *align*. In
26199a2dd95SBruce Richardson  *   this case, it must obviously be a power of two. (Minimum alignment is the
26299a2dd95SBruce Richardson  *   cacheline size, i.e. 64-bytes)
26399a2dd95SBruce Richardson  * @param socket
26499a2dd95SBruce Richardson  *   NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this function
26599a2dd95SBruce Richardson  *   will behave the same as rte_calloc().
26699a2dd95SBruce Richardson  * @return
26799a2dd95SBruce Richardson  *   - NULL on error. Not enough memory, or invalid arguments (size is 0,
26899a2dd95SBruce Richardson  *     align is not a power of two).
26999a2dd95SBruce Richardson  *   - Otherwise, the pointer to the allocated object.
27099a2dd95SBruce Richardson  */
27199a2dd95SBruce Richardson void *
27299a2dd95SBruce Richardson rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket)
273*80da7efbSStephen Hemminger 	__rte_alloc_size(2, 3)	__rte_alloc_align(4)
274*80da7efbSStephen Hemminger 	__rte_malloc __rte_dealloc_free;
27599a2dd95SBruce Richardson 
27699a2dd95SBruce Richardson /**
27799a2dd95SBruce Richardson  * If malloc debug is enabled, check a memory block for header
27899a2dd95SBruce Richardson  * and trailer markers to indicate that all is well with the block.
27999a2dd95SBruce Richardson  * If size is non-null, also return the size of the block.
28099a2dd95SBruce Richardson  *
28199a2dd95SBruce Richardson  * @param ptr
28299a2dd95SBruce Richardson  *   pointer to the start of a data block, must have been returned
28399a2dd95SBruce Richardson  *   by a previous call to rte_malloc(), rte_zmalloc(), rte_calloc()
28499a2dd95SBruce Richardson  *   or rte_realloc()
28599a2dd95SBruce Richardson  * @param size
28699a2dd95SBruce Richardson  *   if non-null, and memory block pointer is valid, returns the size
28799a2dd95SBruce Richardson  *   of the memory block
28899a2dd95SBruce Richardson  * @return
28999a2dd95SBruce Richardson  *   -1 on error, invalid pointer passed or header and trailer markers
29099a2dd95SBruce Richardson  *   are missing or corrupted
29199a2dd95SBruce Richardson  *   0 on success
29299a2dd95SBruce Richardson  */
29399a2dd95SBruce Richardson int
29499a2dd95SBruce Richardson rte_malloc_validate(const void *ptr, size_t *size);
29599a2dd95SBruce Richardson 
29699a2dd95SBruce Richardson /**
29799a2dd95SBruce Richardson  * Get heap statistics for the specified heap.
29899a2dd95SBruce Richardson  *
29999a2dd95SBruce Richardson  * @note This function is not thread-safe with respect to
30099a2dd95SBruce Richardson  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
30199a2dd95SBruce Richardson  *
30299a2dd95SBruce Richardson  * @param socket
30399a2dd95SBruce Richardson  *   An unsigned integer specifying the socket to get heap statistics for
30499a2dd95SBruce Richardson  * @param socket_stats
30599a2dd95SBruce Richardson  *   A structure which provides memory to store statistics
30699a2dd95SBruce Richardson  * @return
30799a2dd95SBruce Richardson  *   Null on error
30899a2dd95SBruce Richardson  *   Pointer to structure storing statistics on success
30999a2dd95SBruce Richardson  */
31099a2dd95SBruce Richardson int
31199a2dd95SBruce Richardson rte_malloc_get_socket_stats(int socket,
31299a2dd95SBruce Richardson 		struct rte_malloc_socket_stats *socket_stats);
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson /**
31599a2dd95SBruce Richardson  * Add memory chunk to a heap with specified name.
31699a2dd95SBruce Richardson  *
31799a2dd95SBruce Richardson  * @note Multiple memory chunks can be added to the same heap
31899a2dd95SBruce Richardson  *
31999a2dd95SBruce Richardson  * @note Before accessing this memory in other processes, it needs to be
32099a2dd95SBruce Richardson  *   attached in each of those processes by calling
32199a2dd95SBruce Richardson  *   ``rte_malloc_heap_memory_attach`` in each other process.
32299a2dd95SBruce Richardson  *
32399a2dd95SBruce Richardson  * @note Memory must be previously allocated for DPDK to be able to use it as a
32499a2dd95SBruce Richardson  *   malloc heap. Failing to do so will result in undefined behavior, up to and
32599a2dd95SBruce Richardson  *   including segmentation faults.
32699a2dd95SBruce Richardson  *
32799a2dd95SBruce Richardson  * @note Calling this function will erase any contents already present at the
32899a2dd95SBruce Richardson  *   supplied memory address.
32999a2dd95SBruce Richardson  *
33099a2dd95SBruce Richardson  * @param heap_name
33199a2dd95SBruce Richardson  *   Name of the heap to add memory chunk to
33299a2dd95SBruce Richardson  * @param va_addr
33399a2dd95SBruce Richardson  *   Start of virtual area to add to the heap. Must be aligned by ``page_sz``.
33499a2dd95SBruce Richardson  * @param len
33599a2dd95SBruce Richardson  *   Length of virtual area to add to the heap. Must be aligned by ``page_sz``.
33699a2dd95SBruce Richardson  * @param iova_addrs
33799a2dd95SBruce Richardson  *   Array of page IOVA addresses corresponding to each page in this memory
33899a2dd95SBruce Richardson  *   area. Can be NULL, in which case page IOVA addresses will be set to
33999a2dd95SBruce Richardson  *   RTE_BAD_IOVA.
34099a2dd95SBruce Richardson  * @param n_pages
34199a2dd95SBruce Richardson  *   Number of elements in the iova_addrs array. Ignored if  ``iova_addrs``
34299a2dd95SBruce Richardson  *   is NULL.
34399a2dd95SBruce Richardson  * @param page_sz
34499a2dd95SBruce Richardson  *   Page size of the underlying memory
34599a2dd95SBruce Richardson  *
34699a2dd95SBruce Richardson  * @return
34799a2dd95SBruce Richardson  *   - 0 on success
34899a2dd95SBruce Richardson  *   - -1 in case of error, with rte_errno set to one of the following:
34999a2dd95SBruce Richardson  *     EINVAL - one of the parameters was invalid
35099a2dd95SBruce Richardson  *     EPERM  - attempted to add memory to a reserved heap
35199a2dd95SBruce Richardson  *     ENOSPC - no more space in internal config to store a new memory chunk
35299a2dd95SBruce Richardson  */
35399a2dd95SBruce Richardson int
35499a2dd95SBruce Richardson rte_malloc_heap_memory_add(const char *heap_name, void *va_addr, size_t len,
35599a2dd95SBruce Richardson 		rte_iova_t iova_addrs[], unsigned int n_pages, size_t page_sz);
35699a2dd95SBruce Richardson 
35799a2dd95SBruce Richardson /**
35899a2dd95SBruce Richardson  * Remove memory chunk from heap with specified name.
35999a2dd95SBruce Richardson  *
36099a2dd95SBruce Richardson  * @note Memory chunk being removed must be the same as one that was added;
36199a2dd95SBruce Richardson  *   partially removing memory chunks is not supported
36299a2dd95SBruce Richardson  *
36399a2dd95SBruce Richardson  * @note Memory area must not contain any allocated elements to allow its
36499a2dd95SBruce Richardson  *   removal from the heap
36599a2dd95SBruce Richardson  *
36699a2dd95SBruce Richardson  * @note All other processes must detach from the memory chunk prior to it being
36799a2dd95SBruce Richardson  *   removed from the heap.
36899a2dd95SBruce Richardson  *
36999a2dd95SBruce Richardson  * @param heap_name
37099a2dd95SBruce Richardson  *   Name of the heap to remove memory from
37199a2dd95SBruce Richardson  * @param va_addr
37299a2dd95SBruce Richardson  *   Virtual address to remove from the heap
37399a2dd95SBruce Richardson  * @param len
37499a2dd95SBruce Richardson  *   Length of virtual area to remove from the heap
37599a2dd95SBruce Richardson  *
37699a2dd95SBruce Richardson  * @return
37799a2dd95SBruce Richardson  *   - 0 on success
37899a2dd95SBruce Richardson  *   - -1 in case of error, with rte_errno set to one of the following:
37999a2dd95SBruce Richardson  *     EINVAL - one of the parameters was invalid
38099a2dd95SBruce Richardson  *     EPERM  - attempted to remove memory from a reserved heap
38199a2dd95SBruce Richardson  *     ENOENT - heap or memory chunk was not found
38299a2dd95SBruce Richardson  *     EBUSY  - memory chunk still contains data
38399a2dd95SBruce Richardson  */
38499a2dd95SBruce Richardson int
38599a2dd95SBruce Richardson rte_malloc_heap_memory_remove(const char *heap_name, void *va_addr, size_t len);
38699a2dd95SBruce Richardson 
38799a2dd95SBruce Richardson /**
38899a2dd95SBruce Richardson  * Attach to an already existing chunk of external memory in another process.
38999a2dd95SBruce Richardson  *
39099a2dd95SBruce Richardson  * @note This function must be called before any attempt is made to use an
39199a2dd95SBruce Richardson  *   already existing external memory chunk. This function does *not* need to
39299a2dd95SBruce Richardson  *   be called if a call to ``rte_malloc_heap_memory_add`` was made in the
39399a2dd95SBruce Richardson  *   current process.
39499a2dd95SBruce Richardson  *
39599a2dd95SBruce Richardson  * @param heap_name
39699a2dd95SBruce Richardson  *   Heap name to which this chunk of memory belongs
39799a2dd95SBruce Richardson  * @param va_addr
39899a2dd95SBruce Richardson  *   Start address of memory chunk to attach to
39999a2dd95SBruce Richardson  * @param len
40099a2dd95SBruce Richardson  *   Length of memory chunk to attach to
40199a2dd95SBruce Richardson  * @return
40299a2dd95SBruce Richardson  *   0 on successful attach
40399a2dd95SBruce Richardson  *   -1 on unsuccessful attach, with rte_errno set to indicate cause for error:
40499a2dd95SBruce Richardson  *     EINVAL - one of the parameters was invalid
40599a2dd95SBruce Richardson  *     EPERM  - attempted to attach memory to a reserved heap
40699a2dd95SBruce Richardson  *     ENOENT - heap or memory chunk was not found
40799a2dd95SBruce Richardson  */
40899a2dd95SBruce Richardson int
40999a2dd95SBruce Richardson rte_malloc_heap_memory_attach(const char *heap_name, void *va_addr, size_t len);
41099a2dd95SBruce Richardson 
41199a2dd95SBruce Richardson /**
41299a2dd95SBruce Richardson  * Detach from a chunk of external memory in secondary process.
41399a2dd95SBruce Richardson  *
41499a2dd95SBruce Richardson  * @note This function must be called in before any attempt is made to remove
41599a2dd95SBruce Richardson  *   external memory from the heap in another process. This function does *not*
41699a2dd95SBruce Richardson  *   need to be called if a call to ``rte_malloc_heap_memory_remove`` will be
41799a2dd95SBruce Richardson  *   called in current process.
41899a2dd95SBruce Richardson  *
41999a2dd95SBruce Richardson  * @param heap_name
42099a2dd95SBruce Richardson  *   Heap name to which this chunk of memory belongs
42199a2dd95SBruce Richardson  * @param va_addr
42299a2dd95SBruce Richardson  *   Start address of memory chunk to attach to
42399a2dd95SBruce Richardson  * @param len
42499a2dd95SBruce Richardson  *   Length of memory chunk to attach to
42599a2dd95SBruce Richardson  * @return
42699a2dd95SBruce Richardson  *   0 on successful detach
42799a2dd95SBruce Richardson  *   -1 on unsuccessful detach, with rte_errno set to indicate cause for error:
42899a2dd95SBruce Richardson  *     EINVAL - one of the parameters was invalid
42999a2dd95SBruce Richardson  *     EPERM  - attempted to detach memory from a reserved heap
43099a2dd95SBruce Richardson  *     ENOENT - heap or memory chunk was not found
43199a2dd95SBruce Richardson  */
43299a2dd95SBruce Richardson int
43399a2dd95SBruce Richardson rte_malloc_heap_memory_detach(const char *heap_name, void *va_addr, size_t len);
43499a2dd95SBruce Richardson 
43599a2dd95SBruce Richardson /**
43699a2dd95SBruce Richardson  * Creates a new empty malloc heap with a specified name.
43799a2dd95SBruce Richardson  *
43899a2dd95SBruce Richardson  * @note Heaps created via this call will automatically get assigned a unique
43999a2dd95SBruce Richardson  *   socket ID, which can be found using ``rte_malloc_heap_get_socket()``
44099a2dd95SBruce Richardson  *
44199a2dd95SBruce Richardson  * @param heap_name
44299a2dd95SBruce Richardson  *   Name of the heap to create.
44399a2dd95SBruce Richardson  *
44499a2dd95SBruce Richardson  * @return
44599a2dd95SBruce Richardson  *   - 0 on successful creation
44699a2dd95SBruce Richardson  *   - -1 in case of error, with rte_errno set to one of the following:
44799a2dd95SBruce Richardson  *     EINVAL - ``heap_name`` was NULL, empty or too long
44899a2dd95SBruce Richardson  *     EEXIST - heap by name of ``heap_name`` already exists
44999a2dd95SBruce Richardson  *     ENOSPC - no more space in internal config to store a new heap
45099a2dd95SBruce Richardson  */
45199a2dd95SBruce Richardson int
45299a2dd95SBruce Richardson rte_malloc_heap_create(const char *heap_name);
45399a2dd95SBruce Richardson 
45499a2dd95SBruce Richardson /**
45599a2dd95SBruce Richardson  * Destroys a previously created malloc heap with specified name.
45699a2dd95SBruce Richardson  *
45799a2dd95SBruce Richardson  * @note This function will return a failure result if not all memory allocated
45899a2dd95SBruce Richardson  *   from the heap has been freed back to the heap
45999a2dd95SBruce Richardson  *
46099a2dd95SBruce Richardson  * @note This function will return a failure result if not all memory segments
46199a2dd95SBruce Richardson  *   were removed from the heap prior to its destruction
46299a2dd95SBruce Richardson  *
46399a2dd95SBruce Richardson  * @param heap_name
46499a2dd95SBruce Richardson  *   Name of the heap to create.
46599a2dd95SBruce Richardson  *
46699a2dd95SBruce Richardson  * @return
46799a2dd95SBruce Richardson  *   - 0 on success
46899a2dd95SBruce Richardson  *   - -1 in case of error, with rte_errno set to one of the following:
46999a2dd95SBruce Richardson  *     EINVAL - ``heap_name`` was NULL, empty or too long
47099a2dd95SBruce Richardson  *     ENOENT - heap by the name of ``heap_name`` was not found
47199a2dd95SBruce Richardson  *     EPERM  - attempting to destroy reserved heap
47299a2dd95SBruce Richardson  *     EBUSY  - heap still contains data
47399a2dd95SBruce Richardson  */
47499a2dd95SBruce Richardson int
47599a2dd95SBruce Richardson rte_malloc_heap_destroy(const char *heap_name);
47699a2dd95SBruce Richardson 
47799a2dd95SBruce Richardson /**
47899a2dd95SBruce Richardson  * Find socket ID corresponding to a named heap.
47999a2dd95SBruce Richardson  *
48099a2dd95SBruce Richardson  * @param name
48199a2dd95SBruce Richardson  *   Heap name to find socket ID for
48299a2dd95SBruce Richardson  * @return
48399a2dd95SBruce Richardson  *   Socket ID in case of success (a non-negative number)
48499a2dd95SBruce Richardson  *   -1 in case of error, with rte_errno set to one of the following:
48599a2dd95SBruce Richardson  *     EINVAL - ``name`` was NULL
48699a2dd95SBruce Richardson  *     ENOENT - heap identified by the name ``name`` was not found
48799a2dd95SBruce Richardson  */
48899a2dd95SBruce Richardson int
48999a2dd95SBruce Richardson rte_malloc_heap_get_socket(const char *name);
49099a2dd95SBruce Richardson 
49199a2dd95SBruce Richardson /**
49299a2dd95SBruce Richardson  * Check if a given socket ID refers to externally allocated memory.
49399a2dd95SBruce Richardson  *
49499a2dd95SBruce Richardson  * @note Passing SOCKET_ID_ANY will return 0.
49599a2dd95SBruce Richardson  *
49699a2dd95SBruce Richardson  * @param socket_id
49799a2dd95SBruce Richardson  *   Socket ID to check
49899a2dd95SBruce Richardson  * @return
49999a2dd95SBruce Richardson  *   1 if socket ID refers to externally allocated memory
50099a2dd95SBruce Richardson  *   0 if socket ID refers to internal DPDK memory
50199a2dd95SBruce Richardson  *   -1 if socket ID is invalid
50299a2dd95SBruce Richardson  */
50399a2dd95SBruce Richardson int
50499a2dd95SBruce Richardson rte_malloc_heap_socket_is_external(int socket_id);
50599a2dd95SBruce Richardson 
50699a2dd95SBruce Richardson /**
50799a2dd95SBruce Richardson  * Dump statistics.
50899a2dd95SBruce Richardson  *
50999a2dd95SBruce Richardson  * Dump for the specified type to a file. If the type argument is
51099a2dd95SBruce Richardson  * NULL, all memory types will be dumped.
51199a2dd95SBruce Richardson  *
51299a2dd95SBruce Richardson  * @note This function is not thread-safe with respect to
51399a2dd95SBruce Richardson  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
51499a2dd95SBruce Richardson  *
51599a2dd95SBruce Richardson  * @param f
51699a2dd95SBruce Richardson  *   A pointer to a file for output
51799a2dd95SBruce Richardson  * @param type
518ae539d90SStephen Hemminger  *   Deprecated parameter unused.
51999a2dd95SBruce Richardson  */
52099a2dd95SBruce Richardson void
52199a2dd95SBruce Richardson rte_malloc_dump_stats(FILE *f, const char *type);
52299a2dd95SBruce Richardson 
52399a2dd95SBruce Richardson /**
52499a2dd95SBruce Richardson  * Dump contents of all malloc heaps to a file.
52599a2dd95SBruce Richardson  *
52699a2dd95SBruce Richardson  * @note This function is not thread-safe with respect to
52799a2dd95SBruce Richardson  *    ``rte_malloc_heap_create()``/``rte_malloc_heap_destroy()`` functions.
52899a2dd95SBruce Richardson  *
52999a2dd95SBruce Richardson  * @param f
53099a2dd95SBruce Richardson  *   A pointer to a file for output
53199a2dd95SBruce Richardson  */
53299a2dd95SBruce Richardson void
53399a2dd95SBruce Richardson rte_malloc_dump_heaps(FILE *f);
53499a2dd95SBruce Richardson 
53599a2dd95SBruce Richardson /**
53699a2dd95SBruce Richardson  * Return the IO address of a virtual address obtained through
53799a2dd95SBruce Richardson  * rte_malloc
53899a2dd95SBruce Richardson  *
53999a2dd95SBruce Richardson  * @param addr
54099a2dd95SBruce Richardson  *   Address obtained from a previous rte_malloc call
54199a2dd95SBruce Richardson  * @return
54299a2dd95SBruce Richardson  *   RTE_BAD_IOVA on error
54399a2dd95SBruce Richardson  *   otherwise return an address suitable for IO
54499a2dd95SBruce Richardson  */
54599a2dd95SBruce Richardson rte_iova_t
54699a2dd95SBruce Richardson rte_malloc_virt2iova(const void *addr);
54799a2dd95SBruce Richardson 
54899a2dd95SBruce Richardson #ifdef __cplusplus
54999a2dd95SBruce Richardson }
55099a2dd95SBruce Richardson #endif
55199a2dd95SBruce Richardson 
55299a2dd95SBruce Richardson #endif /* _RTE_MALLOC_H_ */
553