2 * decorate.c - decorate a git object with some arbitrary
5 #include "git-compat-util.h"
10 static unsigned int hash_obj(const struct object
*obj
, unsigned int n
)
12 return oidhash(&obj
->oid
) % n
;
15 static void *insert_decoration(struct decoration
*n
, const struct object
*base
, void *decoration
)
18 struct decoration_entry
*entries
= n
->entries
;
19 unsigned int j
= hash_obj(base
, size
);
21 while (entries
[j
].base
) {
22 if (entries
[j
].base
== base
) {
23 void *old
= entries
[j
].decoration
;
24 entries
[j
].decoration
= decoration
;
30 entries
[j
].base
= base
;
31 entries
[j
].decoration
= decoration
;
36 static void grow_decoration(struct decoration
*n
)
39 int old_size
= n
->size
;
40 struct decoration_entry
*old_entries
= n
->entries
;
42 n
->size
= (old_size
+ 1000) * 3 / 2;
43 CALLOC_ARRAY(n
->entries
, n
->size
);
46 for (i
= 0; i
< old_size
; i
++) {
47 const struct object
*base
= old_entries
[i
].base
;
48 void *decoration
= old_entries
[i
].decoration
;
52 insert_decoration(n
, base
, decoration
);
57 void *add_decoration(struct decoration
*n
, const struct object
*obj
,
62 if (nr
> n
->size
* 2 / 3)
64 return insert_decoration(n
, obj
, decoration
);
67 void *lookup_decoration(struct decoration
*n
, const struct object
*obj
)
71 /* nothing to lookup */
74 j
= hash_obj(obj
, n
->size
);
76 struct decoration_entry
*ref
= n
->entries
+ j
;
78 return ref
->decoration
;