Merge branch 'jk/forbid-lf-in-git-url' into maint
[git/debian.git] / decorate.c
bloba605b1b5f4ac7844693919ca8afee3a699d1b424
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"
9 static unsigned int hash_obj(const struct object *obj, unsigned int n)
11 return oidhash(&obj->oid) % n;
14 static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
16 int size = n->size;
17 struct decoration_entry *entries = n->entries;
18 unsigned int j = hash_obj(base, size);
20 while (entries[j].base) {
21 if (entries[j].base == base) {
22 void *old = entries[j].decoration;
23 entries[j].decoration = decoration;
24 return old;
26 if (++j >= size)
27 j = 0;
29 entries[j].base = base;
30 entries[j].decoration = decoration;
31 n->nr++;
32 return NULL;
35 static void grow_decoration(struct decoration *n)
37 int i;
38 int old_size = n->size;
39 struct decoration_entry *old_entries = n->entries;
41 n->size = (old_size + 1000) * 3 / 2;
42 n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
43 n->nr = 0;
45 for (i = 0; i < old_size; i++) {
46 const struct object *base = old_entries[i].base;
47 void *decoration = old_entries[i].decoration;
49 if (!decoration)
50 continue;
51 insert_decoration(n, base, decoration);
53 free(old_entries);
56 void *add_decoration(struct decoration *n, const struct object *obj,
57 void *decoration)
59 int nr = n->nr + 1;
61 if (nr > n->size * 2 / 3)
62 grow_decoration(n);
63 return insert_decoration(n, obj, decoration);
66 void *lookup_decoration(struct decoration *n, const struct object *obj)
68 unsigned int j;
70 /* nothing to lookup */
71 if (!n->size)
72 return NULL;
73 j = hash_obj(obj, n->size);
74 for (;;) {
75 struct decoration_entry *ref = n->entries + j;
76 if (ref->base == obj)
77 return ref->decoration;
78 if (!ref->base)
79 return NULL;
80 if (++j == n->size)
81 j = 0;