In malloc, why use brk at all? Why not just use mmap?

前端 未结 5 1592
失恋的感觉
失恋的感觉 2021-02-13 10:28

Typical implementations of malloc use brk/sbrk as the primary means of claiming memory from the OS. However, they also use mmap

5条回答
  •  猫巷女王i
    2021-02-13 10:51

    I don't know the details on Linux specifically, but on FreeBSD for several years now mmap is preferred and jemalloc in FreeBSD's libc has sbrk() completely disabled. brk()/sbrk() are not implemented in the kernel on the newer ports to aarch64 and risc-v.

    If I understand the history of jemalloc correctly, it was originally the new allocator in FreeBSD's libc before it was broken out and made portable. Now FreeBSD is a downstream consumer of jemalloc. Its very possible that its preference for mmap() over sbrk() originated with the characteristics of the FreeBSD VM system that was built around implementing the mmap interface.

    It's worth noting that in SUS and POSIX brk/sbrk are deprecated and should be considered non-portable at this point. If you are working on a new allocator you probably don't want to depend on them.

提交回复
热议问题