summaryrefslogtreecommitdiffstats
path: root/lib/memzero.h
blob: 1137e830d3236bfb7b4435bce92e43861b0c1d8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * SPDX-FileCopyrightText: 2022-2023, Christian Göttsche <cgzones@googlemail.com>
 * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
 * SPDX-License-Identifier: BSD-3-Clause
 */


#ifndef SHADOW_INCLUDE_LIBMISC_MEMZERO_H_
#define SHADOW_INCLUDE_LIBMISC_MEMZERO_H_


#include <config.h>

#include <stddef.h>
#include <string.h>
#include <strings.h>

#include "sizeof.h"


#define MEMZERO(arr)  memzero(arr, SIZEOF_ARRAY(arr))


inline void memzero(void *ptr, size_t size);
inline void strzero(char *s);


inline void
memzero(void *ptr, size_t size)
{
#if defined(HAVE_MEMSET_EXPLICIT)
	memset_explicit(ptr, 0, size);
#elif defined(HAVE_EXPLICIT_BZERO)
	explicit_bzero(ptr, size);
#else
	bzero(ptr, size);
	__asm__ __volatile__ ("" : : "r"(ptr) : "memory");
#endif
}


inline void
strzero(char *s)
{
	memzero(s, strlen(s));
}


#endif  // include guard