*.[ch] refactoring: make use of the FREE_AND_NULL() macro
[git] / split-index.c
1 #include "cache.h"
2 #include "split-index.h"
3 #include "ewah/ewok.h"
4
5 struct 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
14 int 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;
19         int ret;
20
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;
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)
40                 return error("garbage at the end of link extension");
41         return 0;
42 }
43
44 int 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);
49         if (!si->delete_bitmap && !si->replace_bitmap)
50                 return 0;
51         ewah_serialize_strbuf(si->delete_bitmap, sb);
52         ewah_serialize_strbuf(si->replace_bitmap, sb);
53         return 0;
54 }
55
56 static 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
63          * from 1 instead of 0, which is reserved to say "this is a new
64          * entry".
65          */
66         for (i = 0; i < base->cache_nr; i++)
67                 base->cache[i]->index = i + 1;
68 }
69
70 void move_cache_to_base_index(struct index_state *istate)
71 {
72         struct split_index *si = istate->split_index;
73         int i;
74
75         /*
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.
79          */
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         }
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;
93         COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
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
99 static 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
109 static 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;
114
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];
126         if (ce_namelen(src))
127                 die("corrupt link extension, entry %d should have "
128                     "zero length name", (int)pos);
129         src->index = pos + 1;
130         src->ce_flags |= CE_UPDATE_IN_BASE;
131         src->ce_namelen = dst->ce_namelen;
132         copy_cache_entry(dst, src);
133         free(src);
134         si->nr_replacements++;
135 }
136
137 void merge_base_index(struct index_state *istate)
138 {
139         struct split_index *si = istate->split_index;
140         unsigned int i;
141
142         mark_base_index_entries(si->base);
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;
149         ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
150         COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
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++) {
160                 if (!ce_namelen(si->saved_cache[i]))
161                         die("corrupt link extension, entry %d should "
162                             "have non-zero length name", i);
163                 add_index_entry(istate, si->saved_cache[i],
164                                 ADD_CACHE_OK_TO_ADD |
165                                 ADD_CACHE_KEEP_CACHE_TREE |
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_AND_NULL(si->saved_cache);
178         si->delete_bitmap  = NULL;
179         si->replace_bitmap = NULL;
180         si->saved_cache_nr = 0;
181 }
182
183 void prepare_to_write_split_index(struct index_state *istate)
184 {
185         struct split_index *si = init_split_index(istate);
186         struct cache_entry **entries = NULL, *ce;
187         int i, nr_entries = 0, nr_alloc = 0;
188
189         si->delete_bitmap = ewah_new();
190         si->replace_bitmap = ewah_new();
191
192         if (si->base) {
193                 /* Go through istate->cache[] and mark CE_MATCHED to
194                  * entry with positive index. We'll go through
195                  * base->cache[] later to delete all entries in base
196                  * that are not marked with either CE_MATCHED or
197                  * CE_UPDATE_IN_BASE. If istate->cache[i] is a
198                  * duplicate, deduplicate it.
199                  */
200                 for (i = 0; i < istate->cache_nr; i++) {
201                         struct cache_entry *base;
202                         /* namelen is checked separately */
203                         const unsigned int ondisk_flags =
204                                 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
205                         unsigned int ce_flags, base_flags, ret;
206                         ce = istate->cache[i];
207                         if (!ce->index)
208                                 continue;
209                         if (ce->index > si->base->cache_nr) {
210                                 ce->index = 0;
211                                 continue;
212                         }
213                         ce->ce_flags |= CE_MATCHED; /* or "shared" */
214                         base = si->base->cache[ce->index - 1];
215                         if (ce == base)
216                                 continue;
217                         if (ce->ce_namelen != base->ce_namelen ||
218                             strcmp(ce->name, base->name)) {
219                                 ce->index = 0;
220                                 continue;
221                         }
222                         ce_flags = ce->ce_flags;
223                         base_flags = base->ce_flags;
224                         /* only on-disk flags matter */
225                         ce->ce_flags   &= ondisk_flags;
226                         base->ce_flags &= ondisk_flags;
227                         ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
228                                      offsetof(struct cache_entry, name) -
229                                      offsetof(struct cache_entry, ce_stat_data));
230                         ce->ce_flags = ce_flags;
231                         base->ce_flags = base_flags;
232                         if (ret)
233                                 ce->ce_flags |= CE_UPDATE_IN_BASE;
234                         free(base);
235                         si->base->cache[ce->index - 1] = ce;
236                 }
237                 for (i = 0; i < si->base->cache_nr; i++) {
238                         ce = si->base->cache[i];
239                         if ((ce->ce_flags & CE_REMOVE) ||
240                             !(ce->ce_flags & CE_MATCHED))
241                                 ewah_set(si->delete_bitmap, i);
242                         else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
243                                 ewah_set(si->replace_bitmap, i);
244                                 ce->ce_flags |= CE_STRIP_NAME;
245                                 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
246                                 entries[nr_entries++] = ce;
247                         }
248                 }
249         }
250
251         for (i = 0; i < istate->cache_nr; i++) {
252                 ce = istate->cache[i];
253                 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
254                         assert(!(ce->ce_flags & CE_STRIP_NAME));
255                         ALLOC_GROW(entries, nr_entries+1, nr_alloc);
256                         entries[nr_entries++] = ce;
257                 }
258                 ce->ce_flags &= ~CE_MATCHED;
259         }
260
261         /*
262          * take cache[] out temporarily, put entries[] in its place
263          * for writing
264          */
265         si->saved_cache = istate->cache;
266         si->saved_cache_nr = istate->cache_nr;
267         istate->cache = entries;
268         istate->cache_nr = nr_entries;
269 }
270
271 void finish_writing_split_index(struct index_state *istate)
272 {
273         struct split_index *si = init_split_index(istate);
274
275         ewah_free(si->delete_bitmap);
276         ewah_free(si->replace_bitmap);
277         si->delete_bitmap = NULL;
278         si->replace_bitmap = NULL;
279         free(istate->cache);
280         istate->cache = si->saved_cache;
281         istate->cache_nr = si->saved_cache_nr;
282 }
283
284 void unshare_split_index(struct index_state *istate, int discard)
285 {
286         struct split_index *si = istate->split_index;
287         int i;
288
289         if (!si || !si->base)
290                 return;
291
292         for (i = 0; i < istate->cache_nr; i++) {
293                 struct cache_entry *ce = istate->cache[i];
294                 struct cache_entry *new = NULL;
295
296                 if (!ce->index ||
297                     ce->index > si->base->cache_nr ||
298                     ce != si->base->cache[ce->index - 1])
299                         continue;
300
301                 if (!discard) {
302                         int len = ce_namelen(ce);
303                         new = xcalloc(1, cache_entry_size(len));
304                         copy_cache_entry(new, ce);
305                         memcpy(new->name, ce->name, len);
306                         new->index = 0;
307                 }
308                 istate->cache[i] = new;
309         }
310 }
311
312
313 void discard_split_index(struct index_state *istate)
314 {
315         struct split_index *si = istate->split_index;
316         if (!si)
317                 return;
318         unshare_split_index(istate, 0);
319         istate->split_index = NULL;
320         si->refcount--;
321         if (si->refcount)
322                 return;
323         if (si->base) {
324                 discard_index(si->base);
325                 free(si->base);
326         }
327         free(si);
328 }
329
330 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
331 {
332         if (ce->index &&
333             istate->split_index &&
334             istate->split_index->base &&
335             ce->index <= istate->split_index->base->cache_nr &&
336             ce == istate->split_index->base->cache[ce->index - 1])
337                 ce->ce_flags |= CE_REMOVE;
338         else
339                 free(ce);
340 }
341
342 void replace_index_entry_in_base(struct index_state *istate,
343                                  struct cache_entry *old,
344                                  struct cache_entry *new)
345 {
346         if (old->index &&
347             istate->split_index &&
348             istate->split_index->base &&
349             old->index <= istate->split_index->base->cache_nr) {
350                 new->index = old->index;
351                 if (old != istate->split_index->base->cache[new->index - 1])
352                         free(istate->split_index->base->cache[new->index - 1]);
353                 istate->split_index->base->cache[new->index - 1] = new;
354         }
355 }
356
357 void add_split_index(struct index_state *istate)
358 {
359         if (!istate->split_index) {
360                 init_split_index(istate);
361                 istate->cache_changed |= SPLIT_INDEX_ORDERED;
362         }
363 }
364
365 void remove_split_index(struct index_state *istate)
366 {
367         if (!istate->split_index)
368                 return;
369         discard_split_index(istate);
370         istate->cache_changed |= SOMETHING_CHANGED;
371 }