diff options
| author | Michael Biebl <biebl@debian.org> | 2019-04-21 21:10:42 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2019-04-21 21:10:42 +0200 |
| commit | 1761e93f84aba1f3a2f0ebc3d753303f27395fed (patch) | |
| tree | 2a0aa70159a57641294d4aee7aee061bc25c0a7b /shared/systemd/src/basic/memory-util.c | |
| parent | 6cbf8459e4602877369dbf20c6597708725758c6 (diff) | |
| parent | 85563b7fc7ec2cd21e38debb9b28db342e2e8e7c (diff) | |
Update upstream source from tag 'upstream/1.18.0'
Update to upstream version '1.18.0' with Debian dir c79ce54aa2bd35bd9bd13eb01cf8687eaae2c0c1
Diffstat (limited to 'shared/systemd/src/basic/memory-util.c')
| -rw-r--r-- | shared/systemd/src/basic/memory-util.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/shared/systemd/src/basic/memory-util.c b/shared/systemd/src/basic/memory-util.c new file mode 100644 index 00000000..bd1f5d73 --- /dev/null +++ b/shared/systemd/src/basic/memory-util.c @@ -0,0 +1,59 @@ +#include "nm-sd-adapt-shared.h" + +#include <unistd.h> + +#include "memory-util.h" + +size_t page_size(void) { + static thread_local size_t pgsz = 0; + long r; + + if (_likely_(pgsz > 0)) + return pgsz; + + r = sysconf(_SC_PAGESIZE); + assert(r > 0); + + pgsz = (size_t) r; + return pgsz; +} + +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; +} + +#if !HAVE_EXPLICIT_BZERO +/* + * The pointer to memset() is volatile so that compiler must de-reference the pointer and can't assume that + * it points to any function in particular (such as memset(), which it then might further "optimize"). This + * approach is inspired by openssl's crypto/mem_clr.c. + */ +typedef void *(*memset_t)(void *,int,size_t); + +static volatile memset_t memset_func = memset; + +void* explicit_bzero_safe(void *p, size_t l) { + if (l > 0) + memset_func(p, '\0', l); + + return p; +} +#endif |