alloc: use darray in xkb_key_names 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
diff --git a/src/alloc.c b/src/alloc.c
index a1d34bf..e95166e 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -367,20 +367,19 @@ XkbcFreeCompatMap(struct xkb_keymap *keymap)
}
int
-XkbcAllocNames(struct xkb_keymap *keymap, unsigned which,
- unsigned nTotalAliases)
+XkbcAllocNames(struct xkb_keymap *keymap, unsigned which, size_t nTotalAliases)
{
- struct xkb_names * names;
-
if (!keymap)
return BadMatch;
if (!keymap->names) {
- keymap->names = uTypedCalloc(1, struct xkb_names);
+ keymap->names = calloc(1, sizeof(*keymap->names));
if (!keymap->names)
return BadAlloc;
+
+ darray_init(keymap->names->keys);
+ darray_init(keymap->names->key_aliases);
}
- names = keymap->names;
if ((which & XkbKTLevelNamesMask) && keymap->map) {
struct xkb_key_type * type;
@@ -394,35 +393,11 @@ XkbcAllocNames(struct xkb_keymap *keymap, unsigned which,
}
}
- if ((which & XkbKeyNamesMask) && !names->keys) {
- names->keys = uTypedCalloc(keymap->max_key_code + 1,
- struct xkb_key_name);
- if (!names->keys)
- return BadAlloc;
- }
-
- if ((which & XkbKeyAliasesMask) && (nTotalAliases > 0)) {
- if (!names->key_aliases)
- names->key_aliases = uTypedCalloc(nTotalAliases,
- struct xkb_key_alias);
- else if (nTotalAliases > names->num_key_aliases) {
- struct xkb_key_alias *prev_aliases = names->key_aliases;
-
- names->key_aliases = uTypedRecalloc(names->key_aliases,
- names->num_key_aliases,
- nTotalAliases,
- struct xkb_key_alias);
- if (!names->key_aliases)
- free(prev_aliases);
- }
-
- if (!names->key_aliases) {
- names->num_key_aliases = 0;
- return BadAlloc;
- }
+ if (which & XkbKeyNamesMask)
+ darray_resize0(keymap->names->keys, keymap->max_key_code + 1);
- names->num_key_aliases = nTotalAliases;
- }
+ if (which & XkbKeyAliasesMask)
+ darray_resize0(keymap->names->key_aliases, nTotalAliases);
return Success;
}
@@ -458,8 +433,8 @@ XkbcFreeNames(struct xkb_keymap *keymap)
for (i = 0; i < XkbNumKbdGroups; i++)
free(UNCONSTIFY(names->groups[i]));
- free(names->keys);
- free(names->key_aliases);
+ darray_free(names->keys);
+ darray_free(names->key_aliases);
free(names);
keymap->names = NULL;
}
diff --git a/src/alloc.h b/src/alloc.h
index c9a3314..0e94682 100644
--- a/src/alloc.h
+++ b/src/alloc.h
@@ -34,7 +34,7 @@ XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI);
extern int
XkbcAllocNames(struct xkb_keymap *keymap, unsigned which,
- unsigned nTotalAliases);
+ size_t nTotalAliases);
extern int
XkbcAllocControls(struct xkb_keymap *keymap);
diff --git a/src/xkb-priv.h b/src/xkb-priv.h
index 876dbfd..4fa85c1 100644
--- a/src/xkb-priv.h
+++ b/src/xkb-priv.h
@@ -330,11 +330,9 @@ struct xkb_names {
const char *vmods[XkbNumVirtualMods];
const char *indicators[XkbNumIndicators];
const char *groups[XkbNumKbdGroups];
- struct xkb_key_name * keys;
- struct xkb_key_alias * key_aliases;
- xkb_keycode_t num_keys;
- xkb_keycode_t num_key_aliases;
+ darray(struct xkb_key_name) keys;
+ darray(struct xkb_key_alias) key_aliases;
};
struct xkb_controls {
diff --git a/src/xkbcomp/alias.c b/src/xkbcomp/alias.c
index 2b05646..ae1ebdc 100644
--- a/src/xkbcomp/alias.c
+++ b/src/xkbcomp/alias.c
@@ -157,8 +157,8 @@ ApplyAliases(struct xkb_keymap *keymap, AliasInfo ** info_in)
if (*info_in == NULL)
return true;
- nOld = (keymap->names ? keymap->names->num_key_aliases : 0);
- old = (keymap->names ? keymap->names->key_aliases : NULL);
+ nOld = (keymap->names ? darray_size(keymap->names->key_aliases) : 0);
+ old = (keymap->names ? &darray_item(keymap->names->key_aliases, 0) : NULL);
for (nNew = 0, info = *info_in; info != NULL;
info = (AliasInfo *) info->def.next)
{
@@ -221,7 +221,7 @@ ApplyAliases(struct xkb_keymap *keymap, AliasInfo ** info_in)
WSGO("Allocation failure in ApplyAliases\n");
return false;
}
- a = keymap->names ? &keymap->names->key_aliases[nOld] : NULL;
+ a = keymap->names ? &darray_item(keymap->names->key_aliases, nOld) : NULL;
for (info = *info_in; info != NULL; info = (AliasInfo *) info->def.next)
{
if (info->alias[0] != '\0')
diff --git a/src/xkbcomp/keycodes.c b/src/xkbcomp/keycodes.c
index cca2688..c5cd613 100644
--- a/src/xkbcomp/keycodes.c
+++ b/src/xkbcomp/keycodes.c
@@ -897,7 +897,8 @@ CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
== Success) {
uint64_t i;
for (i = info.computedMin; i <= info.computedMax; i++)
- LongToKeyName(info.names[i], keymap->names->keys[i].name);
+ LongToKeyName(info.names[i],
+ darray_item(keymap->names->keys, i).name);
} else {
WSGO("Cannot create struct xkb_names in CompileKeycodes\n");
goto err_info;
diff --git a/src/xkbcomp/misc.c b/src/xkbcomp/misc.c
index e29c9ae..5ff1e65 100644
--- a/src/xkbcomp/misc.c
+++ b/src/xkbcomp/misc.c
@@ -244,12 +244,12 @@ FindNamedKey(struct xkb_keymap *keymap, unsigned long name,
}
*kc_rtrn = 0; /* some callers rely on this */
- if (keymap && keymap->names && keymap->names->keys)
+ if (keymap && keymap->names && !darray_empty(keymap->names->keys))
{
for (n = start_from; n <= keymap->max_key_code; n++)
{
unsigned long tmp;
- tmp = KeyNameToLong(keymap->names->keys[n].name);
+ tmp = KeyNameToLong(darray_item(keymap->names->keys, n).name);
if (tmp == name)
{
*kc_rtrn = n;
@@ -266,7 +266,7 @@ FindNamedKey(struct xkb_keymap *keymap, unsigned long name,
}
if (create)
{
- if ((!keymap->names) || (!keymap->names->keys))
+ if ((!keymap->names) || darray_empty(keymap->names->keys))
{
if (XkbcAllocNames(keymap, XkbKeyNamesMask, 0) != Success)
{
@@ -282,11 +282,12 @@ FindNamedKey(struct xkb_keymap *keymap, unsigned long name,
/* Find first unused keycode and store our key here */
for (n = keymap->min_key_code; n <= keymap->max_key_code; n++)
{
- if (keymap->names->keys[n].name[0] == '\0')
+ if (darray_item(keymap->names->keys, n).name[0] == '\0')
{
char buf[XkbKeyNameLength + 1];
LongToKeyName(name, buf);
- memcpy(keymap->names->keys[n].name, buf, XkbKeyNameLength);
+ memcpy(darray_item(keymap->names->keys, n).name, buf,
+ XkbKeyNameLength);
*kc_rtrn = n;
return true;
}
@@ -299,19 +300,14 @@ bool
FindKeyNameForAlias(struct xkb_keymap *keymap, unsigned long lname,
unsigned long *real_name)
{
- unsigned int i;
char name[XkbKeyNameLength + 1];
+ struct xkb_key_alias *a;
- if (keymap && keymap->names && keymap->names->key_aliases)
- {
- struct xkb_key_alias * a;
- a = keymap->names->key_aliases;
+ if (keymap && keymap->names) {
LongToKeyName(lname, name);
name[XkbKeyNameLength] = '\0';
- for (i = 0; i < keymap->names->num_key_aliases; i++, a++)
- {
- if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
- {
+ darray_foreach(a, keymap->names->key_aliases) {
+ if (strncmp(name, a->alias, XkbKeyNameLength) == 0) {
*real_name = KeyNameToLong(a->real);
return true;
}
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index 3ae10cf..8b371b9 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -2236,12 +2236,12 @@ CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
if (warningLevel > 3) {
for (i = keymap->min_key_code; i <= keymap->max_key_code; i++) {
- if (keymap->names->keys[i].name[0] == '\0')
+ if (darray_item(keymap->names->keys, i).name[0] == '\0')
continue;
if (XkbKeyNumGroups(keymap, i) < 1) {
char buf[5];
- memcpy(buf, keymap->names->keys[i].name, 4);
+ memcpy(buf, darray_item(keymap->names->keys, i).name, 4);
buf[4] = '\0';
WARN("No symbols defined for <%s> (keycode %d)\n", buf, i);
}