message: Refactor jemalloc setup
  Summary: Use a DEFINE to enable Jemalloc hooks.
diff: * LICENSE file in the root directory of this source tree.
  */
  -#if defined(__unix__) || defined(__APPLE__)
  -#include <dlfcn.h>
  -#endif
  +#include "JemallocUtil.h"
  #include <iostream>
  +#ifdef USE_JEMALLOC
  +#include <jemalloc/jemalloc.h>
  +
  #include "Debug.h"
  +#endif
  -extern "C" {
  +namespace jemalloc_util {
  -using MallctlFn = int (*)(const char*, void*, size_t*, void*, size_t);
  -
  -#if defined(__unix__) || defined(__APPLE__)
  -// We use dynamic lookup to avoid making jemalloc a required build dependency.
  -static auto mallctl =
  - reinterpret_cast<MallctlFn>(dlsym(RTLD_DEFAULT, "mallctl"));
  -#else
  -MallctlFn mallctl = nullptr;
  -#endif
  -}
  +#ifdef USE_JEMALLOC
  namespace {
  void set_profile_active(bool active) {
  - if (mallctl == nullptr) {
  - return;
  - }
  int err =
  mallctl("prof.active", nullptr, nullptr, (void*)&active, sizeof(active));
  always_assert_log(err == 0, "mallctl failed with: %d", err);
  @@ -39,16 +29,11 @@ void set_profile_active(bool active) {
  } // namespace
  -namespace jemalloc_util {
  -
  void enable_profiling() { set_profile_active(true); }
  void disable_profiling() { set_profile_active(false); }
  void dump(const std::string& file_name) {
  - if (mallctl == nullptr) {
  - return;
  - }
  auto* c_str = file_name.c_str();
  int err = mallctl("prof.dump", nullptr, nullptr, &c_str, sizeof(const char*));
  if (err != 0) {
  @@ -56,4 +41,16 @@ void dump(const std::string& file_name) {
  }
  }
  +#else // !USE_JEMALLOC
  +
  +void enable_profiling() {}
  +
  +void disable_profiling() {}
  +
  +void dump(const std::string&) {
  + std::cerr << "Jemalloc dump unsupported" << std::endl;
  +}
  +
  +#endif
  +
  } // namespace jemalloc_util
  