Introduce ga_append_via_ptr() and GA_APPEND_VIA_PTR()

Similar to GA_APPEND(). Replaces this pattern:

    ga_grow(&ga, 1);
    item_type *p = ((item_type *)ga.ga_data) + ga.ga_len;
    p->field1 = v1;
    p->field2 = v2;
    ga.ga_len++;
This commit is contained in:
Felipe Oliveira Carvalho
2014-06-11 03:01:46 -03:00
committed by Justin M. Keyes
parent 45e7814e6a
commit 5ed74cfb7c
8 changed files with 82 additions and 111 deletions

View File

@@ -1619,12 +1619,10 @@ void putdigraph(char_u *str)
// Add a new digraph to the table. // Add a new digraph to the table.
if (i == user_digraphs.ga_len) { if (i == user_digraphs.ga_len) {
ga_grow(&user_digraphs, 1); dp = GA_APPEND_VIA_PTR(digr_T, &user_digraphs);
dp = (digr_T *)user_digraphs.ga_data + user_digraphs.ga_len;
dp->char1 = char1; dp->char1 = char1;
dp->char2 = char2; dp->char2 = char2;
dp->result = n; dp->result = n;
++user_digraphs.ga_len;
} }
} }
} }
@@ -1772,7 +1770,6 @@ void ex_loadkeymap(exarg_T *eap)
char_u *line; char_u *line;
char_u *p; char_u *p;
char_u *s; char_u *s;
kmap_T *kp;
#define KMAP_LLEN 200 // max length of "to" and "from" together #define KMAP_LLEN 200 // max length of "to" and "from" together
char_u buf[KMAP_LLEN + 11]; char_u buf[KMAP_LLEN + 11];
@@ -1803,8 +1800,7 @@ void ex_loadkeymap(exarg_T *eap)
p = skipwhite(line); p = skipwhite(line);
if ((*p != '"') && (*p != NUL)) { if ((*p != '"') && (*p != NUL)) {
ga_grow(&curbuf->b_kmap_ga, 1); kmap_T *kp = GA_APPEND_VIA_PTR(kmap_T, &curbuf->b_kmap_ga);
kp = (kmap_T *)curbuf->b_kmap_ga.ga_data + curbuf->b_kmap_ga.ga_len;
s = skiptowhite(p); s = skiptowhite(p);
kp->from = vim_strnsave(p, (int)(s - p)); kp->from = vim_strnsave(p, (int)(s - p));
p = skipwhite(s); p = skipwhite(s);
@@ -1819,8 +1815,7 @@ void ex_loadkeymap(exarg_T *eap)
} }
free(kp->from); free(kp->from);
free(kp->to); free(kp->to);
} else { --curbuf->b_kmap_ga.ga_len;
++curbuf->b_kmap_ga.ga_len;
} }
} }
free(line); free(line);

View File

