Commit 561504162fe84094f87e8389e1eb249fa31feb8e

Ran Benita 2012-05-22T00:52:28

compat: use darray for sym_interprets Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/alloc.c b/src/alloc.c
index ccdea78..2e61646 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -363,49 +363,21 @@ XkbcFreeServerMap(struct xkb_keymap *keymap)
 int
 XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
 {
-    struct xkb_compat_map * compat;
-    struct xkb_sym_interpret *prev_interpret;
-
     if (!keymap)
         return BadMatch;
 
-    if (keymap->compat) {
-        if (keymap->compat->size_si >= nSI)
-            return Success;
-
-        compat = keymap->compat;
-        compat->size_si = nSI;
-        if (!compat->sym_interpret)
-            compat->num_si = 0;
-
-        prev_interpret = compat->sym_interpret;
-        compat->sym_interpret = uTypedRecalloc(compat->sym_interpret,
-                                               compat->num_si, nSI,
-                                               struct xkb_sym_interpret);
-        if (!compat->sym_interpret) {
-            free(prev_interpret);
-            compat->size_si = compat->num_si = 0;
+    if (!keymap->compat) {
+        keymap->compat = calloc(1, sizeof(*keymap->compat));
+        if (!keymap->compat)
             return BadAlloc;
-        }
-
-        return Success;
+        darray_init(keymap->compat->sym_interpret);
     }
 
-    compat = uTypedCalloc(1, struct xkb_compat_map);
-    if (!compat)
-        return BadAlloc;
+    darray_growalloc(keymap->compat->sym_interpret, nSI);
+    darray_resize(keymap->compat->sym_interpret, 0);
 
-    if (nSI > 0) {
-        compat->sym_interpret = uTypedCalloc(nSI, struct xkb_sym_interpret);
-        if (!compat->sym_interpret) {
-            free(compat);
-            return BadAlloc;
-        }
-    }
-    compat->size_si = nSI;
-    compat->num_si = 0;
-    memset(&compat->groups[0], 0, XkbNumKbdGroups * sizeof(struct xkb_mods));
-    keymap->compat = compat;
+    memset(keymap->compat->groups, 0,
+           XkbNumKbdGroups * sizeof(*keymap->compat->groups));
 
     return Success;
 }
@@ -414,15 +386,11 @@ XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
 static void
 XkbcFreeCompatMap(struct xkb_keymap *keymap)
 {
-    struct xkb_compat_map * compat;
-
     if (!keymap || !keymap->compat)
         return;
 
-    compat = keymap->compat;
-
-    free(compat->sym_interpret);
-    free(compat);
+    darray_free(keymap->compat->sym_interpret);
+    free(keymap->compat);
     keymap->compat = NULL;
 }
 
diff --git a/src/xkb-priv.h b/src/xkb-priv.h
index d89287b..db07442 100644
--- a/src/xkb-priv.h
+++ b/src/xkb-priv.h
@@ -265,10 +265,8 @@ struct xkb_sym_interpret {
 };
 
 struct xkb_compat_map {
-    struct xkb_sym_interpret *      sym_interpret;
+    darray(struct xkb_sym_interpret) sym_interpret;
     struct xkb_mods              groups[XkbNumKbdGroups];
-    unsigned short           num_si;
-    unsigned short           size_si;
 };
 
 struct xkb_sym_map {
diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c
index a2a4143..2cb3905 100644
--- a/src/xkbcomp/compat.c
+++ b/src/xkbcomp/compat.c
@@ -776,13 +776,7 @@ CopyInterps(CompatInfo * info,
             (needSymbol && (si->interp.sym == XKB_KEY_NoSymbol)) ||
             ((!needSymbol) && (si->interp.sym != XKB_KEY_NoSymbol)))
             continue;
-        if (compat->num_si >= compat->size_si)
-        {
-            WSGO("No room to merge symbol interpretations\n");
-            ACTION("Symbol interpretations lost\n");
-            return;
-        }
-        compat->sym_interpret[compat->num_si++] = si->interp;
+        darray_append(compat->sym_interpret, si->interp);
     }
 }
 
@@ -893,16 +887,15 @@ FindInterpForKey(struct xkb_keymap *keymap, xkb_keycode_t key,
                  uint32_t group, uint32_t level)
 {
     struct xkb_sym_interpret *ret = NULL;
+    struct xkb_sym_interpret *interp;
     const xkb_keysym_t *syms;
     int num_syms;
-    int i;
 
     num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
     if (num_syms == 0)
         return NULL;
 
-    for (i = 0; i < keymap->compat->num_si; i++) {
-        struct xkb_sym_interpret *interp = &keymap->compat->sym_interpret[i];
+    darray_foreach(interp, keymap->compat->sym_interpret) {
         uint32_t mods;
         bool found;