Commit 9014cf8cd850b8685571590fe060a8a4239c03e8

Ran Benita 2014-04-22T13:15:21

keymap, keycodes, compat: don't use darray for LEDs Use a static array of size XKB_MAX_LEDS instead, as in xkb_mod_set. 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
diff --git a/src/keymap.c b/src/keymap.c
index 0b8bd96..6ed0408 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -93,7 +93,6 @@ xkb_keymap_unref(struct xkb_keymap *keymap)
     free(keymap->sym_interprets);
     free(keymap->key_aliases);
     free(keymap->group_names);
-    darray_free(keymap->leds);
     free(keymap->keycodes_section_name);
     free(keymap->symbols_section_name);
     free(keymap->types_section_name);
@@ -373,7 +372,7 @@ xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
 XKB_EXPORT xkb_led_index_t
 xkb_keymap_num_leds(struct xkb_keymap *keymap)
 {
-    return darray_size(keymap->leds);
+    return keymap->num_leds;
 }
 
 /**
@@ -382,10 +381,10 @@ xkb_keymap_num_leds(struct xkb_keymap *keymap)
 XKB_EXPORT const char *
 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
 {
-    if (idx >= darray_size(keymap->leds))
+    if (idx >= keymap->num_leds)
         return NULL;
 
-    return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
+    return xkb_atom_text(keymap->ctx, keymap->leds[idx].name);
 }
 
 /**
@@ -401,7 +400,7 @@ xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
     if (atom == XKB_ATOM_NONE)
         return XKB_LED_INVALID;
 
-    darray_enumerate(i, led, keymap->leds)
+    xkb_leds_enumerate(i, led, keymap)
         if (led->name == atom)
             return i;
 
diff --git a/src/keymap.h b/src/keymap.h
index 91f271a..d56c2c4 100644
--- a/src/keymap.h
+++ b/src/keymap.h
@@ -390,7 +390,8 @@ struct xkb_keymap {
     xkb_layout_index_t num_group_names;
     xkb_atom_t *group_names;
 
-    darray(struct xkb_led) leds;
+    struct xkb_led leds[XKB_MAX_LEDS];
+    unsigned int num_leds;
 
     char *keycodes_section_name;
     char *symbols_section_name;
@@ -413,6 +414,16 @@ struct xkb_keymap {
          (idx) < (mods_)->num_mods; \
          (idx)++, (iter)++)
 
+#define xkb_leds_foreach(iter, keymap) \
+    for ((iter) = (keymap)->leds; \
+         (iter) < (keymap)->leds + (keymap)->num_leds; \
+         (iter)++)
+
+#define xkb_leds_enumerate(idx, iter, keymap) \
+    for ((idx) = 0, (iter) = (keymap)->leds; \
+         (idx) < (keymap)->num_leds; \
+         (idx)++, (iter)++)
+
 static inline const struct xkb_key *
 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
 {
diff --git a/src/state.c b/src/state.c
index 4b5f3cb..94545bc 100644
--- a/src/state.c
+++ b/src/state.c
@@ -619,7 +619,7 @@ xkb_state_led_update_all(struct xkb_state *state)
 
     state->components.leds = 0;
 
-    darray_enumerate(idx, led, state->keymap->leds) {
+    xkb_leds_enumerate(idx, led, state->keymap) {
         xkb_mod_mask_t mod_mask = 0;
         xkb_layout_mask_t group_mask = 0;
 
@@ -1252,8 +1252,8 @@ xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
 XKB_EXPORT int
 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
 {
-    if (idx >= darray_size(state->keymap->leds) ||
-        darray_item(state->keymap->leds, idx).name == XKB_ATOM_NONE)
+    if (idx >= state->keymap->num_leds ||
+        state->keymap->leds[idx].name == XKB_ATOM_NONE)
         return -1;
 
     return !!(state->components.leds & (1u << idx));
diff --git a/src/x11/keymap.c b/src/x11/keymap.c
index 0142c8b..5b18997 100644
--- a/src/x11/keymap.c
+++ b/src/x11/keymap.c
@@ -683,12 +683,12 @@ get_indicators(struct xkb_keymap *keymap, xcb_connection_t *conn,
     xcb_xkb_indicator_map_iterator_t iter =
         xcb_xkb_get_indicator_map_maps_iterator(reply);
 
-    darray_resize0(keymap->leds, msb_pos(reply->which));
+    keymap->num_leds = msb_pos(reply->which);
 
     for (unsigned i = 0; i < NUM_INDICATORS; i++) {
         if (reply->which & (1u << i)) {
             xcb_xkb_indicator_map_t *wire = iter.data;
-            struct xkb_led *led = &darray_item(keymap->leds, i);
+            struct xkb_led *led = &keymap->leds[i];
 
             if (wire->whichGroups & XCB_XKB_IM_GROUPS_WHICH_USE_BASE)
                 led->which_groups |= XKB_STATE_LAYOUT_DEPRESSED;
@@ -885,12 +885,12 @@ get_indicator_names(struct xkb_keymap *keymap, xcb_connection_t *conn,
 {
     xcb_atom_t *iter = xcb_xkb_get_names_value_list_indicator_names(list);
 
-    FAIL_UNLESS(msb_pos(reply->indicators) <= darray_size(keymap->leds));
+    FAIL_UNLESS(msb_pos(reply->indicators) <= keymap->num_leds);
 
     for (unsigned i = 0; i < NUM_INDICATORS; i++) {
         if (reply->indicators & (1u << i)) {
             xcb_atom_t wire = *iter;
-            struct xkb_led *led = &darray_item(keymap->leds, i);
+            struct xkb_led *led = &keymap->leds[i];
 
             if (!adopt_atom(keymap->ctx, conn, wire, &led->name))
                 return false;
diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c
index 698eb32..61711ae 100644
--- a/src/xkbcomp/compat.c
+++ b/src/xkbcomp/compat.c
@@ -87,7 +87,8 @@ typedef struct {
     SymInterpInfo default_interp;
     darray(SymInterpInfo) interps;
     LedInfo default_led;
-    darray(LedInfo) leds;
+    LedInfo leds[XKB_MAX_LEDS];
+    unsigned int num_leds;
     ActionsInfo *actions;
     struct xkb_mod_set mods;
 
@@ -159,7 +160,6 @@ ClearCompatInfo(CompatInfo *info)
 {
     free(info->name);
     darray_free(info->interps);
-    darray_free(info->leds);
 }
 
 static SymInterpInfo *
@@ -307,12 +307,13 @@ UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
 static bool
 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
 {
-    LedInfo *old;
     enum led_field collide;
     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
     const bool report = (same_file && verbosity > 0) || verbosity > 9;
 
-    darray_foreach(old, info->leds) {
+    for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
+        LedInfo *old = &info->leds[i];
+
         if (old->led.name != new->led.name)
             continue;
 
@@ -362,7 +363,13 @@ AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
         return true;
     }
 
-    darray_append(info->leds, *new);
+    if (info->num_leds >= XKB_MAX_LEDS) {
+        log_err(info->ctx,
+                "Too many LEDs defined (maximum %d)\n",
+                XKB_MAX_LEDS);
+        return false;
+    }
+    info->leds[info->num_leds++] = *new;
     return true;
 }
 
@@ -371,7 +378,6 @@ MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
                         enum merge_mode merge)
 {
     SymInterpInfo *si;
-    LedInfo *ledi;
 
     if (from->errorCount > 0) {
         into->errorCount += from->errorCount;
@@ -397,12 +403,14 @@ MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
         }
     }
 
-    if (darray_empty(into->leds)) {
-        into->leds = from->leds;
-        darray_init(from->leds);
+    if (into->num_leds == 0) {
+        memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
+        into->num_leds = from->num_leds;
+        from->num_leds = 0;
     }
     else {
-        darray_foreach(ledi, from->leds) {
+        for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
+            LedInfo *ledi = &from->leds[i];
             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
             if (!AddLedMap(into, ledi, false))
                 into->errorCount++;
@@ -810,31 +818,31 @@ CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
 static void
 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
 {
-    LedInfo *ledi;
-    xkb_led_index_t i;
-    struct xkb_led *led;
+    for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
+        LedInfo *ledi = &info->leds[idx];
+        xkb_led_index_t i;
+        struct xkb_led *led;
 
-    darray_foreach(ledi, info->leds) {
         /*
          * Find the LED with the given name, if it was already declared
          * in keycodes.
          */
