Commit 9e801ff782c592d89273939c1d84d8c62674f57e

Ran Benita 2013-07-21T17:01:20

ctx: adapt to the len-aware atom functions xkb_atom_intern now takes a len parameter. Turns out though that almost all of our xkb_atom_intern calls are called on string literals, the length of which we know statically. So we add a macro to micro-optimize this case. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/context.c b/src/context.c
index 923060e..25a727c 100644
--- a/src/context.c
+++ b/src/context.c
@@ -335,9 +335,9 @@ xkb_atom_lookup(struct xkb_context *ctx, const char *string)
 }
 
 xkb_atom_t
-xkb_atom_intern(struct xkb_context *ctx, const char *string)
+xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
 {
-    return atom_intern(ctx->atom_table, string, strlen(string), false);
+    return atom_intern(ctx->atom_table, string, len, false);
 }
 
 xkb_atom_t
diff --git a/src/context.h b/src/context.h
index 7c061a0..c9bb277 100644
--- a/src/context.h
+++ b/src/context.h
@@ -43,11 +43,14 @@ xkb_atom_t
 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
 
 xkb_atom_t
-xkb_atom_intern(struct xkb_context *ctx, const char *string);
+xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len);
+
+#define xkb_atom_intern_literal(ctx, literal) \
+    xkb_atom_intern((ctx), (literal), sizeof(literal) - 1)
 
 /**
- * If @string is dynamically allocated, free'd immediately after
- * being interned, and not used afterwards, use this function
+ * If @string is dynamically allocated, NUL-terminated, free'd immediately
+ * after being interned, and not used afterwards, use this function
  * instead of xkb_atom_intern to avoid some unnecessary allocations.
  * The caller should not use or free the passed in string afterwards.
  */
diff --git a/src/xkbcomp/keymap.c b/src/xkbcomp/keymap.c
index 320216e..d9dd0f6 100644
--- a/src/xkbcomp/keymap.c
+++ b/src/xkbcomp/keymap.c
@@ -235,14 +235,14 @@ UpdateBuiltinKeymapFields(struct xkb_keymap *keymap)
      * The order is important!
      */
     darray_appends_t(keymap->mods, struct xkb_mod,
-        { .name = xkb_atom_intern(ctx, "Shift"),   .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Lock"),    .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Control"), .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Mod1"),    .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Mod2"),    .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Mod3"),    .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Mod4"),    .type = MOD_REAL },
-        { .name = xkb_atom_intern(ctx, "Mod5"),    .type = MOD_REAL });
+        { .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 });
 
     return true;
 }
diff --git a/src/xkbcomp/parser.y b/src/xkbcomp/parser.y
index aaf87db..8618e36 100644
--- a/src/xkbcomp/parser.y
+++ b/src/xkbcomp/parser.y
@@ -530,19 +530,19 @@ FieldSpec       :       Ident   { $$ = $1; }
                 ;
 
 Element         :       ACTION_TOK
-                        { $$ = xkb_atom_intern(param->ctx, "action"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "action"); }
                 |       INTERPRET
-                        { $$ = xkb_atom_intern(param->ctx, "interpret"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "interpret"); }
                 |       TYPE
-                        { $$ = xkb_atom_intern(param->ctx, "type"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "type"); }
                 |       KEY
-                        { $$ = xkb_atom_intern(param->ctx, "key"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "key"); }
                 |       GROUP
-                        { $$ = xkb_atom_intern(param->ctx, "group"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "group"); }
                 |       MODIFIER_MAP
-                        {$$ = xkb_atom_intern(param->ctx, "modifier_map");}
+                        {$$ = xkb_atom_intern_literal(param->ctx, "modifier_map");}
                 |       INDICATOR
-                        { $$ = xkb_atom_intern(param->ctx, "indicator"); }
+                        { $$ = xkb_atom_intern_literal(param->ctx, "indicator"); }
                 |       SHAPE
                         { $$ = XKB_ATOM_NONE; }
                 |       ROW
@@ -737,7 +737,7 @@ KeyCode         :       INTEGER { $$ = $1; }
                 ;
 
 Ident           :       IDENT   { $$ = xkb_atom_steal(param->ctx, $1); }
-                |       DEFAULT { $$ = xkb_atom_intern(param->ctx, "default"); }
+                |       DEFAULT { $$ = xkb_atom_intern_literal(param->ctx, "default"); }
                 ;
 
 String          :       STRING  { $$ = xkb_atom_steal(param->ctx, $1); }
diff --git a/src/xkbcomp/scanner.c b/src/xkbcomp/scanner.c
index 4be2c40..0296c58 100644
--- a/src/xkbcomp/scanner.c
+++ b/src/xkbcomp/scanner.c
@@ -125,7 +125,7 @@ skip_more_whitespace_and_comments:
             return scanner_error(yylloc, s, "empty key name literal");
         if (!buf_append(s, '\0') || !chr(s, '>'))
             return scanner_error(yylloc, s, "unterminated key name literal");
-        yylval->sval = xkb_atom_intern(s->ctx, s->buf);
+        yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
         return KEYNAME;
     }
 
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index f08c204..5cf68d9 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -143,7 +143,7 @@ InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
 {
     memset(keyi, 0, sizeof(*keyi));
     keyi->merge = MERGE_OVERRIDE;
-    keyi->name = xkb_atom_intern(ctx, "*");
+    keyi->name = xkb_atom_intern_literal(ctx, "*");
     keyi->out_of_range_group_action = RANGE_WRAP;
 }
 
@@ -1339,19 +1339,19 @@ FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
         darray_item(groupi->levels, level).u.syms[0])
 
     if (width == 1 || width <= 0)
-        return xkb_atom_intern(ctx, "ONE_LEVEL");
+        return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
 
     sym0 = GET_SYM(0);
     sym1 = GET_SYM(1);
 
     if (width == 2) {
         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
-            return xkb_atom_intern(ctx, "ALPHABETIC");
+            return xkb_atom_intern_literal(ctx, "ALPHABETIC");
 
         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
-            return xkb_atom_intern(ctx, "KEYPAD");
+            return xkb_atom_intern_literal(ctx, "KEYPAD");
 
-        return xkb_atom_intern(ctx, "TWO_LEVEL");
+        return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
     }
 
     if (width <= 4) {
@@ -1360,15 +1360,15 @@ FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
             sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
 
             if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
-                return xkb_atom_intern(ctx, "FOUR_LEVEL_ALPHABETIC");
+                return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
 
-            return xkb_atom_intern(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
+            return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
         }
 
         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
-            return xkb_atom_intern(ctx, "FOUR_LEVEL_KEYPAD");
+            return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
 
-        return xkb_atom_intern(ctx, "FOUR_LEVEL");
+        return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
     }
 
     return XKB_ATOM_NONE;
diff --git a/src/xkbcomp/types.c b/src/xkbcomp/types.c
index c6a2672..e8c1f93 100644
--- a/src/xkbcomp/types.c
+++ b/src/xkbcomp/types.c
@@ -794,7 +794,7 @@ CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
         type->num_levels = 1;
         type->entries = NULL;
         type->num_entries = 0;
-        type->name = xkb_atom_intern(keymap->ctx, "default");
+        type->name = xkb_atom_intern_literal(keymap->ctx, "default");
         type->level_names = NULL;
 
         return true;