|
1 | 1 | #include "cache.h" |
2 | 2 | #include "object.h" |
| 3 | +#include "decorate.h" |
3 | 4 |
|
4 | 5 | int track_object_refs = 0; |
5 | 6 |
|
6 | | -static unsigned int refs_hash_size, nr_object_refs; |
7 | | -static struct object_refs **refs_hash; |
| 7 | +static struct decoration ref_decorate; |
8 | 8 |
|
9 | | -static unsigned int hash_obj(struct object *obj, unsigned int n) |
| 9 | +struct object_refs *lookup_object_refs(struct object *base) |
10 | 10 | { |
11 | | - unsigned int hash = *(unsigned int *)obj->sha1; |
12 | | - return hash % n; |
| 11 | + return lookup_decoration(&ref_decorate, base); |
13 | 12 | } |
14 | 13 |
|
15 | | -static void insert_ref_hash(struct object_refs *ref, struct object_refs **hash, unsigned int size) |
| 14 | +static void add_object_refs(struct object *obj, struct object_refs *refs) |
16 | 15 | { |
17 | | - int j = hash_obj(ref->base, size); |
18 | | - |
19 | | - while (hash[j]) { |
20 | | - j++; |
21 | | - if (j >= size) |
22 | | - j = 0; |
23 | | - } |
24 | | - hash[j] = ref; |
25 | | -} |
26 | | - |
27 | | -static void grow_refs_hash(void) |
28 | | -{ |
29 | | - int i; |
30 | | - int new_hash_size = (refs_hash_size + 1000) * 3 / 2; |
31 | | - struct object_refs **new_hash; |
32 | | - |
33 | | - new_hash = xcalloc(new_hash_size, sizeof(struct object_refs *)); |
34 | | - for (i = 0; i < refs_hash_size; i++) { |
35 | | - struct object_refs *ref = refs_hash[i]; |
36 | | - if (!ref) |
37 | | - continue; |
38 | | - insert_ref_hash(ref, new_hash, new_hash_size); |
39 | | - } |
40 | | - free(refs_hash); |
41 | | - refs_hash = new_hash; |
42 | | - refs_hash_size = new_hash_size; |
43 | | -} |
44 | | - |
45 | | -static void add_object_refs(struct object *obj, struct object_refs *ref) |
46 | | -{ |
47 | | - int nr = nr_object_refs + 1; |
48 | | - |
49 | | - if (nr > refs_hash_size * 2 / 3) |
50 | | - grow_refs_hash(); |
51 | | - ref->base = obj; |
52 | | - insert_ref_hash(ref, refs_hash, refs_hash_size); |
53 | | - nr_object_refs = nr; |
54 | | -} |
55 | | - |
56 | | -struct object_refs *lookup_object_refs(struct object *obj) |
57 | | -{ |
58 | | - struct object_refs *ref; |
59 | | - int j; |
60 | | - |
61 | | - /* nothing to lookup */ |
62 | | - if (!refs_hash_size) |
63 | | - return NULL; |
64 | | - j = hash_obj(obj, refs_hash_size); |
65 | | - while ((ref = refs_hash[j]) != NULL) { |
66 | | - if (ref->base == obj) |
67 | | - break; |
68 | | - j++; |
69 | | - if (j >= refs_hash_size) |
70 | | - j = 0; |
71 | | - } |
72 | | - return ref; |
| 16 | + if (add_decoration(&ref_decorate, obj, refs)) |
| 17 | + die("object %s tried to add refs twice!", sha1_to_hex(obj->sha1)); |
73 | 18 | } |
74 | 19 |
|
75 | 20 | struct object_refs *alloc_object_refs(unsigned count) |
|
0 commit comments