简体   繁体   English

是否可以在不使用brk()系统调用的情况下实现Malloc函数

[英]Can we Implement Malloc function without use of brk() system call

Is it possible to implement the malloc library function without using brk ? 是否可以在不使用brk情况下实现malloc库函数? Can I use sbrk(0) to find out the current position of the program break, and then use sbrk(size) to increment it? 我可以使用sbrk(0)找出程序中断的当前位置,然后使用sbrk(size)递增它吗?

If yes, then why was brk added in the first place? 如果是,那么为什么首先添加brk

The use of brk and sbrk is discouraged in most current Unixes. 在大多数当前的Unix中不鼓励使用brksbrk Instead, malloc typically calls mmap anonymously (without file backing), and the address of a page is returned. 取而代之的是, malloc通常以匿名方式调用mmap (无文件支持),并返回页面地址。

brk and sbrk assume a contiguous address space, which is no longer the case. brksbrk假定使用连续的地址空间,现在不再如此。 Nowadays, a process may have many mappings in different ranges, with unmapped gaps in between. 如今,一个过程可能在不同范围内具有许多映射,并且它们之间存在未映射的间隙。

The POSIX standard also warns us that: POSIX标准还警告我们:

The behaviour of brk() and sbrk() is unspecified if an application also uses any other memory functions (such as malloc(), mmap(), free()). 如果应用程序还使用任何其他内存函数(例如malloc(),mmap(),free()),则未指定brk()和sbrk()的行为。 Other functions may use these other memory functions silently. 其他功能可能会静默使用这些其他存储功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM