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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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;