Commit 64711548 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Ensure that getCoreAllocator() is shared across DSOs

Summary: The allocator instances may not be shared across DSOs that are loaded with `RTLD_LOCAL`.

Reviewed By: yfeldblum

Differential Revision: D29955690

fbshipit-source-id: 914196a072a1bb0709d772ba0cddc6319acde862
parent cc4308cf
......@@ -32,6 +32,7 @@
#include <folly/Likely.h>
#include <folly/Memory.h>
#include <folly/Portability.h>
#include <folly/detail/StaticSingletonManager.h>
#include <folly/lang/Align.h>
#include <folly/lang/Exception.h>
......@@ -474,9 +475,9 @@ class CoreRawAllocator {
}
};
Allocator* get(size_t stripe) {
Allocator& get(size_t stripe) {
assert(stripe < Stripes);
return &allocators_[stripe];
return allocators_[stripe];
}
private:
......@@ -484,14 +485,11 @@ class CoreRawAllocator {
};
template <typename T, size_t Stripes>
FOLLY_EXPORT
CxxAllocatorAdaptor<T, typename CoreRawAllocator<Stripes>::Allocator>
getCoreAllocator(size_t stripe) {
// We cannot make sure that the allocator will be destroyed after
// all the objects allocated with it, so we leak it.
static Indestructible<CoreRawAllocator<Stripes>> allocator;
return CxxAllocatorAdaptor<T, typename CoreRawAllocator<Stripes>::Allocator>(
*allocator->get(stripe));
CxxAllocatorAdaptor<T, typename CoreRawAllocator<Stripes>::Allocator>
getCoreAllocator(size_t stripe) {
using RawAllocator = CoreRawAllocator<Stripes>;
return CxxAllocatorAdaptor<T, typename RawAllocator::Allocator>(
detail::createGlobal<RawAllocator, void>().get(stripe));
}
} // namespace folly
......@@ -1144,28 +1144,28 @@ TEST(AccessSpreader, Wrapping) {
TEST(CoreRawAllocator, Basic) {
CoreRawAllocator<32> alloc;
auto a = alloc.get(0);
auto res = a->allocate(8);
auto& a = alloc.get(0);
auto res = a.allocate(8);
memset(res, 0, 8);
a->deallocate(res);
res = a->allocate(8);
a.deallocate(res);
res = a.allocate(8);
EXPECT_TRUE((intptr_t)res % 8 == 0); // check alignment
memset(res, 0, 8);
a->deallocate(res);
res = a->allocate(12);
a.deallocate(res);
res = a.allocate(12);
EXPECT_TRUE((intptr_t)res % 16 == 0); // check alignment
memset(res, 0, 12);
a->deallocate(res);
res = a->allocate(257);
a.deallocate(res);
res = a.allocate(257);
memset(res, 0, 257);
a->deallocate(res);
a.deallocate(res);
std::vector<void*> mems;
for (int i = 0; i < 10000; i++) {
mems.push_back(a->allocate(1));
mems.push_back(a.allocate(1));
}
for (auto& mem : mems) {
a->deallocate(mem);
a.deallocate(mem);
}
mems.clear();
}
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