199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef MALLOC_HEAP_H_ 699a2dd95SBruce Richardson #define MALLOC_HEAP_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson #include <stdbool.h> 999a2dd95SBruce Richardson #include <sys/queue.h> 1099a2dd95SBruce Richardson 1199a2dd95SBruce Richardson #include <rte_malloc.h> 1299a2dd95SBruce Richardson #include <rte_spinlock.h> 1399a2dd95SBruce Richardson 1499a2dd95SBruce Richardson /* Number of free lists per heap, grouped by size. */ 1599a2dd95SBruce Richardson #define RTE_HEAP_NUM_FREELISTS 13 1699a2dd95SBruce Richardson #define RTE_HEAP_NAME_MAX_LEN 32 1799a2dd95SBruce Richardson 1899a2dd95SBruce Richardson /* dummy definition, for pointers */ 1999a2dd95SBruce Richardson struct malloc_elem; 2099a2dd95SBruce Richardson 2199a2dd95SBruce Richardson /** 2299a2dd95SBruce Richardson * Structure to hold malloc heap 2399a2dd95SBruce Richardson */ 24c6552d9aSTyler Retzlaff struct __rte_cache_aligned malloc_heap { 2599a2dd95SBruce Richardson rte_spinlock_t lock; 2699a2dd95SBruce Richardson LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS]; 2799a2dd95SBruce Richardson struct malloc_elem *volatile first; 2899a2dd95SBruce Richardson struct malloc_elem *volatile last; 2999a2dd95SBruce Richardson 3099a2dd95SBruce Richardson unsigned int alloc_count; 3199a2dd95SBruce Richardson unsigned int socket_id; 3299a2dd95SBruce Richardson size_t total_size; 3399a2dd95SBruce Richardson char name[RTE_HEAP_NAME_MAX_LEN]; 34c6552d9aSTyler Retzlaff }; 3599a2dd95SBruce Richardson 3699a2dd95SBruce Richardson void * 37*62ea63a6SStephen Hemminger malloc_heap_alloc(size_t size, int socket, unsigned int flags, size_t align, 38*62ea63a6SStephen Hemminger size_t bound, bool contig); 3999a2dd95SBruce Richardson 4099a2dd95SBruce Richardson void * 41*62ea63a6SStephen Hemminger malloc_heap_alloc_biggest(int socket, unsigned int flags, size_t align, bool contig); 4299a2dd95SBruce Richardson 4399a2dd95SBruce Richardson int 4499a2dd95SBruce Richardson malloc_heap_create(struct malloc_heap *heap, const char *heap_name); 4599a2dd95SBruce Richardson 4699a2dd95SBruce Richardson int 4799a2dd95SBruce Richardson malloc_heap_destroy(struct malloc_heap *heap); 4899a2dd95SBruce Richardson 4999a2dd95SBruce Richardson struct rte_memseg_list * 5099a2dd95SBruce Richardson malloc_heap_create_external_seg(void *va_addr, rte_iova_t iova_addrs[], 5199a2dd95SBruce Richardson unsigned int n_pages, size_t page_sz, const char *seg_name, 5299a2dd95SBruce Richardson unsigned int socket_id); 5399a2dd95SBruce Richardson 5499a2dd95SBruce Richardson struct rte_memseg_list * 5599a2dd95SBruce Richardson malloc_heap_find_external_seg(void *va_addr, size_t len); 5699a2dd95SBruce Richardson 5799a2dd95SBruce Richardson int 5899a2dd95SBruce Richardson malloc_heap_destroy_external_seg(struct rte_memseg_list *msl); 5999a2dd95SBruce Richardson 6099a2dd95SBruce Richardson int 6199a2dd95SBruce Richardson malloc_heap_add_external_memory(struct malloc_heap *heap, 6299a2dd95SBruce Richardson struct rte_memseg_list *msl); 6399a2dd95SBruce Richardson 6499a2dd95SBruce Richardson int 6599a2dd95SBruce Richardson malloc_heap_remove_external_memory(struct malloc_heap *heap, void *va_addr, 6699a2dd95SBruce Richardson size_t len); 6799a2dd95SBruce Richardson 6899a2dd95SBruce Richardson int 6999a2dd95SBruce Richardson malloc_heap_free(struct malloc_elem *elem); 7099a2dd95SBruce Richardson 7199a2dd95SBruce Richardson int 7299a2dd95SBruce Richardson malloc_heap_resize(struct malloc_elem *elem, size_t size); 7399a2dd95SBruce Richardson 7499a2dd95SBruce Richardson int 7599a2dd95SBruce Richardson malloc_heap_get_stats(struct malloc_heap *heap, 7699a2dd95SBruce Richardson struct rte_malloc_socket_stats *socket_stats); 7799a2dd95SBruce Richardson 7899a2dd95SBruce Richardson void 7999a2dd95SBruce Richardson malloc_heap_dump(struct malloc_heap *heap, FILE *f); 8099a2dd95SBruce Richardson 8199a2dd95SBruce Richardson int 8299a2dd95SBruce Richardson malloc_socket_to_heap_id(unsigned int socket_id); 8399a2dd95SBruce Richardson 8499a2dd95SBruce Richardson int 8599a2dd95SBruce Richardson rte_eal_malloc_heap_init(void); 8699a2dd95SBruce Richardson 872e2f0272SDavid Marchand int 882e2f0272SDavid Marchand rte_eal_malloc_heap_populate(void); 892e2f0272SDavid Marchand 90a0cc7be2SStephen Hemminger void 91a0cc7be2SStephen Hemminger rte_eal_malloc_heap_cleanup(void); 92a0cc7be2SStephen Hemminger 9399a2dd95SBruce Richardson #endif /* MALLOC_HEAP_H_ */ 94