任何理由重载全局的新和删除? [英] Any reason to overload global new and delete?

查看:165
本文介绍了任何理由重载全局的新和删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非您正在编写操作系统或嵌入式系统的部分,有什么理由这样做吗?我可以想象,对于一些被创建和销毁的特定类,经常重载内存管理函数或引入一个对象池可能会降低开销,但是在全局这些事情上做这些事情。

Unless you're programming parts of an OS or an embedded system are there any reasons to do so? I can imagine that for some particular classes that are created and destroyed frequently overloading memory management functions or introducing a pool of objects might lower the overhead, but doing these things globally?

添加

我刚刚在重载的删除函数中发现了一个错误 - 内存不总是被释放。这是在一个不那么内存关键的应用程序。

Addition
I've just found a bug in an overloaded delete function - memory wasn't always freed. And that was in a not-so memory critical application. Also, disabling these overloads decreases performance by ~0.5% only.

推荐答案

我们重载我工作的全局新的和删除操作符很多原因:

We overload the global new and delete operators where I work for many reasons:


  • 池化所有小分配 - 减少开销,减少碎片, alloc-heavy apps

  • framing 已知生命周期的分配;忽略所有的释放,直到这个时期结束,然后将所有的释放在一起我们使用本地运算符重载而不是全局运算符)

  • 对齐调整 - 缓存行边界等。

  • strong> alloc fill - 帮助暴露未初始化变量的使用

  • 免费填充 >
  • 延迟免费 - 提高免费填充的效果,偶尔提高成效

  • strong> fenceposts - 有助于显示缓冲区溢出,欠量程和偶尔的指针

  • 重定向存储器区域,或甚至保持单独的系统在存储器中分离(例如,嵌入式脚本语言或DSL)

  • 垃圾回收或清理 - 对于嵌入式脚本语言非常有用

  • 堆验证 - 您可以每N次分配/释放遍历堆数据结构以确保一切正常

  • accounting strong>泄漏跟踪和使用快照/统计信息(堆栈,分配年龄等)

  • pooling all small allocations -- decreases overhead, decreases fragmentation, can increase performance for small-alloc-heavy apps
  • framing allocations with a known lifetime -- ignore all the frees until the very end of this period, then free all of them together (admittedly we do this more with local operator overloads than global)
  • alignment adjustment -- to cacheline boundaries, etc
  • alloc fill -- helping to expose usage of uninitialized variables
  • free fill -- helping to expose usage of previously deleted memory
  • delayed free -- increasing the effectiveness of free fill, occasionally increasing performance
  • sentinels or fenceposts -- helping to expose buffer overruns, underruns, and the occasional wild pointer
  • redirecting allocations -- to account for NUMA, special memory areas, or even to keep separate systems separate in memory (for e.g. embedded scripting languages or DSLs)
  • garbage collection or cleanup -- again useful for those embedded scripting languages
  • heap verification -- you can walk through the heap data structure every N allocs/frees to make sure everything looks ok
  • accounting, including leak tracking and usage snapshots/statistics (stacks, allocation ages, etc)

新的/删除记帐的想法是非常灵活和强大的:你可以,例如,记录活动线程的每个alloc发生时的整个callstack,并聚合统计。如果你没有空间来保存它本地的任何原因,你可以通过网络发送堆栈信息。

The idea of new/delete accounting is really flexible and powerful: you can, for example, record the entire callstack for the active thread whenever an alloc occurs, and aggregate statistics about that. You could ship the stack info over the network if you don't have space to keep it locally for whatever reason. The types of info you can gather here are only limited by your imagination (and performance, of course).

我们使用全局重载,因为它很方便的挂起了很多常见的调试功能,以及根据我们从这些相同的重载收集的统计数据,在整个应用程序中进行彻底的改进。

We use global overloads because it's convenient to hang lots of common debugging functionality there, as well as make sweeping improvements across the entire app, based on the statistics we gather from those same overloads.

我们仍然使用自定义分配器为个别类型;在许多情况下,您可以通过提供自定义分配器来获得加速或功能。 STL数据结构的单个使用点远远超过了你可以从全局重载获得的一般加速。

We still do use custom allocators for individual types too; in many cases the speedup or capabilities you can get by providing custom allocators for e.g. a single point-of-use of an STL data structure far exceeds the general speedup you can get from the global overloads.

看看一些分配器和调试系统中的C / C ++,你会很快想出这些和其他想法:

Take a look at some of the allocators and debugging systems that are out there for C/C++ and you'll rapidly come up with these and other ideas:

  • valgrind
  • electricfence
  • dmalloc
  • dlmalloc
  • Application Verifier
  • Insure++
  • BoundsChecker
  • ...and many others... (the gamedev industry is a great place to look)

一本古老但却具有创意的书籍是写作实体代码,其中讨论了您可能想要提供自定义)

(One old but seminal book is Writing Solid Code, which discusses many of the reasons you might want to provide custom allocators in C, most of which are still very relevant.)

显然,如果你可以使用任何这些精细的工具,你会想这样做,而不是滚动自己的。

Obviously if you can use any of these fine tools you will want to do so rather than rolling your own.

有些情况下,它更快,更容易,更少的商业/法律麻烦,没有什么可用于您的平台,或只是更有指导:挖和写全局超载。

There are situations in which it is faster, easier, less of a business/legal hassle, nothing's available for your platform yet, or just more instructive: dig in and write a global overload.

这篇关于任何理由重载全局的新和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