3 ms·
I have had an absolute hell of a time finding a memory allocator that will work in a fundamentally single-threaded environment (where I need compliant C only wi
by saurik 9y ago
I have had an absolute hell of a time finding a memory allocator that will work in a fundamentally single-threaded environment (where I need compliant C only with no library functions or processor features that offer anything for threads); do you have a suggestion? Everyone seems to care a lot about how to "make it work well with 2 threads", at which point the very premise tends to make the allocator unusable for me :(.
- pm215 9y agoIf you have no threads at all, I would naively think it should be fairly simple to just stub out the locking and similar calls. Have you looked at dlmalloc, which lets you build with USE_LOCKS=0 to turn off the threadsafety?
- saurik 9y agoThank you so much! I had stared at a few malloc implementations (both standalone and from a few libcs), and I had finally gotten to the point (after seeing how thouroughly integrated the threading often is, with not just atomic variables and locks but sometimes thread local storage) that I had largely given up on any implementation that even mentioned multithreading as a feature, but I clearly should have kept going: I just managed to get dlmalloc compiled using nothing but mmap, munmap, abort, memcpy, memset, sysconf (which should be fine), and time (which also should be fine). That just did not seem to be plausible with TCMalloc or jemalloc. Again: thanks so much!!
- justincormack 9y agoNetBSD and Musl libc are good sources for simpler implementations. From memory the NetBSD jemalloc is an early simple single file version that is pretty easy to adapt.
- apenwarr 9y agoLook up "buddy system allocator." The algorithm is super fast and easy to understand and implement. It was also the one used in the very first Linux kernels (probably for those reasons).
- nathell 9y agoIt still is, as the underlying page allocator for the SLAB/SLUB/SLOB allocators. See http://elixir.free-electrons.com/linux/latest/source/mm/page_alloc.c http://elixir.free-electrons.com/linux/latest/source/mm/page...
- gjm11 9y agoSuper-fast, sure, but it wastes a ton of space on typical workloads.