Commit df9c39d7 authored by Doron Roberts-Kedes's avatar Doron Roberts-Kedes Committed by Facebook Github Bot

IndexedMemPool: Initialize and destroy Atom in slots_

Summary:
This is only a bug when Atom != std::atomic (for example when Atom = DeterministicAtomic) since the mmap region is zero-intialized, and the std::atomic constructor does no initialization other than zero initialization. However, this assumption does not hold when Atom != std::atomic.

I'm not super familiar with the details of IndexedMemPool, so perhaps there is a more clever way to initialize and destroy the Atom instances in slots_, but I decided to go with this simple solution since it only modifies the construtor and destructor.

Reviewed By: yfeldblum

Differential Revision: D10277293

fbshipit-source-id: 1e70a07963117b04e76f341c5b5148602d4ca48d
parent c1585ed8
......@@ -168,6 +168,10 @@ struct IndexedMemPool : boost::noncopyable {
LocalListLimit = LocalListLimit_,
};
static_assert(
std::is_nothrow_default_constructible<Atom<uint32_t>>::value,
"Atom must be nothrow default constructible");
// these are public because clients may need to reason about the number
// of bits required to hold indices from a pool, given its capacity
......@@ -206,13 +210,23 @@ struct IndexedMemPool : boost::noncopyable {
assert(errno == ENOMEM);
throw std::bad_alloc();
}
for (size_t i = 1; i < actualCapacity_ + 1; i++) {
// Atom is enforced above to be nothrow-default-constructible
new (&slots_[i].localNext) Atom<uint32_t>();
new (&slots_[i].globalNext) Atom<uint32_t>();
}
}
/// Destroys all of the contained elements
~IndexedMemPool() {
using A = Atom<uint32_t>;
for (uint32_t i = maxAllocatedIndex(); i > 0; --i) {
Traits::cleanup(&slots_[i].elem);
}
for (size_t i = 1; i < actualCapacity_ + 1; i++) {
slots_[i].localNext.~A();
slots_[i].globalNext.~A();
}
munmap(slots_, mmapLength_);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment