From 87dff888abd107541bf232258793d0bcb162b37d Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 10 Aug 2012 18:14:35 +0300 Subject: [PATCH] Store actions inside struct xkb_key Cuts out a lot of useless redirection and space. Signed-off-by: Ran Benita --- src/keymap-dump.c | 11 +++++------ src/state.c | 4 ++-- src/xkb-priv.h | 33 ++++----------------------------- src/xkbcomp/action.c | 42 ------------------------------------------ src/xkbcomp/action.h | 4 ---- src/xkbcomp/compat.c | 23 +++++++++++++---------- src/xkbcomp/symbols.c | 31 ++++++++++++------------------- src/xkbcomp/xkbcomp.c | 3 +-- 8 files changed, 37 insertions(+), 114 deletions(-) diff --git a/src/keymap-dump.c b/src/keymap-dump.c index f1d11bd..7593068 100644 --- a/src/keymap-dump.c +++ b/src/keymap-dump.c @@ -732,7 +732,7 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf) } if (key->explicit & XkbExplicitInterpretMask) - showActions = XkbKeyHasActions(key); + showActions = (key->actions != NULL); else showActions = false; @@ -746,10 +746,8 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf) write_buf(buf, " ] };\n"); } else { - union xkb_action *acts; - int level; + xkb_level_index_t level; - acts = XkbKeyActionsPtr(keymap, key); for (group = 0; group < key->num_groups; group++) { if (group != 0) write_buf(buf, ","); @@ -765,10 +763,11 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf) level++) { if (level != 0) write_buf(buf, ", "); - write_action(keymap, buf, &acts[level], NULL, NULL); + write_action(keymap, buf, + XkbKeyActionEntry(key, group, level), + NULL, NULL); } write_buf(buf, " ]"); - acts += key->width; } } write_buf(buf, "\n\t\t};\n"); diff --git a/src/state.c b/src/state.c index 758bbf7..c77365b 100644 --- a/src/state.c +++ b/src/state.c @@ -118,7 +118,7 @@ xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc) if (XkbKeycodeInRange(state->keymap, kc)) key = XkbKey(state->keymap, kc); - if (!key || !XkbKeyHasActions(key)) + if (!key || !key->actions) return &fake; group = xkb_key_get_group(state, kc); @@ -129,7 +129,7 @@ xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc) if (level == XKB_LEVEL_INVALID) return &fake; - return XkbKeyActionEntry(state->keymap, key, group, level); + return XkbKeyActionEntry(key, group, level); } static struct xkb_filter * diff --git a/src/xkb-priv.h b/src/xkb-priv.h index e38703d..630760a 100644 --- a/src/xkb-priv.h +++ b/src/xkb-priv.h @@ -321,8 +321,7 @@ struct xkb_key { bool repeats; - /* Index into keymap->acts */ - size_t acts_index; + union xkb_action *actions; unsigned char kt_index[XkbNumKbdGroups]; @@ -369,8 +368,6 @@ struct xkb_keymap { struct xkb_mods groups[XkbNumKbdGroups]; const char *group_names[XkbNumKbdGroups]; - darray(union xkb_action) acts; - struct xkb_indicator_map indicators[XkbNumIndicators]; const char *indicator_names[XkbNumIndicators]; @@ -446,33 +443,11 @@ XkbKeySymEntry(struct xkb_key *key, xkb_group_index_t group, return XkbKeySym(key, XkbKeySymOffset(key, group, level)); } -static inline bool -XkbKeyHasActions(struct xkb_key *key) -{ - return key->acts_index != 0; -} - -static inline unsigned int -XkbKeyNumActions(struct xkb_key *key) -{ - if (XkbKeyHasActions(key)) - return key->width * key->num_groups; - return 1; -} - static inline union xkb_action * -XkbKeyActionsPtr(struct xkb_keymap *keymap, struct xkb_key *key) +XkbKeyActionEntry(struct xkb_key *key, xkb_group_index_t group, + xkb_level_index_t level) { - return darray_mem(keymap->acts, key->acts_index); -} - -static inline union xkb_action * -XkbKeyActionEntry(struct xkb_keymap *keymap, struct xkb_key *key, - xkb_group_index_t group, xkb_level_index_t level) -{ - if (XkbKeyHasActions(key)) - return &XkbKeyActionsPtr(keymap, key)[key->width * group + level]; - return NULL; + return &key->actions[key->width * group + level]; } static inline bool diff --git a/src/xkbcomp/action.c b/src/xkbcomp/action.c index 7e9851f..a2dd9ed 100644 --- a/src/xkbcomp/action.c +++ b/src/xkbcomp/action.c @@ -1296,45 +1296,3 @@ SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field, old->next = new; return true; } - -/***====================================================================***/ - -union xkb_action * -ResizeKeyActions(struct xkb_keymap *keymap, struct xkb_key *key, - uint32_t needed) -{ - size_t old_ndx, old_num_acts, new_ndx; - - if (needed == 0) { - key->acts_index = 0; - return NULL; - } - - if (XkbKeyHasActions(key) && key->width >= needed) - return XkbKeyActionsPtr(keymap, key); - - /* - * The key may already be in the array, but without enough space. - * This should not happen often, so in order to avoid moving and - * copying stuff from acts and key_acts, we just allocate new - * space for the key at the end, and leave the old space alone. - */ - - old_ndx = key->acts_index; - old_num_acts = XkbKeyNumActions(key); - new_ndx = darray_size(keymap->acts); - - darray_resize0(keymap->acts, new_ndx + needed); - key->acts_index = new_ndx; - - /* - * The key was already in the array, copy the old actions to the - * new space. - */ - if (old_ndx != 0) - memcpy(darray_mem(keymap->acts, new_ndx), - darray_mem(keymap->acts, old_ndx), - old_num_acts * sizeof(union xkb_action)); - - return XkbKeyActionsPtr(keymap, key); -} diff --git a/src/xkbcomp/action.h b/src/xkbcomp/action.h index 6ca0685..3d63468 100644 --- a/src/xkbcomp/action.h +++ b/src/xkbcomp/action.h @@ -76,10 +76,6 @@ SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field, ExprDef *index, ExprDef *value, ActionInfo **info_rtrn); -union xkb_action * -ResizeKeyActions(struct xkb_keymap *keymap, struct xkb_key *key, - uint32_t needed); - extern const LookupEntry ctrlNames[]; #endif /* ACTION_H */ diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c index a9d6c8d..1fbf2b1 100644 --- a/src/xkbcomp/compat.c +++ b/src/xkbcomp/compat.c @@ -1382,7 +1382,6 @@ ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key) { #define INTERP_SIZE (8 * 4) struct xkb_sym_interpret *interps[INTERP_SIZE]; - union xkb_action *acts; xkb_mod_mask_t vmodmask = 0; int num_acts = 0; xkb_group_index_t group; @@ -1408,11 +1407,12 @@ ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key) } } - if (num_acts) - num_acts = key->num_groups * key->width; - acts = ResizeKeyActions(keymap, key, num_acts); - if (num_acts && !acts) - return false; + if (num_acts && !key->actions) { + key->actions = calloc(key->num_groups * key->width, + sizeof(*key->actions)); + if (!key->actions) + return false; + } for (group = 0; group < key->num_groups; group++) { for (level = 0; level < XkbKeyGroupWidth(keymap, key, group); @@ -1437,7 +1437,8 @@ ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key) if (interp->virtual_mod != XkbNoModifier) vmodmask |= (1 << interp->virtual_mod); } - acts[i] = interp->act; + + key->actions[i] = interp->act; } } @@ -1496,9 +1497,11 @@ UpdateModifiersFromCompat(struct xkb_keymap *keymap) /* Update action modifiers. */ xkb_foreach_key(key, keymap) { - union xkb_action *acts = XkbKeyActionsPtr(keymap, key); - for (i = 0; i < XkbKeyNumActions(key); i++) - UpdateActionMods(keymap, &acts[i], key->modmap); + if (!key->actions) + continue; + + for (i = 0; i < key->num_groups * key->width; i++) + UpdateActionMods(keymap, &key->actions[i], key->modmap); } /* Update group modifiers. */ diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c index cb549c8..5993a9a 100644 --- a/src/xkbcomp/symbols.c +++ b/src/xkbcomp/symbols.c @@ -1691,7 +1691,6 @@ CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi, struct xkb_key_type * type; bool haveActions, autoType, useAlias; unsigned types[XkbNumKbdGroups]; - union xkb_action *outActs; unsigned int symIndex = 0; useAlias = (start_from == 0); @@ -1769,27 +1768,23 @@ CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi, darray_resize0(key->syms, sizeSyms); + key->num_groups = nGroups; + + key->width = width; + + key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index)); + + key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms)); + if (haveActions) { - outActs = ResizeKeyActions(keymap, key, width * nGroups); - if (outActs == NULL) { - log_wsgo(info->keymap->ctx, - "Could not enlarge actions for %s (key %d)\n", - LongKeyNameText(keyi->name), kc); - return false; - } + key->actions = calloc(nGroups * width, sizeof(*key->actions)); key->explicit |= XkbExplicitInterpretMask; } - else - outActs = NULL; - key->num_groups = nGroups; if (keyi->defined & KEY_FIELD_GROUPINFO) { key->out_of_range_group_number = keyi->out_of_range_group_number; key->out_of_range_group_action = keyi->out_of_range_group_action; } - key->width = width; - key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index)); - key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms)); for (i = 0; i < nGroups; i++) { /* assign kt_index[i] to the index of the type in map->types. @@ -1820,11 +1815,11 @@ CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi, key->sym_index[(i * width) + tmp] = -1; key->num_syms[(i * width) + tmp] = 0; } - if (outActs != NULL && !darray_empty(keyi->acts[i])) { + if (key->actions && !darray_empty(keyi->acts[i])) { if (tmp < keyi->numLevels[i]) - outActs[tmp] = darray_item(keyi->acts[i], tmp); + key->actions[tmp] = darray_item(keyi->acts[i], tmp); else - outActs[tmp].type = XkbSA_NoAction; + key->actions[tmp].type = XkbSA_NoAction; } } } @@ -1906,8 +1901,6 @@ CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, if (info.errorCount != 0) goto err_info; - darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1); - if (info.name) keymap->symbols_section_name = strdup(info.name); diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c index bddeaac..141a48e 100644 --- a/src/xkbcomp/xkbcomp.c +++ b/src/xkbcomp/xkbcomp.c @@ -333,11 +333,10 @@ xkb_map_unref(struct xkb_keymap *keymap) free(key->sym_index); free(key->num_syms); darray_free(key->syms); + free(key->actions); } darray_free(keymap->keys); - darray_free(keymap->acts); - darray_free(keymap->sym_interpret); darray_free(keymap->key_aliases);