Unofficial Hacker News client - receive Push Notifications on replies.

Doug Lea's Malloc (opens external site)

52 points by grep_it 4 days ago | gee.cs.oswego.edu | 32 comments on HN | Vote on HN (opens Hacker News)

View full thread · View on Hacker News

Comment thread (9)

  1. Considering that global allocators are usually an anti-pattern and that most applications should have component-specific allocators, dlmalloc isn't all that bad when used through its mspace API. Of course, if one doesn't actually need a general allocator, they should simply use an arena allocator.
    Replies (3)
    1. Musl still supports single threaded slab allocations. In Musl, the difference comes down to one lock, some atomics, and a #define. My library extracts out its algorithm and let's you use scoped single and multi threaded Musl allocators freely mixed with each other.
      Replies (1)
      1. > the difference comes down to one lock, some atomics, and a #define

        Hopefully not on the alloc and free paths

        Replies (1)
        1. Musl does lock on alloc and free paths, yes. It is not a lockless allocator. It doesn't claim to be the very fastest allocator, but it is a reasonably fast modern (slab) allocator with relatively aggressive security mitigations. I assume the lock makes it easier to guarantee these mitigations.
          Replies (2)
          1. But it shouldn't take locks in your private arena?
            Replies (1)
            1. In typical modern allocators like jemalloc or tcmalloc[*], sharded arenas are protected by locks, but the assumption is that with enough sharding there won't be significant lock contention, and also that you can mostly avoid the locks with thread-local caches. (mimalloc uses thread-local arenas instead, so no locks are required for allocations, and atomic RMW ops for cross-thread frees.)

              * I'm ignoring rseq here for simplicity.

          2. jemalloc and tcmalloc are not lockless allocators, but they take care not to lock in the fast path of malloc and free.

            BTW mimalloc (a lockless allocator) has a secure mode, not sure how it compares to musl's mitigations.

    2. Unfortunately, ancient Unix tradition makes everything gravitate towards a global heap. You can do something different, but you're fighting the system when you do. There should be a Windows-style multi-heap allocator API, for example, but there isn't. Instead, making a new heap is something every random heap implementation has its own API. Common practice is for one component to free() memory allocated by another too, sadly.
    3. Global allocators are quite good actually. Most applications do not have component-specific allocators and are better off for it.