]> git.ipfire.org Git - thirdparty/git.git/blame - pack-objects.c
dir.h: move DTYPE defines from cache.h
[thirdparty/git.git] / pack-objects.c
CommitLineData
36bf1958
EN
1#include "git-compat-util.h"
2#include "alloc.h"
2834bc27
VM
3#include "object.h"
4#include "pack.h"
5#include "pack-objects.h"
43fa44fa
NTND
6#include "packfile.h"
7#include "config.h"
2834bc27
VM
8
9static uint32_t locate_object_entry_hash(struct packing_data *pdata,
5e7ac680 10 const struct object_id *oid,
2834bc27
VM
11 int *found)
12{
039dc71a 13 uint32_t i, mask = (pdata->index_size - 1);
2834bc27 14
d40abc8e 15 i = oidhash(oid) & mask;
2834bc27
VM
16
17 while (pdata->index[i] > 0) {
18 uint32_t pos = pdata->index[i] - 1;
19
5e7ac680 20 if (oideq(oid, &pdata->objects[pos].idx.oid)) {
2834bc27
VM
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
fb799474 52 free(pdata->index);
ca56dadb 53 CALLOC_ARRAY(pdata->index, pdata->index_size);
2834bc27
VM
54
55 entry = pdata->objects;
56
57 for (i = 0; i < pdata->nr_objects; i++) {
58 int found;
e6a492b7 59 uint32_t ix = locate_object_entry_hash(pdata,
5e7ac680 60 &entry->idx.oid,
e6a492b7 61 &found);
2834bc27
VM
62
63 if (found)
033abf97 64 BUG("Duplicate object in hash");
2834bc27
VM
65
66 pdata->index[ix] = i + 1;
67 entry++;
68 }
69}
70
71struct object_entry *packlist_find(struct packing_data *pdata,
3a37876b 72 const struct object_id *oid)
2834bc27
VM
73{
74 uint32_t i;
75 int found;
76
77 if (!pdata->index_size)
78 return NULL;
79
5e7ac680 80 i = locate_object_entry_hash(pdata, oid, &found);
2834bc27 81
2834bc27
VM
82 if (!found)
83 return NULL;
84
85 return &pdata->objects[pdata->index[i] - 1];
86}
87
43fa44fa
NTND
88static void prepare_in_pack_by_idx(struct packing_data *pdata)
89{
90 struct packed_git **mapping, *p;
91 int cnt = 0, nr = 1U << OE_IN_PACK_BITS;
92
93 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 mapping[cnt++] = NULL;
7c141127 99 for (p = get_all_packs(pdata->repo); p; p = p->next, cnt++) {
43fa44fa
NTND
100 if (cnt == nr) {
101 free(mapping);
102 return;
103 }
104 p->index = cnt;
105 mapping[cnt] = p;
106 }
107 pdata->in_pack_by_idx = mapping;
108}
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 */
c409d108 119void oe_map_new_pack(struct packing_data *pack)
43fa44fa
NTND
120{
121 uint32_t i;
122
f66e0401
JK
123 if (pack->in_pack)
124 BUG("packing_data has already been converted to pack array");
125
126 ALLOC_ARRAY(pack->in_pack, pack->nr_alloc);
43fa44fa
NTND
127
128 for (i = 0; i < pack->nr_objects; i++)
129 pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);
130
131 FREE_AND_NULL(pack->in_pack_by_idx);
132}
133
134/* assume pdata is already zero'd by caller */
7c141127 135void prepare_packing_data(struct repository *r, struct packing_data *pdata)
43fa44fa 136{
7c141127
NTND
137 pdata->repo = r;
138
43fa44fa
NTND
139 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 } else {
145 prepare_in_pack_by_idx(pdata);
146 }
ac77d0c3
NTND
147
148 pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
149 1U << OE_SIZE_BITS);
9ac3f0e5
NTND
150 pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
151 1UL << OE_DELTA_SIZE_BITS);
edb673cf 152 init_recursive_mutex(&pdata->odb_lock);
43fa44fa
NTND
153}
154
2834bc27 155struct object_entry *packlist_alloc(struct packing_data *pdata,
3a37876b 156 const struct object_id *oid)
2834bc27
VM
157{
158 struct object_entry *new_entry;
159
160 if (pdata->nr_objects >= pdata->nr_alloc) {
161 pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
2756ca43 162 REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
43fa44fa
NTND
163
164 if (!pdata->in_pack_by_idx)
165 REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
9ac3f0e5
NTND
166 if (pdata->delta_size)
167 REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
108f5303
CC
168
169 if (pdata->tree_depth)
170 REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);
fe0ac2fb
CC
171
172 if (pdata->layer)
173 REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
5dfaf49a
TB
174
175 if (pdata->cruft_mtime)
176 REALLOC_ARRAY(pdata->cruft_mtime, pdata->nr_alloc);
2834bc27
VM
177 }
178
179 new_entry = pdata->objects + pdata->nr_objects++;
180
181 memset(new_entry, 0, sizeof(*new_entry));
f1cbd033 182 oidcpy(&new_entry->idx.oid, oid);
2834bc27
VM
183
184 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
185 rehash_objects(pdata);
3a37876b
JK
186 else {
187 int found;
188 uint32_t pos = locate_object_entry_hash(pdata,
189 &new_entry->idx.oid,
190 &found);
191 if (found)
192 BUG("duplicate object inserted into hash");
193 pdata->index[pos] = pdata->nr_objects;
194 }
2834bc27 195
43fa44fa
NTND
196 if (pdata->in_pack)
197 pdata->in_pack[pdata->nr_objects - 1] = NULL;
198
108f5303
CC
199 if (pdata->tree_depth)
200 pdata->tree_depth[pdata->nr_objects - 1] = 0;
201
fe0ac2fb
CC
202 if (pdata->layer)
203 pdata->layer[pdata->nr_objects - 1] = 0;
204
5dfaf49a
TB
205 if (pdata->cruft_mtime)
206 pdata->cruft_mtime[pdata->nr_objects - 1] = 0;
207
2834bc27
VM
208 return new_entry;
209}
6a1e32d5
JK
210
211void oe_set_delta_ext(struct packing_data *pdata,
212 struct object_entry *delta,
a93c141d 213 const struct object_id *oid)
6a1e32d5
JK
214{
215 struct object_entry *base;
216
217 ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);
218 base = &pdata->ext_bases[pdata->nr_ext++];
219 memset(base, 0, sizeof(*base));
a93c141d 220 oidcpy(&base->idx.oid, oid);
6a1e32d5
JK
221
222 /* These flags mark that we are not part of the actual pack output. */
223 base->preferred_base = 1;
224 base->filled = 1;
225
226 delta->ext_base = 1;
227 delta->delta_idx = base - pdata->ext_bases + 1;
228}