Linux malloc/free 是系统调用还是 libc 提供的库例程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8457278/
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-06 03:34:55  来源:igfitidea点击:

Is malloc/free a syscall or a library routine provided by libc?

clinuxsystem-callslibc

提问by Pwn

If malloc/free is implemented as a library routine in libc, then is it implemented on top of the sbrksyscall or the mmapsyscall, or something else?

如果 malloc/free 在 libc 中作为库例程实现,那么它是在sbrk系统调用或mmap系统调用之上实现的,还是其他什么?

And to be general, does the function declared in sys/syscall.hcontains ALL the system calls in the target machine?

一般而言,在sys/syscall.h中声明的函数是否包含目标机器中的所有系统调用?

采纳答案by Alok Save

mallocand freeare standard C library functions which are to be implemented by each C implementation.

mallocfree是标准 C 库函数,由每个 C 实现实现。

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.

C 标准仅定义了这些函数的行为方式以及对它们的预期行为。如何在每个实现中实现它们。

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.)

(“实现”由编译器、链接器、运行时库和其他一些东西组成。)

回答by Basile Starynkevitch

Very often, mallocand freeare using lower-level virtual memory allocation services and allocating several pages (or even megabytes) at once, using system callslike mmapand munmap(and perhaps sbrk). Often mallocprefers to reuse previouslyfreed memory space when relevant. Most mallocimplementations use various and different strategies for "large" and "small" allocations, etc...

很多时候,malloc并且free使用较低级别的虚拟内存分配服务并一次分配多个页面(甚至兆字节),使用系统调用,mmapmunmap(可能还有sbrk)。通常malloc更喜欢在相关时重用以前的free内存空间。大多数malloc实现使用各种不同的策略来进行“大”和“小”分配等......

Notice that virtual address spacecan 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/mapsfor your own one or /proc/1234/maps- also the pmap 1234command - for process of pid 1234).

请注意,可以限制虚拟地址空间,例如使用setrlimit(2)。在 Linux 上使用pmap(1)proc(5)以了解有关某个进程的虚拟地址空间的更多信息(例如,/proc/self/maps对于您自己的进程或/proc/1234/maps- 也是pmap 1234命令 - 用于 pid 1234 进程)。

You could look at your GNU libcsource code, look into the source code of other C standard libraries (such as musl-libc), read about mallocimplementations, choose some other onesor implement your own, or use straceto find out experimentally.

您可以查看您的 GNU libc源代码,查看其他 C 标准库(例如musl-libc)的源代码,阅读malloc实现,选择其他一些或实现您自己的,或者使用strace进行实验找出。

Read the syscalls man page(i.e. syscalls(2)) and the file <asm/unistd.h>for a list of system calls.

阅读系统调用手册页(即syscalls(2))和<asm/unistd.h>系统调用列表文件。



a very fast malloc

一个非常快 malloc

(I believe that this could be the fastest implementation of malloc; however it is not very useful; it is conforming to the standards)

(我相信这可能是 最快的实现malloc;但是它不是很有用;它符合标准)

I strongly believe that the C standard is very vague about mallocand free. I'm pretty sure that the following functions are respecting the letter (but not the spirit) of the standard:

我坚信 C 标准对malloc和非常模糊free。我很确定以下功能尊重标准的文字(但不是精神):

 /* politically incorrect, but very probably standard conforming */
 void *malloc (size_t sz) { if (sz>0) errno = ENOMEM; return NULL; }
 void free(void*ptr) { }

Of course you'll code callocand reallocaccordingly.

当然,你的代码callocrealloc相应。

(BTW every code using mallocshould test against its failure, but some -incorrectly- don't; malloccan return NULLon failure and people should test against that case)

(顺便说一句,每个使用的代码malloc都应该针对其失败进行测试,但有些 - 错误地 - 不;malloc可以返回NULL失败,人们应该针对这种情况进行测试)



The GNU libc gives you hooksfor your own mallocfunctions (and you could even probably use Boehm's Garbage Collectortransparently thru them). These hooks could become deprecated and are non-standard.

GNU libc为你自己的函数提供了钩子malloc(你甚至可以通过它们透明地使用Boehm 的垃圾收集器)。这些钩子可能会被弃用并且是非标准的。

If using GNU libc, look also into mallinfo(3)and malloc_stat(3)and related functions.

如果使用 GNU libc,还要查看mallinfo(3)malloc_stat(3)以及相关函数。

回答by Gabriel Southern

You can also use an alternate implementation for mallocand freeif you use a different memory allocator. For example, the hoard memory allocatoris sometimes used to improve performance of multithreaded applications.

您也可以使用备用实施mallocfree如果您使用的是不同的内存分配器。例如,囤积内存分配器有时用于提高多线程应用程序的性能。