THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4KB

  1. #include "cache.h"
  2. #include "oidmap.h"
  3. static int oidmap_neq(const void *hashmap_cmp_fn_data,
  4. const struct hashmap_entry *e1,
  5. const struct hashmap_entry *e2,
  6. const void *keydata)
  7. {
  8. const struct oidmap_entry *a, *b;
  9. a = container_of(e1, const struct oidmap_entry, internal_entry);
  10. b = container_of(e2, const struct oidmap_entry, internal_entry);
  11. if (keydata)
  12. return !oideq(&a->oid, (const struct object_id *) keydata);
  13. return !oideq(&a->oid, &b->oid);
  14. }
  15. void oidmap_init(struct oidmap *map, size_t initial_size)
  16. {
  17. hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
  18. }
  19. void oidmap_free(struct oidmap *map, int free_entries)
  20. {
  21. if (!map)
  22. return;
  23. /* TODO: make oidmap itself not depend on struct layouts */
  24. hashmap_free_(&map->map, free_entries ? 0 : -1);
  25. }
  26. void *oidmap_get(const struct oidmap *map, const struct object_id *key)
  27. {
  28. if (!map->map.cmpfn)
  29. return NULL;
  30. return hashmap_get_from_hash(&map->map, oidhash(key), key);
  31. }
  32. void *oidmap_remove(struct oidmap *map, const struct object_id *key)
  33. {
  34. struct hashmap_entry entry;
  35. if (!map->map.cmpfn)
  36. oidmap_init(map, 0);
  37. hashmap_entry_init(&entry, oidhash(key));
  38. return hashmap_remove(&map->map, &entry, key);
  39. }
  40. void *oidmap_put(struct oidmap *map, void *entry)
  41. {
  42. struct oidmap_entry *to_put = entry;
  43. if (!map->map.cmpfn)
  44. oidmap_init(map, 0);
  45. hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
  46. return hashmap_put(&map->map, &to_put->internal_entry);
  47. }