2022-07-07 23:16:20 +03:00
|
|
|
#ifndef IOU_ALLOC_CACHE_H
|
|
|
|
#define IOU_ALLOC_CACHE_H
|
|
|
|
|
2022-07-07 23:20:54 +03:00
|
|
|
/*
|
|
|
|
* Don't allow the cache to grow beyond this size.
|
|
|
|
*/
|
|
|
|
#define IO_ALLOC_CACHE_MAX 512
|
|
|
|
|
2022-07-07 23:16:20 +03:00
|
|
|
struct io_cache_entry {
|
|
|
|
struct hlist_node node;
|
|
|
|
};
|
|
|
|
|
2022-07-07 23:20:54 +03:00
|
|
|
static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
|
2022-07-07 23:16:20 +03:00
|
|
|
struct io_cache_entry *entry)
|
|
|
|
{
|
2022-07-07 23:20:54 +03:00
|
|
|
if (cache->nr_cached < IO_ALLOC_CACHE_MAX) {
|
|
|
|
cache->nr_cached++;
|
|
|
|
hlist_add_head(&entry->node, &cache->list);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-07-07 23:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct io_cache_entry *io_alloc_cache_get(struct io_alloc_cache *cache)
|
|
|
|
{
|
|
|
|
if (!hlist_empty(&cache->list)) {
|
|
|
|
struct hlist_node *node = cache->list.first;
|
|
|
|
|
|
|
|
hlist_del(node);
|
2023-03-30 15:52:38 +03:00
|
|
|
cache->nr_cached--;
|
2022-07-07 23:16:20 +03:00
|
|
|
return container_of(node, struct io_cache_entry, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void io_alloc_cache_init(struct io_alloc_cache *cache)
|
|
|
|
{
|
|
|
|
INIT_HLIST_HEAD(&cache->list);
|
2022-07-07 23:20:54 +03:00
|
|
|
cache->nr_cached = 0;
|
2022-07-07 23:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
|
|
|
|
void (*free)(struct io_cache_entry *))
|
|
|
|
{
|
|
|
|
while (!hlist_empty(&cache->list)) {
|
|
|
|
struct hlist_node *node = cache->list.first;
|
|
|
|
|
|
|
|
hlist_del(node);
|
|
|
|
free(container_of(node, struct io_cache_entry, node));
|
|
|
|
}
|
2022-07-07 23:20:54 +03:00
|
|
|
cache->nr_cached = 0;
|
2022-07-07 23:16:20 +03:00
|
|
|
}
|
|
|
|
#endif
|