Commit c2ac3a2b20a3f762a0089b983f0b72032cf8d110

Ran Benita 2012-05-22T08:07:52

keytypes: use darray for key types in the client map Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/alloc.c b/src/alloc.c
index 2e61646..a1d34bf 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -28,62 +28,35 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 int
 XkbcAllocClientMap(struct xkb_keymap *keymap, unsigned which,
-                   unsigned nTotalTypes)
+                   size_t nTotalTypes)
 {
-    struct xkb_client_map * map;
-
     if (!keymap || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
         return BadValue;
 
     if (!keymap->map) {
-        map = uTypedCalloc(1, struct xkb_client_map);
-        if (!map)
+        keymap->map = calloc(1, sizeof(*keymap->map));
+        if (!keymap->map)
             return BadAlloc;
-        keymap->map = map;
+        darray_init(keymap->map->types);
     }
-    else
-        map = keymap->map;
-
-    if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
-        if (!map->types) {
-            map->types = uTypedCalloc(nTotalTypes, struct xkb_key_type);
-            if (!map->types)
-                return BadAlloc;
-
-            map->num_types = 0;
-            map->size_types = nTotalTypes;
-        }
-        else if (map->size_types < nTotalTypes) {
-            struct xkb_key_type *prev_types = map->types;
-
-            map->types = uTypedRealloc(map->types, nTotalTypes,
-                                          struct xkb_key_type);
-            if (!map->types) {
-                free(prev_types);
-                map->num_types = map->size_types = 0;
-                return BadAlloc;
-            }
 
-            map->size_types = nTotalTypes;
-            memset(&map->types[map->num_types], 0,
-                   (map->size_types - map->num_types) * sizeof(struct xkb_key_type));
-        }
-    }
+    if (which & XkbKeyTypesMask)
+        darray_growalloc(keymap->map->types, nTotalTypes);
 
     if (which & XkbKeySymsMask) {
-        if (!map->key_sym_map) {
-            map->key_sym_map = uTypedCalloc(keymap->max_key_code + 1,
-                                            struct xkb_sym_map);
-            if (!map->key_sym_map)
+        if (!keymap->map->key_sym_map) {
+            keymap->map->key_sym_map = uTypedCalloc(keymap->max_key_code + 1,
+                                                    struct xkb_sym_map);
+            if (!keymap->map->key_sym_map)
                 return BadAlloc;
         }
     }
 
     if (which & XkbModifierMapMask) {
-        if (!map->modmap) {
-            map->modmap = uTypedCalloc(keymap->max_key_code + 1,
-                                       unsigned char);
-            if (!map->modmap)
+        if (!keymap->map->modmap) {
+            keymap->map->modmap = uTypedCalloc(keymap->max_key_code + 1,
+                                               unsigned char);
+            if (!keymap->map->modmap)
                 return BadAlloc;
         }
     }
@@ -309,14 +282,13 @@ XkbcFreeClientMap(struct xkb_keymap *keymap)
     struct xkb_client_map * map;
     struct xkb_key_type * type;
     xkb_keycode_t key;
-    int i;
 
     if (!keymap || !keymap->map)
         return;
 
     map = keymap->map;
 
-    for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
+    darray_foreach(type, map->types) {
         int j;
         free(type->map);
         free(type->preserve);
@@ -325,7 +297,7 @@ XkbcFreeClientMap(struct xkb_keymap *keymap)
         free(type->level_names);
         free(UNCONSTIFY(type->name));
     }
-    free(map->types);
+    darray_free(map->types);
 
     if (map->key_sym_map) {
         for (key = keymap->min_key_code; key < keymap->max_key_code; key++) {
@@ -410,12 +382,10 @@ XkbcAllocNames(struct xkb_keymap *keymap, unsigned which,
     }
     names = keymap->names;
 
-    if ((which & XkbKTLevelNamesMask) && keymap->map && keymap->map->types) {
-        int i;
+    if ((which & XkbKTLevelNamesMask) && keymap->map) {
         struct xkb_key_type * type;
 
-        type = keymap->map->types;
-        for (i = 0; i < keymap->map->num_types; i++, type++) {
+        darray_foreach(type, keymap->map->types) {
             if (!type->level_names) {
                 type->level_names = uTypedCalloc(type->num_levels, const char *);
                 if (!type->level_names)
@@ -462,6 +432,7 @@ XkbcFreeNames(struct xkb_keymap *keymap)
 {
     struct xkb_names * names;
     struct xkb_client_map * map;
+    struct xkb_key_type *type;
     int i;
 
     if (!keymap || !keymap->names)
@@ -470,10 +441,8 @@ XkbcFreeNames(struct xkb_keymap *keymap)
     names = keymap->names;
     map = keymap->map;
 
-    if (map && map->types) {
-        struct xkb_key_type * type = map->types;
-
-        for (i = 0; i < map->num_types; i++, type++) {
+    if (map) {
+        darray_foreach(type, map->types) {
             int j;
             for (j = 0; j < type->num_levels; j++)
                 free(UNCONSTIFY(type->level_names[j]));
diff --git a/src/alloc.h b/src/alloc.h
index aec0249..c9a3314 100644
--- a/src/alloc.h
+++ b/src/alloc.h
@@ -50,7 +50,7 @@ XkbcFreeKeyboard(struct xkb_keymap *keymap);
 
 extern int
 XkbcAllocClientMap(struct xkb_keymap *keymap, unsigned which,
-                   unsigned nTotalTypes);
+                   size_t nTotalTypes);
 
 extern int
 XkbcAllocServerMap(struct xkb_keymap *keymap, unsigned which,
diff --git a/src/misc.c b/src/misc.c
index 748d83c..ac54077 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -80,7 +80,7 @@ XkbcInitCanonicalKeyTypes(struct xkb_keymap *keymap, unsigned which,
                           int keypadVMod)
 {
     struct xkb_client_map * map;
-    struct xkb_key_type *from, *to;
+    struct xkb_key_type *from;
     int rtrn;
 
     if (!keymap)
@@ -96,23 +96,25 @@ XkbcInitCanonicalKeyTypes(struct xkb_keymap *keymap, unsigned which,
 
     rtrn = Success;
     from = canonicalTypes;
-    to = map->types;
 
     if (which & XkbOneLevelMask)
-        rtrn = XkbcCopyKeyType(&from[XkbOneLevelIndex], &to[XkbOneLevelIndex]);
+        rtrn = XkbcCopyKeyType(&from[XkbOneLevelIndex],
+                               &darray_item(map->types, XkbOneLevelIndex));
 
     if ((which & XkbTwoLevelMask) && (rtrn == Success))
-        rtrn = XkbcCopyKeyType(&from[XkbTwoLevelIndex], &to[XkbTwoLevelIndex]);
+        rtrn = XkbcCopyKeyType(&from[XkbTwoLevelIndex],
+                               &darray_item(map->types, XkbTwoLevelIndex));
 
     if ((which & XkbAlphabeticMask) && (rtrn == Success))
         rtrn = XkbcCopyKeyType(&from[XkbAlphabeticIndex],
-                               &to[XkbAlphabeticIndex]);
+                               &darray_item(map->types, XkbAlphabeticIndex));
 
     if ((which & XkbKeypadMask) && (rtrn == Success)) {
         struct xkb_key_type * type;
 
-        rtrn = XkbcCopyKeyType(&from[XkbKeypadIndex], &to[XkbKeypadIndex]);
-        type = &to[XkbKeypadIndex];
+        rtrn = XkbcCopyKeyType(&from[XkbKeypadIndex],
+                               &darray_item(map->types, XkbKeypadIndex));
+        type = &darray_item(map->types, XkbKeypadIndex);
 
         if ((keypadVMod >= 0) && (keypadVMod < XkbNumVirtualMods) &&
             (rtrn == Success)) {
diff --git a/src/xkb-priv.h b/src/xkb-priv.h
index db07442..876dbfd 100644
--- a/src/xkb-priv.h
+++ b/src/xkb-priv.h
@@ -280,9 +280,7 @@ struct xkb_sym_map {
 };
 
 struct xkb_client_map {
-    unsigned char            size_types;
-    unsigned char            num_types;
-    struct xkb_key_type *           types;
+    darray(struct xkb_key_type) types;
     struct xkb_sym_map *             key_sym_map;
     unsigned char           *modmap;
 };
@@ -387,7 +385,7 @@ struct xkb_keymap {
 #define	XkbKeyGroupWidth(d,k,g) (XkbKeyType(d,k,g)->num_levels)
 #define	XkbKeyGroupsWidth(d,k)  ((d)->map->key_sym_map[k].width)
 #define	XkbKeyTypeIndex(d,k,g)  ((d)->map->key_sym_map[k].kt_index[g&0x3])
-#define	XkbKeyType(d,k,g)	(&(d)->map->types[XkbKeyTypeIndex(d,k,g)])
+#define	XkbKeyType(d,k,g)	(&darray_item((d)->map->types, XkbKeyTypeIndex(d,k,g)))
 #define	XkbKeyNumSyms(d,k,g,sl) \
         ((d)->map->key_sym_map[k].num_syms[(g*XkbKeyGroupsWidth(d,k))+sl])
 #define	XkbKeySym(d,k,n)	(&(d)->map->key_sym_map[k].syms[n])
diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c
index 2cb3905..4b5caca 100644
--- a/src/xkbcomp/compat.c
+++ b/src/xkbcomp/compat.c
@@ -1025,6 +1025,7 @@ UpdateModifiersFromCompat(struct xkb_keymap *keymap)
 {
     xkb_keycode_t key;
     int i;
+    struct xkb_key_type *type;
 
     /* Find all the interprets for the key and bind them to actions,
      * which will also update the vmodmap. */
@@ -1046,8 +1047,7 @@ UpdateModifiersFromCompat(struct xkb_keymap *keymap)
     }
 
     /* Now update the level masks for all the types to reflect the vmods. */
-    for (i = 0; i < keymap->map->num_types; i++) {
-        struct xkb_key_type *type = &keymap->map->types[i];
+    darray_foreach(type, keymap->map->types) {
         uint32_t mask = 0;
         int j;
         type->mods.mask = type->mods.real_mods;
diff --git a/src/xkbcomp/keytypes.c b/src/xkbcomp/keytypes.c
index b8b019b..5914b32 100644
--- a/src/xkbcomp/keytypes.c
+++ b/src/xkbcomp/keytypes.c
@@ -1171,7 +1171,7 @@ CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
         goto err_info;
     }
 
-    keymap->map->num_types = i;
+    darray_resize0(keymap->map->types, i);
 
     if (XkbAllRequiredTypes & (~info.stdPresent)) {
         unsigned missing, keypadVMod;
@@ -1185,29 +1185,29 @@ CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
         }
 
         if (missing & XkbOneLevelMask)
-            keymap->map->types[XkbOneLevelIndex].name =
+            darray_item(keymap->map->types, XkbOneLevelIndex).name =
                 xkb_atom_strdup(keymap->ctx, tok_ONE_LEVEL);
         if (missing & XkbTwoLevelMask)
-            keymap->map->types[XkbTwoLevelIndex].name =
+            darray_item(keymap->map->types, XkbTwoLevelIndex).name =
                 xkb_atom_strdup(keymap->ctx, tok_TWO_LEVEL);
         if (missing & XkbAlphabeticMask)
-            keymap->map->types[XkbAlphabeticIndex].name =
+            darray_item(keymap->map->types, XkbAlphabeticIndex).name =
                 xkb_atom_strdup(keymap->ctx, tok_ALPHABETIC);
         if (missing & XkbKeypadMask)
-            keymap->map->types[XkbKeypadIndex].name =
+            darray_item(keymap->map->types, XkbKeypadIndex).name =
                 xkb_atom_strdup(keymap->ctx, tok_KEYPAD);
     }
 
-    next = &keymap->map->types[XkbLastRequiredType + 1];
+    next = &darray_item(keymap->map->types, XkbLastRequiredType + 1);
     for (i = 0, def = info.types; i < info.nTypes; i++) {
         if (def->name == tok_ONE_LEVEL)
-            type = &keymap->map->types[XkbOneLevelIndex];
+            type = &darray_item(keymap->map->types, XkbOneLevelIndex);
         else if (def->name == tok_TWO_LEVEL)
-            type = &keymap->map->types[XkbTwoLevelIndex];
+            type = &darray_item(keymap->map->types, XkbTwoLevelIndex);
         else if (def->name == tok_ALPHABETIC)
-            type = &keymap->map->types[XkbAlphabeticIndex];
+            type = &darray_item(keymap->map->types, XkbAlphabeticIndex);
         else if (def->name == tok_KEYPAD)
-            type = &keymap->map->types[XkbKeypadIndex];
+            type = &darray_item(keymap->map->types, XkbKeypadIndex);
         else
             type = next++;
 
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index f40bf9c..3ae10cf 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -1696,18 +1696,17 @@ FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym,
 static bool
 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
 {
-    unsigned n;
+    unsigned n = 0;
     const char *name = xkb_atom_text(keymap->ctx, atom);
+    struct xkb_key_type *type;
 
-    if (keymap && keymap->map && keymap->map->types)
-    {
-        for (n = 0; n < keymap->map->num_types; n++)
-        {
-            if (strcmp(keymap->map->types[n].name, name) == 0)
-            {
+    if (keymap && keymap->map) {
+        darray_foreach(type, keymap->map->types) {
+            if (strcmp(type->name, name) == 0) {
                 *type_rtrn = n;
                 return true;
             }
+            n++;
         }
     }
     return false;
@@ -2005,7 +2004,7 @@ CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *key, int start_from)
             types[i] = XkbTwoLevelIndex;
         }
         /* if the type specifies fewer levels than the key has, shrink the key */
-        type = &keymap->map->types[types[i]];
+        type = &darray_item(keymap->map->types, types[i]);
         if (type->num_levels < key->numLevels[i])
         {
             if (warningLevel > 0)