@@ -5290,8 +5290,7 @@ list_join_inner (
len = (int)STRLEN(s); len = (int)STRLEN(s);
sumlen += len; sumlen += len;
ga_grow(join_gap, 1); p = GA_APPEND_VIA_PTR(join_T, join_gap);
p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
if (tofree != NULL || s != numbuf) { if (tofree != NULL || s != numbuf) {
p->s = s; p->s = s;
p->tofree = tofree; p->tofree = tofree;
@@ -10218,11 +10217,9 @@ static void f_inputrestore(typval_T *argvars, typval_T *rettv)
*/ */
static void f_inputsave(typval_T *argvars, typval_T *rettv) static void f_inputsave(typval_T *argvars, typval_T *rettv)
{ {
/* Add an entry to the stack of typeahead storage. */ // Add an entry to the stack of typeahead storage.
ga_grow(&ga_userinput, 1); tasave_T *p = GA_APPEND_VIA_PTR(tasave_T, &ga_userinput);
save_typeahead((tasave_T *)(ga_userinput.ga_data) save_typeahead(p);
+ ga_userinput.ga_len);
++ga_userinput.ga_len;
} }
/* /*

View File

@@ -1037,10 +1037,9 @@ static char_u *get_loop_line(int c, void *cookie, int indent)
*/ */
static void store_loop_line(garray_T *gap, char_u *line) static void store_loop_line(garray_T *gap, char_u *line)
{ {
ga_grow(gap, 1); wcmd_T *p = GA_APPEND_VIA_PTR(wcmd_T, gap);
((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line); p->line = vim_strsave(line);
((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum; p->lnum = sourcing_lnum;
++gap->ga_len;
} }
/* /*

View File

@@ -1,6 +1,8 @@
#ifndef NVIM_GARRAY_H #ifndef NVIM_GARRAY_H
#define NVIM_GARRAY_H #define NVIM_GARRAY_H
#include "nvim/log.h"
/// Structure used for growing arrays. /// Structure used for growing arrays.
/// This is used to store information that only grows, is deleted all at /// This is used to store information that only grows, is deleted all at
/// once, and needs to be accessed by index. See ga_clear() and ga_grow(). /// once, and needs to be accessed by index. See ga_clear() and ga_grow().
@@ -22,7 +24,20 @@ typedef struct growarray {
((item_type *)(gap)->ga_data)[(gap)->ga_len++] = (item); \ ((item_type *)(gap)->ga_data)[(gap)->ga_len++] = (item); \
} while (0) } while (0)
#define GA_APPEND_VIA_PTR(item_type, gap) \
ga_append_via_ptr(gap, sizeof(item_type))
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "garray.h.generated.h" # include "garray.h.generated.h"
#endif #endif
static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size)
{
if ((int)item_size != gap->ga_itemsize) {
ELOG("wrong item size in garray(%d), should be %d", item_size);
}
ga_grow(gap, 1);
return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++);
}
#endif // NVIM_GARRAY_H #endif // NVIM_GARRAY_H

View File

@@ -1444,7 +1444,6 @@ static garray_T menutrans_ga = GA_EMPTY_INIT_VALUE;
void ex_menutranslate(exarg_T *eap) void ex_menutranslate(exarg_T *eap)
{ {
char_u *arg = eap->arg; char_u *arg = eap->arg;
menutrans_T *tp;
char_u *from, *from_noamp, *to; char_u *from, *from_noamp, *to;
if (menutrans_ga.ga_itemsize == 0) if (menutrans_ga.ga_itemsize == 0)
@@ -1454,7 +1453,7 @@ void ex_menutranslate(exarg_T *eap)
* ":menutrans clear": clear all translations. * ":menutrans clear": clear all translations.
*/ */
if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) { if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) {
tp = (menutrans_T *)menutrans_ga.ga_data; menutrans_T *tp = (menutrans_T *)menutrans_ga.ga_data;
for (int i = 0; i < menutrans_ga.ga_len; ++i) { for (int i = 0; i < menutrans_ga.ga_len; ++i) {
free(tp[i].from); free(tp[i].from);
free(tp[i].from_noamp); free(tp[i].from_noamp);
@@ -1473,8 +1472,6 @@ void ex_menutranslate(exarg_T *eap)
if (arg == to) if (arg == to)
EMSG(_(e_invarg)); EMSG(_(e_invarg));
else { else {
ga_grow(&menutrans_ga, 1);
tp = (menutrans_T *)menutrans_ga.ga_data;
from = vim_strsave(from); from = vim_strsave(from);
from_noamp = menu_text(from, NULL, NULL); from_noamp = menu_text(from, NULL, NULL);
to = vim_strnsave(to, (int)(arg - to)); to = vim_strnsave(to, (int)(arg - to));
@@ -1483,10 +1480,10 @@ void ex_menutranslate(exarg_T *eap)
menu_translate_tab_and_shift(to); menu_translate_tab_and_shift(to);
menu_unescape_name(from); menu_unescape_name(from);
menu_unescape_name(to); menu_unescape_name(to);
tp[menutrans_ga.ga_len].from = from; menutrans_T* tp = GA_APPEND_VIA_PTR(menutrans_T, &menutrans_ga);
tp[menutrans_ga.ga_len].from_noamp = from_noamp; tp->from = from;
tp[menutrans_ga.ga_len].to = to; tp->from_noamp = from_noamp;
++menutrans_ga.ga_len; tp->to = to;
} else { } else {
free(from); free(from);
free(from_noamp); free(from_noamp);

View File

@@ -4177,7 +4177,6 @@ regmatch (
case BACK: case BACK:
{ {
int i; int i;
backpos_T *bp;
/* /*
* When we run into BACK we need to check if we don't keep * When we run into BACK we need to check if we don't keep
@@ -4187,17 +4186,13 @@ regmatch (
* The positions are stored in "backpos" and found by the * The positions are stored in "backpos" and found by the
* current value of "scan", the position in the RE program. * current value of "scan", the position in the RE program.
*/ */
bp = (backpos_T *)backpos.ga_data; backpos_T *bp = (backpos_T *)backpos.ga_data;
for (i = 0; i < backpos.ga_len; ++i) for (i = 0; i < backpos.ga_len; ++i)
if (bp[i].bp_scan == scan) if (bp[i].bp_scan == scan)
break; break;
if (i == backpos.ga_len) { if (i == backpos.ga_len) {
/* First time at this BACK, make room to store the pos. */ backpos_T *p = GA_APPEND_VIA_PTR(backpos_T, &backpos);
ga_grow(&backpos, 1); p->bp_scan = scan;
/* get "ga_data" again, it may have changed */
bp = (backpos_T *)backpos.ga_data;
bp[i].bp_scan = scan;
++backpos.ga_len;
} else if (reg_save_equal(&bp[i].bp_pos)) } else if (reg_save_equal(&bp[i].bp_pos))
/* Still at same position as last time, fail. */ /* Still at same position as last time, fail. */
status = RA_NOMATCH; status = RA_NOMATCH;

View File

@@ -3335,7 +3335,6 @@ static int init_syl_tab(slang_T *slang)
char_u *p; char_u *p;
char_u *s; char_u *s;
int l; int l;
syl_item_T *syl;
ga_init(&slang->sl_syl_items, sizeof(syl_item_T), 4); ga_init(&slang->sl_syl_items, sizeof(syl_item_T), 4);
p = vim_strchr(slang->sl_syllable, '/'); p = vim_strchr(slang->sl_syllable, '/');
@@ -3351,9 +3350,8 @@ static int init_syl_tab(slang_T *slang)
l = (int)(p - s); l = (int)(p - s);
if (l >= SY_MAXLEN) if (l >= SY_MAXLEN)
return SP_FORMERROR; return SP_FORMERROR;
ga_grow(&slang->sl_syl_items, 1);
syl = ((syl_item_T *)slang->sl_syl_items.ga_data) syl_item_T *syl = GA_APPEND_VIA_PTR(syl_item_T, &slang->sl_syl_items);
+ slang->sl_syl_items.ga_len++;
STRLCPY(syl->sy_chars, s, l + 1); STRLCPY(syl->sy_chars, s, l + 1);
syl->sy_len = l; syl->sy_len = l;
} }
@@ -3835,10 +3833,10 @@ char_u *did_set_spelllang(win_T *wp)
} }
if (region_mask != 0) { if (region_mask != 0) {
ga_grow(&ga, 1); langp_T *p = GA_APPEND_VIA_PTR(langp_T, &ga);
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; p->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; p->lp_region = region_mask;
++ga.ga_len;
use_midword(slang, wp); use_midword(slang, wp);
if (slang->sl_nobreak) if (slang->sl_nobreak)
nobreak = TRUE; nobreak = TRUE;
@@ -3896,7 +3894,6 @@ char_u *did_set_spelllang(win_T *wp)
slang->sl_nobreak = TRUE; slang->sl_nobreak = TRUE;
} }
if (slang != NULL) { if (slang != NULL) {
ga_grow(&ga, 1);
region_mask = REGION_ALL; region_mask = REGION_ALL;
if (use_region != NULL && !dont_use_region) { if (use_region != NULL && !dont_use_region) {
// find region in sl_regions // find region in sl_regions
@@ -3909,11 +3906,12 @@ char_u *did_set_spelllang(win_T *wp)
} }
if (region_mask != 0) { if (region_mask != 0) {
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; langp_T *p = GA_APPEND_VIA_PTR(langp_T, &ga);
LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL; p->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL; p->lp_sallang = NULL;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; p->lp_replang = NULL;
++ga.ga_len; p->lp_region = region_mask;
use_midword(slang, wp); use_midword(slang, wp);
} }
} }
@@ -4851,16 +4849,11 @@ static afffile_T *spell_read_aff(spellinfo_T *spin, char_u *fname)
break; break;
} }
if (idx < 0) { if (idx < 0) {
ga_grow(&spin->si_prefcond, 1);
// Not found, add a new condition. // Not found, add a new condition.
idx = spin->si_prefcond.ga_len++; idx = spin->si_prefcond.ga_len;
pp = ((char_u **)spin->si_prefcond.ga_data) pp = GA_APPEND_VIA_PTR(char_u *, &spin->si_prefcond);
+ idx; *pp = (aff_entry->ae_cond == NULL) ?
if (aff_entry->ae_cond == NULL) NULL : getroom_save(spin, aff_entry->ae_cond);
*pp = NULL;
else
*pp = getroom_save(spin,
aff_entry->ae_cond);
} }
// Add the prefix to the prefix tree. // Add the prefix to the prefix tree.
@@ -5328,16 +5321,13 @@ static int str_equal(char_u *s1, char_u *s2)
// They are stored case-folded. // They are stored case-folded.
static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to) static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)
{ {
fromto_T *ftp;
char_u word[MAXWLEN]; char_u word[MAXWLEN];
ga_grow(gap, 1); fromto_T *ftp = GA_APPEND_VIA_PTR(fromto_T, gap);
ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
(void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN); (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
ftp->ft_from = getroom_save(spin, word); ftp->ft_from = getroom_save(spin, word);
(void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN); (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
ftp->ft_to = getroom_save(spin, word); ftp->ft_to = getroom_save(spin, word);
++gap->ga_len;
} }
// Convert a boolean argument in a SAL line to TRUE or FALSE; // Convert a boolean argument in a SAL line to TRUE or FALSE;
@@ -11503,9 +11493,8 @@ add_suggestion (
} }
if (i < 0) { if (i < 0) {
ga_grow(gap, 1);
// Add a suggestion. // Add a suggestion.
stp = &SUG(*gap, gap->ga_len); stp = GA_APPEND_VIA_PTR(suggest_T, gap);
stp->st_word = vim_strnsave(goodword, goodlen); stp->st_word = vim_strnsave(goodword, goodlen);
stp->st_wordlen = goodlen; stp->st_wordlen = goodlen;
stp->st_score = score; stp->st_score = score;
@@ -11513,7 +11502,6 @@ add_suggestion (
stp->st_had_bonus = had_bonus; stp->st_had_bonus = had_bonus;
stp->st_orglen = badlen; stp->st_orglen = badlen;
stp->st_slang = slang; stp->st_slang = slang;
++gap->ga_len;
// If we have too many suggestions now, sort the list and keep // If we have too many suggestions now, sort the list and keep
// the best suggestions. // the best suggestions.

View File

@@ -2485,10 +2485,9 @@ update_si_end (
*/ */
static void push_current_state(int idx) static void push_current_state(int idx)
{ {
ga_grow(&current_state, 1); stateitem_T *p = GA_APPEND_VIA_PTR(stateitem_T, &current_state);
memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T)); memset(p, 0, sizeof(*p));
CUR_STATE(current_state.ga_len).si_idx = idx; p->si_idx = idx;
++current_state.ga_len;
} }
/* /*
@@ -4279,28 +4278,26 @@ syn_cmd_match (
if (!ends_excmd(*rest) || eap->skip) if (!ends_excmd(*rest) || eap->skip)
rest = NULL; rest = NULL;
else { else {
ga_grow(&curwin->w_s->b_syn_patterns, 1);
if ((syn_id = syn_check_group(arg, (int)(group_name_end - arg))) != 0) { if ((syn_id = syn_check_group(arg, (int)(group_name_end - arg))) != 0) {
syn_incl_toplevel(syn_id, &syn_opt_arg.flags); syn_incl_toplevel(syn_id, &syn_opt_arg.flags);
/* /*
* Store the pattern in the syn_items list * Store the pattern in the syn_items list
*/ */
int idx = curwin->w_s->b_syn_patterns.ga_len; synpat_T *spp = GA_APPEND_VIA_PTR(synpat_T,
SYN_ITEMS(curwin->w_s)[idx] = item; &curwin->w_s->b_syn_patterns);
SYN_ITEMS(curwin->w_s)[idx].sp_syncing = syncing; *spp = item;
SYN_ITEMS(curwin->w_s)[idx].sp_type = SPTYPE_MATCH; spp->sp_syncing = syncing;
SYN_ITEMS(curwin->w_s)[idx].sp_syn.id = syn_id; spp->sp_type = SPTYPE_MATCH;
SYN_ITEMS(curwin->w_s)[idx].sp_syn.inc_tag = current_syn_inc_tag; spp->sp_syn.id = syn_id;
SYN_ITEMS(curwin->w_s)[idx].sp_flags = syn_opt_arg.flags; spp->sp_syn.inc_tag = current_syn_inc_tag;
SYN_ITEMS(curwin->w_s)[idx].sp_sync_idx = sync_idx; spp->sp_flags = syn_opt_arg.flags;
SYN_ITEMS(curwin->w_s)[idx].sp_cont_list = syn_opt_arg.cont_list; spp->sp_sync_idx = sync_idx;
SYN_ITEMS(curwin->w_s)[idx].sp_syn.cont_in_list = spp->sp_cont_list = syn_opt_arg.cont_list;
syn_opt_arg.cont_in_list; spp->sp_syn.cont_in_list = syn_opt_arg.cont_in_list;
SYN_ITEMS(curwin->w_s)[idx].sp_cchar = conceal_char; spp->sp_cchar = conceal_char;
if (syn_opt_arg.cont_in_list != NULL) if (syn_opt_arg.cont_in_list != NULL)
curwin->w_s->b_syn_containedin = TRUE; curwin->w_s->b_syn_containedin = TRUE;
SYN_ITEMS(curwin->w_s)[idx].sp_next_list = syn_opt_arg.next_list; spp->sp_next_list = syn_opt_arg.next_list;
++curwin->w_s->b_syn_patterns.ga_len;
/* remember that we found a match for syncing on */ /* remember that we found a match for syncing on */
if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE)) if (syn_opt_arg.flags & (HL_SYNC_HERE|HL_SYNC_THERE))
@@ -4755,16 +4752,12 @@ static int syn_add_cluster(char_u *name)
return 0; return 0;
} }
/* syn_cluster_T *scp = GA_APPEND_VIA_PTR(syn_cluster_T,
* Make room for at least one other cluster entry. &curwin->w_s->b_syn_clusters);
*/ memset(scp, 0, sizeof(*scp));
ga_grow(&curwin->w_s->b_syn_clusters, 1); scp->scl_name = name;
scp->scl_name_u = vim_strsave_up(name);
memset(&(SYN_CLSTR(curwin->w_s)[len]), 0, sizeof(syn_cluster_T)); scp->scl_list = NULL;
SYN_CLSTR(curwin->w_s)[len].scl_name = name;
SYN_CLSTR(curwin->w_s)[len].scl_name_u = vim_strsave_up(name);
SYN_CLSTR(curwin->w_s)[len].scl_list = NULL;
++curwin->w_s->b_syn_clusters.ga_len;
if (STRICMP(name, "Spell") == 0) if (STRICMP(name, "Spell") == 0)
curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER; curwin->w_s->b_spell_cluster_id = len + SYNID_CLUSTER;
@@ -5701,8 +5694,7 @@ static void syntime_report(void)
for (int idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx) { for (int idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len; ++idx) {
spp = &(SYN_ITEMS(curwin->w_s)[idx]); spp = &(SYN_ITEMS(curwin->w_s)[idx]);
if (spp->sp_time.count > 0) { if (spp->sp_time.count > 0) {
ga_grow(&ga, 1); p = GA_APPEND_VIA_PTR(time_entry_T, &ga);
p = ((time_entry_T *)ga.ga_data) + ga.ga_len;
p->total = spp->sp_time.total; p->total = spp->sp_time.total;
profile_add(&total_total, &spp->sp_time.total); profile_add(&total_total, &spp->sp_time.total);
p->count = spp->sp_time.count; p->count = spp->sp_time.count;
@@ -5713,7 +5705,6 @@ static void syntime_report(void)
p->average = tm; p->average = tm;
p->id = spp->sp_syn.id; p->id = spp->sp_syn.id;
p->pattern = spp->sp_pattern; p->pattern = spp->sp_pattern;
++ga.ga_len;
} }
} }
@@ -6810,10 +6801,8 @@ static int get_attr_entry(garray_T *table, attrentry_T *aep)
/* /*
* This is a new combination of colors and font, add an entry. * This is a new combination of colors and font, add an entry.
*/ */
ga_grow(table, 1); taep = GA_APPEND_VIA_PTR(attrentry_T, table);
memset(taep, 0, sizeof(*taep));
taep = &(((attrentry_T *)table->ga_data)[table->ga_len]);
memset(taep, 0, sizeof(attrentry_T));
taep->ae_attr = aep->ae_attr; taep->ae_attr = aep->ae_attr;
if (table == &term_attr_table) { if (table == &term_attr_table) {
if (aep->ae_u.term.start == NULL) if (aep->ae_u.term.start == NULL)
@@ -6828,7 +6817,7 @@ static int get_attr_entry(garray_T *table, attrentry_T *aep)
taep->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color; taep->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
taep->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color; taep->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
} }
++table->ga_len;
return table->ga_len - 1 + ATTR_OFF; return table->ga_len - 1 + ATTR_OFF;
} }
@@ -7319,15 +7308,11 @@ static int syn_add_group(char_u *name)
return 0; return 0;
} }
/* // Append another syntax_highlight entry.
* Make room for at least one other syntax_highlight entry. struct hl_group* hlgp = GA_APPEND_VIA_PTR(struct hl_group, &highlight_ga);
*/ memset(hlgp, 0, sizeof(*hlgp));
ga_grow(&highlight_ga, 1); hlgp->sg_name = name;
hlgp->sg_name_u = vim_strsave_up(name);
memset(&(HL_TABLE()[highlight_ga.ga_len]), 0, sizeof(struct hl_group));
HL_TABLE()[highlight_ga.ga_len].sg_name = name;
HL_TABLE()[highlight_ga.ga_len].sg_name_u = vim_strsave_up(name);
++highlight_ga.ga_len;
return highlight_ga.ga_len; /* ID is index plus one */ return highlight_ga.ga_len; /* ID is index plus one */
} }