at master 855 B view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Copyright (C) 2024 Google LLC 4 */ 5 6#include "gendwarfksyms.h" 7 8struct cache_item { 9 unsigned long key; 10 int value; 11 struct hlist_node hash; 12}; 13 14void cache_set(struct cache *cache, unsigned long key, int value) 15{ 16 struct cache_item *ci; 17 18 ci = xmalloc(sizeof(*ci)); 19 ci->key = key; 20 ci->value = value; 21 hash_add(cache->cache, &ci->hash, hash_32(key)); 22} 23 24int 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 36void cache_init(struct cache *cache) 37{ 38 hash_init(cache->cache); 39} 40 41void 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}