]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/memory-util.c
Merge pull request #17444 from BtbN/fix_ib_dhcp4
[thirdparty/systemd.git] / src / basic / memory-util.c
CommitLineData
4368277c
ZJS
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
0a970718
LP
3#include <unistd.h>
4
5#include "memory-util.h"
6
7size_t page_size(void) {
8 static thread_local size_t pgsz = 0;
9 long r;
10
11 if (_likely_(pgsz > 0))
12 return pgsz;
13
14 r = sysconf(_SC_PAGESIZE);
15 assert(r > 0);
16
17 pgsz = (size_t) r;
18 return pgsz;
19}
20
21bool 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.
26 */
27
28 const uint8_t *p = data;
29 size_t i;
30
31 /* Check first 16 bytes manually */
32 for (i = 0; i < 16; i++, length--) {
33 if (length == 0)
34 return true;
35 if (p[i])
36 return false;
37 }
38
39 /* Now we know first 16 bytes are NUL, memcmp with self. */
40 return memcmp(data, p + i, length) == 0;
41}
090a9c1e
LP
42
43#if !HAVE_EXPLICIT_BZERO
44/*
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.
48 */
49typedef void *(*memset_t)(void *,int,size_t);
50
51static volatile memset_t memset_func = memset;
52
53void* explicit_bzero_safe(void *p, size_t l) {
54 if (l > 0)
55 memset_func(p, '\0', l);
56
57 return p;
58}
59#endif