xref: /dpdk/lib/eal/common/eal_memalloc.h (revision 4dd146e597d81d900140a904e0b3b9feb1b0be6c)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017-2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef EAL_MEMALLOC_H
699a2dd95SBruce Richardson #define EAL_MEMALLOC_H
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <stdbool.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include <rte_memory.h>
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson /*
1399a2dd95SBruce Richardson  * Allocate segment of specified page size.
1499a2dd95SBruce Richardson  */
1599a2dd95SBruce Richardson struct rte_memseg *
1699a2dd95SBruce Richardson eal_memalloc_alloc_seg(size_t page_sz, int socket);
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson /*
1999a2dd95SBruce Richardson  * Allocate `n_segs` segments.
2099a2dd95SBruce Richardson  *
2199a2dd95SBruce Richardson  * Note: `ms` can be NULL.
2299a2dd95SBruce Richardson  *
2399a2dd95SBruce Richardson  * Note: it is possible to request best-effort allocation by setting `exact` to
2499a2dd95SBruce Richardson  * `false`, in which case allocator will return however many pages it managed to
2599a2dd95SBruce Richardson  * allocate successfully.
2699a2dd95SBruce Richardson  */
2799a2dd95SBruce Richardson int
2899a2dd95SBruce Richardson eal_memalloc_alloc_seg_bulk(struct rte_memseg **ms, int n_segs, size_t page_sz,
2999a2dd95SBruce Richardson 		int socket, bool exact);
3099a2dd95SBruce Richardson 
3199a2dd95SBruce Richardson /*
3299a2dd95SBruce Richardson  * Deallocate segment
3399a2dd95SBruce Richardson  */
3499a2dd95SBruce Richardson int
3599a2dd95SBruce Richardson eal_memalloc_free_seg(struct rte_memseg *ms);
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson /*
3899a2dd95SBruce Richardson  * Deallocate `n_segs` segments. Returns 0 on successful deallocation of all
3999a2dd95SBruce Richardson  * segments, returns -1 on error. Any segments that could have been deallocated,
4099a2dd95SBruce Richardson  * will be deallocated even in case of error.
4199a2dd95SBruce Richardson  */
4299a2dd95SBruce Richardson int
4399a2dd95SBruce Richardson eal_memalloc_free_seg_bulk(struct rte_memseg **ms, int n_segs);
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson /*
4699a2dd95SBruce Richardson  * Check if memory pointed to by `start` and of `length` that resides in
4799a2dd95SBruce Richardson  * memseg list `msl` is IOVA-contiguous.
4899a2dd95SBruce Richardson  */
4999a2dd95SBruce Richardson bool
5099a2dd95SBruce Richardson eal_memalloc_is_contig(const struct rte_memseg_list *msl, void *start,
5199a2dd95SBruce Richardson 		size_t len);
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson /* synchronize local memory map to primary process */
5499a2dd95SBruce Richardson int
5599a2dd95SBruce Richardson eal_memalloc_sync_with_primary(void);
5699a2dd95SBruce Richardson 
5799a2dd95SBruce Richardson int
5899a2dd95SBruce Richardson eal_memalloc_mem_event_callback_register(const char *name,
5999a2dd95SBruce Richardson 		rte_mem_event_callback_t clb, void *arg);
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson int
6299a2dd95SBruce Richardson eal_memalloc_mem_event_callback_unregister(const char *name, void *arg);
6399a2dd95SBruce Richardson 
6499a2dd95SBruce Richardson void
6599a2dd95SBruce Richardson eal_memalloc_mem_event_notify(enum rte_mem_event event, const void *start,
6699a2dd95SBruce Richardson 		size_t len);
6799a2dd95SBruce Richardson 
6899a2dd95SBruce Richardson int
6999a2dd95SBruce Richardson eal_memalloc_mem_alloc_validator_register(const char *name,
7099a2dd95SBruce Richardson 		rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson int
7399a2dd95SBruce Richardson eal_memalloc_mem_alloc_validator_unregister(const char *name, int socket_id);
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson int
7699a2dd95SBruce Richardson eal_memalloc_mem_alloc_validate(int socket_id, size_t new_len);
7799a2dd95SBruce Richardson 
7899a2dd95SBruce Richardson /* returns fd or -errno */
7999a2dd95SBruce Richardson int
8099a2dd95SBruce Richardson eal_memalloc_get_seg_fd(int list_idx, int seg_idx);
8199a2dd95SBruce Richardson 
8299a2dd95SBruce Richardson /* returns 0 or -errno */
8399a2dd95SBruce Richardson int
8499a2dd95SBruce Richardson eal_memalloc_set_seg_fd(int list_idx, int seg_idx, int fd);
8599a2dd95SBruce Richardson 
8699a2dd95SBruce Richardson /* returns 0 or -errno */
8799a2dd95SBruce Richardson int
8899a2dd95SBruce Richardson eal_memalloc_set_seg_list_fd(int list_idx, int fd);
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson int
9199a2dd95SBruce Richardson eal_memalloc_get_seg_fd_offset(int list_idx, int seg_idx, size_t *offset);
9299a2dd95SBruce Richardson 
9399a2dd95SBruce Richardson int
94*4dd146e5SArtemy Kovalyov eal_memalloc_init(void)
95*4dd146e5SArtemy Kovalyov 	__rte_shared_locks_required(rte_mcfg_mem_get_lock());
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson int
9899a2dd95SBruce Richardson eal_memalloc_cleanup(void);
9999a2dd95SBruce Richardson 
10099a2dd95SBruce Richardson #endif /* EAL_MEMALLOC_H */
101