Rendered at 22:37:12 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
senderista 4 days ago [-]
It was a good single-threaded allocator back in the day but in the multicore era the design is now thoroughly obsolete. I suggest reading e.g. the jemalloc[1] and mimalloc[2] papers if you want to understand how modern allocators are implemented.
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.
saagarjha 3 days ago [-]
Global allocators are quite good actually. Most applications do not have component-specific allocators and are better off for it.
Conscat 4 days ago [-]
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.
saagarjha 3 days ago [-]
> the difference comes down to one lock, some atomics, and a #define
Hopefully not on the alloc and free paths
Conscat 3 days ago [-]
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.
senderista 2 days ago [-]
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.
saagarjha 2 days ago [-]
But it shouldn't take locks in your private arena?
senderista 2 days ago [-]
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.
quotemstr 3 days ago [-]
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.
jart 4 days ago [-]
Cosmopolitan uses dlmalloc and just wraps it with an rseq allocator for multicore performance. See also https://jart.dev/rseq/
uvdn7 3 days ago [-]
I recently contributed to jemalloc (a change related to heap profiling) and got curious how popular is jemalloc in general. https://github.com/jemalloc/jemalloc/wiki/Background the list is a little dated but still.
Still useful as an instructional aid though. Everyone should implement a simple malloc at least once just to demystify the heap.
grep_it 3 days ago [-]
Would this logic apply to domain specific allocators? Memcached for example uses a pretty simple slab style allocator[0] to manage a preallocated pool of memory, and afaict, they have a mutex over any threads interaction with it.
In the mid 2000s I was a student in upstate NY and I very briefly met Doug Lea. (I'm 100% sure he doesn't remember me at all.) He was a very warm, approachable, down to earth guy. The computer labs at Oswego had a bunch of giant Sun Microsystems logos on all the gear, I guess because they were funding him.
pcwalton 4 days ago [-]
Besides the lack of thread-local allocation buffers that other comments have mentioned, the other problem with dlmalloc is that the metadata is stored inline, right next to the chunks. This is bad for security as it makes it much easier to weaponize out-of-bounds writes.
Use case: it’s a fantastic memory allocator for your hobby OS. After you have implemented a page allocator, copy dlmalloc.c, adapt it slightly and you’re good to go.
Also it’s public domain.
acyclic 7 days ago [-]
The only algorithm to be named after someone's full name, first and last name
But there are definitely some that get a full name - if only to disambiguate them.
It might be because I'm reading beginners references now and again but off the top of my head Robert Tarjan's strongly connected components algorithm is one example, and going back to Dijkstra his shortest path algorithm is usually just called "Dijkstra's algorithm", but his shunting-yard algorithm is often fully qualified with his complete name.
Someone 4 days ago [-]
Nitpick: “Doug” likely is shorthand for “Douglas”. Also I think he’s American. If so he likely has a middle name.
I believe this was the original malloc used in Android's bionic libc.
egberts1 4 days ago [-]
False.
Android up to v4.1 bionic is based on Doug Lea's dlmalloc v2.8.3
Android v4.2 upgraded to dlmalloc v2.8.6
Android v5.0 transitioned to jemalloc.
Modern Android is based on jemalloc or Scudo, based on build configuration.
Android never used original dlmalloc.
Conscat 4 days ago [-]
Am I confused or did you agree with your parent comment that dlmalloc was the original algorithm used by Android?
incognito124 3 days ago [-]
The confusion is from the interpretation of the word original. One of them thought original meant dlmalloc v1, the other meant dlmalloc was generally the first allocator in Android. Common lingustics issue of adjective binding
egberts1 3 days ago [-]
In continuation of this confusion (sarcasm), Android v4.1 added large memory allocation, multiple independent heaps, corruption detection, thread synchronization, and portability since dlmalloc
V0.9
zahlman 3 days ago [-]
I can't help but parse the name "Lea" as "load effective address", which seems somewhat appropriate in context.
unwind 2 days ago [-]
Great catch! Love ye olde nominative determinism [1], always fun!
[1] https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemal...
[2] https://www.microsoft.com/en-us/research/wp-content/uploads/...
Hopefully not on the alloc and free paths
BTW mimalloc (a lockless allocator) has a secure mode, not sure how it compares to musl's mitigations.
* I'm ignoring rseq here for simplicity.
> Major uses of jemalloc include:
FreeBSD NetBSD Mozilla Firefox Varnish Cassandra Redis hhvm MariaDB Aerospike Android
It was the default global memory allocator implementation for rust when it started (until 8 years ago https://www.reddit.com/r/rust/s/g6nwaGAdGA). Firefox adopted jemalloc on windows and later diverged (https://searchfox.org/firefox-main/source/memory/build/mozje...).
0: https://github.com/memcached/memcached/blob/master/slabs.c
Also it’s public domain.
But there are definitely some that get a full name - if only to disambiguate them.
It might be because I'm reading beginners references now and again but off the top of my head Robert Tarjan's strongly connected components algorithm is one example, and going back to Dijkstra his shortest path algorithm is usually just called "Dijkstra's algorithm", but his shunting-yard algorithm is often fully qualified with his complete name.
And indeed, Wikipedia says he’s “Douglas S Lea” (https://en.wikipedia.org/wiki/Doug_Lea)
I recently switched to using TLSF for that: https://github.com/ncruces/wasm2go/blob/main/libc-gen/c/mall...
Android up to v4.1 bionic is based on Doug Lea's dlmalloc v2.8.3
Android v4.2 upgraded to dlmalloc v2.8.6
Android v5.0 transitioned to jemalloc.
Modern Android is based on jemalloc or Scudo, based on build configuration.
Android never used original dlmalloc.
1: https://en.wikipedia.org/wiki/Nominative_determinism
Those are lines from the basic algorithm.