]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/memory-util.c
3 #include "memory-util.h"
5 size_t page_size(void) {
6 static thread_local
size_t pgsz
= 0;
9 if (_likely_(pgsz
> 0))
12 r
= sysconf(_SC_PAGESIZE
);
19 bool memeqzero(const void *data
, size_t length
) {
20 /* Does the buffer consist entirely of NULs?
21 * Copied from https://github.com/systemd/casync/, copied in turn from
22 * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
23 * which is licensed CC-0.
26 const uint8_t *p
= data
;
29 /* Check first 16 bytes manually */
30 for (i
= 0; i
< 16; i
++, length
--) {
37 /* Now we know first 16 bytes are NUL, memcmp with self. */
38 return memcmp(data
, p
+ i
, length
) == 0;
41 #if !HAVE_EXPLICIT_BZERO
43 * The pointer to memset() is volatile so that compiler must de-reference the pointer and can't assume that
44 * it points to any function in particular (such as memset(), which it then might further "optimize"). This
45 * approach is inspired by openssl's crypto/mem_clr.c.
47 typedef void *(*memset_t
)(void *,int,size_t);
49 static volatile memset_t memset_func
= memset
;
51 void* explicit_bzero_safe(void *p
, size_t l
) {
53 memset_func(p
, '\0', l
);