diff --git a/src/darray.h b/src/darray.h index 0e4f5f2..d85edea 100644 --- a/src/darray.h +++ b/src/darray.h @@ -44,6 +44,13 @@ darray_init(arr); \ } while (0) +#define darray_steal(arr, to, to_size) do { \ + *(to) = (arr).item; \ + if (to_size) \ + *(unsigned int *) (to_size) = (arr).size; \ + darray_init(arr); \ +} while (0) + /* * Typedefs for darrays of common types. These are useful * when you want to pass a pointer to an darray(T) around. @@ -78,7 +85,6 @@ typedef darray (unsigned long) darray_ulong; #define darray_item(arr, i) ((arr).item[i]) #define darray_size(arr) ((arr).size) #define darray_empty(arr) ((arr).size == 0) -#define darray_mem(arr, offset) ((arr).item + (offset)) /*** Insertion (single item) ***/ diff --git a/src/xkbcomp/ast-build.c b/src/xkbcomp/ast-build.c index b80a8dd..4ce1cc4 100644 --- a/src/xkbcomp/ast-build.c +++ b/src/xkbcomp/ast-build.c @@ -226,15 +226,15 @@ ExprAppendKeysymList(ExprDef *expr, xkb_keysym_t sym) ExprDef * ExprAppendMultiKeysymList(ExprDef *expr, ExprDef *append) { + xkb_keysym_t *syms; unsigned nSyms = darray_size(expr->keysym_list.syms); unsigned numEntries = darray_size(append->keysym_list.syms); darray_append(expr->keysym_list.symsMapIndex, nSyms); darray_append(expr->keysym_list.symsNumEntries, numEntries); - darray_append_items(expr->keysym_list.syms, - darray_mem(append->keysym_list.syms, 0), numEntries); + darray_steal(append->keysym_list.syms, &syms, NULL); + darray_append_items(expr->keysym_list.syms, syms, numEntries); - darray_resize(append->keysym_list.syms, 0); FreeStmt((ParseCommon *) &append); return expr; diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c index ac06ea4..ff83645 100644 --- a/src/xkbcomp/compat.c +++ b/src/xkbcomp/compat.c @@ -890,8 +890,8 @@ CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info) CopyInterps(info, false, MATCH_ANY, &collect); CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect); - keymap->num_sym_interprets = darray_size(collect.sym_interprets); - keymap->sym_interprets = darray_mem(collect.sym_interprets, 0); + darray_steal(collect.sym_interprets, + &keymap->sym_interprets, &keymap->num_sym_interprets); } CopyLedMapDefsToKeymap(keymap, info); diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c index 39125da..94ceee6 100644 --- a/src/xkbcomp/rules.c +++ b/src/xkbcomp/rules.c @@ -946,14 +946,11 @@ finish: darray_empty(m->kccgst[KCCGST_SYMBOLS])) goto error; - out->keycodes = darray_mem(m->kccgst[KCCGST_KEYCODES], 0); - out->types = darray_mem(m->kccgst[KCCGST_TYPES], 0); - out->compat = darray_mem(m->kccgst[KCCGST_COMPAT], 0); - /* out->geometry = darray_mem(m->kccgst[KCCGST_GEOMETRY], 0); */ + darray_steal(m->kccgst[KCCGST_KEYCODES], &out->keycodes, NULL); + darray_steal(m->kccgst[KCCGST_TYPES], &out->types, NULL); + darray_steal(m->kccgst[KCCGST_COMPAT], &out->compat, NULL); + darray_steal(m->kccgst[KCCGST_SYMBOLS], &out->symbols, NULL); darray_free(m->kccgst[KCCGST_GEOMETRY]); - out->symbols = darray_mem(m->kccgst[KCCGST_SYMBOLS], 0); - for (int i = 0; i < _KCCGST_NUM_ENTRIES; i++) - darray_init(m->kccgst[i]); return true; diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c index 67be8e2..5cca1f5 100644 --- a/src/xkbcomp/symbols.c +++ b/src/xkbcomp/symbols.c @@ -1468,10 +1468,8 @@ CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info, } /* Copy levels. */ - darray_enumerate(i, groupi, keyi->groups) { - key->groups[i].levels = darray_mem(groupi->levels, 0); - darray_init(groupi->levels); - } + darray_enumerate(i, groupi, keyi->groups) + darray_steal(groupi->levels, &key->groups[i].levels, NULL); key->out_of_range_group_number = keyi->out_of_range_group_number; key->out_of_range_group_action = keyi->out_of_range_group_action; @@ -1540,9 +1538,8 @@ CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info) keymap->mods = info->mods; - keymap->num_group_names = darray_size(info->group_names); - keymap->group_names = darray_mem(info->group_names, 0); - darray_init(info->group_names); + darray_steal(info->group_names, + &keymap->group_names, &keymap->num_group_names); darray_foreach(keyi, info->keys) if (!CopySymbolsDefToKeymap(keymap, info, keyi)) diff --git a/src/xkbcomp/types.c b/src/xkbcomp/types.c index f8d7e7f..ec20adc 100644 --- a/src/xkbcomp/types.c +++ b/src/xkbcomp/types.c @@ -700,14 +700,12 @@ CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info) KeyTypeInfo *def = &darray_item(info->types, i); struct xkb_key_type *type = &types[i]; - type->mods.mods = def->mods; - type->num_levels = def->num_levels; - type->entries = darray_mem(def->entries, 0); - type->num_entries = darray_size(def->entries); - darray_init(def->entries); type->name = def->name; - type->level_names = darray_mem(def->level_names, 0); - darray_init(def->level_names); + type->mods.mods = def->mods; + darray_steal(def->level_names, + &type->level_names, &type->num_levels); + darray_steal(def->entries, + &type->entries, &type->num_entries); } }