xref: /linux/scripts/gendwarfksyms/cache.c (revision ba6ec09911b805778a2fed6d626bfe77b011a717)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2024 Google LLC
4  */
5 
6 #include "gendwarfksyms.h"
7 
8 struct cache_item {
9 	unsigned long key;
10 	int value;
11 	struct hlist_node hash;
12 };
13 
cache_set(struct cache * cache,unsigned long key,int value)14 void cache_set(struct cache *cache, unsigned long key, int value)
15 {
16 	struct cache_item *ci;
17 
18 	ci = xmalloc(sizeof(struct cache_item));
19 	ci->key = key;
20 	ci->value = value;
21 	hash_add(cache->cache, &ci->hash, hash_32(key));
22 }
23 
cache_get(struct cache * cache,unsigned long key)24 int cache_get(struct cache *cache, unsigned long key)
25 {
26 	struct cache_item *ci;
27 
28 	hash_for_each_possible(cache->cache, ci, hash, hash_32(key)) {
29 		if (ci->key == key)
30 			return ci->value;
31 	}
32 
33 	return -1;
34 }
35 
cache_init(struct cache * cache)36 void cache_init(struct cache *cache)
37 {
38 	hash_init(cache->cache);
39 }
40 
cache_free(struct cache * cache)41 void cache_free(struct cache *cache)
42 {
43 	struct hlist_node *tmp;
44 	struct cache_item *ci;
45 
46 	hash_for_each_safe(cache->cache, ci, tmp, hash) {
47 		free(ci);
48 	}
49 
50 	hash_init(cache->cache);
51 }
52