summaryrefslogtreecommitdiff
path: root/scripts/gendwarfksyms/cache.c
blob: c9c19b86a686f8d2177997607ce52016462203c4 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2024 Google LLC
 */

#include "gendwarfksyms.h"

struct cache_item {
	unsigned long key;
	int value;
	struct hlist_node hash;
};

void cache_set(struct cache *cache, unsigned long key, int value)
{
	struct cache_item *ci;

	ci = xmalloc(sizeof(struct cache_item));
	ci->key = key;
	ci->value = value;
	hash_add(cache->cache, &ci->hash, hash_32(key));
}

int cache_get(struct cache *cache, unsigned long key)
{
	struct cache_item *ci;

	hash_for_each_possible(cache->cache, ci, hash, hash_32(key)) {
		if (ci->key == key)
			return ci->value;
	}

	return -1;
}

void cache_init(struct cache *cache)
{
	hash_init(cache->cache);
}

void cache_free(struct cache *cache)
{
	struct hlist_node *tmp;
	struct cache_item *ci;

	hash_for_each_safe(cache->cache, ci, tmp, hash) {
		free(ci);
	}

	hash_init(cache->cache);
}