From 99d4b81d3fcedb7c912acbd813573a89d32ade22 Mon Sep 17 00:00:00 2001
From: Junhao Li <streaver91@gmail.com>
Date: Sun, 20 May 2018 13:45:32 -0400
Subject: [PATCH] Disable large allocation report by default

Fixes issue #360.

[alkondratenko@gmail.com: adjusted commit message a bit]
[alkondratenko@gmail.com: adjusted configure help message]
Signed-off-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
---
 configure.ac    | 9 +++++++++
 src/tcmalloc.cc | 4 ++++
 2 files changed, 13 insertions(+)

diff --git a/configure.ac b/configure.ac
index fa03f1a..f874e19 100644
--- a/configure.ac
+++ b/configure.ac
@@ -663,6 +663,15 @@ AS_IF([test "x$enable_generic_dynamic_tls" = xyes],
       [AC_DEFINE([ENABLE_GENERIC_DYNAMIC_TLS], 1,
                  [Use the generic dynamic TLS model])])
 
+# Disable large allocation report by default.
+AC_ARG_ENABLE([large-alloc-report],
+              [AS_HELP_STRING([--enable-large-alloc-report],
+                              [report very large allocations to stderr])],
+              [enable_large_alloc_report="$enableval"],
+              [enable_large_alloc_report=no])
+AS_IF([test "x$enable_large_alloc_report" = xyes],
+      [AC_DEFINE([ENABLE_LARGE_ALLOC_REPORT], 1, [report large allocation])])
+
 # Write generated configuration file
 AC_CONFIG_FILES([Makefile
                  src/gperftools/tcmalloc.h src/windows/gperftools/tcmalloc.h])
diff --git a/src/tcmalloc.cc b/src/tcmalloc.cc
index 7b18ddb..48d4530 100644
--- a/src/tcmalloc.cc
+++ b/src/tcmalloc.cc
@@ -1272,9 +1272,11 @@ void* handle_oom(malloc_fn retry_fn,
 
 // Copy of FLAGS_tcmalloc_large_alloc_report_threshold with
 // automatic increases factored in.
+#ifdef ENABLE_LARGE_ALLOC_REPORT
 static int64_t large_alloc_threshold =
   (kPageSize > FLAGS_tcmalloc_large_alloc_report_threshold
    ? kPageSize : FLAGS_tcmalloc_large_alloc_report_threshold);
+#endif
 
 static void ReportLargeAlloc(Length num_pages, void* result) {
   StackTrace stack;
@@ -1295,6 +1297,7 @@ static void ReportLargeAlloc(Length num_pages, void* result) {
 
 // Must be called with the page lock held.
 inline bool should_report_large(Length num_pages) {
+#ifdef ENABLE_LARGE_ALLOC_REPORT
   const int64 threshold = large_alloc_threshold;
   if (threshold > 0 && num_pages >= (threshold >> kPageShift)) {
     // Increase the threshold by 1/8 every time we generate a report.
@@ -1303,6 +1306,7 @@ inline bool should_report_large(Length num_pages) {
                              ? threshold + threshold/8 : 8ll<<30);
     return true;
   }
+#endif
   return false;
 }
 
-- 
1.8.3.1

