Skip to content

Commit

Permalink
Implement DESKTOP_APP_REQUIRE_JEMALLOC switch
Browse files Browse the repository at this point in the history
  • Loading branch information
mymedia2 committed Jul 29, 2021
1 parent de08973 commit e646575
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 41 additions & 2 deletions Telegram/SourceFiles/platform/linux/specific_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ For license and copyright information please follow this link:
#include <sys/sendfile.h>
#endif // Q_OS_LINUX
#include <cstdlib>
#include <dlfcn.h>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
Expand Down Expand Up @@ -684,6 +685,42 @@ void HaikuAutostart(bool start) {
}
#endif // __HAIKU__

decltype(mallctl)* mallctlLoad() {
#ifdef DESKTOP_APP_REQUIRE_JEMALLOC
return &mallctl;
#elif defined(__GLIBC__)
// Despite using glibc, one can choose jemalloc via LD_PRELOAD.
// Detect the case and configure the allocator.

Dl_info info;
int rc = dladdr(reinterpret_cast<void*>(&malloc), &info);
if (!rc) {
LOG(("Can't retrieve malloc info."));
return nullptr;
}
LOG(("We use allocator from %1").arg(info.dli_fname));
if (!QLatin1String(info.dli_fname)
.endsWith(QLatin1String("/libjemalloc.so.2"))) {
return nullptr;
}

void *handle = dlopen(info.dli_fname, RTLD_NOW);
if (!handle) {
LOG(("Can't reopen jemalloc dynamic object."));
return nullptr;
}

auto pointer = reinterpret_cast<decltype(mallctl)*>(
dlsym(handle, "mallctl"));
if (!pointer) {
LOG(("Can't find mallctl function in our jemalloc."));
}
return pointer;
#else // __GLIBC__
return nullptr;
#endif
}

} // namespace

QString psAppDataPath() {
Expand Down Expand Up @@ -727,8 +764,10 @@ int psFixPrevious() {
namespace Platform {

void start() {
auto backgroundThread = true;
mallctl("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool));
if (auto mallctlLoaded = mallctlLoad()) {
bool backgroundThread = true;
mallctlLoaded("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool));
}

LOG(("Launcher filename: %1").arg(QGuiApplication::desktopFileName()));

Expand Down
2 changes: 1 addition & 1 deletion cmake

0 comments on commit e646575

Please sign in to comment.