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

前端 未结 5 1578
失恋的感觉
失恋的感觉 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条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-13 10:59

    The obvious advantage is that you can grow the last allocation in place, which is something you can't do with mmap(2) (mremap(2) is a Linux extension, not portable).

    For naive (and not-so-naive) programs which are using realloc(3) eg. to append to a string, this translates in a 1 or 2 orders of magnitude speed boost ;-)

提交回复
热议问题