Merge pull request #34217 from bfredl/shadajump

refactor(shada): expand macros taking macros and code fragments
This commit is contained in:
bfredl
2025-06-09 09:54:30 +02:00
committed by GitHub

View File

@@ -877,51 +877,38 @@ static inline bool marks_equal(const pos_T a, const pos_T b)
return (a.lnum == b.lnum) && (a.col == b.col); return (a.lnum == b.lnum) && (a.col == b.col);
} }
#define MERGE_JUMPS(jumps_size, jumps, jumps_type, timestamp_attr, mark_attr, \ /// adjust "jumps_arr" to make space to insert an item just before the item at "i"
entry, fname_cond, free_func, fin_func, \ /// (or after the last if i == jl_len)
idxadj_func, afterfree_func) \ ///
do { \ /// Higher incidies indicate newer items. If the list is full, discard the oldest item
const int jl_len = (int)(jumps_size); \ /// (or don't insert the considered item if it is older)
int i; \ ///
for (i = jl_len; i > 0; i--) { \ /// @return the actual position a new item should be inserted or -1 if it shouldn't be inserted
const jumps_type jl_entry = (jumps)[i - 1]; \ static int marklist_insert(void *jumps_arr, size_t jump_size, int jl_len, int i)
if (jl_entry.timestamp_attr <= (entry).timestamp) { \ {
if (marks_equal(jl_entry.mark_attr, (entry).data.filemark.mark) \ char *jumps = (char *)jumps_arr; // for pointer maffs
&& (fname_cond)) { \ if (i > 0) {
i = -1; \ if (jl_len == JUMPLISTSIZE) {
} \ i--;
break; \ if (i > 0) {
} \ // delete oldest item to make room for new element
} \ memmove(jumps, jumps + jump_size, jump_size * (size_t)i);
if (i > 0) { \ }
if (jl_len == JUMPLISTSIZE) { \ } else if (i != jl_len) {
free_func((jumps)[0]); \ // insert at position i, move newer items out of the way
i--; \ memmove(jumps + (size_t)(i + 1) * jump_size, jumps + (size_t)i * jump_size,
if (i > 0) { \ jump_size * (size_t)(jl_len - i));
memmove(&(jumps)[0], &(jumps)[1], sizeof((jumps)[1]) * (size_t)i); \ }
} \ } else if (i == 0) {
} else if (i != jl_len) { \ if (jl_len == JUMPLISTSIZE) {
memmove(&(jumps)[i + 1], &(jumps)[i], \ return -1; // don't insert, older than the entire list
sizeof((jumps)[0]) * (size_t)(jl_len - i)); \ } else if (jl_len > 0) {
} \ // insert i as the oldest item
} else if (i == 0) { \ memmove(jumps + jump_size, jumps, jump_size * (size_t)jl_len);
if (jl_len == JUMPLISTSIZE) { \ }
i = -1; \ }
} else if (jl_len > 0) { \ return i;
memmove(&(jumps)[1], &(jumps)[0], sizeof((jumps)[0]) * (size_t)jl_len); \ }
} \
} \
if (i != -1) { \
(jumps)[i] = fin_func(entry); \
if (jl_len < JUMPLISTSIZE) { \
(jumps_size)++; \
} \
idxadj_func(i); \
} else { \
shada_free_shada_entry(&(entry)); \
afterfree_func(entry); \
} \
} while (0)
/// Read data from ShaDa file /// Read data from ShaDa file
/// ///
@@ -1122,24 +1109,38 @@ static void shada_read(FileDescriptor *const sd_reader, const int flags)
break; break;
} }
} else { } else {
#define SDE_TO_XFMARK(entry) fm int i;
#define ADJUST_IDX(i) \ for (i = curwin->w_jumplistlen; i > 0; i--) {
if (curwin->w_jumplistidx >= (i) \ const xfmark_T jl_entry = curwin->w_jumplist[i - 1];
&& curwin->w_jumplistidx + 1 <= curwin->w_jumplistlen) { \ if (jl_entry.fmark.timestamp <= cur_entry.timestamp) {
curwin->w_jumplistidx++; \ if (marks_equal(jl_entry.fmark.mark, cur_entry.data.filemark.mark)
} && (buf == NULL
#define DUMMY_AFTERFREE(entry) ? (jl_entry.fname != NULL && strcmp(fm.fname, jl_entry.fname) == 0)
MERGE_JUMPS(curwin->w_jumplistlen, curwin->w_jumplist, xfmark_T, : fm.fmark.fnum == jl_entry.fmark.fnum)) {
fmark.timestamp, fmark.mark, cur_entry, i = -1;
(buf == NULL }
? (jl_entry.fname != NULL break;
&& strcmp(fm.fname, jl_entry.fname) == 0) }
: fm.fmark.fnum == jl_entry.fmark.fnum), }
free_xfmark, SDE_TO_XFMARK, ADJUST_IDX, DUMMY_AFTERFREE); if (i > 0 && curwin->w_jumplistlen == JUMPLISTSIZE) {
#undef SDE_TO_XFMARK free_xfmark(curwin->w_jumplist[0]);
#undef ADJUST_IDX }
#undef DUMMY_AFTERFREE i = marklist_insert(curwin->w_jumplist, sizeof(*curwin->w_jumplist),
curwin->w_jumplistlen, i);
if (i != -1) {
curwin->w_jumplist[i] = fm;
if (curwin->w_jumplistlen < JUMPLISTSIZE) {
curwin->w_jumplistlen++;
}
if (curwin->w_jumplistidx >= i && curwin->w_jumplistidx + 1 <= curwin->w_jumplistlen) {
curwin->w_jumplistidx++;
}
} else {
shada_free_shada_entry(&cur_entry);
}
} }
// Do not free shada entry: its allocated memory was saved above. // Do not free shada entry: its allocated memory was saved above.
break; break;
} }
@@ -1203,18 +1204,30 @@ static void shada_read(FileDescriptor *const sd_reader, const int flags)
} }
} else { } else {
set_put(ptr_t, &cl_bufs, buf); set_put(ptr_t, &cl_bufs, buf);
#define SDE_TO_FMARK(entry) fm int i;
#define AFTERFREE(entry) (entry).data.filemark.fname = NULL for (i = buf->b_changelistlen; i > 0; i--) {
#define DUMMY_IDX_ADJ(i) const fmark_T jl_entry = buf->b_changelist[i - 1];
MERGE_JUMPS(buf->b_changelistlen, buf->b_changelist, fmark_T, if (jl_entry.timestamp <= cur_entry.timestamp) {
timestamp, mark, cur_entry, true, if (marks_equal(jl_entry.mark, cur_entry.data.filemark.mark)) {
free_fmark, SDE_TO_FMARK, DUMMY_IDX_ADJ, AFTERFREE); i = -1;
#undef SDE_TO_FMARK }
#undef AFTERFREE break;
#undef DUMMY_IDX_ADJ }
}
if (i > 0 && buf->b_changelistlen == JUMPLISTSIZE) {
free_fmark(buf->b_changelist[0]);
}
i = marklist_insert(buf->b_changelist, sizeof(*buf->b_changelist), buf->b_changelistlen, i);
if (i != -1) {
buf->b_changelist[i] = fm;
if (buf->b_changelistlen < JUMPLISTSIZE) {
buf->b_changelistlen++;
}
} else {
xfree(fm.additional_data);
}
} }
// Do not free shada entry: except for fname, its allocated memory (i.e. // only free fname part of shada entry, as additional_data was saved or freed above.
// additional_data attribute contents if non-NULL) was saved above.
xfree(cur_entry.data.filemark.fname); xfree(cur_entry.data.filemark.fname);
break; break;
} }
@@ -1761,16 +1774,6 @@ static inline ShaDaWriteResult shada_read_when_writing(FileDescriptor *const sd_
*wms_entry = pfs_entry; \ *wms_entry = pfs_entry; \
} while (0) } while (0)
#define FREE_POSSIBLY_FREED_SHADA_ENTRY(entry) \
do { \
if ((entry).can_free_entry) { \
shada_free_shada_entry(&(entry).data); \
} \
} while (0)
#define SDE_TO_PFSDE(entry) \
((PossiblyFreedShadaEntry) { .can_free_entry = true, .data = (entry) })
while ((srni_ret = shada_read_next_item(sd_reader, &entry, srni_flags, while ((srni_ret = shada_read_next_item(sd_reader, &entry, srni_flags,
max_kbyte)) max_kbyte))
!= kSDReadStatusFinished) { != kSDReadStatusFinished) {
@@ -1952,27 +1955,62 @@ static inline ShaDaWriteResult shada_read_when_writing(FileDescriptor *const sd_
} }
} }
} else { } else {
#define AFTERFREE_DUMMY(entry) int i;
#define DUMMY_IDX_ADJ(i) for (i = (int)filemarks->changes_size; i > 0; i--) {
MERGE_JUMPS(filemarks->changes_size, filemarks->changes, const PossiblyFreedShadaEntry jl_entry = filemarks->changes[i - 1];
PossiblyFreedShadaEntry, data.timestamp, if (jl_entry.data.timestamp <= (entry).timestamp) {
data.data.filemark.mark, entry, true, if (marks_equal(jl_entry.data.data.filemark.mark, entry.data.filemark.mark)) {
FREE_POSSIBLY_FREED_SHADA_ENTRY, SDE_TO_PFSDE, i = -1;
DUMMY_IDX_ADJ, AFTERFREE_DUMMY); }
break;
}
}
if (i > 0 && filemarks->changes_size == JUMPLISTSIZE) {
if (filemarks->changes[0].can_free_entry) {
shada_free_shada_entry(&filemarks->changes[0].data);
}
}
i = marklist_insert(filemarks->changes, sizeof(*filemarks->changes),
(int)filemarks->changes_size, i);
if (i != -1) {
filemarks->changes[i] = (PossiblyFreedShadaEntry) { .can_free_entry = true,
.data = entry };
if (filemarks->changes_size < JUMPLISTSIZE) {
filemarks->changes_size++;
}
} else {
shada_free_shada_entry(&(entry));
}
} }
break; break;
} }
case kSDItemJump: case kSDItemJump:
MERGE_JUMPS(wms->jumps_size, wms->jumps, PossiblyFreedShadaEntry, ;
data.timestamp, data.data.filemark.mark, entry, int i;
strcmp(jl_entry.data.data.filemark.fname, for (i = (int)wms->jumps_size; i > 0; i--) {
entry.data.filemark.fname) == 0, const PossiblyFreedShadaEntry jl_entry = wms->jumps[i - 1];
FREE_POSSIBLY_FREED_SHADA_ENTRY, SDE_TO_PFSDE, if (jl_entry.data.timestamp <= entry.timestamp) {
DUMMY_IDX_ADJ, AFTERFREE_DUMMY); if (marks_equal(jl_entry.data.data.filemark.mark, entry.data.filemark.mark)
#undef FREE_POSSIBLY_FREED_SHADA_ENTRY && strcmp(jl_entry.data.data.filemark.fname, entry.data.filemark.fname) == 0) {
#undef SDE_TO_PFSDE i = -1;
#undef DUMMY_IDX_ADJ }
#undef AFTERFREE_DUMMY break;
}
}
if (i > 0 && wms->jumps_size == JUMPLISTSIZE) {
if (wms->jumps[0].can_free_entry) {
shada_free_shada_entry(&wms->jumps[0].data);
}
}
i = marklist_insert(wms->jumps, sizeof(*wms->jumps), (int)wms->jumps_size, i);
if (i != -1) {
wms->jumps[i] = (PossiblyFreedShadaEntry) { .can_free_entry = true, .data = entry };
if (wms->jumps_size < JUMPLISTSIZE) {
wms->jumps_size++;
}
} else {
shada_free_shada_entry(&(entry));
}
break; break;
} }
} }