简体   繁体   English

C ++分配器在销毁/复制/移动时应如何处理其分配的内存?

[英]How should a C++ allocator handle its allocated memory when it is destroyed/copied/moved?

I am currently writing an allocator that should be usable by C++ standard data structures, ie, it implements the Allocator concept. 我目前正在编写一个应该由C ++标准数据结构使用的分配器,即,它实现了分配器的概念。

The allocator is quite simple: It allocates chunks of x objects and always hands out the next object if the current chunk is not full, otherwise it allocates a new chunk. 分配器非常简单:它分配x对象的块,如果当前块未满,则始终分发下一个对象,否则分配一个新的块。

Now my question is: How to handle these chunks when the allocator is destroyed/copied/moved? 现在我的问题是:当分配器被销毁/复制/移动时,如何处理这些块? The allocator concept says nothing about what must happen in these cases. 分配器概念没有说明在这些情况下必须发生的情况。

Here are my thoughts: 这是我的想法:

  • Destruction: The allocator may destroy all its chunks. 销毁:分配器可能销毁其所有块。 But then, no object that uses any of the allocated objects may outlive the allocator. 但是,使用任何已分配对象的对象都不会超过分配器的寿命。
  • Copying: The most straightforward idea would be to copy the chunks. 复制:最直接的想法是复制块。 But on second thought, this makes no sense: Nobody knows the address of the objects in the copied chunks, so they are just copied without any benefit. 但是,再三考虑,这没有任何意义:没人知道复制的块中对象的地址,因此仅复制它们就没有任何好处。 Maybe a copied allocator should start with an empty list of chunks. 也许复制的分配器应以空块列表开头。
  • Moving: The chunks should be moved to the new allocator. 移动:应该将块移动到新的分配器。 The old one should be left with an empty list of chunks. 旧的应该留空的块列表。

Are my assumptions correct? 我的假设正确吗? If not, then why and where is this defined? 如果没有,那么为什么在何处定义?

Allocators usually are lightweight objects that can be copied around and destroyed, eg in the standard container classes. 分配器通常是轻量级的对象,可以在周围复制和销毁,例如在标准容器类中。 Therefore they should not do the heavy memory management themselves but relay it to some more permanent memory manager object. 因此,他们不应该自己执行繁重的内存管理,而应将其中继给其他一些永久性的内存管理器对象。 If you do that, the lifetime of the memory chunks does not depend on the allocator lifetimes but on the lifetime of the memory manager object. 如果这样做,则内存块的生存期不取决于分配程序的生存期,而是取决于内存管理器对象的生存期。 The lifetime thoughts therefore have to be applied to both types of objects: 因此,一生的想法必须应用于两种类型的对象:

Allocator (short lifetime): 分配器 (寿命短):

  • Copying/Moving: copy the reference to the memory manager. 复制/移动:将引用复制到内存管理器。
  • Destruction: either does nothing (external lifetime management of the memory manager) or possibly destroys the memory manager, eg each allocator has a shared_ptr to the memory manager 破坏:不执行任何操作(内存管理器的外部生命周期管理)或可能破坏内存管理器,例如,每个分配器都具有与内存管理器的shared_ptr

Memory manager (long lifetime): 内存管理器 (使用寿命长):

  • Copying should be forbidden, it makes no sense to duplicate the manager and its managed storage 应该禁止复制,复制管理器及其托管存储是没有意义的
  • Moving could be allowed, but does not make much sense. 可以允许移动,但意义不大。 The memory manager could evene be a singleton-like class, ie one fixed instance that does not need to be moved around. 内存管理器甚至可以是类单例类,即一个不需要移动的固定实例。
  • Destruction should involve the destruction of the managed memory, since no other object knows how to deallocate the managed storage. 销毁应涉及销毁托管内存,因为没有其他对象知道如何取消分配托管存储。

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

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