cache.c 855 B

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