Instead of a single locate() function hardcoding references to a fixed array, we declare a generic function taking references to the array and its metadata. Declaring an array produces a lightweight wrapper for this specific array, not modifying calling API any further and keeping it readable. This will allow to declare several arrays of the same type without causing duplication of the locate function. Signed-off-by: Yann Dirson <ydirson@xxxxxxxxxx> --- diffcore-rename.c | 5 +++-- sorted-array.h | 39 ++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/diffcore-rename.c b/diffcore-rename.c index 1626bdc..a146adf 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -24,8 +24,9 @@ static void rename_dst_init(struct diff_rename_dst *elem, struct diff_filespec * fill_filespec(elem->two, ref_spec->sha1, ref_spec->mode); elem->pair = NULL; } -declare_sorted_array(static, struct diff_rename_dst, rename_dst, - rename_dst_cmp, rename_dst_init) +declare_sorted_array_type(static, struct diff_rename_dst, rename_dst, + rename_dst_cmp, rename_dst_init); +declare_sorted_array(static, struct diff_rename_dst, rename_dst, rename_dst); /* Table of rename/copy src files */ static struct diff_rename_src { diff --git a/sorted-array.h b/sorted-array.h index 03d5d1e..54fad20 100644 --- a/sorted-array.h +++ b/sorted-array.h @@ -1,15 +1,15 @@ -#define declare_sorted_array(MAYBESTATIC,ELEMTYPE,LIST,CMP,INIT) \ -MAYBESTATIC ELEMTYPE *LIST; \ -MAYBESTATIC int LIST##_nr, LIST##_alloc; \ -MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \ +#define declare_sorted_array_type(MAYBESTATIC,ELEMTYPE,TYPENICK,CMP,INIT) \ +MAYBESTATIC ELEMTYPE *locate_type_##TYPENICK( \ + ELEMTYPE **list_p, int *list_nr_p, int *list_nr_alloc, \ + void *data, int insert_ok) \ { \ int first, last; \ \ first = 0; \ - last = LIST##_nr; \ + last = *list_nr_p; \ while (last > first) { \ int next = (last + first) >> 1; \ - ELEMTYPE *nextelem = &(LIST[next]); \ + ELEMTYPE *nextelem = &((*list_p)[next]); \ int cmp = CMP(data, nextelem); \ if (!cmp) \ return nextelem; \ @@ -23,14 +23,23 @@ MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \ if (!insert_ok) \ return NULL; \ /* insert to make it at "first" */ \ - if (LIST##_alloc <= LIST##_nr) { \ - LIST##_alloc = alloc_nr(LIST##_alloc); \ - LIST = xrealloc(LIST, LIST##_alloc * sizeof(*LIST)); \ + if (*list_nr_alloc <= *list_nr_p) { \ + (*list_nr_alloc) = alloc_nr((*list_nr_alloc)); \ + *list_p = xrealloc(*list_p, (*list_nr_alloc) * sizeof(**list_p)); \ } \ - LIST##_nr++; \ - if (first < LIST##_nr) \ - memmove(LIST + first + 1, LIST + first, \ - (LIST##_nr - first - 1) * sizeof(*LIST)); \ - INIT(&LIST[first], data); \ - return &(LIST[first]); \ + (*list_nr_p)++; \ + if (first < *list_nr_p) \ + memmove(*list_p + first + 1, *list_p + first, \ + (*list_nr_p - first - 1) * sizeof(**list_p)); \ + INIT(&(*list_p)[first], data); \ + return &((*list_p)[first]); \ +} + +#define declare_sorted_array(MAYBESTATIC,ELEMTYPE,TYPENICK,LIST) \ +MAYBESTATIC ELEMTYPE *LIST; \ +MAYBESTATIC int LIST##_nr, LIST##_alloc; \ +MAYBESTATIC ELEMTYPE *locate_##LIST(void *data, int insert_ok) \ +{ \ + return locate_type_##TYPENICK(&LIST, &LIST##_nr, &LIST##_alloc, \ + data, insert_ok); \ } -- 1.7.2.3 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html