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