News for hackers. Stuff that matters.
Re:How fast can you allocate a large block of memory in C++? by blackrock on Thursday January 16, 2020 @02:21AM (#22062322)
Anyone want to take a stab at how malloc actually works? What’s under the hood? What data structures and algorithms does it use?
Re:How fast can you allocate a large block of memory in C++? by jabl on Thursday January 16, 2020 @02:33AM (#22062371)
In a comment the blog author says he's using Ubuntu and glibc malloc. And glibc malloc has a threshold (128 kB or thereabouts) where it switches to using mmap(). So in practice this benchmark is a test of how the Linux kernel implements the mmap() syscall, and how the compiler implements the zeroing loop.
Re:How fast can you allocate a large block of memory in C++? by Const-me on Thursday January 16, 2020 @08:30AM (#22064299)
> And glibc malloc has a threshold (128 kB or thereabouts) where it switches to using mmap()

Very similar on Windows. In modern CRT, malloc is a thin wrapper over HeapAlloc WinAPI. That one has a threshold (512kb or 1MB depending on 32- or 64-bit process) where it switches to VirtualAlloc.

Re:How fast can you allocate a large block of memory in C++? by pjc50 on Thursday January 16, 2020 @08:02AM (#22064065)
K&R actually contains an example malloc()/free() implementation using a "freelist" - i.e. a list of spare memory blocks held as a linked list in the "unallocated" (or de-allocated) blocks. Memory is retrieved from the OS using the ugly sbrk() interface.