2 * decorate.c - decorate a git object with some arbitrary
9 static unsigned int hash_obj(const struct object
*obj
, unsigned int n
)
11 return sha1hash(get_object_hash(*obj
)) % n
;
14 static void *insert_decoration(struct decoration
*n
, const struct object
*base
, void *decoration
)
17 struct object_decoration
*hash
= n
->hash
;
18 unsigned int j
= hash_obj(base
, size
);
20 while (hash
[j
].base
) {
21 if (hash
[j
].base
== base
) {
22 void *old
= hash
[j
].decoration
;
23 hash
[j
].decoration
= decoration
;
30 hash
[j
].decoration
= decoration
;
35 static void grow_decoration(struct decoration
*n
)
38 int old_size
= n
->size
;
39 struct object_decoration
*old_hash
= n
->hash
;
41 n
->size
= (old_size
+ 1000) * 3 / 2;
42 n
->hash
= xcalloc(n
->size
, sizeof(struct object_decoration
));
45 for (i
= 0; i
< old_size
; i
++) {
46 const struct object
*base
= old_hash
[i
].base
;
47 void *decoration
= old_hash
[i
].decoration
;
51 insert_decoration(n
, base
, decoration
);
56 /* Add a decoration pointer, return any old one */
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 /* Lookup a decoration pointer */
68 void *lookup_decoration(struct decoration
*n
, const struct object
*obj
)
72 /* nothing to lookup */
75 j
= hash_obj(obj
, n
->size
);
77 struct object_decoration
*ref
= n
->hash
+ j
;
79 return ref
->decoration
;