title: How to turn off mmap usage for malloc() in multithread?
body: <p>Seems like <code>malloc()</code> prefer to use <code>mmap()</code> to allocate space in multi-thread program. I just tried set <code>M_TRIM_THRESHOLD</code> and <code>M_MMAP_MAX</code> to turn off <code>mmap</code> usage but failed:</p>
  
  <pre><code>// Turn off malloc trimming.
  mallopt(M_TRIM_THRESHOLD, -1);
  
  // Turn off mmap usage.
  mallopt(M_MMAP_MAX, 0);
  </code></pre>
  
  <p>A simple test piece of code is as following:</p>
  
  <pre><code>#include &lt;malloc.h&gt;
  #include &lt;cassert&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  void alloc_assert()
  {
      // Turn off malloc trimming.
      mallopt(M_TRIM_THRESHOLD, -1);
  
      // Turn off mmap usage.
      mallopt(M_MMAP_MAX, 0);
  
      void* p = malloc(100);
  
      printf("size_t(p): %zu\n", size_t(p));
  
      assert(size_t(p) &lt; 0x100000000000l);
  }
  
  void* thread_func(void *arg)
  {
      alloc_assert();
  
      pthread_exit(NULL);
  
      return NULL;
  }
  
  int main()
  {
      pthread_t thr[2];
      int data = 0;
  
      // Multi-thread enabled.
      if (pthread_create(&amp;thr[0], NULL, &amp;thread_func, (void*) &amp;data) != 0)
      {
          printf("Create thread error\n");
      }
  
      pthread_join(thr[0], NULL);
  
      //alloc_assert();
  
      return 0;
  }
  </code></pre>
  
  <p>The output is as following:</p>
  
  <pre><code>size_t(p): 140154111002816
  a.out: main.cpp:37: void alloc_assert(): Assertion `size_t(p) &lt; 0x100000000000l' failed.
  [1]    154060 abort      ./a.out
  </code></pre>
  
  <p><code>malloc()</code> allocated the space on a high-address instead of normal heap address. However, if we change the code in <code>main()</code> to following:</p>
  
  <pre><code>int main()
  {
      alloc_assert();
  
      return 0;
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>size_t(p): 31775776
  </code></pre>
  
  <p>Instead of using <code>mmap()</code>, <code>malloc()</code> allocated the space on a normal heap. I wanna if it's possible to turn off <code>mmap()</code> usage for <code>malloc()</code> in multi-thread program?</p>
  
  <p>My environment configurations:</p>
  
  <pre><code>Thread model: posix
  gcc version 5.2.0 (GCC)
  Linux fsdev32 2.6.32-573.el6.x86_64
  </code></pre>