1 #ifndef IOU_ALLOC_CACHE_H
2 #define IOU_ALLOC_CACHE_H
5 * Don't allow the cache to grow beyond this size.
7 #define IO_ALLOC_CACHE_MAX 128
9 static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
12 if (cache->nr_cached < cache->max_cached) {
13 if (!kasan_mempool_poison_object(entry))
15 cache->entries[cache->nr_cached++] = entry;
21 static inline void *io_alloc_cache_get(struct io_alloc_cache *cache)
23 if (cache->nr_cached) {
24 void *entry = cache->entries[--cache->nr_cached];
26 kasan_mempool_unpoison_object(entry, cache->elem_size);
33 /* returns false if the cache was initialized properly */
34 static inline bool io_alloc_cache_init(struct io_alloc_cache *cache,
35 unsigned max_nr, size_t size)
37 cache->entries = kvmalloc_array(max_nr, sizeof(void *), GFP_KERNEL);
40 cache->max_cached = max_nr;
41 cache->elem_size = size;
47 static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
48 void (*free)(const void *))
55 while ((entry = io_alloc_cache_get(cache)) != NULL)
58 kvfree(cache->entries);
59 cache->entries = NULL;