File: alloc_cache.h

package info (click to toggle)
linux 6.1.148-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 1,496,492 kB
  • sloc: ansic: 23,474,232; asm: 266,624; sh: 110,569; makefile: 49,899; python: 36,948; perl: 36,834; cpp: 6,055; yacc: 4,908; lex: 2,725; awk: 1,440; ruby: 25; sed: 5
file content (54 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef IOU_ALLOC_CACHE_H
#define IOU_ALLOC_CACHE_H

/*
 * Don't allow the cache to grow beyond this size.
 */
#define IO_ALLOC_CACHE_MAX	512

struct io_cache_entry {
	struct hlist_node	node;
};

static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
				      struct io_cache_entry *entry)
{
	if (cache->nr_cached < IO_ALLOC_CACHE_MAX) {
		cache->nr_cached++;
		hlist_add_head(&entry->node, &cache->list);
		return true;
	}
	return false;
}

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);
		cache->nr_cached--;
		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);
	cache->nr_cached = 0;
}

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));
	}
	cache->nr_cached = 0;
}
#endif