-        darray_enumerate(i, led, keymap->leds)
+        xkb_leds_enumerate(i, led, keymap)
             if (led->name == ledi->led.name)
                 break;
 
         /* Not previously declared; create it with next free index. */
-        if (i >= darray_size(keymap->leds)) {
+        if (i >= keymap->num_leds) {
             log_dbg(keymap->ctx,
                     "Indicator name \"%s\" was not declared in the keycodes section; "
                     "Adding new indicator\n",
                     xkb_atom_text(keymap->ctx, ledi->led.name));
 
-            darray_enumerate(i, led, keymap->leds)
+            xkb_leds_enumerate(i, led, keymap)
                 if (led->name == XKB_ATOM_NONE)
                     break;
 
-            if (i >= darray_size(keymap->leds)) {
+            if (i >= keymap->num_leds) {
                 /* Not place to put it; ignore. */
                 if (i >= XKB_MAX_LEDS) {
                     log_err(keymap->ctx,
@@ -844,9 +852,9 @@ CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
                             xkb_atom_text(keymap->ctx, ledi->led.name));
                     continue;
                 }
+
                 /* Add a new LED. */
-                darray_resize(keymap->leds, i + 1);
-                led = &darray_item(keymap->leds, i);
+                led = &keymap->leds[keymap->num_leds++];
             }
         }
 
diff --git a/src/xkbcomp/keycodes.c b/src/xkbcomp/keycodes.c
index d90f6a4..c05ad24 100644
--- a/src/xkbcomp/keycodes.c
+++ b/src/xkbcomp/keycodes.c
@@ -49,7 +49,8 @@ typedef struct {
     xkb_keycode_t min_key_code;
     xkb_keycode_t max_key_code;
     darray(xkb_atom_t) key_names;
-    darray(LedNameInfo) led_names;
+    LedNameInfo led_names[XKB_MAX_LEDS];
+    unsigned int num_led_names;
     darray(AliasInfo) aliases;
 
     struct xkb_context *ctx;
@@ -71,10 +72,8 @@ static LedNameInfo *
 FindLedByName(KeyNamesInfo *info, xkb_atom_t name,
               xkb_led_index_t *idx_out)
 {
-    LedNameInfo *ledi;
-    xkb_led_index_t idx;
-
-    darray_enumerate(idx, ledi, info->led_names) {
+    for (xkb_led_index_t idx; idx < info->num_led_names; idx++) {
+        LedNameInfo *ledi = &info->led_names[idx];
         if (ledi->name == name) {
             *idx_out = idx;
             return ledi;
@@ -119,11 +118,11 @@ AddLedName(KeyNamesInfo *info, enum merge_mode merge, bool same_file,
         return true;
     }
 
-    if (new_idx >= darray_size(info->led_names))
-        darray_resize0(info->led_names, new_idx + 1);
+    if (new_idx >= info->num_led_names)
+        info->num_led_names = new_idx + 1;
 
     /* LED with the same index already exists. */
-    old = &darray_item(info->led_names, new_idx);
+    old = &info->led_names[new_idx];
     if (old->name != XKB_ATOM_NONE) {
         if (report) {
             const xkb_atom_t use = (replace ? new->name : old->name);
@@ -140,7 +139,7 @@ AddLedName(KeyNamesInfo *info, enum merge_mode merge, bool same_file,
         return true;
     }
 
-    darray_item(info->led_names, new_idx) = *new;
+    *old = *new;
     return true;
 }
 
@@ -150,7 +149,6 @@ ClearKeyNamesInfo(KeyNamesInfo *info)
     free(info->name);
     darray_free(info->key_names);
     darray_free(info->aliases);
-    darray_free(info->led_names);
 }
 
 static void
@@ -308,15 +306,16 @@ MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
     }
 
     /* Merge LED names. */
-    if (darray_empty(into->led_names)) {
-        into->led_names = from->led_names;
-        darray_init(from->led_names);
+    if (into->num_led_names == 0) {
+        memcpy(into->led_names, from->led_names,
+               sizeof(*from->led_names) * from->num_led_names);
+        into->num_led_names = from->num_led_names;
+        from->num_led_names = 0;
     }
     else {
-        xkb_led_index_t idx;
-        LedNameInfo *ledi;
+        for (xkb_led_index_t idx = 0; idx < from->num_led_names; idx++) {
+            LedNameInfo *ledi = &from->led_names[idx];
 
-        darray_enumerate(idx, ledi, from->led_names) {
             if (ledi->name == XKB_ATOM_NONE)
                 continue;
 
@@ -528,8 +527,6 @@ static bool
 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
 {
     xkb_keycode_t kc;
-    xkb_led_index_t idx;
-    LedNameInfo *ledi;
     AliasInfo *alias;
     unsigned i;
 
@@ -600,10 +597,15 @@ CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
     }
 
     /* Copy LED names. */
-    darray_resize0(keymap->leds, darray_size(info->led_names));
-    darray_enumerate(idx, ledi, info->led_names)
-        if (ledi->name != XKB_ATOM_NONE)
-            darray_item(keymap->leds, idx).name = ledi->name;
+    keymap->num_leds = info->num_led_names;
+    for (xkb_led_index_t idx = 0; idx < info->num_led_names; idx++) {
+        LedNameInfo *ledi = &info->led_names[idx];
+
+        if (ledi->name == XKB_ATOM_NONE)
+            continue;
+
+        keymap->leds[idx].name = ledi->name;
+    }
 
     return true;
 }
diff --git a/src/xkbcomp/keymap-dump.c b/src/xkbcomp/keymap-dump.c
index 43f1642..8ebd475 100644
--- a/src/xkbcomp/keymap-dump.c
+++ b/src/xkbcomp/keymap-dump.c
@@ -172,7 +172,7 @@ write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
                   KeyNameText(keymap->ctx, key->name), key->keycode);
     }
 
-    darray_enumerate(idx, led, keymap->leds)
+    xkb_leds_enumerate(idx, led, keymap)
         if (led->name != XKB_ATOM_NONE)
             write_buf(buf, "\tindicator %u = \"%s\";\n",
                       idx + 1, xkb_atom_text(keymap->ctx, led->name));
@@ -441,7 +441,7 @@ write_compat(struct xkb_keymap *keymap, struct buf *buf)
         write_buf(buf, "\t};\n");
     }
 
-    darray_foreach(led, keymap->leds)
+    xkb_leds_foreach(led, keymap)
         if (led->which_groups || led->groups || led->which_mods ||
             led->mods.mods || led->ctrls)
             write_led_map(keymap, buf, led);
diff --git a/src/xkbcomp/keymap.c b/src/xkbcomp/keymap.c
index acba662..d846fa8 100644
--- a/src/xkbcomp/keymap.c
+++ b/src/xkbcomp/keymap.c
@@ -180,10 +180,10 @@ ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
 static bool
 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
 {
+    struct xkb_key *key;
     struct xkb_mod *mod;
     struct xkb_led *led;
     unsigned int i, j;
-    struct xkb_key *key;
 
     /* Find all the interprets for the key and bind them to actions,
      * which will also update the vmodmap. */
@@ -215,7 +215,7 @@ UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
                                  key->modmap);
 
     /* Update vmod -> led maps. */
-    darray_foreach(led, keymap->leds)
+    xkb_leds_foreach(led, keymap)
         ComputeEffectiveMask(keymap, &led->mods);
 
     /* Find maximum number of groups out of all keys in the keymap. */