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) {
|
if (sub_firstline == NULL) {
|
||||||
sub_firstline = vim_strsave(ml_get(sub_firstlnum));
|
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
|
/* 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()).
|
* too many calls to alloc()/free()).
|
||||||
*/
|
*/
|
||||||
new_start_len = needed_len + 50;
|
new_start_len = needed_len + 50;
|
||||||
if ((new_start = alloc_check(new_start_len)) == NULL)
|
new_start = (char_u *)xmalloc((size_t)new_start_len);
|
||||||
goto outofmem;
|
|
||||||
*new_start = NUL;
|
*new_start = NUL;
|
||||||
new_end = new_start;
|
new_end = new_start;
|
||||||
} else {
|
} else {
|
||||||
@ -4168,10 +4163,7 @@ void do_sub(exarg_T *eap)
|
|||||||
needed_len += len;
|
needed_len += len;
|
||||||
if (needed_len > (int)new_start_len) {
|
if (needed_len > (int)new_start_len) {
|
||||||
new_start_len = needed_len + 50;
|
new_start_len = needed_len + 50;
|
||||||
if ((p1 = alloc_check(new_start_len)) == NULL) {
|
p1 = (char_u *) xmalloc((size_t)new_start_len);
|
||||||
vim_free(new_start);
|
|
||||||
goto outofmem;
|
|
||||||
}
|
|
||||||
memmove(p1, new_start, (size_t)(len + 1));
|
memmove(p1, new_start, (size_t)(len + 1));
|
||||||
vim_free(new_start);
|
vim_free(new_start);
|
||||||
new_start = p1;
|
new_start = p1;
|
||||||
@ -4388,7 +4380,6 @@ skip:
|
|||||||
changed_lines(first_line, 0, last_line - i, i);
|
changed_lines(first_line, 0, last_line - i, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
outofmem:
|
|
||||||
vim_free(sub_firstline); /* may have to free allocated copy of the line */
|
vim_free(sub_firstline); /* may have to free allocated copy of the line */
|
||||||
|
|
||||||
/* ":s/pat//n" doesn't move the cursor */
|
/* ":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);
|
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.
|
* 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(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_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);
|
char_u *lalloc_clear(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
|
||||||
|
|
||||||
/// malloc() wrapper
|
/// malloc() wrapper
|
||||||
|
@ -1510,9 +1510,7 @@ void ins_char_bytes(char_u *buf, int charlen)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newp = alloc_check((unsigned)(linelen + newlen - oldlen));
|
newp = (char_u *) xmalloc((size_t)(linelen + newlen - oldlen));
|
||||||
if (newp == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* Copy bytes before the cursor. */
|
/* Copy bytes before the cursor. */
|
||||||
if (col > 0)
|
if (col > 0)
|
||||||
@ -1580,9 +1578,7 @@ void ins_str(char_u *s)
|
|||||||
oldp = ml_get(lnum);
|
oldp = ml_get(lnum);
|
||||||
oldlen = (int)STRLEN(oldp);
|
oldlen = (int)STRLEN(oldp);
|
||||||
|
|
||||||
newp = alloc_check((unsigned)(oldlen + newlen + 1));
|
newp = (char_u *) xmalloc((size_t)(oldlen + newlen + 1));
|
||||||
if (newp == NULL)
|
|
||||||
return;
|
|
||||||
if (col > 0)
|
if (col > 0)
|
||||||
memmove(newp, oldp, (size_t)col);
|
memmove(newp, oldp, (size_t)col);
|
||||||
memmove(newp + col, s, (size_t)newlen);
|
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 */
|
/* if we're splitting a TAB, allow for it */
|
||||||
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
|
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
|
||||||
len = (int)STRLEN(bd.textstart) + 1;
|
len = (int)STRLEN(bd.textstart) + 1;
|
||||||
newp = alloc_check((unsigned)(bd.textcol + i + j + len));
|
newp = (char_u *) xmalloc((size_t)(bd.textcol + i + j + len));
|
||||||
if (newp == NULL)
|
|
||||||
return;
|
|
||||||
memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
|
memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
|
||||||
memmove(newp, oldp, (size_t)bd.textcol);
|
memmove(newp, oldp, (size_t)bd.textcol);
|
||||||
copy_chars(newp + bd.textcol, (size_t)i, TAB);
|
copy_chars(newp + bd.textcol, (size_t)i, TAB);
|
||||||
@ -469,9 +467,7 @@ static void shift_block(oparg_T *oap, int amount)
|
|||||||
+ fill
|
+ fill
|
||||||
+ (unsigned)STRLEN(non_white) + 1;
|
+ (unsigned)STRLEN(non_white) + 1;
|
||||||
|
|
||||||
newp = alloc_check(new_line_len);
|
newp = (char_u *) xmalloc((size_t)(new_line_len));
|
||||||
if (newp == NULL)
|
|
||||||
return;
|
|
||||||
memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
|
memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
|
||||||
copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
|
copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
|
||||||
STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
|
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);
|
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + s_len + count + 1));
|
||||||
if (newp == NULL)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* copy up to shifted part */
|
/* copy up to shifted part */
|
||||||
memmove(newp, oldp, (size_t)(offset));
|
memmove(newp, oldp, (size_t)(offset));
|
||||||
@ -1496,9 +1490,7 @@ int op_delete(oparg_T *oap)
|
|||||||
*/
|
*/
|
||||||
n = bd.textlen - bd.startspaces - bd.endspaces;
|
n = bd.textlen - bd.startspaces - bd.endspaces;
|
||||||
oldp = ml_get(lnum);
|
oldp = ml_get(lnum);
|
||||||
newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
|
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + 1 - n));
|
||||||
if (newp == NULL)
|
|
||||||
continue;
|
|
||||||
/* copy up to deleted part */
|
/* copy up to deleted part */
|
||||||
memmove(newp, oldp, (size_t)bd.textcol);
|
memmove(newp, oldp, (size_t)bd.textcol);
|
||||||
/* insert spaces */
|
/* insert spaces */
|
||||||
@ -1778,9 +1770,7 @@ int op_replace(oparg_T *oap, int c)
|
|||||||
|
|
||||||
oldp = ml_get_curline();
|
oldp = ml_get_curline();
|
||||||
oldlen = STRLEN(oldp);
|
oldlen = STRLEN(oldp);
|
||||||
newp = alloc_check((unsigned)oldlen + 1 + n);
|
newp = (char_u *) xmalloc((size_t)(oldlen + 1 + n));
|
||||||
if (newp == NULL)
|
|
||||||
continue;
|
|
||||||
memset(newp, NUL, (size_t)(oldlen + 1 + n));
|
memset(newp, NUL, (size_t)(oldlen + 1 + n));
|
||||||
/* copy up to deleted part */
|
/* copy up to deleted part */
|
||||||
memmove(newp, oldp, (size_t)bd.textcol);
|
memmove(newp, oldp, (size_t)bd.textcol);
|
||||||
@ -1804,10 +1794,8 @@ int op_replace(oparg_T *oap, int c)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Replacing with \r or \n means splitting the line. */
|
/* Replacing with \r or \n means splitting the line. */
|
||||||
after_p = alloc_check(
|
after_p = (char_u *) xmalloc((size_t)(oldlen + 1 + n - STRLEN(newp)));
|
||||||
(unsigned)(oldlen + 1 + n - STRLEN(newp)));
|
STRMOVE(after_p, oldp);
|
||||||
if (after_p != NULL)
|
|
||||||
STRMOVE(after_p, oldp);
|
|
||||||
}
|
}
|
||||||
/* replace the line */
|
/* replace the line */
|
||||||
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
||||||
@ -2279,43 +2267,37 @@ int op_change(oparg_T *oap)
|
|||||||
if (ins_len > 0) {
|
if (ins_len > 0) {
|
||||||
/* Subsequent calls to ml_get() flush the firstline data - take a
|
/* Subsequent calls to ml_get() flush the firstline data - take a
|
||||||
* copy of the inserted text. */
|
* copy of the inserted text. */
|
||||||
if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) {
|
ins_text = (char_u *) xmalloc((size_t)(ins_len + 1));
|
||||||
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
|
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
|
||||||
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
|
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
|
||||||
linenr++) {
|
linenr++) {
|
||||||
block_prep(oap, &bd, linenr, TRUE);
|
block_prep(oap, &bd, linenr, TRUE);
|
||||||
if (!bd.is_short || virtual_op) {
|
if (!bd.is_short || virtual_op) {
|
||||||
pos_T vpos;
|
pos_T vpos;
|
||||||
|
|
||||||
/* If the block starts in virtual space, count the
|
/* If the block starts in virtual space, count the
|
||||||
* initial coladd offset as part of "startspaces" */
|
* initial coladd offset as part of "startspaces" */
|
||||||
if (bd.is_short) {
|
if (bd.is_short) {
|
||||||
vpos.lnum = linenr;
|
vpos.lnum = linenr;
|
||||||
(void)getvpos(&vpos, oap->start_vcol);
|
(void)getvpos(&vpos, oap->start_vcol);
|
||||||
} else
|
} else
|
||||||
vpos.coladd = 0;
|
vpos.coladd = 0;
|
||||||
oldp = ml_get(linenr);
|
oldp = ml_get(linenr);
|
||||||
newp = alloc_check((unsigned)(STRLEN(oldp)
|
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + vpos.coladd + ins_len + 1));
|
||||||
+ vpos.coladd
|
/* copy up to block start */
|
||||||
+ ins_len + 1));
|
memmove(newp, oldp, (size_t)bd.textcol);
|
||||||
if (newp == NULL)
|
offset = bd.textcol;
|
||||||
continue;
|
copy_spaces(newp + offset, (size_t)vpos.coladd);
|
||||||
/* copy up to block start */
|
offset += vpos.coladd;
|
||||||
memmove(newp, oldp, (size_t)bd.textcol);
|
memmove(newp + offset, ins_text, (size_t)ins_len);
|
||||||
offset = bd.textcol;
|
offset += ins_len;
|
||||||
copy_spaces(newp + offset, (size_t)vpos.coladd);
|
oldp += bd.textcol;
|
||||||
offset += vpos.coladd;
|
STRMOVE(newp + offset, oldp);
|
||||||
memmove(newp + offset, ins_text, (size_t)ins_len);
|
ml_replace(linenr, newp, FALSE);
|
||||||
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);
|
vim_free(ins_text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2920,9 +2902,7 @@ do_put (
|
|||||||
|
|
||||||
/* insert the new text */
|
/* insert the new text */
|
||||||
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
|
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
|
||||||
newp = alloc_check((unsigned)totlen + oldlen + 1);
|
newp = (char_u *) xmalloc((size_t)(totlen + oldlen + 1));
|
||||||
if (newp == NULL)
|
|
||||||
break;
|
|
||||||
/* copy part up to cursor to new line */
|
/* copy part up to cursor to new line */
|
||||||
ptr = newp;
|
ptr = newp;
|
||||||
memmove(ptr, oldp, (size_t)bd.textcol);
|
memmove(ptr, oldp, (size_t)bd.textcol);
|
||||||
@ -3018,9 +2998,7 @@ do_put (
|
|||||||
totlen = count * yanklen;
|
totlen = count * yanklen;
|
||||||
if (totlen > 0) {
|
if (totlen > 0) {
|
||||||
oldp = ml_get(lnum);
|
oldp = ml_get(lnum);
|
||||||
newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
|
newp = (char_u *) xmalloc((size_t)(STRLEN(oldp) + totlen + 1));
|
||||||
if (newp == NULL)
|
|
||||||
goto end; /* alloc() gave an error message */
|
|
||||||
memmove(newp, oldp, (size_t)col);
|
memmove(newp, oldp, (size_t)col);
|
||||||
ptr = newp + col;
|
ptr = newp + col;
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
@ -3063,9 +3041,7 @@ do_put (
|
|||||||
lnum = new_cursor.lnum;
|
lnum = new_cursor.lnum;
|
||||||
ptr = ml_get(lnum) + col;
|
ptr = ml_get(lnum) + col;
|
||||||
totlen = (int)STRLEN(y_array[y_size - 1]);
|
totlen = (int)STRLEN(y_array[y_size - 1]);
|
||||||
newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
|
newp = (char_u *) xmalloc((size_t)(STRLEN(ptr) + totlen + 1));
|
||||||
if (newp == NULL)
|
|
||||||
goto error;
|
|
||||||
STRCPY(newp, y_array[y_size - 1]);
|
STRCPY(newp, y_array[y_size - 1]);
|
||||||
STRCAT(newp, ptr);
|
STRCAT(newp, ptr);
|
||||||
/* insert second line */
|
/* insert second line */
|
||||||
@ -3073,9 +3049,7 @@ do_put (
|
|||||||
vim_free(newp);
|
vim_free(newp);
|
||||||
|
|
||||||
oldp = ml_get(lnum);
|
oldp = ml_get(lnum);
|
||||||
newp = alloc_check((unsigned)(col + yanklen + 1));
|
newp = (char_u *) xmalloc((size_t)(col + yanklen + 1));
|
||||||
if (newp == NULL)
|
|
||||||
goto error;
|
|
||||||
/* copy first part of line */
|
/* copy first part of line */
|
||||||
memmove(newp, oldp, (size_t)col);
|
memmove(newp, oldp, (size_t)col);
|
||||||
/* append to first line */
|
/* 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];
|
col = sumsize - currsize - spaces[count - 1];
|
||||||
|
|
||||||
/* allocate the space for the new line */
|
/* 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 = newp + sumsize;
|
||||||
*cend = 0;
|
*cend = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user