Commit | Line | Data |
---|---|---|
9b797a37 JA |
1 | #ifndef IOU_ALLOC_CACHE_H |
2 | #define IOU_ALLOC_CACHE_H | |
3 | ||
9731bc98 JA |
4 | /* |
5 | * Don't allow the cache to grow beyond this size. | |
6 | */ | |
7 | #define IO_ALLOC_CACHE_MAX 512 | |
8 | ||
9b797a37 | 9 | struct io_cache_entry { |
efba1a9e | 10 | struct io_wq_work_node node; |
9b797a37 JA |
11 | }; |
12 | ||
9731bc98 | 13 | static inline bool io_alloc_cache_put(struct io_alloc_cache *cache, |
9b797a37 JA |
14 | struct io_cache_entry *entry) |
15 | { | |
69bbc6ad | 16 | if (cache->nr_cached < cache->max_cached) { |
9731bc98 | 17 | cache->nr_cached++; |
efba1a9e | 18 | wq_stack_add_head(&entry->node, &cache->list); |
280ec6cc | 19 | kasan_mempool_poison_object(entry); |
9731bc98 JA |
20 | return true; |
21 | } | |
22 | return false; | |
9b797a37 JA |
23 | } |
24 | ||
528407b1 PB |
25 | static inline bool io_alloc_cache_empty(struct io_alloc_cache *cache) |
26 | { | |
27 | return !cache->list.next; | |
28 | } | |
29 | ||
9b797a37 JA |
30 | static inline struct io_cache_entry *io_alloc_cache_get(struct io_alloc_cache *cache) |
31 | { | |
efba1a9e BL |
32 | if (cache->list.next) { |
33 | struct io_cache_entry *entry; | |
9b797a37 | 34 | |
efba1a9e | 35 | entry = container_of(cache->list.next, struct io_cache_entry, node); |
8ab3b097 | 36 | kasan_mempool_unpoison_object(entry, cache->elem_size); |
efba1a9e | 37 | cache->list.next = cache->list.next->next; |
fd30d1cd | 38 | cache->nr_cached--; |
efba1a9e | 39 | return entry; |
9b797a37 JA |
40 | } |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
69bbc6ad PB |
45 | static inline void io_alloc_cache_init(struct io_alloc_cache *cache, |
46 | unsigned max_nr, size_t size) | |
9b797a37 | 47 | { |
efba1a9e | 48 | cache->list.next = NULL; |
9731bc98 | 49 | cache->nr_cached = 0; |
69bbc6ad | 50 | cache->max_cached = max_nr; |
e1fe7ee8 | 51 | cache->elem_size = size; |
9b797a37 JA |
52 | } |
53 | ||
54 | static inline void io_alloc_cache_free(struct io_alloc_cache *cache, | |
55 | void (*free)(struct io_cache_entry *)) | |
56 | { | |
efba1a9e BL |
57 | while (1) { |
58 | struct io_cache_entry *entry = io_alloc_cache_get(cache); | |
9b797a37 | 59 | |
efba1a9e BL |
60 | if (!entry) |
61 | break; | |
62 | free(entry); | |
9b797a37 | 63 | } |
9731bc98 | 64 | cache->nr_cached = 0; |
9b797a37 JA |
65 | } |
66 | #endif |