coccinelle: make use of the "expression" FREE_AND_NULL() rule
[git/git.git] / split-index.c
CommitLineData
5fc2fc8f
NTND
1#include "cache.h"
2#include "split-index.h"
96a1d8d3 3#include "ewah/ewok.h"
5fc2fc8f
NTND
4
5struct split_index *init_split_index(struct index_state *istate)
6{
7 if (!istate->split_index) {
8 istate->split_index = xcalloc(1, sizeof(*istate->split_index));
9 istate->split_index->refcount = 1;
10 }
11 return istate->split_index;
12}
13
14int read_link_extension(struct index_state *istate,
15 const void *data_, unsigned long sz)
16{
17 const unsigned char *data = data_;
18 struct split_index *si;
76b07b37
NTND
19 int ret;
20
5fc2fc8f
NTND
21 if (sz < 20)
22 return error("corrupt link extension (too short)");
23 si = init_split_index(istate);
24 hashcpy(si->base_sha1, data);
25 data += 20;
26 sz -= 20;
76b07b37
NTND
27 if (!sz)
28 return 0;
29 si->delete_bitmap = ewah_new();
30 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
31 if (ret < 0)
32 return error("corrupt delete bitmap in link extension");
33 data += ret;
34 sz -= ret;
35 si->replace_bitmap = ewah_new();
36 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
37 if (ret < 0)
38 return error("corrupt replace bitmap in link extension");
39 if (ret != sz)
5fc2fc8f
NTND
40 return error("garbage at the end of link extension");
41 return 0;
42}
43
44int write_link_extension(struct strbuf *sb,
45 struct index_state *istate)
46{
47 struct split_index *si = istate->split_index;
48 strbuf_add(sb, si->base_sha1, 20);
96a1d8d3
NTND
49 if (!si->delete_bitmap && !si->replace_bitmap)
50 return 0;
be0d9d53
NTND
51 ewah_serialize_strbuf(si->delete_bitmap, sb);
52 ewah_serialize_strbuf(si->replace_bitmap, sb);
5fc2fc8f
NTND
53 return 0;
54}
55
56static void mark_base_index_entries(struct index_state *base)
57{
58 int i;
59 /*
60 * To keep track of the shared entries between
61 * istate->base->cache[] and istate->cache[], base entry
62 * position is stored in each base entry. All positions start
832c0e5e 63 * from 1 instead of 0, which is reserved to say "this is a new
5fc2fc8f
NTND
64 * entry".
65 */
66 for (i = 0; i < base->cache_nr; i++)
67 base->cache[i]->index = i + 1;
68}
69
c18b80a0
NTND
70void move_cache_to_base_index(struct index_state *istate)
71{
72 struct split_index *si = istate->split_index;
73 int i;
74
75 /*
f9d7abec
NTND
76 * If "si" is shared with another index_state (e.g. by
77 * unpack-trees code), we will need to duplicate split_index
78 * struct. It's not happening now though, luckily.
c18b80a0 79 */
f9d7abec
NTND
80 assert(si->refcount <= 1);
81
82 unshare_split_index(istate, 0);
83 if (si->base) {
84 discard_index(si->base);
85 free(si->base);
86 }
c18b80a0
NTND
87 si->base = xcalloc(1, sizeof(*si->base));
88 si->base->version = istate->version;
89 /* zero timestamp disables racy test in ce_write_index() */
90 si->base->timestamp = istate->timestamp;
91 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
92 si->base->cache_nr = istate->cache_nr;
45ccef87 93 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
c18b80a0
NTND
94 mark_base_index_entries(si->base);
95 for (i = 0; i < si->base->cache_nr; i++)
96 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
97}
98
76b07b37
NTND
99static void mark_entry_for_delete(size_t pos, void *data)
100{
101 struct index_state *istate = data;
102 if (pos >= istate->cache_nr)
103 die("position for delete %d exceeds base index size %d",
104 (int)pos, istate->cache_nr);
105 istate->cache[pos]->ce_flags |= CE_REMOVE;
106 istate->split_index->nr_deletions = 1;
107}
108
109static void replace_entry(size_t pos, void *data)
110{
111 struct index_state *istate = data;
112 struct split_index *si = istate->split_index;
113 struct cache_entry *dst, *src;
b3c96fb1 114
76b07b37
NTND
115 if (pos >= istate->cache_nr)
116 die("position for replacement %d exceeds base index size %d",
117 (int)pos, istate->cache_nr);
118 if (si->nr_replacements >= si->saved_cache_nr)
119 die("too many replacements (%d vs %d)",
120 si->nr_replacements, si->saved_cache_nr);
121 dst = istate->cache[pos];
122 if (dst->ce_flags & CE_REMOVE)
123 die("entry %d is marked as both replaced and deleted",
124 (int)pos);
125 src = si->saved_cache[si->nr_replacements];
b3c96fb1
NTND
126 if (ce_namelen(src))
127 die("corrupt link extension, entry %d should have "
128 "zero length name", (int)pos);
76b07b37
NTND
129 src->index = pos + 1;
130 src->ce_flags |= CE_UPDATE_IN_BASE;
b3c96fb1
NTND
131 src->ce_namelen = dst->ce_namelen;
132 copy_cache_entry(dst, src);
133 free(src);
76b07b37
NTND
134 si->nr_replacements++;
135}
136
5fc2fc8f
NTND
137void merge_base_index(struct index_state *istate)
138{
139 struct split_index *si = istate->split_index;
76b07b37 140 unsigned int i;
5fc2fc8f
NTND
141
142 mark_base_index_entries(si->base);
76b07b37
NTND
143
144 si->saved_cache = istate->cache;
145 si->saved_cache_nr = istate->cache_nr;
146 istate->cache_nr = si->base->cache_nr;
147 istate->cache = NULL;
148 istate->cache_alloc = 0;
5fc2fc8f 149 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
45ccef87 150 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
76b07b37
NTND
151
152 si->nr_deletions = 0;
153 si->nr_replacements = 0;
154 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
155 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
156 if (si->nr_deletions)
157 remove_marked_cache_entries(istate);
158
159 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
b3c96fb1
NTND
160 if (!ce_namelen(si->saved_cache[i]))
161 die("corrupt link extension, entry %d should "
162 "have non-zero length name", i);
76b07b37
NTND
163 add_index_entry(istate, si->saved_cache[i],
164 ADD_CACHE_OK_TO_ADD |
ce7c614b 165 ADD_CACHE_KEEP_CACHE_TREE |
76b07b37
NTND
166 /*
167 * we may have to replay what
168 * merge-recursive.c:update_stages()
169 * does, which has this flag on
170 */
171 ADD_CACHE_SKIP_DFCHECK);
172 si->saved_cache[i] = NULL;
173 }
174
175 ewah_free(si->delete_bitmap);
176 ewah_free(si->replace_bitmap);
177 free(si->saved_cache);
178 si->delete_bitmap = NULL;
179 si->replace_bitmap = NULL;
180 si->saved_cache = NULL;
181 si->saved_cache_nr = 0;
5fc2fc8f
NTND
182}
183
184void prepare_to_write_split_index(struct index_state *istate)
185{
186 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
187 struct cache_entry **entries = NULL, *ce;
188 int i, nr_entries = 0, nr_alloc = 0;
189
190 si->delete_bitmap = ewah_new();
191 si->replace_bitmap = ewah_new();
192
193 if (si->base) {
194 /* Go through istate->cache[] and mark CE_MATCHED to
195 * entry with positive index. We'll go through
196 * base->cache[] later to delete all entries in base
753c4515 197 * that are not marked with either CE_MATCHED or
96a1d8d3
NTND
198 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
199 * duplicate, deduplicate it.
200 */
201 for (i = 0; i < istate->cache_nr; i++) {
202 struct cache_entry *base;
203 /* namelen is checked separately */
204 const unsigned int ondisk_flags =
205 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
206 unsigned int ce_flags, base_flags, ret;
207 ce = istate->cache[i];
208 if (!ce->index)
209 continue;
210 if (ce->index > si->base->cache_nr) {
211 ce->index = 0;
212 continue;
213 }
214 ce->ce_flags |= CE_MATCHED; /* or "shared" */
215 base = si->base->cache[ce->index - 1];
216 if (ce == base)
217 continue;
218 if (ce->ce_namelen != base->ce_namelen ||
219 strcmp(ce->name, base->name)) {
220 ce->index = 0;
221 continue;
222 }
223 ce_flags = ce->ce_flags;
224 base_flags = base->ce_flags;
225 /* only on-disk flags matter */
226 ce->ce_flags &= ondisk_flags;
227 base->ce_flags &= ondisk_flags;
228 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
229 offsetof(struct cache_entry, name) -
230 offsetof(struct cache_entry, ce_stat_data));
231 ce->ce_flags = ce_flags;
232 base->ce_flags = base_flags;
233 if (ret)
234 ce->ce_flags |= CE_UPDATE_IN_BASE;
235 free(base);
236 si->base->cache[ce->index - 1] = ce;
237 }
238 for (i = 0; i < si->base->cache_nr; i++) {
239 ce = si->base->cache[i];
240 if ((ce->ce_flags & CE_REMOVE) ||
241 !(ce->ce_flags & CE_MATCHED))
242 ewah_set(si->delete_bitmap, i);
243 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
244 ewah_set(si->replace_bitmap, i);
b3c96fb1 245 ce->ce_flags |= CE_STRIP_NAME;
96a1d8d3
NTND
246 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
247 entries[nr_entries++] = ce;
248 }
249 }
250 }
251
252 for (i = 0; i < istate->cache_nr; i++) {
253 ce = istate->cache[i];
254 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
b3c96fb1 255 assert(!(ce->ce_flags & CE_STRIP_NAME));
96a1d8d3
NTND
256 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
257 entries[nr_entries++] = ce;
258 }
259 ce->ce_flags &= ~CE_MATCHED;
260 }
261
262 /*
263 * take cache[] out temporarily, put entries[] in its place
264 * for writing
265 */
266 si->saved_cache = istate->cache;
5fc2fc8f 267 si->saved_cache_nr = istate->cache_nr;
96a1d8d3
NTND
268 istate->cache = entries;
269 istate->cache_nr = nr_entries;
5fc2fc8f
NTND
270}
271
272void finish_writing_split_index(struct index_state *istate)
273{
274 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
275
276 ewah_free(si->delete_bitmap);
277 ewah_free(si->replace_bitmap);
278 si->delete_bitmap = NULL;
279 si->replace_bitmap = NULL;
280 free(istate->cache);
281 istate->cache = si->saved_cache;
5fc2fc8f
NTND
282 istate->cache_nr = si->saved_cache_nr;
283}
284
f9d7abec
NTND
285void unshare_split_index(struct index_state *istate, int discard)
286{
287 struct split_index *si = istate->split_index;
288 int i;
289
290 if (!si || !si->base)
291 return;
292
293 for (i = 0; i < istate->cache_nr; i++) {
294 struct cache_entry *ce = istate->cache[i];
295 struct cache_entry *new = NULL;
296
297 if (!ce->index ||
298 ce->index > si->base->cache_nr ||
299 ce != si->base->cache[ce->index - 1])
300 continue;
301
302 if (!discard) {
303 int len = ce_namelen(ce);
304 new = xcalloc(1, cache_entry_size(len));
305 copy_cache_entry(new, ce);
306 memcpy(new->name, ce->name, len);
307 new->index = 0;
308 }
309 istate->cache[i] = new;
310 }
311}
312
313
5fc2fc8f
NTND
314void discard_split_index(struct index_state *istate)
315{
316 struct split_index *si = istate->split_index;
317 if (!si)
318 return;
f9d7abec 319 unshare_split_index(istate, 0);
5fc2fc8f
NTND
320 istate->split_index = NULL;
321 si->refcount--;
322 if (si->refcount)
323 return;
324 if (si->base) {
325 discard_index(si->base);
326 free(si->base);
327 }
328 free(si);
329}
045113a5
NTND
330
331void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
332{
333 if (ce->index &&
334 istate->split_index &&
335 istate->split_index->base &&
336 ce->index <= istate->split_index->base->cache_nr &&
337 ce == istate->split_index->base->cache[ce->index - 1])
338 ce->ce_flags |= CE_REMOVE;
339 else
340 free(ce);
341}
078a58e8
NTND
342
343void replace_index_entry_in_base(struct index_state *istate,
344 struct cache_entry *old,
345 struct cache_entry *new)
346{
347 if (old->index &&
348 istate->split_index &&
349 istate->split_index->base &&
350 old->index <= istate->split_index->base->cache_nr) {
351 new->index = old->index;
352 if (old != istate->split_index->base->cache[new->index - 1])
353 free(istate->split_index->base->cache[new->index - 1]);
354 istate->split_index->base->cache[new->index - 1] = new;
355 }
356}
cef4fc7e
CC
357
358void add_split_index(struct index_state *istate)
359{
360 if (!istate->split_index) {
361 init_split_index(istate);
362 istate->cache_changed |= SPLIT_INDEX_ORDERED;
363 }
364}
365
366void remove_split_index(struct index_state *istate)
367{
f9d7abec
NTND
368 if (!istate->split_index)
369 return;
370 discard_split_index(istate);
371 istate->cache_changed |= SOMETHING_CHANGED;
cef4fc7e 372}