]> git.ipfire.org Git - thirdparty/git.git/blame - decorate.c
path.c: clarify trie_find()'s in-code comment
[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{
d40abc8e 11 return oidhash(&obj->oid) % 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;
ddd3e312 17 struct decoration_entry *entries = n->entries;
91fe2f90 18 unsigned int j = hash_obj(base, size);
a59b276e 19
ddd3e312
JT
20 while (entries[j].base) {
21 if (entries[j].base == base) {
22 void *old = entries[j].decoration;
23 entries[j].decoration = decoration;
a59b276e
LT
24 return old;
25 }
a59b276e
LT
26 if (++j >= size)
27 j = 0;
28 }
ddd3e312
JT
29 entries[j].base = base;
30 entries[j].decoration = decoration;
a59b276e
LT
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;
ddd3e312 39 struct decoration_entry *old_entries = n->entries;
a59b276e
LT
40
41 n->size = (old_size + 1000) * 3 / 2;
ddd3e312 42 n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
a59b276e
LT
43 n->nr = 0;
44
45 for (i = 0; i < old_size; i++) {
ddd3e312
JT
46 const struct object *base = old_entries[i].base;
47 void *decoration = old_entries[i].decoration;
a59b276e 48
83f0412f 49 if (!decoration)
a59b276e
LT
50 continue;
51 insert_decoration(n, base, decoration);
52 }
ddd3e312 53 free(old_entries);
a59b276e
LT
54}
55
54988bda
JK
56void *add_decoration(struct decoration *n, const struct object *obj,
57 void *decoration)
a59b276e
LT
58{
59 int nr = n->nr + 1;
60
61 if (nr > n->size * 2 / 3)
62 grow_decoration(n);
63 return insert_decoration(n, obj, decoration);
64}
65
54988bda 66void *lookup_decoration(struct decoration *n, const struct object *obj)
a59b276e 67{
91fe2f90 68 unsigned int j;
a59b276e
LT
69
70 /* nothing to lookup */
71 if (!n->size)
72 return NULL;
73 j = hash_obj(obj, n->size);
74 for (;;) {
ddd3e312 75 struct decoration_entry *ref = n->entries + j;
a59b276e
LT
76 if (ref->base == obj)
77 return ref->decoration;
78 if (!ref->base)
79 return NULL;
80 if (++j == n->size)
81 j = 0;
82 }
83}