1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-24 02:04:21 +03:00

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>
This commit is contained in:
Volker Lendecke 2020-03-25 22:07:44 +01:00 committed by Ralph Boehme
parent be394406ee
commit 94d580c062
2 changed files with 48 additions and 0 deletions

View File

@ -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
*/

View File

@ -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;
}