内存管理中的大小的重新分配功能C ++ 1y [英] Sized Deallocation Feature In Memory Management in C++1y

查看:251
本文介绍了内存管理中的大小的重新分配功能C ++ 1y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已建议在C ++ 1y中包含大小调整功能。但是,我想了解如何影响/改进 当前的c ++ 低级内存管理 p>

此提案位于 N3778 strong>,程序员可以定义一个静态成员函数操作符
delete,它使用一个大小参数指示要删除的对象
的大小。等效全局运算符delete不可用。
此省略具有不良的性能后果。



现代内存分配器通常按大小类别分配,对于
空间效率原因,不存储
对象附近的对象的大小。然后,deallocation需要搜索包含对象的大小类别
存储。这种搜索可能很昂贵,特别是因为搜索数据结构通常不在内存
缓存中。解决方案是允许实现和程序员
定义全局操作符的大小版本delete

编译器在大小可用的版本可用时优先于未定义的
版本调用大小的版本。


从上面的段落,它看起来像操作员删除所需的大小信息可以保持,因此通过使用的程序。这将避免任何搜索大小而释放。但是根据我的理解,在分配时,内存管理将大小信息存储在某种头部(在

 <$ c>  $ c> T * p = new T(); 
//现在的大小信息将存储在标题中
// *(char *)(p - 0x4)= size;
//这将在我们删除内存时使用。
delete p;

如果大小信息存储在标题中,为什么解除分配需要搜索?
看起来我缺少一些明显的东西,并且不完全理解这个概念。



此外,这个功能如何在程序中使用,同时处理低C ++中的高级内存管理。希望有人能帮我理解这些概念。

解决方案

如你的报价:


[现代内存分配器]出于空间效率的原因,不要存储对象附近的对象大小。


为了添加显式大小信息而增加每个分配的大小显然将使用比替代方案更多的存储器,例如每个分配池存储大小信息,或者在重新分配时提供信息。 p>

Sized Deallocation feature has been proposed to include in C++1y. However I wanted to understand how it would affect/improve the current c++ low-level memory management?

This proposal is in N3778, which states following about the intent of this.

With C++11, programmers may define a static member function operator delete that takes a size parameter indicating the size of the object to be deleted. The equivalent global operator delete is not available. This omission has unfortunate performance consequences.

Modern memory allocators often allocate in size categories, and, for space efficiency reasons, do not store the size of the object near the object. Deallocation then requires searching for the size category store that contains the object. This search can be expensive, particularly as the search data structures are often not in memory caches. The solution is to permit implementations and programmers to define sized versions of the global operator delete. The compiler shall call the sized version in preference to the unsized version when the sized version is available.

Well from above paragraph, it look like the size information which operator delete require can be maintained and hence passed by used program. This would avoid any search for the size while deallocation. But as per my understanding, while allocating, memory management store the size information in some sort of header(explained boundary-tag method in dlmalloc), which would be used while deallocation.

T* p = new T();
// Now size information would be stored in the header
// *(char*)(p - 0x4) = size;
// This would be used when we delete the memory????.
delete p;

If size information is stored in the header, why deallocation require searching for it? It looks like I am missing something obvious and did not understand this concepts completely.

Additionally,how this feature can be used in program while dealing with the low level memory management in C++. Hope that somebody will help me to understand these concept.

解决方案

As in your quote:

[Modern memory allocators] for space efficiency reasons, do not store the size of the object near the object.

Increasing the size of every allocation in order to add explicit size information is obviously going to use more memory than alternatives such as storing the size information once per allocation pool, or supplying the information upon deallocation.

这篇关于内存管理中的大小的重新分配功能C ++ 1y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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