]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/util: import memeqzero from casync
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 18 Nov 2018 09:42:39 +0000 (10:42 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 18 Nov 2018 15:12:53 +0000 (16:12 +0100)
src/basic/util.c
src/basic/util.h
src/test/test-util.c

index cd75529cfe38981e2c7a3f03df6f465825c5344d..00052c5e1dea00417e35c3da7a42fa833e2ea83d 100644 (file)
@@ -165,6 +165,28 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
         return NULL;
 }
 
+bool memeqzero(const void *data, size_t length) {
+        /* Does the buffer consist entirely of NULs?
+         * Copied from https://github.com/systemd/casync/, copied in turn from
+         * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
+         * which is licensed CC-0.
+         */
+
+        const uint8_t *p = data;
+        size_t i;
+
+        /* Check first 16 bytes manually */
+        for (i = 0; i < 16; i++, length--) {
+                if (length == 0)
+                        return true;
+                if (p[i])
+                        return false;
+        }
+
+        /* Now we know first 16 bytes are NUL, memcmp with self.  */
+        return memcmp(data, p + i, length) == 0;
+}
+
 int on_ac_power(void) {
         bool found_offline = false, found_online = false;
         _cleanup_closedir_ DIR *d = NULL;
index 8cba4ed7260e2380a9a46f7d098aa4445bee5eeb..fd180cb78741b2e7470dca59a50a6989ff0064ec 100644 (file)
@@ -159,6 +159,10 @@ int on_ac_power(void);
 
 #define zero(x) (memzero(&(x), sizeof(x)))
 
+bool memeqzero(const void *data, size_t length);
+
+#define eqzero(x) memeqzero(x, sizeof(x))
+
 static inline void *mempset(void *s, int c, size_t n) {
         memset(s, c, n);
         return (uint8_t*)s + n;
index 80856e638886bcf454c63695277a4badf4253976..eeee09a7d2b1cb55b523b3c86a41cf306a4b0e9e 100644 (file)
@@ -237,6 +237,20 @@ static void test_log2i(void) {
         assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
 }
 
+static void test_eqzero(void) {
+        const uint32_t zeros[] = {0, 0, 0};
+        const uint32_t ones[] = {1, 1};
+        const uint32_t mixed[] = {0, 1, 0, 0, 0};
+        const uint8_t longer[] = {[55] = 255};
+
+        log_info("/* %s */", __func__);
+
+        assert_se(eqzero(zeros));
+        assert_se(!eqzero(ones));
+        assert_se(!eqzero(mixed));
+        assert_se(!eqzero(longer));
+}
+
 static void test_raw_clone(void) {
         pid_t parent, pid, pid2;
 
@@ -370,6 +384,7 @@ int main(int argc, char *argv[]) {
         test_protect_errno();
         test_in_set();
         test_log2i();
+        test_eqzero();
         test_raw_clone();
         test_physical_memory();
         test_physical_memory_scale();