diff options
author | Ran Benita <ran234@gmail.com> | 2014-04-22 12:23:36 +0300 |
---|---|---|
committer | Ran Benita <ran234@gmail.com> | 2014-04-22 14:56:44 +0300 |
commit | 787faf36654ed75672613c560286caddadce3ec5 (patch) | |
tree | a3d72f5562234c15141faa3331f6ad1550e0df07 | |
parent | 6b1cdee107caaf8009037763a3cc3cd51132a1dd (diff) | |
download | libxkbcommon-787faf36654ed75672613c560286caddadce3ec5.tar.gz libxkbcommon-787faf36654ed75672613c560286caddadce3ec5.tar.bz2 libxkbcommon-787faf36654ed75672613c560286caddadce3ec5.zip |
keymap: don't use darray in xkb_mod_set
Instead just statically allocate the mods array (of size MAX_MOD_SIZE =
32). The limit is not going anywhere, and static allocations are nicer
(nicer code, no OOM, etc.). It's also small and dense enough.
Signed-off-by: Ran Benita <ran234@gmail.com>
-rw-r--r-- | src/keymap-priv.c | 33 | ||||
-rw-r--r-- | src/keymap.c | 8 | ||||
-rw-r--r-- | src/keymap.h | 11 | ||||
-rw-r--r-- | src/text.c | 4 | ||||
-rw-r--r-- | src/x11/keymap.c | 15 | ||||
-rw-r--r-- | src/xkbcomp/compat.c | 5 | ||||
-rw-r--r-- | src/xkbcomp/symbols.c | 5 | ||||
-rw-r--r-- | src/xkbcomp/types.c | 5 | ||||
-rw-r--r-- | src/xkbcomp/vmod.c | 29 | ||||
-rw-r--r-- | src/xkbcomp/vmod.h | 6 |
10 files changed, 48 insertions, 73 deletions
diff --git a/src/keymap-priv.c b/src/keymap-priv.c index f391336..d21dda7 100644 --- a/src/keymap-priv.c +++ b/src/keymap-priv.c @@ -29,24 +29,25 @@ static void update_builtin_keymap_fields(struct xkb_keymap *keymap) { - struct xkb_context *ctx = keymap->ctx; - const struct xkb_mod builtin_mods[] = { - { .name = xkb_atom_intern_literal(ctx, "Shift"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Lock"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Control"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Mod1"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Mod2"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Mod3"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Mod4"), .type = MOD_REAL }, - { .name = xkb_atom_intern_literal(ctx, "Mod5"), .type = MOD_REAL }, + /* Predefined (AKA real, core, X11) modifiers. The order is important! */ + static const char *const builtin_mods[] = { + [0] = "Shift", + [1] = "Lock", + [2] = "Control", + [3] = "Mod1", + [4] = "Mod2", + [5] = "Mod3", + [6] = "Mod4", + [7] = "Mod5" }; - /* - * Add predefined (AKA real, core, X11) modifiers. - * The order is important! - */ - darray_append_items(keymap->mods.mods, - builtin_mods, ARRAY_SIZE(builtin_mods)); + for (unsigned i = 0; i < ARRAY_SIZE(builtin_mods); i++) { + keymap->mods.mods[i].name = xkb_atom_intern(keymap->ctx, + builtin_mods[i], + strlen(builtin_mods[i])); + keymap->mods.mods[i].type = MOD_REAL; + } + keymap->mods.num_mods = ARRAY_SIZE(builtin_mods); } struct xkb_keymap * diff --git a/src/keymap.c b/src/keymap.c index dc2541c..0b8bd96 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -93,7 +93,6 @@ xkb_keymap_unref(struct xkb_keymap *keymap) free(keymap->sym_interprets); free(keymap->key_aliases); free(keymap->group_names); - darray_free(keymap->mods.mods); darray_free(keymap->leds); free(keymap->keycodes_section_name); free(keymap->symbols_section_name); @@ -263,7 +262,7 @@ xkb_keymap_get_as_string(struct xkb_keymap *keymap, XKB_EXPORT xkb_mod_index_t xkb_keymap_num_mods(struct xkb_keymap *keymap) { - return darray_size(keymap->mods.mods); + return keymap->mods.num_mods; } /** @@ -272,11 +271,10 @@ xkb_keymap_num_mods(struct xkb_keymap *keymap) XKB_EXPORT const char * xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx) { - if (idx >= darray_size(keymap->mods.mods)) + if (idx >= keymap->mods.num_mods) return NULL; - return xkb_atom_text(keymap->ctx, - darray_item(keymap->mods.mods, idx).name); + return xkb_atom_text(keymap->ctx, keymap->mods.mods[idx].name); } /** diff --git a/src/keymap.h b/src/keymap.h index ed2f8e7..91f271a 100644 --- a/src/keymap.h +++ b/src/keymap.h @@ -354,7 +354,8 @@ struct xkb_mod { }; struct xkb_mod_set { - darray(struct xkb_mod) mods; + struct xkb_mod mods[XKB_MAX_MODS]; + unsigned int num_mods; }; /* Common keyboard description structure */ @@ -403,10 +404,14 @@ struct xkb_keymap { (iter)++) #define xkb_mods_foreach(iter, mods_) \ - darray_foreach((iter), (mods_)->mods) + for ((iter) = (mods_)->mods; \ + (iter) < (mods_)->mods + (mods_)->num_mods; \ + (iter)++) #define xkb_mods_enumerate(idx, iter, mods_) \ - darray_enumerate((idx), (iter), (mods_)->mods) + for ((idx) = 0, (iter) = (mods_)->mods; \ + (idx) < (mods_)->num_mods; \ + (idx)++, (iter)++) static inline const struct xkb_key * XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc) @@ -213,10 +213,10 @@ ModIndexText(struct xkb_context *ctx, const struct xkb_mod_set *mods, if (ndx == XKB_MOD_INVALID) return "none"; - if (ndx >= darray_size(mods->mods)) + if (ndx >= mods->num_mods) return NULL; - return xkb_atom_text(ctx, darray_item(mods->mods, ndx).name); + return xkb_atom_text(ctx, mods->mods[ndx].name); } const char * diff --git a/src/x11/keymap.c b/src/x11/keymap.c index 716fc51..0142c8b 100644 --- a/src/x11/keymap.c +++ b/src/x11/keymap.c @@ -508,17 +508,14 @@ get_vmods(struct xkb_keymap *keymap, xcb_connection_t *conn, { uint8_t *iter = xcb_xkb_get_map_map_vmods_rtrn(map); - darray_resize0(keymap->mods.mods, - NUM_REAL_MODS + msb_pos(reply->virtualMods)); + keymap->mods.num_mods = NUM_REAL_MODS + msb_pos(reply->virtualMods); for (unsigned i = 0; i < NUM_VMODS; i++) { if (reply->virtualMods & (1u << i)) { uint8_t wire = *iter; - struct xkb_mod *mod = &darray_item(keymap->mods.mods, - NUM_REAL_MODS + i); - mod->type = MOD_VIRT; - mod->mapping = translate_mods(wire, 0, 0); + keymap->mods.mods[i].type = MOD_VIRT; + keymap->mods.mods[i].mapping = translate_mods(wire, 0, 0); iter++; } @@ -920,14 +917,12 @@ get_vmod_names(struct xkb_keymap *keymap, xcb_connection_t *conn, * tells us which vmods exist (a vmod must have a name), so we fix * up the size here. */ - darray_resize0(keymap->mods.mods, - NUM_REAL_MODS + msb_pos(reply->virtualMods)); + keymap->mods.num_mods = NUM_REAL_MODS + msb_pos(reply->virtualMods); for (unsigned i = 0; i < NUM_VMODS; i++) { if (reply->virtualMods & (1u << i)) { xcb_atom_t wire = *iter; - struct xkb_mod *mod = &darray_item(keymap->mods.mods, - NUM_REAL_MODS + i); + struct xkb_mod *mod = &keymap->mods.mods[NUM_REAL_MODS + i]; if (!adopt_atom(keymap->ctx, conn, wire, &mod->name)) return false; diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c index eec69e6..698eb32 100644 --- a/src/xkbcomp/compat.c +++ b/src/xkbcomp/compat.c @@ -158,7 +158,6 @@ static void ClearCompatInfo(CompatInfo *info) { free(info->name); - ClearModSet(&info->mods); darray_free(info->interps); darray_free(info->leds); } @@ -379,7 +378,7 @@ MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from, return; } - MoveModSet(&into->mods, &from->mods); + CopyModSet(&into->mods, &from->mods); if (into->name == NULL) { into->name = from->name; @@ -865,7 +864,7 @@ CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info) keymap->compat_section_name = strdup_safe(info->name); XkbEscapeMapName(keymap->compat_section_name); - MoveModSet(&keymap->mods, &info->mods); + CopyModSet(&keymap->mods, &info->mods); if (!darray_empty(info->interps)) { struct collect collect; diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c index 015f388..06d19ca 100644 --- a/src/xkbcomp/symbols.c +++ b/src/xkbcomp/symbols.c @@ -203,7 +203,6 @@ ClearSymbolsInfo(SymbolsInfo *info) { KeyInfo *keyi; free(info->name); - ClearModSet(&info->mods); darray_foreach(keyi, info->keys) ClearKeyInfo(keyi); darray_free(info->keys); @@ -496,7 +495,7 @@ MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from, return; } - MoveModSet(&into->mods, &from->mods); + CopyModSet(&into->mods, &from->mods); if (into->name == NULL) { into->name = from->name; @@ -1539,7 +1538,7 @@ CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info) keymap->symbols_section_name = strdup_safe(info->name); XkbEscapeMapName(keymap->symbols_section_name); - MoveModSet(&keymap->mods, &info->mods); + CopyModSet(&keymap->mods, &info->mods); keymap->num_group_names = darray_size(info->group_names); keymap->group_names = darray_mem(info->group_names, 0); diff --git a/src/xkbcomp/types.c b/src/xkbcomp/types.c index e3f1d9c..2624cde 100644 --- a/src/xkbcomp/types.c +++ b/src/xkbcomp/types.c @@ -116,7 +116,6 @@ static void ClearKeyTypesInfo(KeyTypesInfo *info) { free(info->name); - ClearModSet(&info->mods); darray_free(info->types); } @@ -182,7 +181,7 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from, return; } - MoveModSet(&into->mods, &from->mods); + CopyModSet(&into->mods, &from->mods); if (into->name == NULL) { into->name = from->name; @@ -677,7 +676,7 @@ CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info) keymap->types_section_name = strdup_safe(info->name); XkbEscapeMapName(keymap->types_section_name); - MoveModSet(&keymap->mods, &info->mods); + CopyModSet(&keymap->mods, &info->mods); keymap->num_types = darray_size(info->types); if (keymap->num_types == 0) diff --git a/src/xkbcomp/vmod.c b/src/xkbcomp/vmod.c index 61f571c..4b03d4d 100644 --- a/src/xkbcomp/vmod.c +++ b/src/xkbcomp/vmod.c @@ -30,24 +30,10 @@ #include "vmod.h" void -MoveModSet(struct xkb_mod_set *into, struct xkb_mod_set *from) -{ - darray_free(into->mods); - into->mods = from->mods; - darray_init(from->mods); -} - -void CopyModSet(struct xkb_mod_set *into, const struct xkb_mod_set *from) { - darray_free(into->mods); - darray_copy(into->mods, from->mods); -} - -void -ClearModSet(struct xkb_mod_set *mods) -{ - darray_free(mods->mods); + memcpy(into->mods, from->mods, sizeof(*from->mods) * from->num_mods); + into->num_mods = from->num_mods; } bool @@ -57,7 +43,6 @@ HandleVModDef(struct xkb_context *ctx, struct xkb_mod_set *mods, xkb_mod_index_t i; struct xkb_mod *mod; xkb_mod_mask_t mapping; - struct xkb_mod new; merge = (merge == MERGE_DEFAULT ? stmt->merge : merge); @@ -112,16 +97,16 @@ HandleVModDef(struct xkb_context *ctx, struct xkb_mod_set *mods, } } - if (darray_size(mods->mods) >= XKB_MAX_MODS) { + if (mods->num_mods >= XKB_MAX_MODS) { log_err(ctx, "Too many modifiers defined (maximum %d)\n", XKB_MAX_MODS); return false; } - new.name = stmt->name; - new.mapping = mapping; - new.type = MOD_VIRT; - darray_append(mods->mods, new); + mods->mods[mods->num_mods].name = stmt->name; + mods->mods[mods->num_mods].type = MOD_VIRT; + mods->mods[mods->num_mods].mapping = mapping; + mods->num_mods++; return true; } diff --git a/src/xkbcomp/vmod.h b/src/xkbcomp/vmod.h index 39020d5..d2644b9 100644 --- a/src/xkbcomp/vmod.h +++ b/src/xkbcomp/vmod.h @@ -28,14 +28,8 @@ #define XKBCOMP_VMOD_H void -MoveModSet(struct xkb_mod_set *into, struct xkb_mod_set *from); - -void CopyModSet(struct xkb_mod_set *into, const struct xkb_mod_set *from); -void -ClearModSet(struct xkb_mod_set *mods); - bool HandleVModDef(struct xkb_context *ctx, struct xkb_mod_set *mods, VModDef *stmt, enum merge_mode merge); |