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, hash, mask = (pdata->index_size - 1);
12 memcpy(&hash, sha1, sizeof(uint32_t));
15 while (pdata->index[i] > 0) {
16 uint32_t pos = pdata->index[i] - 1;
18 if (!hashcmp(sha1, pdata->objects[pos].idx.sha1)) {
30 static inline uint32_t closest_pow2(uint32_t v)
41 static void rehash_objects(struct packing_data *pdata)
44 struct object_entry *entry;
46 pdata->index_size = closest_pow2(pdata->nr_objects * 3);
47 if (pdata->index_size < 1024)
48 pdata->index_size = 1024;
50 pdata->index = xrealloc(pdata->index, sizeof(uint32_t) * pdata->index_size);
51 memset(pdata->index, 0, sizeof(int) * pdata->index_size);
53 entry = pdata->objects;
55 for (i = 0; i < pdata->nr_objects; i++) {
57 uint32_t ix = locate_object_entry_hash(pdata, entry->idx.sha1, &found);
60 die("BUG: Duplicate object in hash");
62 pdata->index[ix] = i + 1;
67 struct object_entry *packlist_find(struct packing_data *pdata,
68 const unsigned char *sha1,
74 if (!pdata->index_size)
77 i = locate_object_entry_hash(pdata, sha1, &found);
85 return &pdata->objects[pdata->index[i] - 1];
88 struct object_entry *packlist_alloc(struct packing_data *pdata,
89 const unsigned char *sha1,
92 struct object_entry *new_entry;
94 if (pdata->nr_objects >= pdata->nr_alloc) {
95 pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
96 pdata->objects = xrealloc(pdata->objects,
97 pdata->nr_alloc * sizeof(*new_entry));
100 new_entry = pdata->objects + pdata->nr_objects++;
102 memset(new_entry, 0, sizeof(*new_entry));
103 hashcpy(new_entry->idx.sha1, sha1);
105 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
106 rehash_objects(pdata);
108 pdata->index[index_pos] = pdata->nr_objects;