]>
Commit | Line | Data |
---|---|---|
a59b276e LT |
1 | /* |
2 | * decorate.c - decorate a git object with some arbitrary | |
3 | * data. | |
4 | */ | |
fc7bd51b EN |
5 | #include "git-compat-util.h" |
6 | #include "hashmap.h" | |
a59b276e LT |
7 | #include "object.h" |
8 | #include "decorate.h" | |
9 | ||
54988bda | 10 | static unsigned int hash_obj(const struct object *obj, unsigned int n) |
a59b276e | 11 | { |
d40abc8e | 12 | return oidhash(&obj->oid) % n; |
a59b276e LT |
13 | } |
14 | ||
54988bda | 15 | static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration) |
a59b276e LT |
16 | { |
17 | int size = n->size; | |
ddd3e312 | 18 | struct decoration_entry *entries = n->entries; |
91fe2f90 | 19 | unsigned int j = hash_obj(base, size); |
a59b276e | 20 | |
ddd3e312 JT |
21 | while (entries[j].base) { |
22 | if (entries[j].base == base) { | |
23 | void *old = entries[j].decoration; | |
24 | entries[j].decoration = decoration; | |
a59b276e LT |
25 | return old; |
26 | } | |
a59b276e LT |
27 | if (++j >= size) |
28 | j = 0; | |
29 | } | |
ddd3e312 JT |
30 | entries[j].base = base; |
31 | entries[j].decoration = decoration; | |
a59b276e LT |
32 | n->nr++; |
33 | return NULL; | |
34 | } | |
35 | ||
36 | static void grow_decoration(struct decoration *n) | |
37 | { | |
38 | int i; | |
39 | int old_size = n->size; | |
ddd3e312 | 40 | struct decoration_entry *old_entries = n->entries; |
a59b276e LT |
41 | |
42 | n->size = (old_size + 1000) * 3 / 2; | |
ca56dadb | 43 | CALLOC_ARRAY(n->entries, n->size); |
a59b276e LT |
44 | n->nr = 0; |
45 | ||
46 | for (i = 0; i < old_size; i++) { | |
ddd3e312 JT |
47 | const struct object *base = old_entries[i].base; |
48 | void *decoration = old_entries[i].decoration; | |
a59b276e | 49 | |
83f0412f | 50 | if (!decoration) |
a59b276e LT |
51 | continue; |
52 | insert_decoration(n, base, decoration); | |
53 | } | |
ddd3e312 | 54 | free(old_entries); |
a59b276e LT |
55 | } |
56 | ||
54988bda JK |
57 | void *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 | ||
54988bda | 67 | void *lookup_decoration(struct decoration *n, const struct object *obj) |
a59b276e | 68 | { |
91fe2f90 | 69 | unsigned int j; |
a59b276e LT |
70 | |
71 | /* nothing to lookup */ | |
72 | if (!n->size) | |
73 | return NULL; | |
74 | j = hash_obj(obj, n->size); | |
75 | for (;;) { | |
ddd3e312 | 76 | struct decoration_entry *ref = n->entries + j; |
a59b276e LT |
77 | if (ref->base == obj) |
78 | return ref->decoration; | |
79 | if (!ref->base) | |
80 | return NULL; | |
81 | if (++j == n->size) | |
82 | j = 0; | |
83 | } | |
84 | } |