run-command: inline prepare_run_command_v_opt()
[git/debian.git] / pack-objects.c
blob9992f3ecf249c32e5be31dc047bad46a4c4ac367
1 #include "cache.h"
2 #include "object.h"
3 #include "pack.h"
4 #include "pack-objects.h"
6 static uint32_t locate_object_entry_hash(struct packing_data *pdata,
7 const unsigned char *sha1,
8 int *found)
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)) {
18 *found = 1;
19 return i;
22 i = (i + 1) & mask;
25 *found = 0;
26 return i;
29 static inline uint32_t closest_pow2(uint32_t v)
31 v = v - 1;
32 v |= v >> 1;
33 v |= v >> 2;
34 v |= v >> 4;
35 v |= v >> 8;
36 v |= v >> 16;
37 return v + 1;
40 static void rehash_objects(struct packing_data *pdata)
42 uint32_t i;
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;
49 free(pdata->index);
50 pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
52 entry = pdata->objects;
54 for (i = 0; i < pdata->nr_objects; i++) {
55 int found;
56 uint32_t ix = locate_object_entry_hash(pdata, entry->idx.sha1, &found);
58 if (found)
59 die("BUG: Duplicate object in hash");
61 pdata->index[ix] = i + 1;
62 entry++;
66 struct object_entry *packlist_find(struct packing_data *pdata,
67 const unsigned char *sha1,
68 uint32_t *index_pos)
70 uint32_t i;
71 int found;
73 if (!pdata->index_size)
74 return NULL;
76 i = locate_object_entry_hash(pdata, sha1, &found);
78 if (index_pos)
79 *index_pos = i;
81 if (!found)
82 return NULL;
84 return &pdata->objects[pdata->index[i] - 1];
87 struct object_entry *packlist_alloc(struct packing_data *pdata,
88 const unsigned char *sha1,
89 uint32_t index_pos)
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 pdata->objects = xrealloc(pdata->objects,
96 pdata->nr_alloc * sizeof(*new_entry));
99 new_entry = pdata->objects + pdata->nr_objects++;
101 memset(new_entry, 0, sizeof(*new_entry));
102 hashcpy(new_entry->idx.sha1, sha1);
104 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
105 rehash_objects(pdata);
106 else
107 pdata->index[index_pos] = pdata->nr_objects;
109 return new_entry;