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.

84 lines
1.7KB

  1. /*
  2. * decorate.c - decorate a git object with some arbitrary
  3. * data.
  4. */
  5. #include "cache.h"
  6. #include "object.h"
  7. #include "decorate.h"
  8. static unsigned int hash_obj(const struct object *obj, unsigned int n)
  9. {
  10. return oidhash(&obj->oid) % n;
  11. }
  12. static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
  13. {
  14. int size = n->size;
  15. struct decoration_entry *entries = n->entries;
  16. unsigned int j = hash_obj(base, size);
  17. while (entries[j].base) {
  18. if (entries[j].base == base) {
  19. void *old = entries[j].decoration;
  20. entries[j].decoration = decoration;
  21. return old;
  22. }
  23. if (++j >= size)
  24. j = 0;
  25. }
  26. entries[j].base = base;
  27. entries[j].decoration = decoration;
  28. n->nr++;
  29. return NULL;
  30. }
  31. static void grow_decoration(struct decoration *n)
  32. {
  33. int i;
  34. int old_size = n->size;
  35. struct decoration_entry *old_entries = n->entries;
  36. n->size = (old_size + 1000) * 3 / 2;
  37. n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
  38. n->nr = 0;
  39. for (i = 0; i < old_size; i++) {
  40. const struct object *base = old_entries[i].base;
  41. void *decoration = old_entries[i].decoration;
  42. if (!decoration)
  43. continue;
  44. insert_decoration(n, base, decoration);
  45. }
  46. free(old_entries);
  47. }
  48. void *add_decoration(struct decoration *n, const struct object *obj,
  49. void *decoration)
  50. {
  51. int nr = n->nr + 1;
  52. if (nr > n->size * 2 / 3)
  53. grow_decoration(n);
  54. return insert_decoration(n, obj, decoration);
  55. }
  56. void *lookup_decoration(struct decoration *n, const struct object *obj)
  57. {
  58. unsigned int j;
  59. /* nothing to lookup */
  60. if (!n->size)
  61. return NULL;
  62. j = hash_obj(obj, n->size);
  63. for (;;) {
  64. struct decoration_entry *ref = n->entries + j;
  65. if (ref->base == obj)
  66. return ref->decoration;
  67. if (!ref->base)
  68. return NULL;
  69. if (++j == n->size)
  70. j = 0;
  71. }
  72. }