malloc/free 是系统调用还是 libc 提供的库例程?
如果 malloc/free 是作为 libc 中的库例程实现的,那么它是在 sbrk 系统调用或 mmap 系统调用之上实现的,还是其他东西?
一般来说,sys/syscall.h 中声明的函数是否包含目标计算机中的所有系统调用?
If malloc/free is implemented as a library routine in libc, then is it implemented on top of the sbrk syscall or the mmap syscall, or something else?
And to be general, does the function declared in sys/syscall.h contains ALL the system calls in the target machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,
malloc
和free
使用较低级别的虚拟内存分配服务,并使用 系统调用,例如 mmap 和 munmap (也许sbrk)。通常,malloc 更喜欢在相关时重用之前释放的内存空间。大多数malloc
实现使用各种不同的策略来进行“大”和“小”分配等...请注意,虚拟地址空间可以被限制,例如使用setrlimit(2)。在 Linux 上使用 pmap(1) 和 proc(5) 了解有关某些进程的虚拟地址空间的更多信息(例如 <代码>/proc/self/maps 为您自己的一个或
/proc/1234/maps
- 也是pmap 1234
命令 - 用于 pid 1234 的进程)。你可以查看你的 GNU libc 源代码,查看其他 C 标准库的源代码(例如musl-libc),阅读
malloc
实现,选择一些其他< /a> 或实现您自己的,或使用 strace 通过实验找出答案。阅读系统调用手册页(即syscalls(2)) 和文件
系统调用列表。非常快的
malloc
(我相信这可能是
malloc
最快的实现;但是它不是很有用;它符合标准,例如 < a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf" rel="noreferrer">n1570 或更好)我强烈相信C标准对于
malloc
和free
非常模糊。我非常确定以下函数遵循标准的文字(但不是精神):当然,您将相应地编写
calloc
和realloc
代码。(顺便说一句,每个使用
malloc
的代码都应该测试其失败情况,但有些错误地没有这样做;malloc
可能会返回NULL
失败,人们应该针对这种情况进行测试)GNU libc 为您提供 hooks 用于您自己的
malloc
函数(您甚至可以使用 Boehm 的垃圾收集器 透明地通过它们)。这些钩子可能会被弃用并且成为非标准的。如果使用 GNU libc,还请查看 mallinfo(3) 和malloc_stat(3) 及相关函数。
Very often,
malloc
andfree
are using lower-level virtual memory allocation services and allocating several pages (or even megabytes) at once, using system calls like mmap and munmap (and perhaps sbrk). Oftenmalloc
prefers to reuse previouslyfree
d memory space when relevant. Mostmalloc
implementations use various and different strategies for "large" and "small" allocations, etc...Notice that virtual address space can be limited, e.g. with setrlimit(2). Use on Linux pmap(1) and proc(5) to learn more about the virtual address space of some process (e.g.
/proc/self/maps
for your own one or/proc/1234/maps
- also thepmap 1234
command - for process of pid 1234).You could look at your GNU libc source code, look into the source code of other C standard libraries (such as musl-libc), read about
malloc
implementations, choose some other ones or implement your own, or use strace to find out experimentally.Read the syscalls man page (i.e. syscalls(2)) and the file
<asm/unistd.h>
for a list of system calls.a very fast
malloc
(I believe that this could be the fastest implementation of
malloc
; however it is not very useful; it is conforming to the standards, e.g. n1570 or better)I strongly believe that the C standard is very vague about
malloc
andfree
. I'm pretty sure that the following functions are respecting the letter (but not the spirit) of the standard:Of course you'll code
calloc
andrealloc
accordingly.(BTW every code using
malloc
should test against its failure, but some -incorrectly- don't;malloc
can returnNULL
on failure and people should test against that case)The GNU libc gives you hooks for your own
malloc
functions (and you could even probably use Boehm's Garbage Collector transparently thru them). These hooks could become deprecated and are non-standard.If using GNU libc, look also into mallinfo(3) and malloc_stat(3) and related functions.
malloc
和free
是标准 C 库函数,由每个 C 实现实现。C 标准仅定义了这些函数的行为方式以及它们期望的行为。如何实施它们留给每个实施。
简而言之,它们是您使用的实现的实现细节。
(“实现”由编译器、链接器、运行时库以及可能的其他一些东西组成。)
malloc
andfree
are standard C library functions which are to be implemented by each C implementation.The C standard only defines the way in which these functions behave and the behavior expected from them. How they are to be implemented in left to each implementation.
In short they are implementation detail of the implementation you use.
(An "implementation" consists of the compiler, the linker, the runtime library, and probably a few other things.)
如果您使用不同的内存分配器,您还可以使用
malloc
和free
的替代实现。例如,hoard 内存分配器有时用于提高多线程应用程序的性能。You can also use an alternate implementation for
malloc
andfree
if you use a different memory allocator. For example, the hoard memory allocator is sometimes used to improve performance of multithreaded applications.