Commit dcdbd149258b0d85db7b58681a804488f242e8c8

Ran Benita 2012-05-22T12:24:54

parser: use darray for keysym list Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/xkbcomp/parseutils.c b/src/xkbcomp/parseutils.c
index 885ec30..7a6afa7 100644
--- a/src/xkbcomp/parseutils.c
+++ b/src/xkbcomp/parseutils.c
@@ -385,59 +385,6 @@ ActionCreate(xkb_atom_t name, ExprDef * args)
     return NULL;
 }
 
-static bool
-ResizeKeysymList(ExprDef *list, unsigned int extra)
-{
-    int i;
-
-    if (list->value.list.nSyms + extra > list->value.list.szSyms)
-    {
-        list->value.list.szSyms *= 2;
-        list->value.list.szSyms += extra;
-        if (list->value.list.szSyms == 1)
-            list->value.list.szSyms = 4;
-        list->value.list.syms = uTypedRecalloc(list->value.list.syms,
-                                               list->value.list.nSyms,
-                                               list->value.list.szSyms,
-                                               char *);
-        if (list->value.list.syms == NULL)
-        {
-            FATAL("Couldn't resize list of symbols for append\n");
-            return false;
-        }
-    }
-    if (list->value.list.nLevels >= list->value.list.szLevels)
-    {
-        list->value.list.szLevels *= 2;
-        if (list->value.list.szLevels == 0)
-            list->value.list.szLevels = 4;
-        list->value.list.symsMapIndex =
-            uTypedRecalloc(list->value.list.symsMapIndex,
-                           list->value.list.nLevels,
-                           list->value.list.szLevels,
-                           int);
-        if (list->value.list.symsMapIndex == NULL)
-        {
-            FATAL("Couldn't resize keysym index map for append\n");
-            return false;
-        }
-        list->value.list.symsNumEntries =
-            uTypedRecalloc(list->value.list.symsNumEntries,
-                           list->value.list.nLevels,
-                           list->value.list.szLevels,
-                           unsigned int);
-        if (list->value.list.symsNumEntries == NULL)
-        {
-            FATAL("Couldn't resize num keysym entries for append\n");
-            return false;
-        }
-        for (i = list->value.list.nLevels; i < list->value.list.szLevels; i++)
-            list->value.list.symsMapIndex[i] = -1;
-    }
-
-    return true;
-}
-
 ExprDef *
 CreateKeysymList(char *sym)
 {
@@ -450,25 +397,13 @@ CreateKeysymList(char *sym)
         return NULL;
     }
 
-    def->value.list.nSyms = 0;
-    def->value.list.szSyms = 0;
-    def->value.list.nLevels = 0;
-    def->value.list.szLevels = 0;
-    def->value.list.syms = NULL;
-    def->value.list.symsMapIndex = NULL;
-    def->value.list.symsNumEntries = NULL;
+    darray_init(def->value.list.syms);
+    darray_init(def->value.list.symsMapIndex);
+    darray_init(def->value.list.symsNumEntries);
 
-    if (!ResizeKeysymList(def, 1))
-    {
-        FreeStmt(&def->common);
-        return NULL;
-    }
-
-    def->value.list.syms[0] = sym;
-    def->value.list.symsMapIndex[0] = 0;
-    def->value.list.symsNumEntries[0] = 1;
-    def->value.list.nLevels = 1;
-    def->value.list.nSyms = 1;
+    darray_append(def->value.list.syms, sym);
+    darray_append(def->value.list.symsMapIndex, 0);
+    darray_append(def->value.list.symsNumEntries, 1);
 
     return def;
 }
@@ -476,16 +411,12 @@ CreateKeysymList(char *sym)
 ExprDef *
 CreateMultiKeysymList(ExprDef *list)
 {
-    int i;
+    size_t nLevels = darray_size(list->value.list.symsMapIndex);
 
-    for (i = 1; i < list->value.list.szLevels; i++)
-    {
-        list->value.list.symsMapIndex[i] = -1;
-        list->value.list.symsNumEntries[i] = 0;
-    }
-    list->value.list.symsMapIndex[0] = 0;
-    list->value.list.symsNumEntries[0] = list->value.list.nLevels;
-    list->value.list.nLevels = 1;
+    darray_resize(list->value.list.symsMapIndex, 1);
+    darray_resize(list->value.list.symsNumEntries, 1);
+    darray_item(list->value.list.symsMapIndex, 0) = 0;
+    darray_item(list->value.list.symsNumEntries, 0) = nLevels;
 
     return list;
 }
