aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Oliveira Carvalho <felipekde@gmail.com>2014-03-29 01:05:04 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-03-31 07:31:47 -0300
commit0e998066b2533de3cf87ece1068839e60e128f1a (patch)
tree0dc09cd25ef39a59e6f9c1c41493e11bacea8f79
parent7bdd1f1898c6c94326e4642d8f51f74cc14b5f9d (diff)
downloadrneovim-0e998066b2533de3cf87ece1068839e60e128f1a.tar.gz
rneovim-0e998066b2533de3cf87ece1068839e60e128f1a.tar.bz2
rneovim-0e998066b2533de3cf87ece1068839e60e128f1a.zip
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.
-rw-r--r--src/eval.c4
-rw-r--r--src/file_search.c2
-rw-r--r--src/garray.c2
-rw-r--r--src/if_cscope.c6
-rw-r--r--src/memline.c2
-rw-r--r--src/misc1.c2
-rw-r--r--src/misc2.c26
-rw-r--r--src/misc2.h2
-rw-r--r--src/normal.c2
-rw-r--r--src/regexp_nfa.c2
10 files changed, 39 insertions, 11 deletions
diff --git a/src/eval.c b/src/eval.c
index bf208b3fde..847082aebf 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -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;
diff --git a/src/file_search.c b/src/file_search.c
index 718e0b024e..00b32d669f 100644
--- a/src/file_search.c
+++ b/src/file_search.c
@@ -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;
diff --git a/src/garray.c b/src/garray.c
index 8bf2b7477e..264001f934 100644
--- a/src/garray.c
+++ b/src/garray.c
@@ -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;
diff --git a/src/if_cscope.c b/src/if_cscope.c
index 45ae82cb81..884fb002ff 100644
--- a/src/if_cscope.c
+++ b/src/if_cscope.c
@@ -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
diff --git a/src/memline.c b/src/memline.c
index cce0882870..6b948f606f 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -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 */
diff --git a/src/misc1.c b/src/misc1.c
index a96755919d..5e80e129d7 100644
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -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) {
diff --git a/src/misc2.c b/src/misc2.c
index 143a106c40..3218de2156 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -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
diff --git a/src/misc2.h b/src/misc2.h
index 0e52993412..ddf71b3ac1 100644
--- a/src/misc2.h
+++ b/src/misc2.h
@@ -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);
diff --git a/src/normal.c b/src/normal.c
index 7610520780..429dbea7b0 100644
--- a/src/normal.c
+++ b/src/normal.c
@@ -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);
diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c
index 74e2f61e76..bdcfa1890d 100644
--- a/src/regexp_nfa.c
+++ b/src/regexp_nfa.c
@@ -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;
}