简体   繁体   English

malloc和free是如何实现的?

[英]How are malloc and free implemented?

I want to implement my own dynamic memory management system in order to add new features that help to manage memory in C++. 我想实现自己的动态内存管理系统,以添加有助于在C ++中管理内存的新功能。

I use Windows (XP) and Linux (Ubuntu). 我使用Windows(XP)和Linux(Ubuntu)。 What is needed to implement functions like 'malloc' and 'free'? 实现'malloc'和'free'等功能需要什么? I think that I have to use lowest level system calls. 我认为我必须使用最低级别的系统调用。

For Windows, I have found the functions: GetProcessHeap, HeapAlloc, HeapCreate, HeapDestroy and HeapFree. 对于Windows,我找到了函数:GetProcessHeap,HeapAlloc,HeapCreate,HeapDestroy和HeapFree。

For Linux, I have not found any system calls for heap management. 对于Linux,我没有找到任何系统调用堆管理。 On Linux, malloc and free are system calls, are not they? 在Linux上,malloc和free是系统调用,不是吗?

Thanks 谢谢

Edit: 编辑:
C++ does not provide garbage collector and garbage collector is slow. C ++不提供垃圾收集器和垃圾收集器很慢。 Some allocations are easy to free, but there are allocations that needs a garbage collector. 有些分配很容易免费,但有些分配需要垃圾收集器。

I want to implement these functions and add new features: 我想实现这些功能并添加新功能:
* Whenever free() be called, check if the pointer belongs to heap. *每当调用free()时,检查指针是否属于堆。
* Help with garbage collection. *帮助垃圾收集。 I have to store some information about the allocated block. 我必须存储有关已分配块的一些信息。
* Use multiple heaps (HeapCreate/HeapDestroy on Windows). *使用多个堆(Windows上的HeapCreate / HeapDestroy)。 I can delete an entire heap with its allocated blocks quickly. 我可以快速删除整个堆及其分配的块。

On linux, malloc and free are not system calls. 在linux上,malloc和free不是系统调用。 malloc/free obtains memory from the kernel by extending and shrinking(if it can) the data segment using the brk system calls as well as obtaining anonymous memory with mmap - and malloc manages memory within those regions. malloc / free通过使用brk系统调用扩展和收缩(如果可以)数据段以及使用mmap获取匿名内存来从内核获取内存 - malloc管理这些区域内的内存。 Some basic information any many great references can be found here 一些基本信息可以在这里找到很多很棒的参考资料

In *nix, malloc() is implemented at the C library level. 在* nix中,malloc()在C库级实现。 It uses brk()/sbrk() to grow/shrink the data segment, and mmap/munmap to request/release memory mappings. 它使用brk()/ sbrk()来扩展/收缩数据段,使用mmap / munmap来请求/释放内存映射。 See this page for a description of the malloc implementation used in glibc and uClibc. 有关glibc和uClibc中使用的malloc实现的说明,请参阅此页面

If you are simply wrapping the system calls then you are probably not gaining anything on using the standard malloc - thats all they are doing. 如果您只是简单地包装系统调用,那么您可能无法使用标准的malloc - 这就是他们所做的一切。

It's more common to malloc (or HeapAlloc() etc ) a single block of memory at the start of the program and manage the allocation into this yourself, this can be more efficient if you know you are going to be creating/discarding a lot of small blocks of memory regularly. 在程序开始时malloc(或HeapAlloc()等)的单块内存更常见并自行管理分配,如果你知道你要创建/丢弃大量的内存,这会更有效率小块记忆经常。

brk is the system call used on Linux to implement malloc and free . brk是Linux上用于实现mallocfree的系统调用。 Try the man page for information. 尝试手册页以获取信息。

You've got the Windows stuff down already. 你已经把Windows的东西搞定了。

Seeing the other answers here, I would like to note that you are probably reinventing the wheel; 看到这里的其他答案,我想指出你可能正在重新发明轮子; there are many good malloc implementations out there already. 那里已经有很多很好的malloc实现了。 But programming malloc is a good thought exercise - take a look here for a nice homework assignment (originally CMU code) implementing the same. 但是编写malloc是一个很好的思考练习 - 看看这里有一个很好的家庭作业(最初的CMU代码)实现相同的。 Their shell gives you a bit more than the Linux OS actually does, though :-). 他们的shell比Linux操作系统实际上提供了更多,但是:-)。

garbage collector is slow 垃圾收集器很慢

This is a completely meaningless statement. 这是一个完全没有意义的陈述。 In many practical situations, programs can get a significant performance boost by using a Garbage Collector, especially in multi-threaded scenarios. 在许多实际情况中,程序可以通过使用垃圾收集器获得显着的性能提升,尤其是在多线程场景中。 In many other situations, Garbage Collectors do incur a performance penalty. 在许多其他情况下,垃圾收集器确实会导致性能下降。

Try http://www.dent.med.uni-muenchen.de/~wmglo/malloc-slides.html for pointers. 请尝试http://www.dent.med.uni-muenchen.de/~wmglo/malloc-slides.html获取指示。

This is a brief performance comparison, with pointers to eight different malloc/free implementations. 这是一个简短的性能比较,指向八个不同的malloc / free实现。 A nice starting point, because a few good reference statistics will help you determine whether you've improved on the available implementations - or not. 一个很好的起点,因为一些好的参考统计数据将帮助您确定是否已经改进了可用的实现 - 或者没有。

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

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