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

发布于 2024-12-20 19:25:15 字数 149 浏览 3 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2024-12-27 19:25:15

通常,mallocfree 使用较低级别的虚拟内存分配服务,并使用 系统调用,例如 mmapmunmap (也许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标准对于mallocfree非常模糊。我非常确定以下函数遵循标准的文字(但不是精神):

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

当然,您将相应地编写 callocrealloc 代码。

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


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

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

Very often, malloc and free 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). Often malloc prefers to reuse previously freed memory space when relevant. Most malloc 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 the pmap 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 and free. I'm pretty sure that the following functions are respecting the letter (but not the spirit) of the standard:

 /* 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 calloc and realloc accordingly.

(BTW every code using malloc should test against its failure, but some -incorrectly- don't; malloc can return NULL 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.

思念满溢 2024-12-27 19:25:15

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

C 标准仅定义了这些函数的行为方式以及它们期望的行为。如何实施它们留给每个实施。

简而言之,它们是您使用的实现的实现细节。

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

malloc and free 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.)

寄人书 2024-12-27 19:25:15

如果您使用不同的内存分配器,您还可以使用 mallocfree 的替代实现。例如,hoard 内存分配器有时用于提高多线程应用程序的性能。

You can also use an alternate implementation for malloc and free if you use a different memory allocator. For example, the hoard memory allocator is sometimes used to improve performance of multithreaded applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文