Linux malloc 和 free 的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6485586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:45:45  来源:igfitidea点击:

Code for malloc and free

clinuxmallocfree

提问by MetallicPriest

Where can I find the code for malloc my gcc compiler is using at the moment? I actually want to write my own malloc function which will be a little different from the original one. I know I can use hooks et all, but I want to see the real code.

我在哪里可以找到我的 gcc 编译器目前正在使用的 malloc 代码?我实际上想编写自己的 malloc 函数,该函数与原始函数略有不同。我知道我可以使用钩子等,但我想看看真正的代码。

采纳答案by Cédric Julien

The POSIX interface of malloc is defined here.

malloc 的 POSIX 接口在这里定义

If you want to find out how the C library in GNU/Linux (glibc) implements malloc, go and get the source code from http://ftp.gnu.org/gnu/glibc/and look at the malloc/malloc.cfile.

如果您想了解 GNU/Linux (glibc) 中的 C 库如何实现malloc,请从http://ftp.gnu.org/gnu/glibc/获取源代码并查看malloc/malloc.c文件。

There is also the base documentation of the Memory Allocator by Doug Leathat describes the theory of a m(emory)alloc(ator) (read this carrefully, and then decide if you really need to implement your own malloc).

还有Doug LeaMemory Allocator的基本文档,它描述了m(emory) alloc(ator) 的理论(仔细阅读,然后决定是否真的需要实现自己的 malloc)。

回答by wallyk

Look in the appropriate release of glibc at the old release site1or here. For example, if you are using glib 2.9, it is in this archive. Look for the file malloc/malloc.c.

旧版本站点1此处查看glibc 的相应版本。例如,如果您使用的是 glib 2.9,它就在这个存档中。查找文件malloc/malloc.c

You will see that it is not a trivial piece of library code.

您将看到它不是一段微不足道的库代码。



1It looks like they changed the directory structure after glibc-2.9.

1看起来他们在 glibc-2.9 之后改变了目录结构。

回答by Karoly Horvath

malloc()should be in glibc.

malloc()应该在 glibc 中。

Further Reading
Multithreading
C++ allocation tricks [Video]

进一步阅读
多线程
C++ 分配技巧 [视频]

回答by plafratt

I am able to browse the source online here.

我可以在这里在线浏览源代码。