xref: /linux-6.15/io_uring/alloc_cache.h (revision efba1a9e)
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 io_wq_work_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 		wq_stack_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 (cache->list.next) {
27 		struct io_cache_entry *entry;
28 
29 		entry = container_of(cache->list.next, struct io_cache_entry, node);
30 		cache->list.next = cache->list.next->next;
31 		cache->nr_cached--;
32 		return entry;
33 	}
34 
35 	return NULL;
36 }
37 
38 static inline void io_alloc_cache_init(struct io_alloc_cache *cache)
39 {
40 	cache->list.next = NULL;
41 	cache->nr_cached = 0;
42 }
43 
44 static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
45 					void (*free)(struct io_cache_entry *))
46 {
47 	while (1) {
48 		struct io_cache_entry *entry = io_alloc_cache_get(cache);
49 
50 		if (!entry)
51 			break;
52 		free(entry);
53 	}
54 	cache->nr_cached = 0;
55 }
56 #endif
57