2 * decorate.c - decorate a git object with some arbitrary
6 #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
)
17 struct decoration_entry
*entries
= n
->entries
;
18 unsigned int j
= hash_obj(base
, n
->size
);
20 while (entries
[j
].base
) {
21 if (entries
[j
].base
== base
) {
22 void *old
= entries
[j
].decoration
;
23 entries
[j
].decoration
= decoration
;
29 entries
[j
].base
= base
;
30 entries
[j
].decoration
= decoration
;
35 static void grow_decoration(struct decoration
*n
)
38 unsigned int old_size
= n
->size
;
39 struct decoration_entry
*old_entries
= n
->entries
;
41 n
->size
= (old_size
+ 1000) * 3 / 2;
42 CALLOC_ARRAY(n
->entries
, n
->size
);
45 for (i
= 0; i
< old_size
; i
++) {
46 const struct object
*base
= old_entries
[i
].base
;
47 void *decoration
= old_entries
[i
].decoration
;
51 insert_decoration(n
, base
, decoration
);
56 void *add_decoration(struct decoration
*n
, const struct object
*obj
,
59 if ((n
->nr
+ 1) > n
->size
* 2 / 3)
61 return insert_decoration(n
, obj
, decoration
);
64 void *lookup_decoration(struct decoration
*n
, const struct object
*obj
)
68 /* nothing to lookup */
71 j
= hash_obj(obj
, n
->size
);
73 struct decoration_entry
*ref
= n
->entries
+ j
;
75 return ref
->decoration
;
83 void clear_decoration(struct decoration
*n
, void (*free_cb
)(void *))
87 for (i
= 0; i
< n
->size
; i
++) {
88 void *d
= n
->entries
[i
].decoration
;
94 FREE_AND_NULL(n
->entries
);