Remove alloc_clear

Use `xcalloc` instead.
Inline `alloc_tv` and `alloc_string_tv` in eval.c
This commit is contained in:
John Schmidt 2014-04-22 11:35:11 +02:00 committed by Thiago de Arruda
parent c70a526a5d
commit 4e1b364a3e
16 changed files with 59 additions and 127 deletions

View File

@ -1378,14 +1378,9 @@ buflist_new (
} }
} }
if (buf != curbuf || curbuf == NULL) { if (buf != curbuf || curbuf == NULL) {
buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T)); buf = xcalloc(1, sizeof(buf_T));
/* init b: variables */ /* init b: variables */
buf->b_vars = dict_alloc(); 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); init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
} }
@ -1395,7 +1390,7 @@ buflist_new (
} }
clear_wininfo(buf); 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)) { if (ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) {
vim_free(buf->b_ffname); 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; break;
if (wip == NULL) { if (wip == NULL) {
/* allocate a new entry */ /* allocate a new entry */
wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); wip = xcalloc(1, sizeof(wininfo_T));
wip->wi_win = win; wip->wi_win = win;
if (lnum == 0) /* set lnum even when it's 0 */ if (lnum == 0) /* set lnum even when it's 0 */
lnum = 1; lnum = 1;
@ -3699,7 +3694,7 @@ do_arg_all (
setpcmark(); setpcmark();
opened_len = ARGCOUNT; 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 /* 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 * freed while we are working here by "locking" it. We still have to

View File

@ -2125,7 +2125,7 @@ ins_compl_add (
* Allocate a new match structure. * Allocate a new match structure.
* Copy the values to the 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; match->cp_number = -1;
if (flags & ORIGINAL_TEXT) if (flags & ORIGINAL_TEXT)
match->cp_number = 0; match->cp_number = 0;
@ -2460,9 +2460,9 @@ void ins_compl_show_pum(void)
} while (compl != NULL && compl != compl_first_match); } while (compl != NULL && compl != compl_first_match);
if (compl_match_arraysize == 0) if (compl_match_arraysize == 0)
return; return;
compl_match_array = (pumitem_T *)alloc_clear(
(unsigned)(sizeof(pumitem_T) assert(compl_match_arraysize >= 0);
* compl_match_arraysize)); compl_match_array = xcalloc(compl_match_arraysize, sizeof(pumitem_T));
/* If the current match is the original text don't find the first /* If the current match is the original text don't find the first
* match after it, don't highlight anything. */ * match after it, don't highlight anything. */
if (compl_shown_match->cp_flags & ORIGINAL_TEXT) if (compl_shown_match->cp_flags & ORIGINAL_TEXT)

View File

@ -799,8 +799,6 @@ static int get_var_tv(char_u *name, int len, typval_T *rettv,
int no_autoload); int no_autoload);
static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate,
int verbose); 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 void init_tv(typval_T *varp);
static long get_tv_number(typval_T *varp); static long get_tv_number(typval_T *varp);
static linenr_T get_tv_lnum(typval_T *argvars); 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) void set_internal_string_var(char_u *name, char_u *value)
{ {
char_u *val; char_u *val = vim_strsave(value);
typval_T *tvp; typval_T *tvp = xcalloc(1, sizeof(typval_T));
val = vim_strsave(value); tvp->v_type = VAR_STRING;
if (val != NULL) { tvp->vval.v_string = val;
tvp = alloc_string_tv(val); set_var(name, tvp, FALSE);
if (tvp != NULL) { free_tv(tvp);
set_var(name, tvp, FALSE);
free_tv(tvp);
}
}
} }
static lval_T *redir_lval = NULL; static lval_T *redir_lval = NULL;
@ -1048,11 +1042,7 @@ var_redir_start (
if (redir_varname == NULL) if (redir_varname == NULL)
return FAIL; return FAIL;
redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T)); redir_lval = xcalloc(1, sizeof(lval_T));
if (redir_lval == NULL) {
var_redir_stop();
return FAIL;
}
/* The output is stored in growarray "redir_ga" until redirection ends. */ /* The output is stored in growarray "redir_ga" until redirection ends. */
ga_init(&redir_ga, (int)sizeof(char), 500); 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) 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; char_u *expr;
typval_T tv; typval_T tv;
list_T *l; list_T *l;
*errp = TRUE; /* default: there is an error */ *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); expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
if (expr == NULL) if (expr == NULL)
return fi; return fi;
@ -5143,18 +5129,15 @@ failret:
*/ */
list_T *list_alloc(void) list_T *list_alloc(void)
{ {
list_T *l; list_T *list = xcalloc(1, sizeof(list_T));
l = (list_T *)alloc_clear(sizeof(list_T)); /* Prepend the list to the list of lists for garbage collection. */
if (l != NULL) { if (first_list != NULL)
/* Prepend the list to the list of lists for garbage collection. */ first_list->lv_used_prev = list;
if (first_list != NULL) list->lv_used_prev = NULL;
first_list->lv_used_prev = l; list->lv_used_next = first_list;
l->lv_used_prev = NULL; first_list = list;
l->lv_used_next = first_list; return list;
first_list = l;
}
return l;
} }
/* /*
@ -16202,33 +16185,6 @@ handle_subscript (
return ret; return ret;
} }
/*
* 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. * Free the memory for a variable type-value.
*/ */
@ -16603,8 +16559,7 @@ void new_script_vars(scid_T id)
} }
while (ga_scripts.ga_len < id) { while (ga_scripts.ga_len < id) {
sv = SCRIPT_SV(ga_scripts.ga_len + 1) = sv = SCRIPT_SV(ga_scripts.ga_len + 1) = xcalloc(1, sizeof(scriptvar_T));
(scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE); init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
++ga_scripts.ga_len; ++ga_scripts.ga_len;
} }
@ -18195,14 +18150,19 @@ static void func_do_profile(ufunc_T *fp)
fp->uf_tm_count = 0; fp->uf_tm_count = 0;
profile_zero(&fp->uf_tm_self); profile_zero(&fp->uf_tm_self);
profile_zero(&fp->uf_tm_total); 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_count == NULL) {
if (fp->uf_tml_total == NULL) fp->uf_tml_count = xcalloc(len, sizeof(int));
fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned) }
(sizeof(proftime_T) * len));
if (fp->uf_tml_self == NULL) if (fp->uf_tml_total == NULL) {
fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned) fp->uf_tml_total = xcalloc(len, sizeof(proftime_T));
(sizeof(proftime_T) * len)); }
if (fp->uf_tml_self == NULL) {
fp->uf_tml_self = xcalloc(len, sizeof(proftime_T));
}
fp->uf_tml_idx = -1; fp->uf_tml_idx = -1;
if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
|| fp->uf_tml_self == 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) { if (rettv != NULL) {
/* Store the value of the pending return. */ /* Store the value of the pending return. */
if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL) cstack->cs_rettv[idx] = xcalloc(1, sizeof(typval_T));
*(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv; *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
else
EMSG(_(e_outofmem));
} else } else
cstack->cs_rettv[idx] = NULL; cstack->cs_rettv[idx] = NULL;

