Fix CHECK on alignment of PendingCounts storage

When PendingCounts only stores PackedCounts structs, its size may be less than
sizeof(PendingCounts). In that case, it's not guaranteed that all allocators
will return 8-byte-aligned addresses.

PiperOrigin-RevId: 333366526
Change-Id: Ib542a4674826eb68054bd0d575d120c3c4da836a
This commit is contained in:
Todd Lipcon 2020-09-23 13:55:32 -07:00 committed by TensorFlower Gardener
parent 705dcc359c
commit 7f5b8ad370

View File

@ -81,13 +81,19 @@ class PendingCounts {
// Create a new PendingCounts object that can hold the state of
// all the Handles allocated from "final_allocator".
explicit PendingCounts(Layout layout)
: num_bytes_(layout.next_offset_), bytes_(new char[num_bytes_]) {}
: num_bytes_(layout.next_offset_), bytes_(new char[num_bytes_]) {
if (num_bytes_ >= sizeof(LargeCounts)) {
CHECK_EQ(uintptr_t(bytes_) % alignof(LargeCounts), 0);
}
}
// Create a new PendingCounts object with the same layout and counts
// as "other".
explicit PendingCounts(const PendingCounts& other)
: num_bytes_(other.num_bytes_), bytes_(new char[num_bytes_]) {
CHECK_EQ(uintptr_t(bytes_) % alignof(LargeCounts), 0);
if (num_bytes_ >= sizeof(LargeCounts)) {
CHECK_EQ(uintptr_t(bytes_) % alignof(LargeCounts), 0);
}
memcpy(bytes_, other.bytes_, other.num_bytes_);
}