Merge branch 'mm/p4-unshelve-windows-fix'
[git.git] / pack-objects.c
blobce33b8906e5c589813c7d873c9173aa3aa2afddc
1 #include "cache.h"
2 #include "object.h"
3 #include "pack.h"
4 #include "pack-objects.h"
5 #include "packfile.h"
6 #include "config.h"
8 static uint32_t locate_object_entry_hash(struct packing_data *pdata,
9 const unsigned char *sha1,
10 int *found)
12 uint32_t i, mask = (pdata->index_size - 1);
14 i = sha1hash(sha1) & mask;
16 while (pdata->index[i] > 0) {
17 uint32_t pos = pdata->index[i] - 1;
19 if (hasheq(sha1, pdata->objects[pos].idx.oid.hash)) {
20 *found = 1;
21 return i;
24 i = (i + 1) & mask;
27 *found = 0;
28 return i;
31 static inline uint32_t closest_pow2(uint32_t v)
33 v = v - 1;
34 v |= v >> 1;
35 v |= v >> 2;
36 v |= v >> 4;
37 v |= v >> 8;
38 v |= v >> 16;
39 return v + 1;
42 static void rehash_objects(struct packing_data *pdata)
44 uint32_t i;
45 struct object_entry *entry;
47 pdata->index_size = closest_pow2(pdata->nr_objects * 3);
48 if (pdata->index_size < 1024)
49 pdata->index_size = 1024;
51 free(pdata->index);
52 pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
54 entry = pdata->objects;
56 for (i = 0; i < pdata->nr_objects; i++) {
57 int found;
58 uint32_t ix = locate_object_entry_hash(pdata,
59 entry->idx.oid.hash,
60 &found);
62 if (found)
63 BUG("Duplicate object in hash");
65 pdata->index[ix] = i + 1;
66 entry++;
70 struct object_entry *packlist_find(struct packing_data *pdata,
71 const unsigned char *sha1,
72 uint32_t *index_pos)
74 uint32_t i;
75 int found;
77 if (!pdata->index_size)
78 return NULL;
80 i = locate_object_entry_hash(pdata, sha1, &found);
82 if (index_pos)
83 *index_pos = i;
85 if (!found)
86 return NULL;
88 return &pdata->objects[pdata->index[i] - 1];
91 static void prepare_in_pack_by_idx(struct packing_data *pdata)
93 struct packed_git **mapping, *p;
94 int cnt = 0, nr = 1U << OE_IN_PACK_BITS;
96 ALLOC_ARRAY(mapping, nr);
98 * oe_in_pack() on an all-zero'd object_entry
99 * (i.e. in_pack_idx also zero) should return NULL.
101 mapping[cnt++] = NULL;
102 for (p = get_all_packs(pdata->repo); p; p = p->next, cnt++) {
103 if (cnt == nr) {
104 free(mapping);
105 return;
107 p->index = cnt;
108 mapping[cnt] = p;
110 pdata->in_pack_by_idx = mapping;
114 * A new pack appears after prepare_in_pack_by_idx() has been
115 * run. This is likely a race.
117 * We could map this new pack to in_pack_by_idx[] array, but then we
118 * have to deal with full array anyway. And since it's hard to test
119 * this fall back code, just stay simple and fall back to using
120 * in_pack[] array.
122 void oe_map_new_pack(struct packing_data *pack)
124 uint32_t i;
126 REALLOC_ARRAY(pack->in_pack, pack->nr_alloc);
128 for (i = 0; i < pack->nr_objects; i++)
129 pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);
131 FREE_AND_NULL(pack->in_pack_by_idx);
134 /* assume pdata is already zero'd by caller */
135 void prepare_packing_data(struct repository *r, struct packing_data *pdata)
137 pdata->repo = r;
139 if (git_env_bool("GIT_TEST_FULL_IN_PACK_ARRAY", 0)) {
141 * do not initialize in_pack_by_idx[] to force the
142 * slow path in oe_in_pack()
144 } else {
145 prepare_in_pack_by_idx(pdata);
148 pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
149 1U << OE_SIZE_BITS);
150 pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
151 1UL << OE_DELTA_SIZE_BITS);
152 init_recursive_mutex(&pdata->odb_lock);
155 struct object_entry *packlist_alloc(struct packing_data *pdata,
156 const unsigned char *sha1,
157 uint32_t index_pos)
159 struct object_entry *new_entry;
161 if (pdata->nr_objects >= pdata->nr_alloc) {
162 pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
163 REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
165 if (!pdata->in_pack_by_idx)
166 REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
167 if (pdata->delta_size)
168 REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
170 if (pdata->tree_depth)
171 REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);
173 if (pdata->layer)
174 REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
177 new_entry = pdata->objects + pdata->nr_objects++;
179 memset(new_entry, 0, sizeof(*new_entry));
180 hashcpy(new_entry->idx.oid.hash, sha1);
182 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
183 rehash_objects(pdata);
184 else
185 pdata->index[index_pos] = pdata->nr_objects;
187 if (pdata->in_pack)
188 pdata->in_pack[pdata->nr_objects - 1] = NULL;
190 if (pdata->tree_depth)
191 pdata->tree_depth[pdata->nr_objects - 1] = 0;
193 if (pdata->layer)
194 pdata->layer[pdata->nr_objects - 1] = 0;
196 return new_entry;
199 void oe_set_delta_ext(struct packing_data *pdata,
200 struct object_entry *delta,
201 const unsigned char *sha1)
203 struct object_entry *base;
205 ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);
206 base = &pdata->ext_bases[pdata->nr_ext++];
207 memset(base, 0, sizeof(*base));
208 hashcpy(base->idx.oid.hash, sha1);
210 /* These flags mark that we are not part of the actual pack output. */
211 base->preferred_base = 1;
212 base->filled = 1;
214 delta->ext_base = 1;
215 delta->delta_idx = base - pdata->ext_bases + 1;