Add benchmarking test to rulescomp
[profile/ivi/libxkbcommon.git] / src / atom.c
index f39ba45..e283632 100644 (file)
@@ -70,76 +70,106 @@ SOFTWARE.
 
  ********************************************************/
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include "xkbmisc.h"
-#include "xkbcommon/xkbcommon.h"
-#include "XKBcommonint.h"
+#include "atom.h"
+
+#define INITIAL_TABLE_SIZE 100
+
+struct atom_node {
+    struct atom_node *left, *right;
+    uint32_t a;
+    unsigned int fingerprint;
+    char *string;
+};
+
+struct atom_table {
+    xkb_atom_t last_atom;
+    struct atom_node *atom_root;
+    size_t table_length;
+    struct atom_node **node_table;
+};
+
+struct atom_table *
+atom_table_new(void)
+{
+    struct atom_table *table;
 
-#define InitialTableSize 100
+    table = calloc(1, sizeof(*table));
+    if (!table)
+        return NULL;
 
-typedef struct _Node {
-    struct _Node    *left, *right;
-    uint32_t          a;
-    unsigned int    fingerPrint;
-    char            *string;
-} NodeRec, *NodePtr;
+    table->last_atom = XKB_ATOM_NONE;
 
-#define BAD_RESOURCE 0xe0000000
+    return table;
+}
 
-static xkb_atom_t lastAtom = XKB_ATOM_NONE;
-static NodePtr atomRoot = NULL;
-static unsigned long tableLength;
-static NodePtr *nodeTable = NULL;
+static void
+free_atom(struct atom_node *patom)
+{
+    if (!patom)
+        return;
 
-const char *
-XkbcAtomText(xkb_atom_t atom)
+    free_atom(patom->left);
+    free_atom(patom->right);
+    free(patom->string);
+    free(patom);
+}
+
+void
+atom_table_free(struct atom_table *table)
 {
-    NodePtr node;
+    if (!table)
+        return;
 
-    if ((atom == XKB_ATOM_NONE) || (atom > lastAtom))
-        return NULL;
-    if (!(node = nodeTable[atom]))
+    free_atom(table->atom_root);
+    free(table->node_table);
+    free(table);
+}
+
+const char *
+atom_text(struct atom_table *table, xkb_atom_t atom)
+{
+    if (atom == XKB_ATOM_NONE || atom > table->last_atom ||
+        !table->node_table[atom])
         return NULL;
-    return node->string;
+
+    return table->node_table[atom]->string;
 }
 
 char *
-XkbcAtomGetString(xkb_atom_t atom)
+atom_strdup(struct atom_table *table, xkb_atom_t atom)
 {
-    const char *ret = XkbcAtomText(atom);
+    const char *ret = atom_text(table, atom);
     return ret ? strdup(ret) : NULL;
 }
 
 xkb_atom_t
-xkb_intern_atom(const char *string)
+atom_intern(struct atom_table *table, const char *string)
 {
-    NodePtr *np;
+    struct atom_node **np;
+    struct atom_node *nd;
     unsigned i;
     int comp;
     unsigned int fp = 0;
-    unsigned len;
-    int makeit = True;
+    size_t len;
 
     if (!string)
-       return XKB_ATOM_NONE;
+        return XKB_ATOM_NONE;
 
     len = strlen(string);
-    np = &atomRoot;
+    np = &table->atom_root;
     for (i = 0; i < (len + 1) / 2; i++) {
         fp = fp * 27 + string[i];
         fp = fp * 27 + string[len - 1 - i];
     }
 
     while (*np) {
-        if (fp < (*np)->fingerPrint)
+        if (fp < (*np)->fingerprint)
             np = &((*np)->left);
-        else if (fp > (*np)->fingerPrint)
+        else if (fp > (*np)->fingerprint)
             np = &((*np)->right);
         else {
             /* now start testing the strings */
-            comp = strncmp(string, (*np)->string, (int)len);
+            comp = strncmp(string, (*np)->string, len);
             if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
                 np = &((*np)->left);
             else if (comp > 0)
@@ -149,74 +179,46 @@ xkb_intern_atom(const char *string)
             }
     }
 
-    if (makeit) {
-        NodePtr nd;
-
-        nd = malloc(sizeof(NodeRec));
-        if (!nd)
-            return BAD_RESOURCE;
+    nd = malloc(sizeof(*nd));
+    if (!nd)
+        return XKB_ATOM_NONE;
 
-        nd->string = malloc(len + 1);
-        if (!nd->string) {
+    nd->string = malloc(len + 1);
+    if (!nd->string) {
+        free(nd);
+        return XKB_ATOM_NONE;
+    }
+    strncpy(nd->string, string, len);
+    nd->string[len] = 0;
+
+    if ((table->last_atom + 1) >= table->table_length) {
+        struct atom_node **new_node_table;
+        int new_length;
+
+        if (table->table_length == 0)
+            new_length = INITIAL_TABLE_SIZE;
+        else
+            new_length = table->table_length * 2;
+
+        new_node_table = realloc(table->node_table,
+                                 new_length * sizeof(*new_node_table));
+        if (!new_node_table) {
+            if (nd->string != string)
+                free(nd->string);
             free(nd);
-            return BAD_RESOURCE;
-        }
-        strncpy(nd->string, string, (int)len);
-        nd->string[len] = 0;
-
-        if ((lastAtom + 1) >= tableLength) {
-            NodePtr *table;
-           int newLength;
-
-           if (tableLength == 0)
-               newLength = InitialTableSize;
-           else
-               newLength = tableLength * 2;
-
-            table = realloc(nodeTable, newLength * sizeof(NodePtr));
-            if (!table) {
-                if (nd->string != string)
-                    free(nd->string);
-                free(nd);
-                return BAD_RESOURCE;
-            }
-            tableLength = newLength;
-           table[XKB_ATOM_NONE] = NULL;
-
-            nodeTable = table;
+            return XKB_ATOM_NONE;
         }
+        new_node_table[XKB_ATOM_NONE] = NULL;
 
-        *np = nd;
-        nd->left = nd->right = NULL;
-        nd->fingerPrint = fp;
-        nd->a = (++lastAtom);
-        *(nodeTable + lastAtom) = nd;
-
-        return nd->a;
+        table->table_length = new_length;
+        table->node_table = new_node_table;
     }
-    else
-        return XKB_ATOM_NONE;
-}
 
-static void
-FreeAtom(NodePtr patom)
-{
-    if (patom->left)
-        FreeAtom(patom->left);
-    if (patom->right)
-        FreeAtom(patom->right);
-    free(patom->string);
-    free(patom);
-}
+    *np = nd;
+    nd->left = nd->right = NULL;
+    nd->fingerprint = fp;
+    nd->a = (++table->last_atom);
+    *(table->node_table + table->last_atom) = nd;
 
-void
-XkbcFreeAllAtoms(void)
-{
-    if (atomRoot == NULL)
-        return;
-    FreeAtom(atomRoot);
-    atomRoot = NULL;
-    free(nodeTable);
-    nodeTable = NULL;
-    lastAtom = XKB_ATOM_NONE;
+    return nd->a;
 }