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