diff options
author | Daniel Stone <daniel@fooishbar.org> | 2012-07-17 10:20:15 +0100 |
---|---|---|
committer | Daniel Stone <daniel@fooishbar.org> | 2012-07-17 10:20:15 +0100 |
commit | 9308a460395679aa3cb403211146e4ff9b920992 (patch) | |
tree | a060f3f7882c8edf35312c2210d9837ac3a4c306 /src/darray.h | |
parent | c6c937abc2e7e9aa9dc952dea392329b88ab1011 (diff) | |
download | xorg-lib-libxkbcommon-9308a460395679aa3cb403211146e4ff9b920992.tar.gz |
Run source tree through uncrustify
.uncrustify.cfg committed for future reference also, but had to manually
fix up a few things: it really likes justifying struct initialisers.
Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Diffstat (limited to 'src/darray.h')
-rw-r--r-- | src/darray.h | 344 |
1 files changed, 189 insertions, 155 deletions
diff --git a/src/darray.h b/src/darray.h index 3185193..9a076ea 100644 --- a/src/darray.h +++ b/src/darray.h @@ -122,14 +122,16 @@ /*** Life cycle ***/ -#define darray(type) struct {type *item; size_t size; size_t alloc;} +#define darray(type) struct { type *item; size_t size; size_t alloc; } -#define darray_new() {0,0,0} -#define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0) -#define darray_free(arr) do {free((arr).item); darray_init(arr);} while(0) +#define darray_new() { 0, 0, 0 } +#define darray_init(arr) do { (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \ +} while (0) +#define darray_free(arr) do { free((arr).item); darray_init(arr); } while (0) /* Only use for immutable darray - e.g. for static const initialzers. */ -#define darray_lit(c_array) {(c_array), sizeof(c_array) / sizeof(*(c_array)), 0} +#define darray_lit(c_array) { (c_array), sizeof(c_array) / sizeof(*(c_array)), \ + 0 } /* * Typedefs for darrays of common types. These are useful @@ -148,153 +150,186 @@ * foo(&arr); */ -typedef darray(char) darray_char; -typedef darray(signed char) darray_schar; -typedef darray(unsigned char) darray_uchar; +typedef darray (char) darray_char; +typedef darray (signed char) darray_schar; +typedef darray (unsigned char) darray_uchar; -typedef darray(short) darray_short; -typedef darray(int) darray_int; -typedef darray(long) darray_long; - -typedef darray(unsigned short) darray_ushort; -typedef darray(unsigned int) darray_uint; -typedef darray(unsigned long) darray_ulong; +typedef darray (short) darray_short; +typedef darray (int) darray_int; +typedef darray (long) darray_long; +typedef darray (unsigned short) darray_ushort; +typedef darray (unsigned int) darray_uint; +typedef darray (unsigned long) darray_ulong; /*** Access ***/ -#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_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) ***/ -#define darray_append(arr, ...) do { \ - darray_resize(arr, (arr).size+1); \ - (arr).item[(arr).size-1] = (__VA_ARGS__); \ - } while(0) +#define darray_append(arr, ...) do { \ + darray_resize(arr, (arr).size + 1); \ + (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__) - + 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 { \ - size_t __count = (count), __oldSize = (arr).size; \ - darray_resize(arr, __oldSize + __count); \ - 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_append_items(arr, items, count) do { \ + size_t __count = (count), __oldSize = (arr).size; \ + darray_resize(arr, __oldSize + __count); \ + 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) + 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) #if HAVE_TYPEOF -#define darray_appends(arr, ...) darray_appends_t(arr, typeof((*(arr).item)), __VA_ARGS__) -#define darray_prepends(arr, ...) darray_prepends_t(arr, typeof((*(arr).item)), __VA_ARGS__) +#define darray_appends(arr, ...) darray_appends_t(arr, typeof((*(arr).item)), \ + __VA_ARGS__) +#define darray_prepends(arr, ...) darray_prepends_t(arr, typeof((*(arr).item)), \ + __VA_ARGS__) #endif -#define darray_appends_t(arr, type, ...) do { \ - type __src[] = {__VA_ARGS__}; \ - darray_append_items(arr, __src, sizeof(__src)/sizeof(*__src)); \ - } 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) - + 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(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) - +#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) /*** String buffer ***/ -#define darray_append_string(arr, str) do {const char *__str = (str); darray_append_items(arr, __str, strlen(__str)+1); (arr).size--;} while(0) -#define darray_append_lit(arr, stringLiteral) do {darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); (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_append_string(arr, str) do { const char *__str = (str); \ + darray_append_items( \ + arr, __str, \ + strlen(__str) + 1); \ + (arr).size--; \ +} while (0) +#define darray_append_lit(arr, stringLiteral) do { darray_append_items( \ + arr, stringLiteral, \ + sizeof(stringLiteral)); \ + (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--;} while(0) -#define darray_from_lit(arr, stringLiteral) do {darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); (arr).size--;} while(0) - + 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--; \ +} while (0) +#define darray_from_lit(arr, stringLiteral) do { darray_from_items( \ + arr, stringLiteral, \ + sizeof(stringLiteral)); \ + (arr).size--; } while (0) /*** Size management ***/ -#define darray_resize(arr, newSize) darray_growalloc(arr, (arr).size = (newSize)) -#define darray_resize0(arr, newSize) do { \ - size_t __oldSize = (arr).size, __newSize = (newSize); \ - (arr).size = __newSize; \ - if (__newSize > __oldSize) { \ - darray_growalloc(arr, __newSize); \ - memset(&(arr).item[__oldSize], 0, (__newSize - __oldSize) * sizeof(*(arr).item)); \ - } \ - } while(0) +#define darray_resize(arr, newSize) darray_growalloc(arr, (arr).size = \ + (newSize)) +#define darray_resize0(arr, newSize) do { \ + size_t __oldSize = (arr).size, __newSize = (newSize); \ + (arr).size = __newSize; \ + if (__newSize > __oldSize) { \ + darray_growalloc(arr, __newSize); \ + memset(&(arr).item[__oldSize], 0, \ + (__newSize - __oldSize) * sizeof(*(arr).item)); \ + } \ +} while (0) #define darray_realloc(arr, newAlloc) do { \ - (arr).item = realloc((arr).item, ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \ - } while(0) -#define darray_growalloc(arr, need) do { \ - size_t __need = (need); \ - if (__need > (arr).alloc) \ - darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \ - } while(0) - -#if HAVE_STATEMENT_EXPR==1 -#define darray_make_room(arr, room) ({size_t newAlloc = (arr).size+(room); if ((arr).alloc<newAlloc) darray_realloc(arr, newAlloc); (arr).item+(arr).size; }) + (arr).item = \ + realloc((arr).item, ((arr).alloc = (newAlloc)) * \ + sizeof(*(arr).item)); \ +} while (0) +#define darray_growalloc(arr, need) do { \ + size_t __need = (need); \ + if (__need > (arr).alloc) \ + darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \ +} while (0) + +#if HAVE_STATEMENT_EXPR == 1 +#define darray_make_room(arr, \ + room) ({ size_t newAlloc = (arr).size + (room); \ + if ((arr).alloc < \ + newAlloc) darray_realloc(arr, newAlloc); \ + (arr).item + (arr).size; }) #endif -static inline size_t darray_next_alloc(size_t alloc, size_t need) +static inline size_t +darray_next_alloc(size_t alloc, size_t need) { - if (alloc == 0) - alloc = 4; - while (alloc < need) - alloc *= 2; - return alloc; + if (alloc == 0) + alloc = 4; + while (alloc < need) + alloc *= 2; + return alloc; } - /*** Traversal ***/ /* @@ -303,7 +338,7 @@ static inline size_t darray_next_alloc(size_t alloc, size_t need) * 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)++) + for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++) /* * darray_foreach_reverse(T *&i, darray(T) arr) {...} @@ -311,53 +346,52 @@ static inline size_t darray_next_alloc(size_t alloc, size_t need) * Like darray_foreach, but traverse in reverse order. */ #define darray_foreach_reverse(i, arr) \ - for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; ) - + 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); -*/ + * + * 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); + */ |