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