Debian Codegraph @

crt/aws-c-common/include/aws/common/zero.inl

raw
1 #ifndef AWS_COMMON_ZERO_INL
2 #define AWS_COMMON_ZERO_INL
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/stdbool.h>
10 #include <aws/common/stdint.h>
11 #include <aws/common/zero.h>
12 #include <string.h>
13
14 AWS_EXTERN_C_BEGIN
15 /**
16 * Returns whether each byte is zero.
17 */
18 AWS_STATIC_IMPL
19 bool aws_is_mem_zeroed(const void *buf, size_t bufsize) {
20 /* Optimization idea: vectorized instructions to check more than 64 bits at a time. */
21
22 /* Check 64 bits at a time */
23 const uint64_t *buf_u64 = (const uint64_t *)buf;
24 const size_t num_u64_checks = bufsize / 8;
25 size_t i;
26 for (i = 0; i < num_u64_checks; ++i) {
27 if (buf_u64[i]) {
28 return false;
29 }
30 }
31
32 /* Update buf to where u64 checks left off */
33 buf = buf_u64 + num_u64_checks;
34 bufsize = bufsize % 8;
35
36 /* Check 8 bits at a time */
37 const uint8_t *buf_u8 = (const uint8_t *)buf;
38 for (i = 0; i < bufsize; ++i) {
39 if (buf_u8[i]) {
40 return false;
41 }
42 }
43
44 return true;
45 }
46
47 AWS_EXTERN_C_END
48
49 #endif /* AWS_COMMON_ZERO_INL */