Fix JemallocNodumpAllocator destructor w/ jemalloc 5.0.
[folly.git] / folly / experimental / JemallocNodumpAllocator.cpp
index e2ce6d8c5061c48ff5fd6ebd73d2322618b778be..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) {
@@ -89,7 +102,13 @@ bool JemallocNodumpAllocator::extend_and_setup_arena() {
   }
 
   // Set the custom hook
-  hooks->alloc = &JemallocNodumpAllocator::alloc;
+  extent_hooks_ = *hooks;
+  extent_hooks_.alloc = &JemallocNodumpAllocator::alloc;
+  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
 
   return true;