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

前端 未结 5 1576
失恋的感觉
失恋的感觉 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:58

    mmap() didn't exist in the early versions of Unix. brk() was the only way to increase the size of the data segment of the process at that time. The first version of Unix with mmap() was SunOS in the mid 80's, the first open-source version was BSD-Reno in 1990.

    And to be usable for malloc() you don't want to require a real file to back up the memory. In 1988 SunOS implemented /dev/zero for this purpose, and in the 1990's HP-UX implemented the MAP_ANONYMOUS flag.

    There are now versions of mmap() that offer a variety of methods to allocate the heap.

提交回复
热议问题