Allow controlling the use of jemalloc

Jemalloc is often controlled through USE=jemalloc in Gentoo. Removing or
replacing it is trivial, and I'm not aware of any particular advantages that
come with using this allocator, especially for an IM client. Benchmarks
regarding this allocator provide a huge variety of results depending on the
workload and system, so it's hard to establish when this allocator provides any
tangible advantage.

Considering things like Hardened Gentoo, the user might prefer to use a more
exploit-resistant allocator, like GrapheneOS/hardened_malloc, and I've heard
Musl is hardening their allocator as well.

I trust the user can better judge if they need this in their particular setup.

If a telegram dev is reading this, feel free to drop me a message (metadata.xml
contains the maintainer's address), if you believe it should be forced due to
usability concerns, or if you can provide me with some context I might be
missing. Jemalloc is currently enabled by default.

--- tdesktop-2.8.9-full.orig/Telegram/SourceFiles/platform/linux/specific_linux.cpp
+++ tdesktop-2.8.9-full/Telegram/SourceFiles/platform/linux/specific_linux.cpp
@@ -48,7 +48,10 @@
 #include <gio/gio.h>
 #include <glibmm.h>
 #include <giomm.h>
+
+#ifndef DESKTOP_APP_DISABLE_JEMALLOC
 #include <jemalloc/jemalloc.h>
+#endif // !DESKTOP_APP_DISABLE_JEMALLOC
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -727,8 +730,10 @@
 namespace Platform {
 
 void start() {
+#ifndef DESKTOP_APP_DISABLE_JEMALLOC
 	auto backgroundThread = true;
 	mallctl("background_thread", nullptr, nullptr, &backgroundThread, sizeof(bool));
+#endif // !DESKTOP_APP_DISABLE_JEMALLOC
 
 	LOG(("Launcher filename: %1").arg(QGuiApplication::desktopFileName()));
 
--- tdesktop-2.8.9-full.orig/cmake/external/CMakeLists.txt
+++ tdesktop-2.8.9-full/cmake/external/CMakeLists.txt
@@ -37,7 +37,7 @@
 	add_checked_subdirectory(hunspell)
 endif()
 add_checked_subdirectory(iconv)
-if (LINUX)
+if (LINUX AND NOT DESKTOP_APP_DISABLE_JEMALLOC)
     add_checked_subdirectory(jemalloc)
 endif()
 add_checked_subdirectory(jpeg)
--- tdesktop-2.8.9-full.orig/cmake/options.cmake
+++ tdesktop-2.8.9-full/cmake/options.cmake
@@ -56,6 +56,13 @@
     )
 endif()
 
+if (DESKTOP_APP_DISABLE_JEMALLOC)
+    target_compile_definitions(common_options
+    INTERFACE
+        DESKTOP_APP_DISABLE_JEMALLOC
+    )
+endif()
+
 if (DESKTOP_APP_USE_PACKAGED)
     target_compile_definitions(common_options
     INTERFACE
--- tdesktop-2.8.9-full.orig/cmake/options_linux.cmake
+++ tdesktop-2.8.9-full/cmake/options_linux.cmake
@@ -58,10 +58,12 @@
     endif()
 endif()
 
-target_link_libraries(common_options
-INTERFACE
-    desktop-app::external_jemalloc
-)
+if (NOT DESKTOP_APP_DISABLE_JEMALLOC)
+    target_link_libraries(common_options
+    INTERFACE
+        desktop-app::external_jemalloc
+    )
+endif()
 
 if (DESKTOP_APP_USE_PACKAGED)
     find_library(ATOMIC_LIBRARY atomic)
--- tdesktop-2.8.9-full.orig/cmake/variables.cmake
+++ tdesktop-2.8.9-full/cmake/variables.cmake
@@ -38,6 +38,7 @@
 option(DESKTOP_APP_USE_PACKAGED_FFMPEG_STATIC "Link ffmpeg statically in packaged mode." OFF)
 option(DESKTOP_APP_DISABLE_SPELLCHECK "Disable spellcheck library." ${osx_special_target})
 option(DESKTOP_APP_DISABLE_WEBKIT "Disable WebkitGTK library (Linux only)." OFF)
+option(DESKTOP_APP_DISABLE_JEMALLOC "Disable use of the jemalloc allocator (Linux only)." OFF)
 option(DESKTOP_APP_DISABLE_CRASH_REPORTS "Disable crash report generation." ${no_special_target})
 option(DESKTOP_APP_DISABLE_AUTOUPDATE "Disable autoupdate." ${disable_autoupdate})
 option(DESKTOP_APP_USE_HUNSPELL_ONLY "Disable system spellchecker and use bundled Hunspell only. (For debugging purposes)" OFF)