View File

@ -5829,9 +5829,7 @@ void ex_sign(exarg_T *eap)
int start = next_sign_typenr; int start = next_sign_typenr;
/* Allocate a new sign. */ /* Allocate a new sign. */
sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); sp = xcalloc(1, sizeof(sign_T));
if (sp == NULL)
return;
/* Check that next_sign_typenr is not already being used. /* Check that next_sign_typenr is not already being used.
* This only happens after wrapping around. Hopefully * This only happens after wrapping around. Hopefully

View File

@ -2361,14 +2361,13 @@ int prepare_crypt_read(FILE *fp)
*/ */
char_u *prepare_crypt_write(buf_T *buf, int *lenp) 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 seed_len = crypt_seed_len[get_crypt_method(buf)];
int salt_len = crypt_salt_len[get_crypt_method(buf)]; int salt_len = crypt_salt_len[get_crypt_method(buf)];
char_u *salt; char_u *salt;
char_u *seed; char_u *seed;
header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
+ CRYPT_SEED_LEN_MAX + 2);
crypt_push_state(); crypt_push_state();
use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */ use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */
vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method], vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],

View File

@ -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 * be enough for most users. If more is needed, csinfo will be
* reallocated. */ * reallocated. */
csinfo_size = 1; csinfo_size = 1;
csinfo = (csinfo_T *)alloc_clear(sizeof(csinfo_T)); csinfo = xcalloc(1, sizeof(csinfo_T));
} else { } else {
/* Reallocate space for more connections. */ /* Reallocate space for more connections. */
csinfo_size *= 2; csinfo_size *= 2;

View File

@ -53,14 +53,6 @@ char_u *alloc(unsigned size)
return lalloc((long_u)size, TRUE); 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. /// Try to free memory. Used when trying to recover from out of memory errors.
/// @see {xmalloc} /// @see {xmalloc}
static void try_to_free_memory() static void try_to_free_memory()

View File

@ -6,7 +6,6 @@
#include "vim.h" #include "vim.h"
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);
/// malloc() wrapper /// malloc() wrapper
/// ///

View File

