X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fdarray.h;h=de659ccadc873515494d0616e452eb5985999188;hb=b900faf77b5d07805f10a1160cd1c6842cc46b11;hp=d3fe37b089cb0353529fdd801f7f47d2a6683ade;hpb=d3d55f1c4e8fbb91b205cfb147c2ebea66fcecb4;p=platform%2Fupstream%2Flibxkbcommon.git diff --git a/src/darray.h b/src/darray.h index d3fe37b..de659cc 100644 --- a/src/darray.h +++ b/src/darray.h @@ -23,7 +23,7 @@ #ifndef CCAN_DARRAY_H #define CCAN_DARRAY_H -/* Originally taken from: http://ccodearchive.net/info/darray.html +/* Originally taken from: https://ccodearchive.net/info/darray.html * But modified for libxkbcommon. */ #include @@ -44,6 +44,13 @@ darray_init(arr); \ } while (0) +#define darray_steal(arr, to, to_size) do { \ + *(to) = (arr).item; \ + if (to_size) \ + *(unsigned int *) (to_size) = (arr).size; \ + darray_init(arr); \ +} while (0) + /* * Typedefs for darrays of common types. These are useful * when you want to pass a pointer to an darray(T) around. @@ -78,7 +85,6 @@ typedef darray (unsigned long) darray_ulong; #define darray_item(arr, i) ((arr).item[i]) #define darray_size(arr) ((arr).size) #define darray_empty(arr) ((arr).size == 0) -#define darray_mem(arr, offset) ((arr).item + (offset)) /*** Insertion (single item) ***/ @@ -98,12 +104,16 @@ typedef darray (unsigned long) darray_ulong; #define darray_from_items(arr, items, count) do { \ unsigned __count = (count); \ darray_resize(arr, __count); \ - memcpy((arr).item, items, __count * sizeof(*(arr).item)); \ + if (__count != 0) \ + memcpy((arr).item, items, __count * sizeof(*(arr).item)); \ } while (0) #define darray_copy(arr_to, arr_from) \ darray_from_items((arr_to), (arr_from).item, (arr_from).size) +#define darray_concat(arr_to, arr_from) \ + darray_append_items((arr_to), (arr_from).item, (arr_from).size) + /*** String buffer ***/ #define darray_append_string(arr, str) do { \ @@ -160,6 +170,12 @@ typedef darray (unsigned long) darray_ulong; sizeof(*(arr).item))); \ } while (0) +#define darray_shrink(arr) do { \ + if ((arr).size > 0) \ + (arr).item = realloc((arr).item, \ + ((arr).alloc = (arr).size) * sizeof(*(arr).item)); \ +} while (0) + static inline unsigned darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize) { @@ -191,6 +207,6 @@ darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize) (idx)++, (val)++) #define darray_foreach_reverse(i, arr) \ - for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; ) + for ((i) = &(arr).item[(arr).size - 1]; (arr).size > 0 && (i) >= &(arr).item[0]; (i)--) #endif /* CCAN_DARRAY_H */