xref: /dpdk/lib/eal/common/malloc_heap.h (revision 62ea63a6c23c982e1fb58fb7d23d40d947d4ecbf)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef MALLOC_HEAP_H_
6 #define MALLOC_HEAP_H_
7 
8 #include <stdbool.h>
9 #include <sys/queue.h>
10 
11 #include <rte_malloc.h>
12 #include <rte_spinlock.h>
13 
14 /* Number of free lists per heap, grouped by size. */
15 #define RTE_HEAP_NUM_FREELISTS  13
16 #define RTE_HEAP_NAME_MAX_LEN 32
17 
18 /* dummy definition, for pointers */
19 struct malloc_elem;
20 
21 /**
22  * Structure to hold malloc heap
23  */
24 struct __rte_cache_aligned malloc_heap {
25 	rte_spinlock_t lock;
26 	LIST_HEAD(, malloc_elem) free_head[RTE_HEAP_NUM_FREELISTS];
27 	struct malloc_elem *volatile first;
28 	struct malloc_elem *volatile last;
29 
30 	unsigned int alloc_count;
31 	unsigned int socket_id;
32 	size_t total_size;
33 	char name[RTE_HEAP_NAME_MAX_LEN];
34 };
35 
36 void *
37 malloc_heap_alloc(size_t size, int socket, unsigned int flags, size_t align,
38 		  size_t bound, bool contig);
39 
40 void *
41 malloc_heap_alloc_biggest(int socket, unsigned int flags, size_t align, bool contig);
42 
43 int
44 malloc_heap_create(struct malloc_heap *heap, const char *heap_name);
45 
46 int
47 malloc_heap_destroy(struct malloc_heap *heap);
48 
49 struct rte_memseg_list *
50 malloc_heap_create_external_seg(void *va_addr, rte_iova_t iova_addrs[],
51 		unsigned int n_pages, size_t page_sz, const char *seg_name,
52 		unsigned int socket_id);
53 
54 struct rte_memseg_list *
55 malloc_heap_find_external_seg(void *va_addr, size_t len);
56 
57 int
58 malloc_heap_destroy_external_seg(struct rte_memseg_list *msl);
59 
60 int
61 malloc_heap_add_external_memory(struct malloc_heap *heap,
62 		struct rte_memseg_list *msl);
63 
64 int
65 malloc_heap_remove_external_memory(struct malloc_heap *heap, void *va_addr,
66 		size_t len);
67 
68 int
69 malloc_heap_free(struct malloc_elem *elem);
70 
71 int
72 malloc_heap_resize(struct malloc_elem *elem, size_t size);
73 
74 int
75 malloc_heap_get_stats(struct malloc_heap *heap,
76 		struct rte_malloc_socket_stats *socket_stats);
77 
78 void
79 malloc_heap_dump(struct malloc_heap *heap, FILE *f);
80 
81 int
82 malloc_socket_to_heap_id(unsigned int socket_id);
83 
84 int
85 rte_eal_malloc_heap_init(void);
86 
87 int
88 rte_eal_malloc_heap_populate(void);
89 
90 void
91 rte_eal_malloc_heap_cleanup(void);
92 
93 #endif /* MALLOC_HEAP_H_ */
94