docs: address inaccurate `--empty` default with `--exec`
[alt-git.git] / decorate.c
blob69aeb142b45e9fee08d61dace73f6dc2bce1c2d0
1 /*
2 * decorate.c - decorate a git object with some arbitrary
3 * data.
4 */
5 #include "git-compat-util.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 CALLOC_ARRAY(n->entries, n->size);
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;
85 void clear_decoration(struct decoration *n, void (*free_cb)(void *))
87 if (free_cb) {
88 unsigned int i;
89 for (i = 0; i < n->size; i++) {
90 void *d = n->entries[i].decoration;
91 if (d)
92 free_cb(d);
96 FREE_AND_NULL(n->entries);
97 n->size = n->nr = 0;