From: Volker Lendecke Date: Wed, 25 Mar 2020 21:07:44 +0000 (+0100) Subject: lib: Add macro ARRAY_DEL_ELEMENT() X-Git-Url: http://git.samba.org/samba.git/?a=commitdiff_plain;h=94d580c062d7a167a7493542cb746b213880358a;p=bbaumbach%2Fsamba-autobuild%2F.git lib: Add macro ARRAY_DEL_ELEMENT() Every time I have to remove an element from within an array I have to scratch my head about the memmove arguments. Make this easier to use. Signed-off-by: Volker Lendecke Reviewed-by: Ralph Boehme --- diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 177be37edbe..59f0b60f8a0 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -849,6 +849,12 @@ typedef unsigned long long ptrdiff_t ; #endif #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) +/** + * Remove an array element by moving the rest one down + */ +#define ARRAY_DEL_ELEMENT(a,i,n) \ +if((i)<((n)-1)){memmove(&((a)[(i)]),&((a)[(i)+1]),(sizeof(*(a))*((n)-(i)-1)));} + /** * Pointer difference macro */ diff --git a/lib/replace/tests/testsuite.c b/lib/replace/tests/testsuite.c index 7f9801e9f98..2ece95332d2 100644 --- a/lib/replace/tests/testsuite.c +++ b/lib/replace/tests/testsuite.c @@ -1095,6 +1095,47 @@ static bool test_closefrom(void) return true; } +static bool test_array_del_element(void) +{ + int a[] = { 1,2,3,4,5 }; + + printf("test: array_del_element\n"); + + ARRAY_DEL_ELEMENT(a, 4, ARRAY_SIZE(a)); + + if ((a[0] != 1) || + (a[1] != 2) || + (a[2] != 3) || + (a[3] != 4) || + (a[4] != 5)) { + return false; + } + + ARRAY_DEL_ELEMENT(a, 0, ARRAY_SIZE(a)); + + if ((a[0] != 2) || + (a[1] != 3) || + (a[2] != 4) || + (a[3] != 5) || + (a[4] != 5)) { + return false; + } + + ARRAY_DEL_ELEMENT(a, 2, ARRAY_SIZE(a)); + + if ((a[0] != 2) || + (a[1] != 3) || + (a[2] != 5) || + (a[3] != 5) || + (a[4] != 5)) { + return false; + } + + printf("success: array_del_element\n"); + + return true; +} + bool torture_local_replace(struct torture_context *ctx) { bool ret = true; @@ -1145,6 +1186,7 @@ bool torture_local_replace(struct torture_context *ctx) ret &= test_utimes(); ret &= test_memmem(); ret &= test_closefrom(); + ret &= test_array_del_element(); return ret; }