Git fork
1#include "git-compat-util.h"
2#include "object.h"
3#include "pack.h"
4#include "pack-objects.h"
5#include "packfile.h"
6#include "parse.h"
7#include "repository.h"
8
9static uint32_t locate_object_entry_hash(struct packing_data *pdata,
10 const struct object_id *oid,
11 int *found)
12{
13 uint32_t i, mask = (pdata->index_size - 1);
14
15 i = oidhash(oid) & mask;
16
17 while (pdata->index[i] > 0) {
18 uint32_t pos = pdata->index[i] - 1;
19
20 if (oideq(oid, &pdata->objects[pos].idx.oid)) {
21 *found = 1;
22 return i;
23 }
24
25 i = (i + 1) & mask;
26 }
27
28 *found = 0;
29 return i;
30}
31
32static inline uint32_t closest_pow2(uint32_t v)
33{
34 v = v - 1;
35 v |= v >> 1;
36 v |= v >> 2;
37 v |= v >> 4;
38 v |= v >> 8;
39 v |= v >> 16;
40 return v + 1;
41}
42
43static void rehash_objects(struct packing_data *pdata)
44{
45 uint32_t i;
46 struct object_entry *entry;
47
48 pdata->index_size = closest_pow2(pdata->nr_objects * 3);
49 if (pdata->index_size < 1024)
50 pdata->index_size = 1024;
51
52 free(pdata->index);
53 CALLOC_ARRAY(pdata->index, pdata->index_size);
54
55 entry = pdata->objects;
56
57 for (i = 0; i < pdata->nr_objects; i++) {
58 int found;
59 uint32_t ix = locate_object_entry_hash(pdata,
60 &entry->idx.oid,
61 &found);
62
63 if (found)
64 BUG("Duplicate object in hash");
65
66 pdata->index[ix] = i + 1;
67 entry++;
68 }
69}
70
71struct object_entry *packlist_find(struct packing_data *pdata,
72 const struct object_id *oid)
73{
74 uint32_t i;
75 int found;
76
77 if (!pdata->index_size)
78 return NULL;
79
80 i = locate_object_entry_hash(pdata, oid, &found);
81
82 if (!found)
83 return NULL;
84
85 return &pdata->objects[pdata->index[i] - 1];
86}
87
88static void prepare_in_pack_by_idx(struct packing_data *pdata)
89{
90 struct packfile_store *packs = pdata->repo->objects->packfiles;
91 struct packed_git **mapping, *p;
92 int cnt = 0, nr = 1U << OE_IN_PACK_BITS;
93
94 ALLOC_ARRAY(mapping, nr);
95 /*
96 * oe_in_pack() on an all-zero'd object_entry
97 * (i.e. in_pack_idx also zero) should return NULL.
98 */
99 mapping[cnt++] = NULL;
100 for (p = packfile_store_get_all_packs(packs); p; p = p->next, cnt++) {
101 if (cnt == nr) {
102 free(mapping);
103 return;
104 }
105 p->index = cnt;
106 mapping[cnt] = p;
107 }
108 pdata->in_pack_by_idx = mapping;
109}
110
111/*
112 * A new pack appears after prepare_in_pack_by_idx() has been
113 * run. This is likely a race.
114 *
115 * We could map this new pack to in_pack_by_idx[] array, but then we
116 * have to deal with full array anyway. And since it's hard to test
117 * this fall back code, just stay simple and fall back to using
118 * in_pack[] array.
119 */
120void oe_map_new_pack(struct packing_data *pack)
121{
122 uint32_t i;
123
124 if (pack->in_pack)
125 BUG("packing_data has already been converted to pack array");
126
127 ALLOC_ARRAY(pack->in_pack, pack->nr_alloc);
128
129 for (i = 0; i < pack->nr_objects; i++)
130 pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);
131
132 FREE_AND_NULL(pack->in_pack_by_idx);
133}
134
135/* assume pdata is already zero'd by caller */
136void prepare_packing_data(struct repository *r, struct packing_data *pdata)
137{
138 pdata->repo = r;
139
140 if (git_env_bool("GIT_TEST_FULL_IN_PACK_ARRAY", 0)) {
141 /*
142 * do not initialize in_pack_by_idx[] to force the
143 * slow path in oe_in_pack()
144 */
145 } else {
146 prepare_in_pack_by_idx(pdata);
147 }
148
149 pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
150 1U << OE_SIZE_BITS);
151 pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
152 1UL << OE_DELTA_SIZE_BITS);
153 init_recursive_mutex(&pdata->odb_lock);
154}
155
156void clear_packing_data(struct packing_data *pdata)
157{
158 if (!pdata)
159 return;
160
161 free(pdata->cruft_mtime);
162 free(pdata->in_pack);
163 free(pdata->in_pack_by_idx);
164 free(pdata->in_pack_pos);
165 free(pdata->index);
166 free(pdata->layer);
167 free(pdata->objects);
168 free(pdata->tree_depth);
169}
170
171struct object_entry *packlist_alloc(struct packing_data *pdata,
172 const struct object_id *oid)
173{
174 struct object_entry *new_entry;
175
176 if (pdata->nr_objects >= pdata->nr_alloc) {
177 pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
178 REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
179
180 if (!pdata->in_pack_by_idx)
181 REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
182 if (pdata->delta_size)
183 REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
184
185 if (pdata->tree_depth)
186 REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);
187
188 if (pdata->layer)
189 REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
190
191 if (pdata->cruft_mtime)
192 REALLOC_ARRAY(pdata->cruft_mtime, pdata->nr_alloc);
193 }
194
195 new_entry = pdata->objects + pdata->nr_objects++;
196
197 memset(new_entry, 0, sizeof(*new_entry));
198 oidcpy(&new_entry->idx.oid, oid);
199
200 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
201 rehash_objects(pdata);
202 else {
203 int found;
204 uint32_t pos = locate_object_entry_hash(pdata,
205 &new_entry->idx.oid,
206 &found);
207 if (found)
208 BUG("duplicate object inserted into hash");
209 pdata->index[pos] = pdata->nr_objects;
210 }
211
212 if (pdata->in_pack)
213 pdata->in_pack[pdata->nr_objects - 1] = NULL;
214
215 if (pdata->tree_depth)
216 pdata->tree_depth[pdata->nr_objects - 1] = 0;
217
218 if (pdata->layer)
219 pdata->layer[pdata->nr_objects - 1] = 0;
220
221 if (pdata->cruft_mtime)
222 pdata->cruft_mtime[pdata->nr_objects - 1] = 0;
223
224 return new_entry;
225}
226
227void oe_set_delta_ext(struct packing_data *pdata,
228 struct object_entry *delta,
229 const struct object_id *oid)
230{
231 struct object_entry *base;
232
233 ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);
234 base = &pdata->ext_bases[pdata->nr_ext++];
235 memset(base, 0, sizeof(*base));
236 oidcpy(&base->idx.oid, oid);
237
238 /* These flags mark that we are not part of the actual pack output. */
239 base->preferred_base = 1;
240 base->filled = 1;
241
242 delta->ext_base = 1;
243 delta->delta_idx = base - pdata->ext_bases + 1;
244}