C++ 编写自己的内存管理器

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

Write your own memory manager

c++memory-management

提问by Tobias Langner

I'd like to write my own memory manager. The target language is C++ and the goal of the memory manager is mainly to help debugging. It should detect double frees, memory overwrite and so on. And of course - I'd like to learn about memory management.

我想编写自己的内存管理器。目标语言是 C++,内存管理器的目标主要是帮助调试。它应该检测双重释放、内存覆盖等。当然 - 我想了解内存管理。

Can someone give me a hint so resources where I can learn how to write such a memory manager?

有人可以给我一个提示,以便我可以学习如何编写这样的内存管理器的资源吗?

Thank you for your help.

感谢您的帮助。

回答by Falaina

I think this is a very interesting project that you might learn a lot from. Here's a little bit of reading material on the subject of memory management. It goes over some of the basics of memory management, leads into a simple malloc implementation, then touches on a couple more advanced topics.

我认为这是一个非常有趣的项目,你可以从中学到很多东西。这里有一些关于内存管理主题的阅读材料。它介绍了内存管理的一些基础知识,引出一个简单的 malloc 实现,然后涉及几个更高级的主题。

Inside memory management

内部内存管理

Also, since you mention that you want to make a memory manager that is useful for debugging, you may want to consider reading this paper by the developers of Memcheck/Valgrind (A wonderful memory debugger for Linux). It details how they track all the the metadata (whether or not a particular byte is defined, intialized, etc.) in memchck. It's a bit detailed, but it's good reading on how to make a scalable and efficient dynamic memory checker.

此外,由于您提到要制作一个对调试有用的内存管理器,您可能需要考虑阅读 Memcheck/Valgrind(一个出色的 Linux 内存调试器)的开发人员撰写的这篇论文。它详细说明了它们如何跟踪 memchck 中的所有元数据(是否定义、初始化特定字节等)。它有点详细,但它是关于如何制作可扩展且高效的动态内存检查器的好读物。

How to Shadow Every Byte of Memory Used by a Program

如何隐藏程序使用的每个内存字节

回答by Norman Ramsey

Dave Hanson's C Interfaces and Implementationspresents first a standard memory manager and then a memory manager with a few debugging features. This would be a great starting point to learn from and extend.

Dave Hanson 的C 接口和实现首先介绍了一个标准的内存管理器,然后是一个带有一些调试功能的内存管理器。这将是学习和扩展的一个很好的起点。

Of course, if you actually want to diagnose memory problems in a running C or C++ program, you should use valgrind.

当然,如果您真的想诊断正在运行的 C 或 C++ 程序中的内存问题,您应该使用valgrind.

回答by Daniel A. White

I have seen some samples use a C-preprocessor macro for malloc. Its a clever idea. I am sure you could write something like that.

我已经看到一些示例将 C 预处理器宏用于malloc. 这是一个聪明的主意。我相信你可以写出这样的东西。

Here is a looks like a good starting point.

这是一个看起来不错的起点。

http://stevehanov.ca/blog/index.php?id=10

http://stevehanov.ca/blog/index.php?id=10

回答by Ralph M. Rickenbach

There is a great open source memory manager written in Delphi: fastMM4. It could be of value to have a look at it. It supports many of the features you want to implement and therefore might be a great showcase.

有一个用 Delphi 编写的很棒的开源内存管理器:fastMM4。看看它可能很有价值。它支持您想要实现的许多功能,因此可能是一个很好的展示。

回答by Salgar

As @Spence said, this has already been done many times. But for the sake of learning it is quite interesting.

正如@Spence 所说,这已经做过很多次了。不过为了学习,还是挺有意思的。

Might I suggest you take a look at ld's --wrap Hereas it is useful

我可以建议你看看 ld 的 --wrap在这里,因为它很有用

回答by Polaris878

I think you could start with a smart pointer implementation which uses basic reference counting in the background. These are memory management basics and will get your feet wet. From there you can use your implementations to make a more advanced memory manager.

我认为您可以从在后台使用基本引用计数的智能指针实现开始。这些是内存管理的基础知识,会让您大吃一惊。从那里您可以使用您的实现来制作更高级的内存管理器。

回答by Michael Burr

"Electric fence" is a basic starting point that might be helpful. Essentially it has custom implementations of malloc and free that provide the debugging.

“电动围栏”是一个可能有用的基本起点。本质上,它具有 malloc 和 free 的自定义实现,可提供调试功能。

However, AFAIK, it does not integrate into C++'s new/delete operators, though it would be not be too big of a stretch to provide custom global new/delete implementations that defer to the electric fence routines.

然而,AFAIK,它没有集成到 C++ 的 new/delete 运算符中,尽管提供遵循电子栅栏例程的自定义全局 new/delete 实现并不会太大。

回答by Timo Geusch

You can implement most of it in terms of malloc and free - in fact a lot of C++ memory managers are implemented, even if they don't have to be.

您可以根据 malloc 和 free 来实现其中的大部分——事实上,很多 C++ 内存管理器都被实现了,即使它们不是必须的。

You could start with a simple implementation that maintains a log of all allocations and deallocations, but forwards the regular allocations/deallocations to malloc and free. Obviously the simple implementation shouldn't use new/delete either...

您可以从一个简单的实现开始,该实现维护所有分配和释放的日志,但将常规分配/释放转发到 malloc 和 free。显然,简单的实现也不应该使用 new/delete ......

So, to start you can

所以,开始你可以

  • come up with the data structure that logs allocations and deallocations
  • Implement it mostly "C-style", although you can use placement new to ensure constructors are called
  • Implement global new and delete as wrappers that first log the access in the above data structure and then forward the call to malloc or free
  • 提出记录分配和解除分配的数据结构
  • 实现它主要是“C 风格”,尽管您可以使用放置 new 来确保调用构造函数
  • 实现 global new 和 delete 作为包装器,首先在上述数据结构中记录访问,然后将调用转发到 malloc 或 free

回答by Spence

Whats your platform? Just thinking whether valgrind or lint might be able to help you before you go and try to reimplement the kitchen sink?

你的平台是什么?在您尝试重新安装厨房水槽之前,只是想一下 valgrind 或 lint 是否可以帮助您?