6 comments

  • pjmlp1 hour ago
    This is compiler specific and cannot be generalised as C++.
    • zabzonk32 minutes ago
      Well, yes, but still quite interesting, IMHO. It's not like GCC is one of the least used compilers.
      • pjmlp14 minutes ago
        Yeah, but that isn't C++ in isolation, thus the tile is incorrect.
    • compiler-guy1 hour ago
      And C++ library specific as well. Perhaps even more so.
  • aliveintucson26 minutes ago
    I think you should read up on what "always" means.
  • Joker_vD2 hours ago
    Huh. Why is this emergency pool not statically allocated? Is it possible to tune the size of this pool on libc++ startup somehow? Because otherwise it absolutely should've been statically allocated.
    • joelsiks2 hours ago
      I did mention it briefly in the post, but you can opt-in for a fixed-size statically allocated buffer by configuring libstdc++ with --enable-libstdcxx-static-eh-pool. Also, you can opt-out of the pool entirely by configuring the number of objects in the pool to zero with the environment variable GLIBCXX_TUNABLES=glibcxx.eh_pool.obj_count=0.
      • ninkendo54 minutes ago
        I wonder why it’s opt-in. Maybe it’s part of the whole “you only pay for what you use” ethos, i.e. you shouldn’t have to pay the cost for a static emergency pool if you don’t even use dynamic memory allocation.
  • throwaway20374 hours ago
    I would like the see the source code for libmymalloc.so, however, I don&#x27;t see anything in the blog post. Nor do I see anything in his GitHub profile: <a href="https:&#x2F;&#x2F;github.com&#x2F;jsikstro" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jsikstro</a><p>Also, I cannot find his email address anywhere (to ask him to share it on GitHub).<p>Am I missing something?
    • joelsiks3 hours ago
      The exact implementation of mymalloc isn&#x27;t relevant to the post. I have an old allocator published at <a href="https:&#x2F;&#x2F;github.com&#x2F;joelsiks&#x2F;jsmalloc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;joelsiks&#x2F;jsmalloc</a> that I did as part of my Master&#x27;s thesis, which uses a similar debug-logging mechanism that is described in the post.
    • nly3 hours ago
      dlsym() with the RTLD_NEXT flag basically:<p><a href="https:&#x2F;&#x2F;catonmat.net&#x2F;simple-ld-preload-tutorial-part-two" rel="nofollow">https:&#x2F;&#x2F;catonmat.net&#x2F;simple-ld-preload-tutorial-part-two</a><p>There&#x27;s actually a better way to hook GNUs malloc:<p><a href="https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;malloc_hook.3.html" rel="nofollow">https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;malloc_hook.3.html</a><p>This is better because you can disable the hook inside the callback, and therefore use malloc within your malloc hook (no recursion)<p>But you can&#x27;t use this mechanism before main()
      • Joker_vD2 hours ago
        <p><pre><code> The use of these hook functions is not safe in multithreaded programs, and they are now deprecated. From glibc 2.24 onwards, the __malloc_initialize_hook variable has been removed from the API, and from glibc 2.34 onwards, all the hook variables have been removed from the API. Programmers should instead preempt calls to the relevant functions by defining and exporting malloc(), free(), realloc(), and calloc().</code></pre>
        • nly1 hour ago
          Yeah. Shame though because it gave you the option to control exactly when you hooked and didn&#x27;t hook, which let stop and start debugging allocations based on arbitrary triggers.<p>The global variable approach was very useful and pretty low overhead.
  • znpy14 minutes ago
    &gt; TLDR; The C++ standard library sets up exception handling infrastructure early on, allocating memory for an “emergency pool” to be able to allocate memory for exceptions in case malloc ever runs out of memory.<p>Reminds me of Perl&#x27;s $^M: <a href="https:&#x2F;&#x2F;perldoc.perl.org&#x2F;variables&#x2F;$%5EM" rel="nofollow">https:&#x2F;&#x2F;perldoc.perl.org&#x2F;variables&#x2F;$%5EM</a><p>In Perl you can &quot;hand-manage&quot; that. This line would allocate a 64K buffer for use in an emergency:<p><pre><code> $^M = &#x27;a&#x27; x (1 &lt;&lt; 16);</code></pre>