cocci: remove 'unused.cocci'
[git.git] / split-index.c
blobc98807c655b1fddb6df70e9c4fbf0122264c5b58
1 #include "cache.h"
2 #include "alloc.h"
3 #include "gettext.h"
4 #include "split-index.h"
5 #include "ewah/ewok.h"
7 struct split_index *init_split_index(struct index_state *istate)
9 if (!istate->split_index) {
10 if (istate->sparse_index)
11 die(_("cannot use split index with a sparse index"));
13 CALLOC_ARRAY(istate->split_index, 1);
14 istate->split_index->refcount = 1;
16 return istate->split_index;
19 int read_link_extension(struct index_state *istate,
20 const void *data_, unsigned long sz)
22 const unsigned char *data = data_;
23 struct split_index *si;
24 int ret;
26 if (sz < the_hash_algo->rawsz)
27 return error("corrupt link extension (too short)");
28 si = init_split_index(istate);
29 oidread(&si->base_oid, data);
30 data += the_hash_algo->rawsz;
31 sz -= the_hash_algo->rawsz;
32 if (!sz)
33 return 0;
34 si->delete_bitmap = ewah_new();
35 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
36 if (ret < 0)
37 return error("corrupt delete bitmap in link extension");
38 data += ret;
39 sz -= ret;
40 si->replace_bitmap = ewah_new();
41 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
42 if (ret < 0)
43 return error("corrupt replace bitmap in link extension");
44 if (ret != sz)
45 return error("garbage at the end of link extension");
46 return 0;
49 int write_link_extension(struct strbuf *sb,
50 struct index_state *istate)
52 struct split_index *si = istate->split_index;
53 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
54 if (!si->delete_bitmap && !si->replace_bitmap)
55 return 0;
56 ewah_serialize_strbuf(si->delete_bitmap, sb);
57 ewah_serialize_strbuf(si->replace_bitmap, sb);
58 return 0;
61 static void mark_base_index_entries(struct index_state *base)
63 int i;
65 * To keep track of the shared entries between
66 * istate->base->cache[] and istate->cache[], base entry
67 * position is stored in each base entry. All positions start
68 * from 1 instead of 0, which is reserved to say "this is a new
69 * entry".
71 for (i = 0; i < base->cache_nr; i++)
72 base->cache[i]->index = i + 1;
75 void move_cache_to_base_index(struct index_state *istate)
77 struct split_index *si = istate->split_index;
78 int i;
81 * If there was a previous base index, then transfer ownership of allocated
82 * entries to the parent index.
84 if (si->base &&
85 si->base->ce_mem_pool) {
87 if (!istate->ce_mem_pool) {
88 istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
89 mem_pool_init(istate->ce_mem_pool, 0);
92 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
95 ALLOC_ARRAY(si->base, 1);
96 index_state_init(si->base, istate->repo);
97 si->base->version = istate->version;
98 /* zero timestamp disables racy test in ce_write_index() */
99 si->base->timestamp = istate->timestamp;
100 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
101 si->base->cache_nr = istate->cache_nr;
104 * The mem_pool needs to move with the allocated entries.
106 si->base->ce_mem_pool = istate->ce_mem_pool;
107 istate->ce_mem_pool = NULL;
109 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
110 mark_base_index_entries(si->base);
111 for (i = 0; i < si->base->cache_nr; i++)
112 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
115 static void mark_entry_for_delete(size_t pos, void *data)
117 struct index_state *istate = data;
118 if (pos >= istate->cache_nr)
119 die("position for delete %d exceeds base index size %d",
120 (int)pos, istate->cache_nr);
121 istate->cache[pos]->ce_flags |= CE_REMOVE;
122 istate->split_index->nr_deletions++;
125 static void replace_entry(size_t pos, void *data)
127 struct index_state *istate = data;
128 struct split_index *si = istate->split_index;
129 struct cache_entry *dst, *src;
131 if (pos >= istate->cache_nr)
132 die("position for replacement %d exceeds base index size %d",
133 (int)pos, istate->cache_nr);
134 if (si->nr_replacements >= si->saved_cache_nr)
135 die("too many replacements (%d vs %d)",
136 si->nr_replacements, si->saved_cache_nr);
137 dst = istate->cache[pos];
138 if (dst->ce_flags & CE_REMOVE)
139 die("entry %d is marked as both replaced and deleted",
140 (int)pos);
141 src = si->saved_cache[si->nr_replacements];
142 if (ce_namelen(src))
143 die("corrupt link extension, entry %d should have "
144 "zero length name", (int)pos);
145 src->index = pos + 1;
146 src->ce_flags |= CE_UPDATE_IN_BASE;
147 src->ce_namelen = dst->ce_namelen;
148 copy_cache_entry(dst, src);
149 discard_cache_entry(src);
150 si->nr_replacements++;
153 void merge_base_index(struct index_state *istate)
155 struct split_index *si = istate->split_index;
156 unsigned int i;
158 mark_base_index_entries(si->base);
160 si->saved_cache = istate->cache;
161 si->saved_cache_nr = istate->cache_nr;
162 istate->cache_nr = si->base->cache_nr;
163 istate->cache = NULL;
164 istate->cache_alloc = 0;
165 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
166 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
168 si->nr_deletions = 0;
169 si->nr_replacements = 0;
170 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
171 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
172 if (si->nr_deletions)
173 remove_marked_cache_entries(istate, 0);
175 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
176 if (!ce_namelen(si->saved_cache[i]))
177 die("corrupt link extension, entry %d should "
178 "have non-zero length name", i);
179 add_index_entry(istate, si->saved_cache[i],
180 ADD_CACHE_OK_TO_ADD |
181 ADD_CACHE_KEEP_CACHE_TREE |
183 * we may have to replay what
184 * merge-recursive.c:update_stages()
185 * does, which has this flag on
187 ADD_CACHE_SKIP_DFCHECK);
188 si->saved_cache[i] = NULL;
191 ewah_free(si->delete_bitmap);
192 ewah_free(si->replace_bitmap);
193 FREE_AND_NULL(si->saved_cache);
194 si->delete_bitmap = NULL;
195 si->replace_bitmap = NULL;
196 si->saved_cache_nr = 0;
200 * Compare most of the fields in two cache entries, i.e. all except the
201 * hashmap_entry and the name.
203 static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
205 const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
206 CE_EXTENDED_FLAGS;
207 unsigned int ce_flags = a->ce_flags;
208 unsigned int base_flags = b->ce_flags;
209 int ret;
211 /* only on-disk flags matter */
212 a->ce_flags &= ondisk_flags;
213 b->ce_flags &= ondisk_flags;
214 ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
215 offsetof(struct cache_entry, name) -
216 offsetof(struct cache_entry, oid)) ||
217 !oideq(&a->oid, &b->oid);
218 a->ce_flags = ce_flags;
219 b->ce_flags = base_flags;
221 return ret;
224 void prepare_to_write_split_index(struct index_state *istate)
226 struct split_index *si = init_split_index(istate);
227 struct cache_entry **entries = NULL, *ce;
228 int i, nr_entries = 0, nr_alloc = 0;
230 si->delete_bitmap = ewah_new();
231 si->replace_bitmap = ewah_new();
233 if (si->base) {
234 /* Go through istate->cache[] and mark CE_MATCHED to
235 * entry with positive index. We'll go through
236 * base->cache[] later to delete all entries in base
237 * that are not marked with either CE_MATCHED or
238 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
239 * duplicate, deduplicate it.
241 for (i = 0; i < istate->cache_nr; i++) {
242 struct cache_entry *base;
243 ce = istate->cache[i];
244 if (!ce->index) {
246 * During simple update index operations this
247 * is a cache entry that is not present in
248 * the shared index. It will be added to the
249 * split index.
251 * However, it might also represent a file
252 * that already has a cache entry in the
253 * shared index, but a new index has just
254 * been constructed by unpack_trees(), and
255 * this entry now refers to different content
256 * than what was recorded in the original
257 * index, e.g. during 'read-tree -m HEAD^' or
258 * 'checkout HEAD^'. In this case the
259 * original entry in the shared index will be
260 * marked as deleted, and this entry will be
261 * added to the split index.
263 continue;
265 if (ce->index > si->base->cache_nr) {
266 BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
267 ce->index, si->base->cache_nr);
269 ce->ce_flags |= CE_MATCHED; /* or "shared" */
270 base = si->base->cache[ce->index - 1];
271 if (ce == base) {
272 /* The entry is present in the shared index. */
273 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
275 * Already marked for inclusion in
276 * the split index, either because
277 * the corresponding file was
278 * modified and the cached stat data
279 * was refreshed, or because there
280 * is already a replacement entry in
281 * the split index.
282 * Nothing more to do here.
284 } else if (!ce_uptodate(ce) &&
285 is_racy_timestamp(istate, ce)) {
287 * A racily clean cache entry stored
288 * only in the shared index: it must
289 * be added to the split index, so
290 * the subsequent do_write_index()
291 * can smudge its stat data.
293 ce->ce_flags |= CE_UPDATE_IN_BASE;
294 } else {
296 * The entry is only present in the
297 * shared index and it was not
298 * refreshed.
299 * Just leave it there.
302 continue;
304 if (ce->ce_namelen != base->ce_namelen ||
305 strcmp(ce->name, base->name)) {
306 ce->index = 0;
307 continue;
310 * This is the copy of a cache entry that is present
311 * in the shared index, created by unpack_trees()
312 * while it constructed a new index.
314 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
316 * Already marked for inclusion in the split
317 * index, either because the corresponding
318 * file was modified and the cached stat data
319 * was refreshed, or because the original
320 * entry already had a replacement entry in
321 * the split index.
322 * Nothing to do.
324 } else if (!ce_uptodate(ce) &&
325 is_racy_timestamp(istate, ce)) {
327 * A copy of a racily clean cache entry from
328 * the shared index. It must be added to
329 * the split index, so the subsequent
330 * do_write_index() can smudge its stat data.
332 ce->ce_flags |= CE_UPDATE_IN_BASE;
333 } else {
335 * Thoroughly compare the cached data to see
336 * whether it should be marked for inclusion
337 * in the split index.
339 * This comparison might be unnecessary, as
340 * code paths modifying the cached data do
341 * set CE_UPDATE_IN_BASE as well.
343 if (compare_ce_content(ce, base))
344 ce->ce_flags |= CE_UPDATE_IN_BASE;
346 discard_cache_entry(base);
347 si->base->cache[ce->index - 1] = ce;
349 for (i = 0; i < si->base->cache_nr; i++) {
350 ce = si->base->cache[i];
351 if ((ce->ce_flags & CE_REMOVE) ||
352 !(ce->ce_flags & CE_MATCHED))
353 ewah_set(si->delete_bitmap, i);
354 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
355 ewah_set(si->replace_bitmap, i);
356 ce->ce_flags |= CE_STRIP_NAME;
357 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
358 entries[nr_entries++] = ce;
360 if (is_null_oid(&ce->oid))
361 istate->drop_cache_tree = 1;
365 for (i = 0; i < istate->cache_nr; i++) {
366 ce = istate->cache[i];
367 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
368 assert(!(ce->ce_flags & CE_STRIP_NAME));
369 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
370 entries[nr_entries++] = ce;
372 ce->ce_flags &= ~CE_MATCHED;
376 * take cache[] out temporarily, put entries[] in its place
377 * for writing
379 si->saved_cache = istate->cache;
380 si->saved_cache_nr = istate->cache_nr;
381 istate->cache = entries;
382 istate->cache_nr = nr_entries;
385 void finish_writing_split_index(struct index_state *istate)
387 struct split_index *si = init_split_index(istate);
389 ewah_free(si->delete_bitmap);
390 ewah_free(si->replace_bitmap);
391 si->delete_bitmap = NULL;
392 si->replace_bitmap = NULL;
393 free(istate->cache);
394 istate->cache = si->saved_cache;
395 istate->cache_nr = si->saved_cache_nr;
398 void discard_split_index(struct index_state *istate)
400 struct split_index *si = istate->split_index;
401 if (!si)
402 return;
403 istate->split_index = NULL;
404 si->refcount--;
405 if (si->refcount)
406 return;
407 if (si->base) {
408 discard_index(si->base);
409 free(si->base);
411 free(si);
414 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
416 if (ce->index &&
417 istate->split_index &&
418 istate->split_index->base &&
419 ce->index <= istate->split_index->base->cache_nr &&
420 ce == istate->split_index->base->cache[ce->index - 1])
421 ce->ce_flags |= CE_REMOVE;
422 else
423 discard_cache_entry(ce);
426 void replace_index_entry_in_base(struct index_state *istate,
427 struct cache_entry *old_entry,
428 struct cache_entry *new_entry)
430 if (old_entry->index &&
431 istate->split_index &&
432 istate->split_index->base &&
433 old_entry->index <= istate->split_index->base->cache_nr) {
434 new_entry->index = old_entry->index;
435 if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
436 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
437 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
441 void add_split_index(struct index_state *istate)
443 if (!istate->split_index) {
444 init_split_index(istate);
445 istate->cache_changed |= SPLIT_INDEX_ORDERED;
449 void remove_split_index(struct index_state *istate)
451 if (istate->split_index) {
452 if (istate->split_index->base) {
454 * When removing the split index, we need to move
455 * ownership of the mem_pool associated with the
456 * base index to the main index. There may be cache entries
457 * allocated from the base's memory pool that are shared with
458 * the_index.cache[].
460 mem_pool_combine(istate->ce_mem_pool,
461 istate->split_index->base->ce_mem_pool);
464 * The split index no longer owns the mem_pool backing
465 * its cache array. As we are discarding this index,
466 * mark the index as having no cache entries, so it
467 * will not attempt to clean up the cache entries or
468 * validate them.
470 istate->split_index->base->cache_nr = 0;
474 * We can discard the split index because its
475 * memory pool has been incorporated into the
476 * memory pool associated with the the_index.
478 discard_split_index(istate);
480 istate->cache_changed |= SOMETHING_CHANGED;