@@ -493,36 +424,28 @@ CreateMultiKeysymList(ExprDef *list)
 ExprDef *
 AppendKeysymList(ExprDef * list, char *sym)
 {
-    if (!ResizeKeysymList(list, 1))
-        return NULL;
+    size_t nSyms = darray_size(list->value.list.syms);
+
+    darray_append(list->value.list.symsMapIndex, nSyms);
+    darray_append(list->value.list.symsNumEntries, 1);
+    darray_append(list->value.list.syms, sym);
 
-    list->value.list.symsMapIndex[list->value.list.nLevels] =
-        list->value.list.nSyms;
-    list->value.list.symsNumEntries[list->value.list.nLevels] = 1;
-    list->value.list.syms[list->value.list.nSyms++] = sym;
-    list->value.list.nLevels++;
     return list;
 }
 
 ExprDef *
 AppendMultiKeysymList(ExprDef * list, ExprDef * append)
 {
-    int i;
+    size_t nSyms = darray_size(list->value.list.syms);
+    size_t numEntries = darray_size(append->value.list.syms);
 
-    if (!ResizeKeysymList(list, append->value.list.nSyms))
-        return NULL;
-
-    list->value.list.symsMapIndex[list->value.list.nLevels] =
-        list->value.list.nSyms;
-    list->value.list.symsNumEntries[list->value.list.nLevels] =
-        append->value.list.nSyms;
-    for (i = 0; i < append->value.list.nSyms; i++) {
-        list->value.list.syms[list->value.list.nSyms++] =
-            append->value.list.syms[i];
-        append->value.list.syms[i] = NULL;
-    }
-    list->value.list.nLevels++;
+    darray_append(list->value.list.symsMapIndex, nSyms);
+    darray_append(list->value.list.symsNumEntries, numEntries);
+    darray_append_items(list->value.list.syms,
+                        &darray_item(append->value.list.syms, 0),
+                        numEntries);
 
+    darray_resize(append->value.list.syms, 0);
     FreeStmt(&append->common);
 
     return list;
@@ -717,7 +640,7 @@ StmtSetMerge(ParseCommon * stmt, unsigned merge, struct YYLTYPE *loc, void *scan
 static void
 FreeExpr(ExprDef *expr)
 {
-    int i;
+    char **sym;
 
     if (!expr)
         return;
@@ -746,11 +669,11 @@ FreeExpr(ExprDef *expr)
         FreeStmt(&expr->value.array.entry->common);
         break;
     case ExprKeysymList:
-        for (i = 0; i < expr->value.list.nSyms; i++)
-            free(expr->value.list.syms[i]);
-        free(expr->value.list.syms);
-        free(expr->value.list.symsMapIndex);
-        free(expr->value.list.symsNumEntries);
+        darray_foreach(sym, expr->value.list.syms)
+            free(*sym);
+        darray_free(expr->value.list.syms);
+        darray_free(expr->value.list.symsMapIndex);
+        darray_free(expr->value.list.symsNumEntries);
         break;
     default:
         break;
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index 8b371b9..58565c1 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -1049,8 +1049,8 @@ AddSymbolsToKey(KeyInfo *key, struct xkb_keymap *keymap,
         ACTION("Ignoring duplicate definition\n");
         return false;
     }
-    nSyms = value->value.list.nSyms;
-    nLevels = value->value.list.nLevels;
+    nSyms = darray_size(value->value.list.syms);
+    nLevels = darray_size(value->value.list.symsMapIndex);
     if (((key->numLevels[ndx] < nSyms) || (key->syms[ndx] == NULL)) &&
         (!ResizeKeyGroup(key, ndx, nLevels, nSyms, false)))
     {
@@ -1061,15 +1061,21 @@ AddSymbolsToKey(KeyInfo *key, struct xkb_keymap *keymap,
     }
     key->symsDefined |= (1 << ndx);
     for (i = 0; i < nLevels; i++) {
-        key->symsMapIndex[ndx][i] = value->value.list.symsMapIndex[i];
-        key->symsMapNumEntries[ndx][i] = value->value.list.symsNumEntries[i];
+        key->symsMapIndex[ndx][i] =
+            darray_item(value->value.list.symsMapIndex, i);
+        key->symsMapNumEntries[ndx][i] =
+            darray_item(value->value.list.symsNumEntries, i);
+
         for (j = 0; j < key->symsMapNumEntries[ndx][i]; j++) {
             if (key->symsMapIndex[ndx][i] + j >= nSyms)
                 abort();
-            if (!LookupKeysym(value->value.list.syms[value->value.list.symsMapIndex[i] + j],
+            if (!LookupKeysym(darray_item(value->value.list.syms,
+                                          darray_item(value->value.list.symsMapIndex, i) + j),
                               &key->syms[ndx][key->symsMapIndex[ndx][i] + j])) {
                 WARN("Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
-                     value->value.list.syms[i], longText(key->name), ndx + 1,
+                     darray_item(value->value.list.syms, i),
+                     longText(key->name),
+                     ndx + 1,
                      xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
                 while (--j >= 0)
                     key->syms[ndx][key->symsMapIndex[ndx][i] + j] = XKB_KEY_NoSymbol;
diff --git a/src/xkbcomp/xkbcomp.h b/src/xkbcomp/xkbcomp.h
index e16d96b..ec0db0f 100644
--- a/src/xkbcomp/xkbcomp.h
+++ b/src/xkbcomp/xkbcomp.h
@@ -131,13 +131,9 @@ typedef struct _Expr
         } action;
         struct
         {
-            int nSyms;
-            int szSyms;
-            char **syms;
-            int nLevels;
-            int szLevels;
-            int *symsMapIndex;
-            unsigned int *symsNumEntries;
+            darray(char *) syms;
+            darray(int) symsMapIndex;
+            darray(unsigned int) symsNumEntries;
         } list;
         struct _Expr *child;
         xkb_atom_t str;