shared: refactor nm_utils_mem_all_zero() to use memcmp()

This commit is contained in:
Thomas Haller 2018-12-27 14:22:59 +01:00
parent aab3e14883
commit af67b4520f
2 changed files with 24 additions and 13 deletions

View file

@ -2317,3 +2317,26 @@ nm_utils_getpagesize (void)
return v;
}
gboolean
nm_utils_mem_all_zero (gconstpointer data, gsize length)
{
const unsigned char *p = data;
int len;
/* Taken from https://github.com/rustyrussell/ccan/blob/9d2d2c49f053018724bcc6e37029da10b7c3d60d/ccan/mem/mem.c#L92,
* CC-0 licensed. */
/* Check first 16 bytes manually */
for (len = 0; len < 16; len++) {
if (!length)
return TRUE;
if (*p)
return FALSE;
p++;
length--;
}
/* Now we know that's zero, memcmp with self. */
return memcmp (data, p, length) == 0;
}

View file

@ -219,19 +219,7 @@ nm_ip_addr_set (int addr_family, gpointer dst, gconstpointer src)
/*****************************************************************************/
static inline gboolean
nm_utils_mem_all_zero (gconstpointer mem, gsize len)
{
const guint8 *p;
for (p = mem; len-- > 0; p++) {
if (*p != 0)
return FALSE;
}
/* incidentally, a buffer with len==0, is also *all-zero*. */
return TRUE;
}
gboolean nm_utils_mem_all_zero (gconstpointer data, gsize length);
/*****************************************************************************/