mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Replace alloc_check
by xmalloc
`alloc_check` is just a wrapper around xmalloc, so we can remove it and use xmalloc directly. ref #487 / #488 The call was replaced in the following files: - ex_cmds.c - misc1.c - ops.c
This commit is contained in:
parent
321c67d610
commit
3fcdb2ab29
@ -3861,10 +3861,6 @@ void do_sub(exarg_T *eap)
|
||||
|
||||
if (sub_firstline == NULL) {
|
||||
sub_firstline = vim_strsave(ml_get(sub_firstlnum));
|
||||
if (sub_firstline == NULL) {
|
||||
vim_free(new_start);
|
||||
goto outofmem;
|
||||
}
|
||||
}
|
||||
|
||||
/* Save the line number of the last change for the final
|
||||
@ -4154,8 +4150,7 @@ void do_sub(exarg_T *eap)
|
||||
* too many calls to alloc()/free()).
|
||||
*/
|
||||
new_start_len = needed_len + 50;
|
||||
if ((new_start = alloc_check(new_start_len)) == NULL)
|
||||
goto outofmem;
|
||||
new_start = (char_u *)xmalloc((size_t)new_start_len);
|
||||
*new_start = NUL;
|
||||
new_end = new_start;
|
||||
} else {
|
||||
@ -4168,10 +4163,7 @@ void do_sub(exarg_T *eap)
|
||||
needed_len += len;
|
||||
if (needed_len > (int)new_start_len) {
|
||||
new_start_len = needed_len + 50;
|
||||
if ((p1 = alloc_check(new_start_len)) == NULL) {
|
||||
vim_free(new_start);
|
||||
goto outofmem;
|
||||
}
|
||||
p1 = (char_u *) xmalloc((size_t)new_start_len);
|
||||
memmove(p1, new_start, (size_t)(len + 1));
|
||||
vim_free(new_start);
|
||||
new_start = p1;
|
||||
@ -4388,7 +4380,6 @@ skip:
|
||||
changed_lines(first_line, 0, last_line - i, i);
|
||||
}
|
||||
|
||||
outofmem:
|
||||
vim_free(sub_firstline); /* may have to free allocated copy of the line */
|
||||
|
||||
/* ":s/pat//n" doesn't move the cursor */
|
||||
|
16
src/memory.c
16
src/memory.c
@ -72,22 +72,6 @@ char_u *alloc_clear(unsigned size)
|
||||
return (char_u *)xcalloc(1, (size_t)size);
|
||||
}
|
||||
|
||||
/*
|
||||
* alloc() with check for maximum line length
|
||||
*/
|
||||
char_u *alloc_check(unsigned size)
|
||||
{
|
||||
#if !defined(UNIX) && !defined(__EMX__)
|
||||
if (sizeof(int) == 2 && size > 0x7fff) {
|
||||
/* Don't hide this message */
|
||||
emsg_silent = 0;
|
||||
EMSG(_("E340: Line is becoming too long"));
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
return lalloc((long_u)size, TRUE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate memory like lalloc() and set all bytes to zero.
|
||||
*/
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
char_u *alloc(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
|
||||
char_u *alloc_clear(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
|
||||
char_u *alloc_check(unsigned size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
|
||||
char_u *lalloc_clear(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
|
||||
|
||||
/// malloc() wrapper
|
||||
|
@ -1510,9 +1510,7 @@ void ins_char_bytes(char_u *buf, int charlen)
|
||||
}
|
||||
}
|
||||
|
||||
newp = alloc_check((unsigned)(linelen + newlen - oldlen));
|
||||
if (newp == NULL)
|
||||
return;
|
||||
newp = (char_u *) xmalloc((size_t)(linelen + newlen - oldlen));
|
||||
|
||||
/* Copy bytes before the cursor. */
|
||||
if (col > 0)
|
||||
@ -1580,9 +1578,7 @@ void ins_str(char_u *s)
|
||||
oldp = ml_get(lnum);
|
||||
oldlen = (int)STRLEN(oldp);
|
||||
|
||||
newp = alloc_check((unsigned)(oldlen + newlen + 1));
|
||||
if (newp == NULL)
|
||||
return;
|
||||
newp = (char_u *) xmalloc((size_t)(oldlen + newlen + 1));
|
||||
if (col > 0)
|
||||
memmove(newp, oldp, (size_t)col);
|
||||
memmove(newp + col, s, (size_t)newlen);
|
||||
|
106
src/ops.c
106
src/ops.c
@ -383,9 +383,7 @@ static void shift_block(oparg_T *oap, int amount)
|
||||
/* if we're splitting a TAB, allow for it */
|
||||
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
|
||||
len = (int)STRLEN(bd.textstart) + 1;
|
||||
newp = alloc_check((unsigned)(bd.textcol + i + j + len));
|
||||
if (newp == NULL)
|
||||
return;
|
||||
newp = (char_u *) xmalloc((size_t)(bd.textcol + i + j + len));
|
||||
memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
|
||||
memmove(newp, oldp, (size_t)bd.textcol);
|
||||
copy_chars(newp + bd.textcol, (size_t)i, TAB);
|
||||
@ -469,9 +467,7 @@ static void shift_block(oparg_T *oap, int amount)
|
||||
+ fill
|
||||
+ (unsigned)STRLEN(non_white) + 1;
|
||||
|
||||
newp = alloc_check(new_line_len);
|
||||
if (newp == NULL)
|
||||
return;
|
||||
newp = (char_u *) xmalloc((size_t)(new_line_len));
|
||||
memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
|
||||
copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
|
||||
STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
|
||||
@ -531,9 +527,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
|
||||
}
|
||||
}
|
||||
|
||||
newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
|
||||
if (newp == NULL)
|
||||
continue;
|
||||
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + s_len + count + 1));
|
||||
|
||||
/* copy up to shifted part */
|
||||
memmove(newp, oldp, (size_t)(offset));
|
||||
@ -1496,9 +1490,7 @@ int op_delete(oparg_T *oap)
|
||||
*/
|
||||
n = bd.textlen - bd.startspaces - bd.endspaces;
|
||||
oldp = ml_get(lnum);
|
||||
newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
|
||||
if (newp == NULL)
|
||||
continue;
|
||||
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + 1 - n));
|
||||
/* copy up to deleted part */
|
||||
memmove(newp, oldp, (size_t)bd.textcol);
|
||||
/* insert spaces */
|
||||
@ -1778,9 +1770,7 @@ int op_replace(oparg_T *oap, int c)
|
||||
|
||||
oldp = ml_get_curline();
|
||||
oldlen = STRLEN(oldp);
|
||||
newp = alloc_check((unsigned)oldlen + 1 + n);
|
||||
if (newp == NULL)
|
||||
continue;
|
||||
newp = (char_u *) xmalloc((size_t)(oldlen + 1 + n));
|
||||
memset(newp, NUL, (size_t)(oldlen + 1 + n));
|
||||
/* copy up to deleted part */
|
||||
memmove(newp, oldp, (size_t)bd.textcol);
|
||||
@ -1804,10 +1794,8 @@ int op_replace(oparg_T *oap, int c)
|
||||
}
|
||||
} else {
|
||||
/* Replacing with \r or \n means splitting the line. */
|
||||
after_p = alloc_check(
|
||||
(unsigned)(oldlen + 1 + n - STRLEN(newp)));
|
||||
if (after_p != NULL)
|
||||
STRMOVE(after_p, oldp);
|
||||
after_p = (char_u *) xmalloc((size_t)(oldlen + 1 + n - STRLEN(newp)));
|
||||
STRMOVE(after_p, oldp);
|
||||
}
|
||||
/* replace the line */
|
||||
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
||||
@ -2279,43 +2267,37 @@ int op_change(oparg_T *oap)
|
||||
if (ins_len > 0) {
|
||||
/* Subsequent calls to ml_get() flush the firstline data - take a
|
||||
* copy of the inserted text. */
|
||||
if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) {
|
||||
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
|
||||
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
|
||||
linenr++) {
|
||||
block_prep(oap, &bd, linenr, TRUE);
|
||||
if (!bd.is_short || virtual_op) {
|
||||
pos_T vpos;
|
||||
ins_text = (char_u *) xmalloc((size_t)(ins_len + 1));
|
||||
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
|
||||
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
|
||||
linenr++) {
|
||||
block_prep(oap, &bd, linenr, TRUE);
|
||||
if (!bd.is_short || virtual_op) {
|
||||
pos_T vpos;
|
||||
|
||||
/* If the block starts in virtual space, count the
|
||||
* initial coladd offset as part of "startspaces" */
|
||||
if (bd.is_short) {
|
||||
vpos.lnum = linenr;
|
||||
(void)getvpos(&vpos, oap->start_vcol);
|
||||
} else
|
||||
vpos.coladd = 0;
|
||||
oldp = ml_get(linenr);
|
||||
newp = alloc_check((unsigned)(STRLEN(oldp)
|
||||
+ vpos.coladd
|
||||
+ ins_len + 1));
|
||||
if (newp == NULL)
|
||||
continue;
|
||||
/* copy up to block start */
|
||||
memmove(newp, oldp, (size_t)bd.textcol);
|
||||
offset = bd.textcol;
|
||||
copy_spaces(newp + offset, (size_t)vpos.coladd);
|
||||
offset += vpos.coladd;
|
||||
memmove(newp + offset, ins_text, (size_t)ins_len);
|
||||
offset += ins_len;
|
||||
oldp += bd.textcol;
|
||||
STRMOVE(newp + offset, oldp);
|
||||
ml_replace(linenr, newp, FALSE);
|
||||
}
|
||||
/* If the block starts in virtual space, count the
|
||||
* initial coladd offset as part of "startspaces" */
|
||||
if (bd.is_short) {
|
||||
vpos.lnum = linenr;
|
||||
(void)getvpos(&vpos, oap->start_vcol);
|
||||
} else
|
||||
vpos.coladd = 0;
|
||||
oldp = ml_get(linenr);
|
||||
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + vpos.coladd + ins_len + 1));
|
||||
/* copy up to block start */
|
||||
memmove(newp, oldp, (size_t)bd.textcol);
|
||||
offset = bd.textcol;
|
||||
copy_spaces(newp + offset, (size_t)vpos.coladd);
|
||||
offset += vpos.coladd;
|
||||
memmove(newp + offset, ins_text, (size_t)ins_len);
|
||||
offset += ins_len;
|
||||
oldp += bd.textcol;
|
||||
STRMOVE(newp + offset, oldp);
|
||||
ml_replace(linenr, newp, FALSE);
|
||||
}
|
||||
check_cursor();
|
||||
|
||||
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
|
||||
}
|
||||
check_cursor();
|
||||
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
|
||||
vim_free(ins_text);
|
||||
}
|
||||
}
|
||||
@ -2920,9 +2902,7 @@ do_put (
|
||||
|
||||
/* insert the new text */
|
||||
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
|
||||
newp = alloc_check((unsigned)totlen + oldlen + 1);
|
||||
if (newp == NULL)
|
||||
break;
|
||||
newp = (char_u *) xmalloc((size_t)(totlen + oldlen + 1));
|
||||
/* copy part up to cursor to new line */
|
||||
ptr = newp;
|
||||
memmove(ptr, oldp, (size_t)bd.textcol);
|
||||
@ -3018,9 +2998,7 @@ do_put (
|
||||
totlen = count * yanklen;
|
||||
if (totlen > 0) {
|
||||
oldp = ml_get(lnum);
|
||||
newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
|
||||
if (newp == NULL)
|
||||
goto end; /* alloc() gave an error message */
|
||||
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + totlen + 1));
|
||||
memmove(newp, oldp, (size_t)col);
|
||||
ptr = newp + col;
|
||||
for (i = 0; i < count; ++i) {
|
||||
@ -3063,9 +3041,7 @@ do_put (
|
||||
lnum = new_cursor.lnum;
|
||||
ptr = ml_get(lnum) + col;
|
||||
totlen = (int)STRLEN(y_array[y_size - 1]);
|
||||
newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
|
||||
if (newp == NULL)
|
||||
goto error;
|
||||
newp = (char_u *) xmalloc((size_t)(STRLEN(ptr) + totlen + 1));
|
||||
STRCPY(newp, y_array[y_size - 1]);
|
||||
STRCAT(newp, ptr);
|
||||
/* insert second line */
|
||||
@ -3073,9 +3049,7 @@ do_put (
|
||||
vim_free(newp);
|
||||
|
||||
oldp = ml_get(lnum);
|
||||
newp = alloc_check((unsigned)(col + yanklen + 1));
|
||||
if (newp == NULL)
|
||||
goto error;
|
||||
newp = (char_u *) xmalloc((size_t)(col + yanklen + 1));
|
||||
/* copy first part of line */
|
||||
memmove(newp, oldp, (size_t)col);
|
||||
/* append to first line */
|
||||
@ -3569,7 +3543,7 @@ int do_join(long count, int insert_space, int save_undo, int use_formatoptions)
|
||||
col = sumsize - currsize - spaces[count - 1];
|
||||
|
||||
/* allocate the space for the new line */
|
||||
newp = alloc_check((unsigned)(sumsize + 1));
|
||||
newp = (char_u *) xmalloc((size_t)(sumsize + 1));
|
||||
cend = newp + sumsize;
|
||||
*cend = 0;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user