]> git.ipfire.org Git - thirdparty/git.git/blame - decorate.c
RelNotes: the eighth batch
[thirdparty/git.git] / decorate.c
CommitLineData
a59b276e
LT
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
54988bda 9static unsigned int hash_obj(const struct object *obj, unsigned int n)
a59b276e 10{
ed1c9977 11 return sha1hash(obj->oid.hash) % n;
a59b276e
LT
12}
13
54988bda 14static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
a59b276e
LT
15{
16 int size = n->size;
17 struct object_decoration *hash = n->hash;
91fe2f90 18 unsigned int j = hash_obj(base, size);
a59b276e
LT
19
20 while (hash[j].base) {
21 if (hash[j].base == base) {
22 void *old = hash[j].decoration;
23 hash[j].decoration = decoration;
24 return old;
25 }
a59b276e
LT
26 if (++j >= size)
27 j = 0;
28 }
29 hash[j].base = base;
30 hash[j].decoration = decoration;
31 n->nr++;
32 return NULL;
33}
34
35static void grow_decoration(struct decoration *n)
36{
37 int i;
38 int old_size = n->size;
69913575 39 struct object_decoration *old_hash = n->hash;
a59b276e
LT
40
41 n->size = (old_size + 1000) * 3 / 2;
42 n->hash = xcalloc(n->size, sizeof(struct object_decoration));
43 n->nr = 0;
44
45 for (i = 0; i < old_size; i++) {
54988bda 46 const struct object *base = old_hash[i].base;
a59b276e
LT
47 void *decoration = old_hash[i].decoration;
48
83f0412f 49 if (!decoration)
a59b276e
LT
50 continue;
51 insert_decoration(n, base, decoration);
52 }
53 free(old_hash);
54}
55
56/* Add a decoration pointer, return any old one */
54988bda
JK
57void *add_decoration(struct decoration *n, const struct object *obj,
58 void *decoration)
a59b276e
LT
59{
60 int nr = n->nr + 1;
61
62 if (nr > n->size * 2 / 3)
63 grow_decoration(n);
64 return insert_decoration(n, obj, decoration);
65}
66
67/* Lookup a decoration pointer */
54988bda 68void *lookup_decoration(struct decoration *n, const struct object *obj)
a59b276e 69{
91fe2f90 70 unsigned int j;
a59b276e
LT
71
72 /* nothing to lookup */
73 if (!n->size)
74 return NULL;
75 j = hash_obj(obj, n->size);
76 for (;;) {
77 struct object_decoration *ref = n->hash + j;
78 if (ref->base == obj)
79 return ref->decoration;
80 if (!ref->base)
81 return NULL;
82 if (++j == n->size)
83 j = 0;
84 }
85}