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
|
#ifndef AWS_COMMON_ZERO_INL
#define AWS_COMMON_ZERO_INL
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/stdbool.h>
#include <aws/common/stdint.h>
#include <aws/common/zero.h>
#include <string.h>
AWS_EXTERN_C_BEGIN
/**
* Returns whether each byte is zero.
*/
AWS_STATIC_IMPL
bool aws_is_mem_zeroed(const void *buf, size_t bufsize) {
/* Optimization idea: vectorized instructions to check more than 64 bits at a time. */
/* Check 64 bits at a time */
const uint64_t *buf_u64 = (const uint64_t *)buf;
const size_t num_u64_checks = bufsize / 8;
size_t i;
for (i = 0; i < num_u64_checks; ++i) {
if (buf_u64[i]) {
return false;
}
}
/* Update buf to where u64 checks left off */
buf = buf_u64 + num_u64_checks;
bufsize = bufsize % 8;
/* Check 8 bits at a time */
const uint8_t *buf_u8 = (const uint8_t *)buf;
for (i = 0; i < bufsize; ++i) {
if (buf_u8[i]) {
return false;
}
}
return true;
}
AWS_EXTERN_C_END
#endif /* AWS_COMMON_ZERO_INL */
|