Merge branch 'work/revert-gitk' into devel
[4msysgit-hv.git] / decorate.c
bloba216001dd87c1999b22798ea89d7171a5fb8c62a
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 const void *p = obj->sha1;
12 unsigned int hash = *(const unsigned int *)p;
13 return hash % n;
16 static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
18 int size = n->size;
19 struct object_decoration *hash = n->hash;
20 int j = hash_obj(base, size);
22 while (hash[j].base) {
23 if (hash[j].base == base) {
24 void *old = hash[j].decoration;
25 hash[j].decoration = decoration;
26 return old;
28 if (++j >= size)
29 j = 0;
31 hash[j].base = base;
32 hash[j].decoration = decoration;
33 n->nr++;
34 return NULL;
37 static void grow_decoration(struct decoration *n)
39 int i;
40 int old_size = n->size;
41 struct object_decoration *old_hash = n->hash;
43 n->size = (old_size + 1000) * 3 / 2;
44 n->hash = xcalloc(n->size, sizeof(struct object_decoration));
45 n->nr = 0;
47 for (i = 0; i < old_size; i++) {
48 const struct object *base = old_hash[i].base;
49 void *decoration = old_hash[i].decoration;
51 if (!base)
52 continue;
53 insert_decoration(n, base, decoration);
55 free(old_hash);
58 /* Add a decoration pointer, return any old one */
59 void *add_decoration(struct decoration *n, const struct object *obj,
60 void *decoration)
62 int nr = n->nr + 1;
64 if (nr > n->size * 2 / 3)
65 grow_decoration(n);
66 return insert_decoration(n, obj, decoration);
69 /* Lookup a decoration pointer */
70 void *lookup_decoration(struct decoration *n, const struct object *obj)
72 int j;
74 /* nothing to lookup */
75 if (!n->size)
76 return NULL;
77 j = hash_obj(obj, n->size);
78 for (;;) {
79 struct object_decoration *ref = n->hash + j;
80 if (ref->base == obj)
81 return ref->decoration;
82 if (!ref->base)
83 return NULL;
84 if (++j == n->size)
85 j = 0;