darray: cleanup We have quite diverged from the upstream file, so let's make it at least easier to look at. Remove some unused macros and rename some for consistency. 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
diff --git a/src/darray.h b/src/darray.h
index 0cf3747..b6ad2cf 100644
--- a/src/darray.h
+++ b/src/darray.h
@@ -26,89 +26,6 @@
#include <stdlib.h>
#include <string.h>
-/*
- * SYNOPSIS
- *
- * Life cycle of a darray (dynamically-allocated array):
- *
- * darray(int) a = darray_new();
- * darray_free(a);
- *
- * struct {darray(int) a;} foo;
- * darray_init(foo.a);
- * darray_free(foo.a);
- *
- * Typedefs for darrays of common types:
- *
- * darray_char, darray_schar, darray_uchar
- * darray_short, darray_int, darray_long
- * darray_ushort, darray_uint, darray_ulong
- *
- * Access:
- *
- * T darray_item(darray(T) arr, size_t index);
- * size_t darray_size(darray(T) arr);
- * size_t darray_alloc(darray(T) arr);
- * bool darray_empty(darray(T) arr);
- *
- * // Access raw memory, starting from the item in offset.
- * // Not safe, be careful, etc.
- * T* darray_mem(darray(T) arr, size_t offset);
- *
- * Insertion (single item):
- *
- * void darray_append(darray(T) arr, T item);
- * void darray_prepend(darray(T) arr, T item);
- * void darray_push(darray(T) arr, T item); // same as darray_append
- *
- * Insertion (multiple items):
- *
- * void darray_append_items(darray(T) arr, T *items, size_t count);
- * void darray_prepend_items(darray(T) arr, T *items, size_t count);
- *
- * void darray_appends(darray(T) arr, [T item, [...]]);
- * void darray_prepends(darray(T) arr, [T item, [...]]);
- *
- * Removal:
- *
- * T darray_pop(darray(T) arr | darray_size(arr) != 0);
- * T* darray_pop_check(darray(T*) arr);
- *
- * Replacement:
- *
- * void darray_from_items(darray(T) arr, T *items, size_t count);
- * void darray_from_c(darray(T) arr, T c_array[N]);
- *
- * String buffer:
- *
- * void darray_append_string(darray(char) arr, const char *str);
- * void darray_append_lit(darray(char) arr, char stringLiteral[N+1]);
- *
- * void darray_prepend_string(darray(char) arr, const char *str);
- * void darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]);
- *
- * void darray_from_string(darray(T) arr, const char *str);
- * void darray_from_lit(darray(char) arr, char stringLiteral[N+1]);
- *
- * Size management:
- *
- * void darray_resize(darray(T) arr, size_t newSize);
- * void darray_resize0(darray(T) arr, size_t newSize);
- *
- * void darray_realloc(darray(T) arr, size_t newAlloc);
- * void darray_growalloc(darray(T) arr, size_t newAlloc);
- *
- * Traversal:
- *
- * darray_foreach(T *&i, darray(T) arr) {...}
- * darray_foreach_reverse(T *&i, darray(T) arr) {...}
- *
- * Except for darray_foreach and darray_foreach_reverse,
- * all macros evaluate their non-darray arguments only once.
- */
-
-/*** Life cycle ***/
-
#define darray(type) struct { type *item; size_t size; size_t alloc; }
#define darray_new() { 0, 0, 0 }
@@ -118,7 +35,8 @@
} while (0)
#define darray_free(arr) do { \
- free((arr).item); darray_init(arr); \
+ free((arr).item); \
+ darray_init(arr); \
} while (0)
/*
@@ -154,11 +72,8 @@ typedef darray (unsigned long) darray_ulong;
#define darray_item(arr, i) ((arr).item[i])
#define darray_size(arr) ((arr).size)
-#define darray_alloc(arr) ((arr).alloc)
#define darray_empty(arr) ((arr).size == 0)
-
#define darray_mem(arr, offset) ((arr).item + (offset))
-#define darray_same(arr1, arr2) ((arr1).item == (arr2).item)
/*** Insertion (single item) ***/
@@ -167,15 +82,6 @@ typedef darray (unsigned long) darray_ulong;
(arr).item[(arr).size - 1] = (__VA_ARGS__); \
} while (0)
-#define darray_prepend(arr, ...) do { \
- darray_resize(arr, (arr).size + 1); \
- memmove((arr).item + 1, (arr).item, \
- ((arr).size - 1) * sizeof(*(arr).item)); \
- (arr).item[0] = (__VA_ARGS__); \
-} while (0)
-
-#define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
-
/*** Insertion (multiple items) ***/
#define darray_append_items(arr, items, count) do { \
@@ -184,57 +90,12 @@ typedef darray (unsigned long) darray_ulong;
memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
} while (0)
-#define darray_prepend_items(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
- darray_resize(arr, __count + __oldSize); \
- memmove((arr).item + __count, (arr).item, \
- __oldSize * sizeof(*(arr).item)); \
- memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
-} while (0)
-
-#define darray_append_items_nullterminate(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
- darray_resize(arr, __oldSize + __count + 1); \
- memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
- (arr).item[--(arr).size] = 0; \
-} while (0)
-
-#define darray_prepend_items_nullterminate(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
- darray_resize(arr, __count + __oldSize + 1); \
- memmove((arr).item + __count, (arr).item, \
- __oldSize * sizeof(*(arr).item)); \
- memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
- (arr).item[--(arr).size] = 0; \
-} while (0)
-
-#define darray_appends_t(arr, type, ...) do { \
- type __src[] = { __VA_ARGS__ }; \
- darray_append_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
-} while (0)
-
-#define darray_prepends_t(arr, type, ...) do { \
- type __src[] = { __VA_ARGS__ }; \
- darray_prepend_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
-} while (0)
-
-/*** Removal ***/
-
-/* Warning: Do not call darray_pop on an empty darray. */
-#define darray_pop(arr) ((arr).item[--(arr).size])
-#define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
-
-/*** Replacement ***/
-
#define darray_from_items(arr, items, count) do { \
size_t __count = (count); \
darray_resize(arr, __count); \
memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
} while (0)
-#define darray_from_c(arr, c_array) \
- darray_from_items(arr, c_array, sizeof(c_array) / sizeof(*(c_array)))
-
#define darray_copy(arr_to, arr_from) \
darray_from_items((arr_to), (arr_from).item, (arr_from).size)
@@ -251,24 +112,20 @@ typedef darray (unsigned long) darray_ulong;
(arr).size--; \
} while (0)
-#define darray_prepend_string(arr, str) do { \
- const char *__str = (str); \
- darray_prepend_items_nullterminate(arr, __str, strlen(__str)); \
-} while (0)
-
-#define darray_prepend_lit(arr, stringLiteral) \
- darray_prepend_items_nullterminate(arr, stringLiteral, \
- sizeof(stringLiteral) - 1)
-
-#define darray_from_string(arr, str) do { \
- const char *__str = (str); \
- darray_from_items(arr, __str, strlen(__str) + 1); \
- (arr).size--; \
+#define darray_appends_nullterminate(arr, items, count) do { \
+ size_t __count = (count), __oldSize = (arr).size; \
+ darray_resize(arr, __oldSize + __count + 1); \
+ memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
+ (arr).item[--(arr).size] = 0; \
} while (0)
-#define darray_from_lit(arr, stringLiteral) do { \
- darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); \
- (arr).size--; \
+#define darray_prepends_nullterminate(arr, items, count) do { \
+ size_t __count = (count), __oldSize = (arr).size; \
+ darray_resize(arr, __count + __oldSize + 1); \
+ memmove((arr).item + __count, (arr).item, \
+ __oldSize * sizeof(*(arr).item)); \
+ memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
+ (arr).item[--(arr).size] = 0; \
} while (0)
/*** Size management ***/
@@ -309,11 +166,6 @@ darray_next_alloc(size_t alloc, size_t need)
/*** Traversal ***/
-/*
- * darray_foreach(T *&i, darray(T) arr) {...}
- *
- * Traverse a darray. `i` must be declared in advance as a pointer to an item.
- */
#define darray_foreach(i, arr) \
for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
@@ -331,58 +183,7 @@ darray_next_alloc(size_t alloc, size_t need)
(idx) < (arr).size; \
(idx)++, (val)++)
-/*
- * darray_foreach_reverse(T *&i, darray(T) arr) {...}
- *
- * Like darray_foreach, but traverse in reverse order.
- */
#define darray_foreach_reverse(i, arr) \
for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
#endif /* CCAN_DARRAY_H */
-
-/*
- *
- * darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items;
- * if not, it increases the alloc to satisfy this requirement, allocating slack
- * space to avoid having to reallocate for every size increment.
- *
- * darray_from_string(arr, str) copies a string to an darray_char.
- *
- * darray_push(arr, item) pushes an item to the end of the darray.
- * darray_pop(arr) pops it back out. Be sure there is at least one item in the darray before calling.
- * darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
- *
- * darray_make_room(arr, room) ensures there's 'room' elements of space after the end of the darray, and it returns a pointer to this space.
- * Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
- *
- * The following require HAVE_TYPEOF==1 :
- *
- * darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
- * darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
- *
- *
- * Examples:
- *
- * darray(int) arr;
- * int *i;
- *
- * darray_appends(arr, 0,1,2,3,4);
- * darray_appends(arr, -5,-4,-3,-2,-1);
- * darray_foreach(i, arr)
- * printf("%d ", *i);
- * printf("\n");
- *
- * darray_free(arr);
- *
- *
- * typedef struct {int n,d;} Fraction;
- * darray(Fraction) fractions;
- * Fraction *i;
- *
- * darray_appends(fractions, {3,4}, {3,5}, {2,1});
- * darray_foreach(i, fractions)
- * printf("%d/%d\n", i->n, i->d);
- *
- * darray_free(fractions);
- */
diff --git a/src/keymap-priv.c b/src/keymap-priv.c
index 9f42040..2b3f8cd 100644
--- a/src/keymap-priv.c
+++ b/src/keymap-priv.c
@@ -30,12 +30,7 @@ static void
update_builtin_keymap_fields(struct xkb_keymap *keymap)
{
struct xkb_context *ctx = keymap->ctx;
-
- /*
- * Add predefined (AKA real, core, X11) modifiers.
- * The order is important!
- */
- darray_appends_t(keymap->mods, struct xkb_mod,
+ const struct xkb_mod builtin_mods[] = {
{ .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 },
@@ -43,7 +38,14 @@ update_builtin_keymap_fields(struct xkb_keymap *keymap)
{ .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 });
+ { .name = xkb_atom_intern_literal(ctx, "Mod5"), .type = MOD_REAL },
+ };
+
+ /*
+ * Add predefined (AKA real, core, X11) modifiers.
+ * The order is important!
+ */
+ darray_append_items(keymap->mods, builtin_mods, ARRAY_SIZE(builtin_mods));
}
struct xkb_keymap *
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index de82d96..955bc00 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -731,7 +731,7 @@ append_expanded_kccgst_value(struct matcher *m, darray_char *to,
*/
if (!darray_empty(*to) && s[0] != '+' && s[0] != '|') {
if (darray_item(*to, 0) == '+' || darray_item(*to, 0) == '|')
- darray_prepend_items_nullterminate(*to, value.start, value.len);
+ darray_prepends_nullterminate(*to, value.start, value.len);
return true;
}
@@ -748,7 +748,7 @@ append_expanded_kccgst_value(struct matcher *m, darray_char *to,
/* Check if that's a start of an expansion. */
if (s[i] != '%') {
/* Just a normal character. */
- darray_append_items_nullterminate(*to, &s[i++], 1);
+ darray_appends_nullterminate(*to, &s[i++], 1);
continue;
}
if (++i >= value.len) goto error;
@@ -824,10 +824,10 @@ append_expanded_kccgst_value(struct matcher *m, darray_char *to,
continue;
if (pfx != 0)
- darray_append_items_nullterminate(*to, &pfx, 1);
- darray_append_items_nullterminate(*to, expanded.start, expanded.len);
+ darray_appends_nullterminate(*to, &pfx, 1);
+ darray_appends_nullterminate(*to, expanded.start, expanded.len);
if (sfx != 0)
- darray_append_items_nullterminate(*to, &sfx, 1);
+ darray_appends_nullterminate(*to, &sfx, 1);
}
return true;