1 /* SPDX-License-Identifier: GPL-2.0 */
3 * memory buffer pool support
5 #ifndef _LINUX_MEMPOOL_H
6 #define _LINUX_MEMPOOL_H
8 #include <linux/sched.h>
9 #include <linux/alloc_tag.h>
10 #include <linux/wait.h>
11 #include <linux/compiler.h>
15 typedef void * (mempool_alloc_t
)(gfp_t gfp_mask
, void *pool_data
);
16 typedef void (mempool_free_t
)(void *element
, void *pool_data
);
18 typedef struct mempool_s
{
20 int min_nr
; /* nr of elements at *elements */
21 int curr_nr
; /* Current nr of elements at *elements */
25 mempool_alloc_t
*alloc
;
27 wait_queue_head_t wait
;
30 static inline bool mempool_initialized(mempool_t
*pool
)
32 return pool
->elements
!= NULL
;
35 static inline bool mempool_is_saturated(mempool_t
*pool
)
37 return READ_ONCE(pool
->curr_nr
) >= pool
->min_nr
;
40 void mempool_exit(mempool_t
*pool
);
41 int mempool_init_node(mempool_t
*pool
, int min_nr
, mempool_alloc_t
*alloc_fn
,
42 mempool_free_t
*free_fn
, void *pool_data
,
43 gfp_t gfp_mask
, int node_id
);
45 int mempool_init_noprof(mempool_t
*pool
, int min_nr
, mempool_alloc_t
*alloc_fn
,
46 mempool_free_t
*free_fn
, void *pool_data
);
47 #define mempool_init(...) \
48 alloc_hooks(mempool_init_noprof(__VA_ARGS__))
50 extern mempool_t
*mempool_create(int min_nr
, mempool_alloc_t
*alloc_fn
,
51 mempool_free_t
*free_fn
, void *pool_data
);
53 extern mempool_t
*mempool_create_node_noprof(int min_nr
, mempool_alloc_t
*alloc_fn
,
54 mempool_free_t
*free_fn
, void *pool_data
,
55 gfp_t gfp_mask
, int nid
);
56 #define mempool_create_node(...) \
57 alloc_hooks(mempool_create_node_noprof(__VA_ARGS__))
59 #define mempool_create(_min_nr, _alloc_fn, _free_fn, _pool_data) \
60 mempool_create_node(_min_nr, _alloc_fn, _free_fn, _pool_data, \
61 GFP_KERNEL, NUMA_NO_NODE)
63 extern int mempool_resize(mempool_t
*pool
, int new_min_nr
);
64 extern void mempool_destroy(mempool_t
*pool
);
66 extern void *mempool_alloc_noprof(mempool_t
*pool
, gfp_t gfp_mask
) __malloc
;
67 #define mempool_alloc(...) \
68 alloc_hooks(mempool_alloc_noprof(__VA_ARGS__))
70 extern void *mempool_alloc_preallocated(mempool_t
*pool
) __malloc
;
71 extern void mempool_free(void *element
, mempool_t
*pool
);
74 * A mempool_alloc_t and mempool_free_t that get the memory from
75 * a slab cache that is passed in through pool_data.
76 * Note: the slab cache may not have a ctor function.
78 void *mempool_alloc_slab(gfp_t gfp_mask
, void *pool_data
);
79 void mempool_free_slab(void *element
, void *pool_data
);
81 #define mempool_init_slab_pool(_pool, _min_nr, _kc) \
82 mempool_init(_pool, (_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc))
83 #define mempool_create_slab_pool(_min_nr, _kc) \
84 mempool_create((_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc))
87 * a mempool_alloc_t and a mempool_free_t to kmalloc and kfree the
88 * amount of memory specified by pool_data
90 void *mempool_kmalloc(gfp_t gfp_mask
, void *pool_data
);
91 void mempool_kfree(void *element
, void *pool_data
);
93 #define mempool_init_kmalloc_pool(_pool, _min_nr, _size) \
94 mempool_init(_pool, (_min_nr), mempool_kmalloc, mempool_kfree, \
95 (void *)(unsigned long)(_size))
96 #define mempool_create_kmalloc_pool(_min_nr, _size) \
97 mempool_create((_min_nr), mempool_kmalloc, mempool_kfree, \
98 (void *)(unsigned long)(_size))
100 void *mempool_kvmalloc(gfp_t gfp_mask
, void *pool_data
);
101 void mempool_kvfree(void *element
, void *pool_data
);
103 static inline int mempool_init_kvmalloc_pool(mempool_t
*pool
, int min_nr
, size_t size
)
105 return mempool_init(pool
, min_nr
, mempool_kvmalloc
, mempool_kvfree
, (void *) size
);
108 static inline mempool_t
*mempool_create_kvmalloc_pool(int min_nr
, size_t size
)
110 return mempool_create(min_nr
, mempool_kvmalloc
, mempool_kvfree
, (void *) size
);
114 * A mempool_alloc_t and mempool_free_t for a simple page allocator that
115 * allocates pages of the order specified by pool_data
117 void *mempool_alloc_pages(gfp_t gfp_mask
, void *pool_data
);
118 void mempool_free_pages(void *element
, void *pool_data
);
120 #define mempool_init_page_pool(_pool, _min_nr, _order) \
121 mempool_init(_pool, (_min_nr), mempool_alloc_pages, \
122 mempool_free_pages, (void *)(long)(_order))
123 #define mempool_create_page_pool(_min_nr, _order) \
124 mempool_create((_min_nr), mempool_alloc_pages, \
125 mempool_free_pages, (void *)(long)(_order))
127 #endif /* _LINUX_MEMPOOL_H */