0001 #ifndef IOU_ALLOC_CACHE_H
0002 #define IOU_ALLOC_CACHE_H
0003
0004
0005
0006
0007 #define IO_ALLOC_CACHE_MAX 512
0008
0009 struct io_cache_entry {
0010 struct hlist_node node;
0011 };
0012
0013 static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
0014 struct io_cache_entry *entry)
0015 {
0016 if (cache->nr_cached < IO_ALLOC_CACHE_MAX) {
0017 cache->nr_cached++;
0018 hlist_add_head(&entry->node, &cache->list);
0019 return true;
0020 }
0021 return false;
0022 }
0023
0024 static inline struct io_cache_entry *io_alloc_cache_get(struct io_alloc_cache *cache)
0025 {
0026 if (!hlist_empty(&cache->list)) {
0027 struct hlist_node *node = cache->list.first;
0028
0029 hlist_del(node);
0030 return container_of(node, struct io_cache_entry, node);
0031 }
0032
0033 return NULL;
0034 }
0035
0036 static inline void io_alloc_cache_init(struct io_alloc_cache *cache)
0037 {
0038 INIT_HLIST_HEAD(&cache->list);
0039 cache->nr_cached = 0;
0040 }
0041
0042 static inline void io_alloc_cache_free(struct io_alloc_cache *cache,
0043 void (*free)(struct io_cache_entry *))
0044 {
0045 while (!hlist_empty(&cache->list)) {
0046 struct hlist_node *node = cache->list.first;
0047
0048 hlist_del(node);
0049 free(container_of(node, struct io_cache_entry, node));
0050 }
0051 cache->nr_cached = 0;
0052 }
0053 #endif