diff options
Diffstat (limited to 'src/nvim/memline.c')
-rw-r--r-- | src/nvim/memline.c | 144 |
1 files changed, 55 insertions, 89 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 8b2ebfe554..a72dc43eb4 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -91,8 +91,6 @@ typedef struct pointer_entry PTR_EN; /* block/line-count pair */ #define PTR_ID (('p' << 8) + 't') /* pointer block id */ #define BLOCK0_ID0 'b' /* block 0 id 0 */ #define BLOCK0_ID1 '0' /* block 0 id 1 */ -#define BLOCK0_ID1_C0 'c' /* block 0 id 1 'cm' 0 */ -#define BLOCK0_ID1_C1 'C' /* block 0 id 1 'cm' 1 */ /* * pointer to a block, used in a pointer block @@ -176,8 +174,7 @@ struct data_block { * variables, because the rest of the swap file is not portable. */ struct block0 { - char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1, - * BLOCK0_ID1_C0, BLOCK0_ID1_C1 */ + char_u b0_id[2]; ///< ID for block 0: BLOCK0_ID0 and BLOCK0_ID1. char_u b0_version[10]; /* Vim version string */ char_u b0_page_size[4]; /* number of bytes per page */ char_u b0_mtime[4]; /* last modification time of file */ @@ -376,7 +373,7 @@ error: if (mfp != NULL) { if (hp) mf_put(mfp, hp, false, false); - mf_close(mfp, true); /* will also free(mfp->mf_fname) */ + mf_close(mfp, true); /* will also xfree(mfp->mf_fname) */ } buf->b_ml.ml_mfp = NULL; return FAIL; @@ -421,7 +418,7 @@ void ml_setname(buf_T *buf) /* if the file name is the same we don't have to do anything */ if (fnamecmp(fname, mfp->mf_fname) == 0) { - free(fname); + xfree(fname); success = TRUE; break; } @@ -434,14 +431,14 @@ void ml_setname(buf_T *buf) /* try to rename the swap file */ if (vim_rename(mfp->mf_fname, fname) == 0) { success = TRUE; - free(mfp->mf_fname); + xfree(mfp->mf_fname); mfp->mf_fname = fname; - free(mfp->mf_ffname); + xfree(mfp->mf_ffname); mf_set_ffname(mfp); ml_upd_block0(buf, UB_SAME_DIR); break; } - free(fname); /* this fname didn't work, try another */ + xfree(fname); /* this fname didn't work, try another */ } if (mfp->mf_fd == -1) { /* need to (re)open the swap file */ @@ -570,9 +567,9 @@ void ml_close(buf_T *buf, int del_file) return; mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */ if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) - free(buf->b_ml.ml_line_ptr); - free(buf->b_ml.ml_stack); - free(buf->b_ml.ml_chunksize); + xfree(buf->b_ml.ml_line_ptr); + xfree(buf->b_ml.ml_stack); + xfree(buf->b_ml.ml_chunksize); buf->b_ml.ml_chunksize = NULL; buf->b_ml.ml_mfp = NULL; @@ -619,22 +616,16 @@ void ml_timestamp(buf_T *buf) ml_upd_block0(buf, UB_FNAME); } -/* - * Return FAIL when the ID of "b0p" is wrong. - */ -static int ml_check_b0_id(ZERO_BL *b0p) +/// Checks whether the IDs in b0 are valid. +static bool ml_check_b0_id(ZERO_BL *b0p) + FUNC_ATTR_NONNULL_ALL { - if (b0p->b0_id[0] != BLOCK0_ID0 - || (b0p->b0_id[1] != BLOCK0_ID1 - && b0p->b0_id[1] != BLOCK0_ID1_C0 - && b0p->b0_id[1] != BLOCK0_ID1_C1) - ) - return FAIL; - return OK; + return b0p->b0_id[0] == BLOCK0_ID0 && b0p->b0_id[1] == BLOCK0_ID1; } -/// Return true if all strings in b0 are correct (nul-terminated). -static bool ml_check_b0_strings(ZERO_BL *b0p) FUNC_ATTR_NONNULL_ALL +/// Checks whether all strings in b0 are valid (i.e. nul-terminated). +static bool ml_check_b0_strings(ZERO_BL *b0p) + FUNC_ATTR_NONNULL_ALL { return (memchr(b0p->b0_version, NUL, 10) && memchr(b0p->b0_uname, NUL, B0_UNAME_SIZE) @@ -833,7 +824,7 @@ void ml_recover(void) (void)recover_names(fname, FALSE, i, &fname_used); } if (fname_used == NULL) - goto theend; /* out of memory */ + goto theend; // user chose invalid number. /* When called from main() still need to initialize storage structure */ if (called_from_main && ml_open(curbuf) == FAIL) @@ -944,7 +935,7 @@ void ml_recover(void) /* need to reallocate the memory used to store the data */ p = xmalloc(mfp->mf_page_size); memmove(p, hp->bh_data, previous_page_size); - free(hp->bh_data); + xfree(hp->bh_data); hp->bh_data = p; b0p = hp->bh_data; } @@ -1017,7 +1008,7 @@ void ml_recover(void) set_fileformat(b0_ff - 1, OPT_LOCAL); if (b0_fenc != NULL) { set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL); - free(b0_fenc); + xfree(b0_fenc); } unchanged(curbuf, TRUE); @@ -1203,7 +1194,7 @@ void ml_recover(void) /* Need to copy one line, fetching the other one may flush it. */ p = vim_strsave(ml_get(idx)); i = STRCMP(p, ml_get(idx + lnum)); - free(p); + xfree(p); if (i != 0) { changed_int(); ++curbuf->b_changedtick; @@ -1246,15 +1237,17 @@ void ml_recover(void) redraw_curbuf_later(NOT_VALID); theend: - free(fname_used); + xfree(fname_used); recoverymode = FALSE; if (mfp != NULL) { if (hp != NULL) mf_put(mfp, hp, false, false); - mf_close(mfp, false); /* will also free(mfp->mf_fname) */ + mf_close(mfp, false); /* will also xfree(mfp->mf_fname) */ + } + if (buf != NULL) { //may be NULL if swap file not found. + xfree(buf->b_ml.ml_stack); + xfree(buf); } - free(buf->b_ml.ml_stack); - free(buf); if (serious_error && called_from_main) ml_close(curbuf, TRUE); else { @@ -1330,53 +1323,35 @@ recover_names ( if (dir_name[0] == '.' && dir_name[1] == NUL) { /* check current dir */ if (fname == NULL) { names[0] = vim_strsave((char_u *)"*.sw?"); -#if defined(UNIX) || defined(WIN3264) /* For Unix names starting with a dot are special. MS-Windows * supports this too, on some file systems. */ names[1] = vim_strsave((char_u *)".*.sw?"); names[2] = vim_strsave((char_u *)".sw?"); num_names = 3; -#else - num_names = 1; -#endif } else num_names = recov_file_names(names, fname_res, TRUE); } else { /* check directory dir_name */ if (fname == NULL) { names[0] = concat_fnames(dir_name, (char_u *)"*.sw?", TRUE); -#if defined(UNIX) || defined(WIN3264) /* For Unix names starting with a dot are special. MS-Windows * supports this too, on some file systems. */ names[1] = concat_fnames(dir_name, (char_u *)".*.sw?", TRUE); names[2] = concat_fnames(dir_name, (char_u *)".sw?", TRUE); num_names = 3; -#else - num_names = 1; -#endif } else { -#if defined(UNIX) || defined(WIN3264) p = dir_name + STRLEN(dir_name); if (after_pathsep(dir_name, p) && p[-1] == p[-2]) { /* Ends with '//', Use Full path for swap name */ tail = make_percent_swname(dir_name, fname_res); - } else -#endif - tail = path_tail(fname_res); - tail = concat_fnames(dir_name, tail, TRUE); + } else { + tail = path_tail(fname_res); + tail = concat_fnames(dir_name, tail, TRUE); + } num_names = recov_file_names(names, tail, FALSE); - free(tail); + xfree(tail); } } - // check for out-of-memory - for (int i = 0; i < num_names; ++i) { - if (names[i] == NULL) { - for (int j = 0; j < num_names; ++j) - free(names[j]); - num_names = 0; - break; - } - } if (num_names == 0) num_files = 0; else if (expand_wildcards(num_names, names, &num_files, &files, @@ -1397,7 +1372,7 @@ recover_names ( swapname = NULL; num_files = 1; } - free(swapname); + xfree(swapname); } } @@ -1411,9 +1386,9 @@ recover_names ( /* Remove the name from files[i]. Move further entries * down. When the array becomes empty free it here, since * FreeWild() won't be called below. */ - free(files[i]); + xfree(files[i]); if (--num_files == 0) - free(files); + xfree(files); else for (; i < num_files; ++i) files[i] = files[i + 1]; @@ -1454,15 +1429,14 @@ recover_names ( file_count += num_files; for (int i = 0; i < num_names; ++i) - free(names[i]); + xfree(names[i]); if (num_files > 0) FreeWild(num_files, files); } - free(dir_name); + xfree(dir_name); return file_count; } -#if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ /* * Append the full path to name with path separators made into percent * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"") @@ -1479,12 +1453,11 @@ static char_u *make_percent_swname(char_u *dir, char_u *name) if (vim_ispathsep(*d)) *d = '%'; d = concat_fnames(dir, s, TRUE); - free(s); - free(f); + xfree(s); + xfree(f); } return d; } -#endif #ifdef UNIX static int process_still_running; @@ -1585,17 +1558,12 @@ static time_t swapfile_info(char_u *fname) } static int recov_file_names(char_u **names, char_u *path, int prepend_dot) + FUNC_ATTR_NONNULL_ALL { - int num_names; - char_u *p; - int i; - - num_names = 0; + int num_names = 0; - /* - * May also add the file name with a dot prepended, for swap file in same - * dir as original file. - */ + // May also add the file name with a dot prepended, for swap file in same + // dir as original file. if (prepend_dot) { names[num_names] = modname(path, (char_u *)".sw?", TRUE); if (names[num_names] == NULL) @@ -1606,15 +1574,15 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot) // Form the normal swap file name pattern by appending ".sw?". names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE); if (num_names >= 1) { /* check if we have the same name twice */ - p = names[num_names - 1]; - i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); + char_u *p = names[num_names - 1]; + int i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); if (i > 0) p += i; /* file name has been expanded to full path */ if (STRCMP(p, names[num_names]) != 0) ++num_names; else - free(names[num_names]); + xfree(names[num_names]); } else ++num_names; @@ -2371,7 +2339,7 @@ int ml_replace(linenr_T lnum, char_u *line, int copy) if (curbuf->b_ml.ml_line_lnum != lnum) /* other line buffered */ ml_flush_line(curbuf); /* flush it */ else if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) /* same line allocated */ - free(curbuf->b_ml.ml_line_ptr); /* free it */ + xfree(curbuf->b_ml.ml_line_ptr); /* free it */ curbuf->b_ml.ml_line_ptr = line; curbuf->b_ml.ml_line_lnum = lnum; curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY; @@ -2726,7 +2694,7 @@ static void ml_flush_line(buf_T *buf) (void)ml_delete_int(buf, lnum, FALSE); } } - free(new_line); + xfree(new_line); entered = FALSE; } @@ -2970,7 +2938,7 @@ static int ml_add_stack(buf_T *buf) infoptr_T *newstack = xmalloc(sizeof(infoptr_T) * (buf->b_ml.ml_stack_size + STACK_INCR)); memmove(newstack, buf->b_ml.ml_stack, (size_t)top * sizeof(infoptr_T)); - free(buf->b_ml.ml_stack); + xfree(buf->b_ml.ml_stack); buf->b_ml.ml_stack = newstack; buf->b_ml.ml_stack_size += STACK_INCR; } @@ -3097,17 +3065,15 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name char_u fname_buf[MAXPATHL]; #endif -#if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ s = dir_name + STRLEN(dir_name); if (after_pathsep(dir_name, s) && s[-1] == s[-2]) { /* Ends with '//', Use Full path */ r = NULL; if ((s = make_percent_swname(dir_name, fname)) != NULL) { r = modname(s, (char_u *)".swp", FALSE); - free(s); + xfree(s); } return r; } -#endif #ifdef HAVE_READLINK /* Expand symlink in the file name, so that we put the swap file with the @@ -3123,7 +3089,7 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name return NULL; s = get_file_in_dir(r, dir_name); - free(r); + xfree(r); return s; } @@ -3163,7 +3129,7 @@ get_file_in_dir ( t = concat_fnames(fname, dname + 2, TRUE); *tail = save_char; retval = concat_fnames(t, tail, TRUE); - free(t); + xfree(t); } } else { retval = concat_fnames(dname, tail, TRUE); @@ -3298,7 +3264,7 @@ findswapname ( if (fname == NULL) /* must be out of memory */ break; if ((n = (int)STRLEN(fname)) == 0) { /* safety check */ - free(fname); + xfree(fname); fname = NULL; break; } @@ -3428,7 +3394,7 @@ findswapname ( if (process_still_running && choice >= 4) choice++; /* Skip missing "Delete it" button */ # endif - free(name); + xfree(name); /* pretend screen didn't scroll, need redraw anyway */ msg_scrolled = 0; @@ -3481,7 +3447,7 @@ findswapname ( if (fname[n - 1] == 'a') { /* ".s?a" */ if (fname[n - 2] == 'a') { /* ".saa": tried enough, give up */ EMSG(_("E326: Too many swap files found")); - free(fname); + xfree(fname); fname = NULL; break; } @@ -3491,7 +3457,7 @@ findswapname ( --fname[n - 1]; /* ".swo", ".swn", etc. */ } - free(dir_name); + xfree(dir_name); return fname; } |