News for hackers. Stuff that matters.
Re:Writing a Memory Allocator (2019) by jiggawatts on Saturday December 12, 2020 @08:51PM (#25403583)
Every time I hear someone talking about writing a memory allocator, it always reminds me of the brilliant CppCon 2015 talk by Andrei Alexandrescu about how he wrote an modular allocator library that can be built up in pieces to make whatever you like.

https://www.youtube.com/watch?v=LIb3L4vKZ7U

Re:Writing a Memory Allocator (2019) by OnlyOneCannolo on Sunday December 13, 2020 @01:12AM (#25404677)
I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.
Re:Writing a Memory Allocator (2019) by ncmncm on Sunday December 13, 2020 @01:50AM (#25404816)
The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays.

And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread.

Most of what you can find about writing allocators is written by people who don't actually know how; and actually useful allocators are made by people who don't write tutorials about it.

Re:Writing a Memory Allocator (2019) by OnlyOneCannolo on Sunday December 13, 2020 @02:23AM (#25404940)
What is he wrong about? He's upfront about limiting the scope to single-threaded arena allocators, but still mentions mmap and threads in passing. For talks that are mostly sales pitches, I think it's a pretty informative introduction to how to select and compare allocators.
Re:Writing a Memory Allocator (2019) by andi999 on Sunday December 13, 2020 @02:29AM (#25404958)
Can you explain what you mean by mmap is how memory is obtained nowadays?
Re:Writing a Memory Allocator (2019) by yaantc on Sunday December 13, 2020 @03:54AM (#25405219)
mmap is not only to map a file content into a process address space, it can also be used to request memory from the OS using the MAP_ANONYMOUS flag. Early on a malloc-like allocator would use brk/sbrk to get a chunk of "raw" memory, and manage this for the user providing access using malloc/free. Nowadays the same "raw" memory can be obtained using an anonymous map. A raw pool, when unused, can be returned to the OS by unmapping it.

Using an anonymous map provides address space randomization by default (for the raw pool location), whereas brk/sbrk extends a limit and because of this are predictable.

Re:Writing a Memory Allocator (2019) by DmitrySoshnikov on Sunday December 13, 2020 @04:04PM (#25410266)
sbrk is emulated via mmap today on MacOS and others, this is an abstraction (for bump allocation), not just a function

you may implement custom sbrk via mmap easily

Re:Writing a Memory Allocator (2019) by mhh__ on Sunday December 13, 2020 @04:23AM (#25405332)
sbrk isn't even supported on some platforms anymore, if I'm not mistaken
Re:Writing a Memory Allocator (2019) by astrange on Sunday December 13, 2020 @02:18AM (#25404921)
Thread-based allocators are a bit overrated. In apps it’s quite common for objects to migrate across threads, especially from workers to the main thread.
Re:Writing a Memory Allocator (2019) by praptak on Sunday December 13, 2020 @02:41AM (#25404991)
That's not the point at all. Having a per-thread pool means synchronization-free alloc.

What happens with the object afterwards is not relevant.

Re:Writing a Memory Allocator (2019) by fh973 on Sunday December 13, 2020 @02:45AM (#25405001)
But objects are passed around between threads and another thread might want to free it?
Re:Writing a Memory Allocator (2019) by praptak on Sunday December 13, 2020 @02:48AM (#25405014)
Then it goes to the freeing thread memory pool. Thread-aware mallocs know this and have clever balancing mechanisms for situations like producer-consumer, where one thread allocs a lot and another frees a lot.

Edit: wrong, I re-checked. The solution to this is that marking an object as free is lock-free. The freed objects do not immediately change thread pools.

Re:Writing a Memory Allocator (2019) by saagarjha on Saturday December 12, 2020 @11:05PM (#25404222)
It’s a great talk, but something I would watch after learning how to implement a malloc as it talks about other kinds of allocators and how to make them composable rather than say “how would I encode a freelist”.
Re:Writing a Memory Allocator (2019) by mhh__ on Saturday December 12, 2020 @09:08PM (#25403688)
His implementation of it can be found in the D programming languages standard library.

You can build a jemalloc in about 10 lines roughly.