#include "base/atomicops.h"
#include <atomic>
#include "base/memory/aligned_memory.h"
namespace base::subtle {
void RelaxedAtomicWriteMemcpy(base::span<uint8_t> dst,
base::span<const uint8_t> src) {
CHECK_EQ(dst.size(), src.size());
size_t bytes = dst.size();
uint8_t* dst_byte_ptr = dst.data();
const uint8_t* src_byte_ptr = src.data();
static_assert(std::atomic_ref<uint8_t>::required_alignment == 1);
constexpr size_t kDesiredAlignment =
std::atomic_ref<uintmax_t>::required_alignment;
while (bytes > 0 && !IsAligned(dst_byte_ptr, kDesiredAlignment)) {
std::atomic_ref<uint8_t>(*dst_byte_ptr)
.store(*src_byte_ptr, std::memory_order_relaxed);
UNSAFE_BUFFERS(++dst_byte_ptr);
UNSAFE_BUFFERS(++src_byte_ptr);
--bytes;
}
if (IsAligned(src_byte_ptr, kDesiredAlignment)) {
while (bytes >= sizeof(uintmax_t)) {
std::atomic_ref<uintmax_t>(*reinterpret_cast<uintmax_t*>(dst_byte_ptr))
.store(*reinterpret_cast<const uintmax_t*>(src_byte_ptr),
std::memory_order_relaxed);
UNSAFE_BUFFERS(dst_byte_ptr += sizeof(uintmax_t));
UNSAFE_BUFFERS(src_byte_ptr += sizeof(uintmax_t));
bytes -= sizeof(uintmax_t);
}
}
while (bytes > 0) {
std::atomic_ref<uint8_t>(*dst_byte_ptr)
.store(*src_byte_ptr, std::memory_order_relaxed);
UNSAFE_BUFFERS(++dst_byte_ptr);
UNSAFE_BUFFERS(++src_byte_ptr);
--bytes;
}
}
}