xrealloc(): similar to xmalloc()

Replaced all calls to realloc by xrealloc. All `== NULL` tests can be removed
and the code within `!= NULL` tests can be unwrapped.
This commit is contained in:
Felipe Oliveira Carvalho 2014-03-29 01:05:04 -03:00 committed by Thiago de Arruda
parent 7bdd1f1898
commit 0e998066b2
10 changed files with 39 additions and 11 deletions

View File

@ -12018,7 +12018,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
/* Change "prev" buffer to be the right size. This way
* the bytes are only copied once, and very long lines are
* allocated only once. */
if ((s = realloc(prev, prevlen + len + 1)) != NULL) {
if ((s = xrealloc(prev, prevlen + len + 1)) != NULL) {
memmove(s + prevlen, start, len);
s[prevlen + len] = NUL;
prev = NULL; /* the list will own the string */
@ -12103,7 +12103,7 @@ static void f_readfile(typval_T *argvars, typval_T *rettv)
prevsize = grow50pc > growmin ? grow50pc : growmin;
}
newprev = prev == NULL ? alloc(prevsize)
: realloc(prev, prevsize);
: xrealloc(prev, prevsize);
if (newprev == NULL) {
do_outofmem_msg((long_u)prevsize);
failed = TRUE;

View File

@ -387,7 +387,7 @@ vim_findfile_init (
void *ptr;
helper = walker;
ptr = realloc(search_ctx->ffsc_stopdirs_v,
ptr = xrealloc(search_ctx->ffsc_stopdirs_v,
(dircount + 1) * sizeof(char_u *));
if (ptr)
search_ctx->ffsc_stopdirs_v = ptr;

View File

@ -74,7 +74,7 @@ int ga_grow(garray_T *gap, int n)
new_len = gap->ga_itemsize * (gap->ga_len + n);
pp = (gap->ga_data == NULL)
? alloc((unsigned)new_len)
: realloc(gap->ga_data, new_len);
: xrealloc(gap->ga_data, new_len);
if (pp == NULL) {
return FAIL;

View File

@ -1331,7 +1331,7 @@ static int cs_insert_filelist(char *fname, char *ppath, char *flags, struct stat
} else {
/* Reallocate space for more connections. */
csinfo_size *= 2;
csinfo = realloc(csinfo, sizeof(csinfo_T)*csinfo_size);
csinfo = xrealloc(csinfo, sizeof(csinfo_T)*csinfo_size);
}
if (csinfo == NULL)
return -1;
@ -1871,7 +1871,7 @@ static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
/* hopefully 'num' (num of matches) will be less than 10^16 */
newsize = (int)(strlen(csfmt_str) + 16 + strlen(lno));
if (bufsize < newsize) {
buf = (char *)realloc(buf, newsize);
buf = (char *)xrealloc(buf, newsize);
if (buf == NULL)
bufsize = 0;
else
@ -1892,7 +1892,7 @@ static void cs_print_tags_priv(char **matches, char **cntxts, int num_matches)
newsize = (int)(strlen(context) + strlen(cntxformat));
if (bufsize < newsize) {
buf = (char *)realloc(buf, newsize);
buf = (char *)xrealloc(buf, newsize);
if (buf == NULL)
bufsize = 0;
else

View File

@ -4372,7 +4372,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks) {
buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2;
buf->b_ml.ml_chunksize = (chunksize_T *)
realloc(buf->b_ml.ml_chunksize,
xrealloc(buf->b_ml.ml_chunksize,
sizeof(chunksize_T) * buf->b_ml.ml_numchunks);
if (buf->b_ml.ml_chunksize == NULL) {
/* Hmmmm, Give up on offset for this buffer */

View File

@ -2418,7 +2418,7 @@ int get_keystroke(void)
/* Need some more space. This might happen when receiving a long
* escape sequence. */
buflen += 100;
buf = realloc(buf, buflen);
buf = xrealloc(buf, buflen);
maxlen = (buflen - 6 - len) / 3;
}
if (buf == NULL) {

View File

@ -696,6 +696,32 @@ void *xmalloc(size_t size)
return ret;
}
/// realloc() wrapper
///
/// @see {xmalloc}
/// @param size
/// @return pointer to reallocated space. Never NULL
void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
if (!ret && !size)
ret = realloc(ptr, 1);
if (!ret) {
try_to_free_memory();
ret = realloc(ptr, size);
if (!ret && !size)
ret = realloc(ptr, 1);
if (!ret) {
OUT_STR("Vim: Error: Out of memory.\n");
preserve_exit();
}
}
return ret;
}
/// Old low level memory allocation function.
///
/// @deprecated use xmalloc() directly instead

View File

@ -29,6 +29,8 @@ 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);
void try_to_free_memory();
void *xmalloc(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
void *xrealloc(void *ptr, size_t size)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2);
char_u *lalloc(long_u size, int message) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1);
void do_outofmem_msg(long_u size);
void free_all_mem(void);

View File

@ -4496,7 +4496,7 @@ static void nv_ident(cmdarg_T *cap)
vim_free(buf);
return;
}
newbuf = (char_u *)realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
newbuf = (char_u *)xrealloc(buf, STRLEN(buf) + STRLEN(p) + 1);
if (newbuf == NULL) {
vim_free(buf);
vim_free(p);

View File

@ -3963,7 +3963,7 @@ skip_add:
subs = &temp_subs;
}
l->t = realloc(l->t, newlen * sizeof(nfa_thread_T));
l->t = xrealloc(l->t, newlen * sizeof(nfa_thread_T));
l->len = newlen;
}