Fix JemallocNodumpAllocator destructor w/ jemalloc 5.0.
authorQi Wang <qiwang@fb.com>
Mon, 5 Jun 2017 19:45:43 +0000 (12:45 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 5 Jun 2017 19:51:41 +0000 (12:51 -0700)
Summary: Destroy created arena which has extent_hooks linked.

Reviewed By: jasone

Differential Revision: D5177535

fbshipit-source-id: 7d75f4276fc174c4d4ce1102dfb8ec8c27c1f56d

folly/experimental/JemallocNodumpAllocator.cpp
folly/experimental/JemallocNodumpAllocator.h

index 8d29dbc82109e2c87f00ccda5a781ff4cddda9a3..77d9724b4962a4b48a1eb04faebde68d0ba11ebc 100644 (file)
@@ -29,6 +29,19 @@ JemallocNodumpAllocator::JemallocNodumpAllocator(State state) {
   }
 }
 
+JemallocNodumpAllocator::~JemallocNodumpAllocator() {
+#ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_EXTENT
+  if (arena_index_ != 0) {
+    // Destroy the arena because the hooks are linked to us.
+    const auto key = folly::to<std::string>("arena.", arena_index_, ".destroy");
+    if (auto ret = mallctl(key.c_str(), nullptr, 0, nullptr, 0)) {
+      LOG(FATAL) << "Unable to destroy arena: " << errnoStr(ret);
+    }
+    LOG(INFO) << "Destroy arena: " << arena_index_;
+  }
+#endif
+}
+
 bool JemallocNodumpAllocator::extend_and_setup_arena() {
 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
   if (mallctl == nullptr) {
@@ -91,8 +104,9 @@ bool JemallocNodumpAllocator::extend_and_setup_arena() {
   // Set the custom hook
   extent_hooks_ = *hooks;
   extent_hooks_.alloc = &JemallocNodumpAllocator::alloc;
-  if (auto ret =
-          mallctl(key.c_str(), nullptr, nullptr, &hooks, sizeof(hooks))) {
+  extent_hooks_t* new_hooks = &extent_hooks_;
+  if (auto ret = mallctl(
+          key.c_str(), nullptr, nullptr, &new_hooks, sizeof(new_hooks))) {
     LOG(FATAL) << "Unable to set the hooks: " << errnoStr(ret);
   }
 #endif
index a87161f02dac9af95420bb4241a6f6fa4140f91c..40ebe99eb7817178aed9caf4c7bb68c737e97c61 100644 (file)
@@ -76,6 +76,7 @@ class JemallocNodumpAllocator {
   static void deallocate(void* p, void* userData);
 
   explicit JemallocNodumpAllocator(State state = State::ENABLED);
+  ~JemallocNodumpAllocator();
 
   void* allocate(size_t size);
   void* reallocate(void* p, size_t size);