xref: /dpdk/drivers/common/mlx5/mlx5_malloc.h (revision 1094dd940ec0cc4e3ce2c5cd94807350855a17f9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #ifndef MLX5_MALLOC_H_
6 #define MLX5_MALLOC_H_
7 
8 #include <rte_compat.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #ifndef MLX5_MALLOC_ALIGNMENT
15 #ifndef RTE_ARCH_64
16 #define MLX5_MALLOC_ALIGNMENT 8
17 #else
18 #define MLX5_MALLOC_ALIGNMENT 16
19 #endif
20 #endif
21 
22 enum mlx5_mem_flags {
23 	MLX5_MEM_ANY = 0,
24 	/* Memory will be allocated depends on sys_mem_en. */
25 	MLX5_MEM_SYS = 1 << 0,
26 	/* Memory should be allocated from system. */
27 	MLX5_MEM_RTE = 1 << 1,
28 	/* Memory should be allocated from rte hugepage. */
29 	MLX5_MEM_ZERO = 1 << 2,
30 	/* Memory should be cleared to zero. */
31 };
32 
33 /**
34  * Select the PMD memory allocate preference.
35  *
36  * Once sys_mem_en is set, the default memory allocate will from
37  * system only if an explicitly flag is set to order the memory
38  * from rte hugepage memory.
39  *
40  * @param sys_mem_en
41  *   Use system memory or not.
42  */
43 void mlx5_malloc_mem_select(uint32_t sys_mem_en);
44 
45 /**
46  * Dump the PMD memory usage statistic.
47  */
48 __rte_internal
49 void mlx5_memory_stat_dump(void);
50 
51 /**
52  * Memory allocate function.
53  *
54  * @param flags
55  *   The bits as enum mlx5_mem_flags defined.
56  * @param size
57  *   Memory size to be allocated.
58  * @param align
59  *   Memory alignment.
60  * @param socket
61  *   The socket memory should allocated.
62  *   Valid only when allocate the memory from rte hugepage.
63  *
64  * @return
65  *   Pointer of the allocated memory, NULL otherwise.
66  */
67 __rte_internal
68 void *mlx5_malloc(uint32_t flags, size_t size, unsigned int align, int socket);
69 
70 /**
71  * Memory reallocate function.
72  *
73  *
74  *
75  * @param addr
76  *   The memory to be reallocated.
77  * @param flags
78  *   The bits as enum mlx5_mem_flags defined.
79  * @param size
80  *   Memory size to be allocated.
81  * @param align
82  *   Memory alignment.
83  * @param socket
84  *   The socket memory should allocated.
85  *   Valid only when allocate the memory from rte hugepage.
86  *
87  * @return
88  *   Pointer of the allocated memory, NULL otherwise.
89  */
90 
91 __rte_internal
92 void *mlx5_realloc(void *addr, uint32_t flags, size_t size, unsigned int align,
93 		   int socket);
94 
95 /**
96  * Memory free function.
97  *
98  * @param addr
99  *   The memory address to be freed..
100  */
101 __rte_internal
102 void mlx5_free(void *addr);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif
109