4 #include "pack-objects.h"
6 static uint32_t locate_object_entry_hash(struct packing_data
*pdata
,
7 const unsigned char *sha1
,
10 uint32_t i
, mask
= (pdata
->index_size
- 1);
12 i
= sha1hash(sha1
) & mask
;
14 while (pdata
->index
[i
] > 0) {
15 uint32_t pos
= pdata
->index
[i
] - 1;
17 if (!hashcmp(sha1
, pdata
->objects
[pos
].idx
.sha1
)) {
29 static inline uint32_t closest_pow2(uint32_t v
)
40 static void rehash_objects(struct packing_data
*pdata
)
43 struct object_entry
*entry
;
45 pdata
->index_size
= closest_pow2(pdata
->nr_objects
* 3);
46 if (pdata
->index_size
< 1024)
47 pdata
->index_size
= 1024;
50 pdata
->index
= xcalloc(pdata
->index_size
, sizeof(*pdata
->index
));
52 entry
= pdata
->objects
;
54 for (i
= 0; i
< pdata
->nr_objects
; i
++) {
56 uint32_t ix
= locate_object_entry_hash(pdata
, entry
->idx
.sha1
, &found
);
59 die("BUG: Duplicate object in hash");
61 pdata
->index
[ix
] = i
+ 1;
66 struct object_entry
*packlist_find(struct packing_data
*pdata
,
67 const unsigned char *sha1
,
73 if (!pdata
->index_size
)
76 i
= locate_object_entry_hash(pdata
, sha1
, &found
);
84 return &pdata
->objects
[pdata
->index
[i
] - 1];
87 struct object_entry
*packlist_alloc(struct packing_data
*pdata
,
88 const unsigned char *sha1
,
91 struct object_entry
*new_entry
;
93 if (pdata
->nr_objects
>= pdata
->nr_alloc
) {
94 pdata
->nr_alloc
= (pdata
->nr_alloc
+ 1024) * 3 / 2;
95 REALLOC_ARRAY(pdata
->objects
, pdata
->nr_alloc
);
98 new_entry
= pdata
->objects
+ pdata
->nr_objects
++;
100 memset(new_entry
, 0, sizeof(*new_entry
));
101 hashcpy(new_entry
->idx
.sha1
, sha1
);
103 if (pdata
->index_size
* 3 <= pdata
->nr_objects
* 4)
104 rehash_objects(pdata
);
106 pdata
->index
[index_pos
] = pdata
->nr_objects
;