xref: /openbmc/linux/io_uring/alloc_cache.h (revision 2fa5ebe3)
1 #ifndef IOU_ALLOC_CACHE_H
2 #define IOU_ALLOC_CACHE_H
3 
4 /*
5  * Don't allow the cache to grow beyond this size.
6  */
7 #define IO_ALLOC_CACHE_MAX	512
8 
9 struct io_cache_entry {
10 	struct hlist_node	node;
11 };
12 
13 static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
14 				      struct io_cache_entry *entry)
15 {
16 	if (cache->nr_cached < IO_ALLOC_CACHE_MAX) {
17 		cache->nr_cached++;
18 		hlist_add_head(&entry->node, &cache->list);
19 		return true;
20 	}
21 	return false;
22 }
23 
24 static inline struct io_cache_entry *io_alloc_cache_get(struct io_alloc_cache *cache)
25 {
26 	if (!hlist_empty(&cache->list)) {
27 		struct hlist_node *node = cache->list.first;
28 
29 		hlist_del(node);
30 		cache->nr_cached--;
31 		return container_of(node, struct io_cache_entry, node);
32 	}
33 
34 	return NULL;
35 }
36 
37 static inline void io_alloc_cache_init(struct io_alloc_cache *cache)
38 {
39 	INIT_HLIST_HEAD(&cache->list);
40 	cache->nr_cached = 0;
41 }
42 
43 static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
44 					void (*free)(struct io_cache_entry *))
45 {
46 	while (!hlist_empty(&cache->list)) {
47 		struct hlist_node *node = cache->list.first;
48 
49 		hlist_del(node);
50 		free(container_of(node, struct io_cache_entry, node));
51 	}
52 	cache->nr_cached = 0;
53 }
54 #endif
55