summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2020-03-25 22:07:44 +0100
committerRalph Boehme <slow@samba.org>2020-03-26 14:43:31 +0000
commit94d580c062d7a167a7493542cb746b213880358a (patch)
tree490759fa4c6cfe2d358a8dd5d09005377d7bd35e /lib
parentbe394406ee016c6bab75fcd96417554468cf495d (diff)
downloadsamba-94d580c062d7a167a7493542cb746b213880358a.tar.gz
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 <vl@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/replace/replace.h6
-rw-r--r--lib/replace/tests/testsuite.c42
2 files changed, 48 insertions, 0 deletions
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
@@ -850,6 +850,12 @@ typedef unsigned long long ptrdiff_t ;
#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
*/
#define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
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;
}