aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Schmidt <john.schmidt.h@gmail.com>2014-04-22 11:35:11 +0200
committerThiago de Arruda <tpadilha84@gmail.com>2014-04-28 07:41:45 -0300
commit4e1b364a3e255742522d4d76c5ccddd7a36c1769 (patch)
tree667a250396bc009ec56f3dad563882b9367b4441 /src
parentc70a526a5df2b8a7353c3c71e241cd51bf099a64 (diff)
downloadrneovim-4e1b364a3e255742522d4d76c5ccddd7a36c1769.tar.gz
rneovim-4e1b364a3e255742522d4d76c5ccddd7a36c1769.tar.bz2
rneovim-4e1b364a3e255742522d4d76c5ccddd7a36c1769.zip
Remove `alloc_clear`
Use `xcalloc` instead. Inline `alloc_tv` and `alloc_string_tv` in eval.c
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c13
-rw-r--r--src/edit.c8
-rw-r--r--src/eval.c106
-rw-r--r--src/ex_cmds.c4
-rw-r--r--src/fileio.c5
-rw-r--r--src/if_cscope.c2
-rw-r--r--src/memory.c8
-rw-r--r--src/memory.h1
-rw-r--r--src/menu.c2
-rw-r--r--src/path.c2
-rw-r--r--src/quickfix.c2
-rw-r--r--src/regexp.c4
-rw-r--r--src/spell.c13
-rw-r--r--src/syntax.c5
-rw-r--r--src/undo.c5
-rw-r--r--src/window.c6
16 files changed, 59 insertions, 127 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 3af7df19f1..dfa7ecb6ee 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1378,14 +1378,9 @@ buflist_new (
}
}
if (buf != curbuf || curbuf == NULL) {
- buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
+ buf = xcalloc(1, sizeof(buf_T));
/* init b: variables */
buf->b_vars = dict_alloc();
- if (buf->b_vars == NULL) {
- vim_free(ffname);
- vim_free(buf);
- return NULL;
- }
init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
}
@@ -1395,7 +1390,7 @@ buflist_new (
}
clear_wininfo(buf);
- buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
+ buf->b_wininfo = xcalloc(1, sizeof(wininfo_T));
if (ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) {
vim_free(buf->b_ffname);
@@ -2004,7 +1999,7 @@ static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col,
break;
if (wip == NULL) {
/* allocate a new entry */
- wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
+ wip = xcalloc(1, sizeof(wininfo_T));
wip->wi_win = win;
if (lnum == 0) /* set lnum even when it's 0 */
lnum = 1;
@@ -3699,7 +3694,7 @@ do_arg_all (
setpcmark();
opened_len = ARGCOUNT;
- opened = alloc_clear((unsigned)opened_len);
+ opened = xcalloc(opened_len, 1);
/* Autocommands may do anything to the argument list. Make sure it's not
* freed while we are working here by "locking" it. We still have to
diff --git a/src/edit.c b/src/edit.c
index 31c794dba3..53bb8815ec 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -2125,7 +2125,7 @@ ins_compl_add (
* Allocate a new match structure.
* Copy the values to the new match structure.
*/
- match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
+ match = xcalloc(1, sizeof(compl_T));
match->cp_number = -1;
if (flags & ORIGINAL_TEXT)
match->cp_number = 0;
@@ -2460,9 +2460,9 @@ void ins_compl_show_pum(void)
} while (compl != NULL && compl != compl_first_match);
if (compl_match_arraysize == 0)
return;
- compl_match_array = (pumitem_T *)alloc_clear(
- (unsigned)(sizeof(pumitem_T)
- * compl_match_arraysize));
+
+ assert(compl_match_arraysize >= 0);
+ compl_match_array = xcalloc(compl_match_arraysize, sizeof(pumitem_T));
/* If the current match is the original text don't find the first
* match after it, don't highlight anything. */
if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
diff --git a/src/eval.c b/src/eval.c
index 0fdabdac6c..ea7ecc4c76 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -799,8 +799,6 @@ static int get_var_tv(char_u *name, int len, typval_T *rettv,
int no_autoload);
static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate,
int verbose);
-static typval_T *alloc_tv(void);
-static typval_T *alloc_string_tv(char_u *string);
static void init_tv(typval_T *varp);
static long get_tv_number(typval_T *varp);
static linenr_T get_tv_lnum(typval_T *argvars);
@@ -1005,17 +1003,13 @@ int current_func_returned(void)
*/
void set_internal_string_var(char_u *name, char_u *value)
{
- char_u *val;
- typval_T *tvp;
+ char_u *val = vim_strsave(value);
+ typval_T *tvp = xcalloc(1, sizeof(typval_T));
- val = vim_strsave(value);
- if (val != NULL) {
- tvp = alloc_string_tv(val);
- if (tvp != NULL) {
- set_var(name, tvp, FALSE);
- free_tv(tvp);
- }
- }
+ tvp->v_type = VAR_STRING;
+ tvp->vval.v_string = val;
+ set_var(name, tvp, FALSE);
+ free_tv(tvp);
}
static lval_T *redir_lval = NULL;
@@ -1048,11 +1042,7 @@ var_redir_start (
if (redir_varname == NULL)
return FAIL;
- redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
- if (redir_lval == NULL) {
- var_redir_stop();
- return FAIL;
- }
+ redir_lval = xcalloc(1, sizeof(lval_T));
/* The output is stored in growarray "redir_ga" until redirection ends. */
ga_init(&redir_ga, (int)sizeof(char), 500);
@@ -2871,17 +2861,13 @@ static void list_fix_watch(list_T *l, listitem_T *item)
*/
void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip)
{
- forinfo_T *fi;
+ forinfo_T *fi = xcalloc(1, sizeof(forinfo_T));
char_u *expr;
typval_T tv;
list_T *l;
*errp = TRUE; /* default: there is an error */
- fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
- if (fi == NULL)
- return NULL;
-
expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
if (expr == NULL)
return fi;
@@ -5143,18 +5129,15 @@ failret:
*/
list_T *list_alloc(void)
{
- list_T *l;
+ list_T *list = xcalloc(1, sizeof(list_T));
- l = (list_T *)alloc_clear(sizeof(list_T));
- if (l != NULL) {
- /* Prepend the list to the list of lists for garbage collection. */
- if (first_list != NULL)
- first_list->lv_used_prev = l;
- l->lv_used_prev = NULL;
- l->lv_used_next = first_list;
- first_list = l;
- }
- return l;
+ /* Prepend the list to the list of lists for garbage collection. */
+ if (first_list != NULL)
+ first_list->lv_used_prev = list;
+ list->lv_used_prev = NULL;
+ list->lv_used_next = first_list;
+ first_list = list;
+ return list;
}
/*
@@ -16203,33 +16186,6 @@ handle_subscript (
}
/*
- * Allocate memory for a variable type-value, and make it empty (0 or NULL
- * value).
- */
-static typval_T *alloc_tv(void)
-{
- return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
-}
-
-/*
- * Allocate memory for a variable type-value, and assign a string to it.
- * The string "s" must have been allocated, it is consumed.
- * Return NULL for out of memory, the variable otherwise.
- */
-static typval_T *alloc_string_tv(char_u *s)
-{
- typval_T *rettv;
-
- rettv = alloc_tv();
- if (rettv != NULL) {
- rettv->v_type = VAR_STRING;
- rettv->vval.v_string = s;
- } else
- vim_free(s);
- return rettv;
-}
-
-/*
* Free the memory for a variable type-value.
*/
void free_tv(typval_T *varp)
@@ -16603,8 +16559,7 @@ void new_script_vars(scid_T id)
}
while (ga_scripts.ga_len < id) {
- sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
- (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
+ sv = SCRIPT_SV(ga_scripts.ga_len + 1) = xcalloc(1, sizeof(scriptvar_T));
init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
++ga_scripts.ga_len;
}
@@ -18195,14 +18150,19 @@ static void func_do_profile(ufunc_T *fp)
fp->uf_tm_count = 0;
profile_zero(&fp->uf_tm_self);
profile_zero(&fp->uf_tm_total);
- if (fp->uf_tml_count == NULL)
- fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
- if (fp->uf_tml_total == NULL)
- fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
- (sizeof(proftime_T) * len));
- if (fp->uf_tml_self == NULL)
- fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
- (sizeof(proftime_T) * len));
+
+ if (fp->uf_tml_count == NULL) {
+ fp->uf_tml_count = xcalloc(len, sizeof(int));
+ }
+
+ if (fp->uf_tml_total == NULL) {
+ fp->uf_tml_total = xcalloc(len, sizeof(proftime_T));
+ }
+
+ if (fp->uf_tml_self == NULL) {
+ fp->uf_tml_self = xcalloc(len, sizeof(proftime_T));
+ }
+
fp->uf_tml_idx = -1;
if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
|| fp->uf_tml_self == NULL)
@@ -19048,10 +19008,8 @@ int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv)
if (rettv != NULL) {
/* Store the value of the pending return. */
- if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
- *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
- else
- EMSG(_(e_outofmem));
+ cstack->cs_rettv[idx] = xcalloc(1, sizeof(typval_T));
+ *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
} else
cstack->cs_rettv[idx] = NULL;
diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index 791ddd04aa..b4cbd87d8f 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -5829,9 +5829,7 @@ void ex_sign(exarg_T *eap)
int start = next_sign_typenr;
/* Allocate a new sign. */
- sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T));
- if (sp == NULL)
- return;
+ sp = xcalloc(1, sizeof(sign_T));
/* Check that next_sign_typenr is not already being used.
* This only happens after wrapping around. Hopefully
diff --git a/src/fileio.c b/src/fileio.c
index aa0368b9ba..3ea06b2aef 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2361,14 +2361,13 @@ int prepare_crypt_read(FILE *fp)
*/
char_u *prepare_crypt_write(buf_T *buf, int *lenp)
{
- char_u *header;
+ char_u *header = xcalloc(1, CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
+ + CRYPT_SEED_LEN_MAX + 2);
int seed_len = crypt_seed_len[get_crypt_method(buf)];
int salt_len = crypt_salt_len[get_crypt_method(buf)];
char_u *salt;
char_u *seed;
- header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
- + CRYPT_SEED_LEN_MAX + 2);
crypt_push_state();
use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */
vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
diff --git a/src/if_cscope.c b/src/if_cscope.c
index de2f3002b4..2f283774fc 100644
--- a/src/if_cscope.c
+++ b/src/if_cscope.c
@@ -1286,7 +1286,7 @@ static int cs_insert_filelist(char *fname, char *ppath, char *flags, struct stat
* be enough for most users. If more is needed, csinfo will be
* reallocated. */
csinfo_size = 1;
- csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T));
+ csinfo = xcalloc(1, sizeof(csinfo_T));
} else {
/* Reallocate space for more connections. */
csinfo_size *= 2;
diff --git a/src/memory.c b/src/memory.c
index 4411c1526f..dff7e41bad 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -53,14 +53,6 @@ char_u *alloc(unsigned size)
return lalloc((long_u)size, TRUE);
}
-/*
- * Allocate memory and set all bytes to zero.
- */
-char_u *alloc_clear(unsigned size)
-{
- return (char_u *)xcalloc(1, (size_t)size);
-}
-
/// Try to free memory. Used when trying to recover from out of memory errors.
/// @see {xmalloc}
static void try_to_free_memory()
diff --git a/src/memory.h b/src/memory.h
index bc20c0420e..29ec4cdd0e 100644
--- a/src/memory.h
+++ b/src/memory.h
@@ -6,7 +6,6 @@
#include "vim.h"
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);
/// malloc() wrapper
///
diff --git a/src/menu.c b/src/menu.c
index f132e6dc2f..01f7f4f9d3 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -373,7 +373,7 @@ add_menu_path (
}
/* Not already there, so lets add it */
- menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
+ menu = xcalloc(1, sizeof(vimmenu_T));
menu->modes = modes;
menu->enabled = MENU_ALL_MODES;
diff --git a/src/path.c b/src/path.c
index 4914c681da..77348fecff 100644
--- a/src/path.c
+++ b/src/path.c
@@ -754,7 +754,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
os_dirname(curdir, MAXPATHL);
expand_path_option(curdir, &path_ga);
- in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
+ in_curdir = xcalloc(gap->ga_len, sizeof(char_u *));
for (i = 0; i < gap->ga_len && !got_int; i++) {
char_u *path = fnames[i];
diff --git a/src/quickfix.c b/src/quickfix.c
index 75c80ed2b0..090fceb975 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -326,7 +326,7 @@ qf_init_ext (
/*
* Allocate a new eformat structure and put it at the end of the list
*/
- fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
+ fmt_ptr = xcalloc(1, sizeof(efm_T));
if (fmt_first == NULL) /* first one */
fmt_first = fmt_ptr;
else
diff --git a/src/regexp.c b/src/regexp.c
index e5f431d3bd..454fdbe01e 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -3586,9 +3586,7 @@ static reg_extmatch_T *make_extmatch(void);
*/
static reg_extmatch_T *make_extmatch(void)
{
- reg_extmatch_T *em;
-
- em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
+ reg_extmatch_T *em = xcalloc(1, sizeof(reg_extmatch_T));
em->refcnt = 1;
return em;
}
diff --git a/src/spell.c b/src/spell.c
index 0f3cb41413..d0d5e1ca8f 100644
--- a/src/spell.c
+++ b/src/spell.c
@@ -2853,8 +2853,7 @@ static int read_prefcond_section(FILE *fd, slang_T *lp)
if (cnt <= 0)
return SP_FORMERROR;
- lp->sl_prefprog = (regprog_T **)alloc_clear(
- (unsigned)sizeof(regprog_T *) * cnt);
+ lp->sl_prefprog = xcalloc(cnt, sizeof(regprog_T *));
lp->sl_prefixcnt = cnt;
for (i = 0; i < cnt; ++i) {
@@ -6523,15 +6522,7 @@ getroom (
bl = NULL;
else
/* Allocate a block of memory. It is not freed until much later. */
- bl = (sblock_T *)alloc_clear(
- (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
- if (bl == NULL) {
- if (!spin->si_did_emsg) {
- EMSG(_("E845: Insufficient memory, word list will be incomplete"));
- spin->si_did_emsg = TRUE;
- }
- return NULL;
- }
+ bl = xcalloc(1, (sizeof(sblock_T) + SBLOCKSIZE));
bl->sb_next = spin->si_blocks;
spin->si_blocks = bl;
bl->sb_used = 0;
diff --git a/src/syntax.c b/src/syntax.c
index f3b8e10291..5105a8c161 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -1123,7 +1123,8 @@ static void syn_stack_alloc(void)
len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2;
}
- sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
+ assert(len >= 0);
+ sstp = xcalloc(len, sizeof(synstate_T));
to = sstp - 1;
if (syn_block->b_sst_array != NULL) {
@@ -4553,7 +4554,7 @@ syn_cmd_region (
ppp = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
ppp->pp_next = pat_ptrs[item];
pat_ptrs[item] = ppp;
- ppp->pp_synp = (synpat_T *)alloc_clear((unsigned)sizeof(synpat_T));
+ ppp->pp_synp = xcalloc(1, sizeof(synpat_T));
/*
* Get the syntax pattern and the following offset(s).
diff --git a/src/undo.c b/src/undo.c
index ee50fa90d9..243d865513 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -1509,8 +1509,9 @@ void u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
}
#ifdef U_DEBUG
- uhp_table_used = (int *)alloc_clear(
- (unsigned)(sizeof(int) * num_head + 1));
+ size_t amount = num_head * sizeof(int) + 1;
+ uhp_table_used = xmalloc(amount);
+ memset(uhp_table_used, 0, amount);
# define SET_FLAG(j) ++ uhp_table_used[j]
#else
# define SET_FLAG(j)
diff --git a/src/window.c b/src/window.c
index d47ed22720..305bf6b7c9 100644
--- a/src/window.c
+++ b/src/window.c
@@ -784,7 +784,7 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir)
}
if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout) {
/* Need to create a new frame in the tree to make a branch. */
- frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
+ frp = xcalloc(1, sizeof(frame_T));
*frp = *curfrp;
curfrp->fr_layout = layout;
frp->fr_parent = curfrp;
@@ -2847,7 +2847,6 @@ static tabpage_T *alloc_tabpage(void)
/* init t: variables */
tp->tp_vars = dict_alloc();
init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
-
tp->tp_diff_invalid = TRUE;
tp->tp_ch_used = p_ch;
@@ -3766,6 +3765,7 @@ static void frame_remove(frame_T *frp)
void win_alloc_lines(win_T *wp)
{
wp->w_lines_valid = 0;
+ assert(Rows >= 0);
wp->w_lines = xcalloc(Rows, sizeof(wline_T));
}
@@ -5019,7 +5019,7 @@ void make_snapshot(int idx)
static void make_snapshot_rec(frame_T *fr, frame_T **frp)
{
- *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
+ *frp = xcalloc(1, sizeof(frame_T));
(*frp)->fr_layout = fr->fr_layout;
(*frp)->fr_width = fr->fr_width;
(*frp)->fr_height = fr->fr_height;