@ -373,7 +373,7 @@ add_menu_path (
} }
/* Not already there, so lets add it */ /* 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->modes = modes;
menu->enabled = MENU_ALL_MODES; menu->enabled = MENU_ALL_MODES;

View File

@ -754,7 +754,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
os_dirname(curdir, MAXPATHL); os_dirname(curdir, MAXPATHL);
expand_path_option(curdir, &path_ga); 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++) { for (i = 0; i < gap->ga_len && !got_int; i++) {
char_u *path = fnames[i]; char_u *path = fnames[i];

View File

@ -326,7 +326,7 @@ qf_init_ext (
/* /*
* Allocate a new eformat structure and put it at the end of the list * 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 */ if (fmt_first == NULL) /* first one */
fmt_first = fmt_ptr; fmt_first = fmt_ptr;
else else

View File

@ -3586,9 +3586,7 @@ static reg_extmatch_T *make_extmatch(void);
*/ */
static reg_extmatch_T *make_extmatch(void) static reg_extmatch_T *make_extmatch(void)
{ {
reg_extmatch_T *em; reg_extmatch_T *em = xcalloc(1, sizeof(reg_extmatch_T));
em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
em->refcnt = 1; em->refcnt = 1;
return em; return em;
} }

View File

@ -2853,8 +2853,7 @@ static int read_prefcond_section(FILE *fd, slang_T *lp)
if (cnt <= 0) if (cnt <= 0)
return SP_FORMERROR; return SP_FORMERROR;
lp->sl_prefprog = (regprog_T **)alloc_clear( lp->sl_prefprog = xcalloc(cnt, sizeof(regprog_T *));
(unsigned)sizeof(regprog_T *) * cnt);
lp->sl_prefixcnt = cnt; lp->sl_prefixcnt = cnt;
for (i = 0; i < cnt; ++i) { for (i = 0; i < cnt; ++i) {
@ -6523,15 +6522,7 @@ getroom (
bl = NULL; bl = NULL;
else else
/* Allocate a block of memory. It is not freed until much later. */ /* Allocate a block of memory. It is not freed until much later. */
bl = (sblock_T *)alloc_clear( bl = xcalloc(1, (sizeof(sblock_T) + SBLOCKSIZE));
(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->sb_next = spin->si_blocks; bl->sb_next = spin->si_blocks;
spin->si_blocks = bl; spin->si_blocks = bl;
bl->sb_used = 0; bl->sb_used = 0;

View File

@ -1123,7 +1123,8 @@ static void syn_stack_alloc(void)
len = syn_block->b_sst_len - syn_block->b_sst_freecount + 2; 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; to = sstp - 1;
if (syn_block->b_sst_array != NULL) { 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 = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
ppp->pp_next = pat_ptrs[item]; ppp->pp_next = pat_ptrs[item];
pat_ptrs[item] = ppp; 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). * Get the syntax pattern and the following offset(s).

View File

@ -1509,8 +1509,9 @@ void u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
} }
#ifdef U_DEBUG #ifdef U_DEBUG
uhp_table_used = (int *)alloc_clear( size_t amount = num_head * sizeof(int) + 1;
(unsigned)(sizeof(int) * num_head + 1)); uhp_table_used = xmalloc(amount);
memset(uhp_table_used, 0, amount);
# define SET_FLAG(j) ++ uhp_table_used[j] # define SET_FLAG(j) ++ uhp_table_used[j]
#else #else
# define SET_FLAG(j) # define SET_FLAG(j)

View File

@ -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) { if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout) {
/* Need to create a new frame in the tree to make a branch. */ /* 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; *frp = *curfrp;
curfrp->fr_layout = layout; curfrp->fr_layout = layout;
frp->fr_parent = curfrp; frp->fr_parent = curfrp;
@ -2847,7 +2847,6 @@ static tabpage_T *alloc_tabpage(void)
/* init t: variables */ /* init t: variables */
tp->tp_vars = dict_alloc(); tp->tp_vars = dict_alloc();
init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE); init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
tp->tp_diff_invalid = TRUE; tp->tp_diff_invalid = TRUE;
tp->tp_ch_used = p_ch; tp->tp_ch_used = p_ch;
@ -3766,6 +3765,7 @@ static void frame_remove(frame_T *frp)
void win_alloc_lines(win_T *wp) void win_alloc_lines(win_T *wp)
{ {
wp->w_lines_valid = 0; wp->w_lines_valid = 0;
assert(Rows >= 0);
wp->w_lines = xcalloc(Rows, sizeof(wline_T)); 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) 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_layout = fr->fr_layout;
(*frp)->fr_width = fr->fr_width; (*frp)->fr_width = fr->fr_width;
(*frp)->fr_height = fr->fr_height; (*frp)->fr_height = fr->fr_height;