boost.pool如何实现对已分配内存的重用? [英] How does boost.pool achieve re-use of allocated memory?

查看:109
本文介绍了boost.pool如何实现对已分配内存的重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

关于boost.pool的我的上一个问题带我详细研究了boost.pool,现在我有一个补充问题可以最终确定我的理解.

My previous question about boost.pool led me to investigate boost.pool in detail, and now I have a supplementary question to finalize my understanding.

前奏

此参考声明了有关对象池模式的以下内容:

This reference states the following about the object pool pattern:

对象池模式是一种软件创建设计模式, 使用一组随时准备使用的已初始化对象,而不是 按需分配和销毁它们.

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand.

据我所知,boost.pool(简化后)主要通过基于element_type的大小的内存分配和管理方式来实现对象池模式,并返回指向已分配对象的简单指针:

From what I can tell, boost.pool (simplified) implements the object pool pattern by means of memory allocation and management mostly based on the size of an element_type, and returns a simple pointer to an allocated object:

element_type * malloc();
void free(element_type * p);

一个简单的boost示例还表明,不必显式free所获取的元素:

A simple boost example also shows that it is not necessary to explicitly free the acquired element:

X * const t = p.malloc();
... // Do something with t; don't take the time to free() it.

问题

我了解分配的内存将在销毁池对象时安全释放,但是池如何知道何时将客户端获取的内存块释放回池中,并且如果其接口移回则可重用直接指向element_type的指针,但仍不需要调用free()吗?即,如果不能确定内存是否仍在使用中,增强池如何重用此内存?而且,如果它不重复使用此内存,那么是否甚至认为该模式与Wiki参考所解释的模式相同?

I understand that the allocated memory will be safely freed on destruction of the pool object, but how does the pool know when a block of memory acquired by a client has been released back into the pool and is reusable if its interface hands back a direct pointer to element_type, yet a call to free() is still not required? i.e. How can the boost pool re-use this memory if it cannot be certain that the memory is not still in use? And if it does not re-use this memory, is this even considered the same pattern as the one explained by the wiki reference?

推荐答案

如果不能确定提升池如何重用此内存, 内存还没用吗?

How can the boost pool re-use this memory if it cannot be certain that the memory is not still in use?

不能.实际上,它不会重用该内存.它仅保证在池被破坏时不会泄漏.

It can't. In fact it won't reuse that memory. It only guarantee that you will have no leaks when the pool is destroyed.

如果它不重复使用此内存,是否甚至认为这与Wiki参考所解释的模式相同?

And if it does not re-use this memory, is this even considered the same pattern as the one explained by the wiki reference?

您链接的文章说:对象池可以在以下方面显着提高性能:初始化类实例的成本很高的情况

来自提升池简介:通常在有大量小对象分配和释放的情况下使用池.

所以不,它们不是相同的模式.一种是重用昂贵的对象来构造(线程,opengl资源等).另一个用于管理许多小对象,使您比标准分配器所提供的控制更多.

So no, they are not the same pattern. One is meant to re-use objects which are expensive to construct (threads, opengl resources, etc.). The other is meant to manage a lot of small objects, giving you more control than the standard allocator gives.

如您所指出的,有两种使用池的方法:

As you pointed out, there are two ways of using pools:

  1. 作为分配器,在适当时调用malloc()/free().这是基本的池分配器用法,有助于减少内存碎片
  2. 构造大量临时对象,不必费心删除它们.

第二种情况的示例:假设您有一个图类,其中每个节点都使用指针存储其邻居.现在,您必须制作图形的深层副本.您将分配一堆新节点并将数据从旧节点复制到新节点,但是现在您必须初始化邻居指针,因此需要从旧指针到新指针的映射:

Example for the second case: imagine you have a graph class, where each node stores its neighbors using pointers. Now you have to make a deep copy of your graph. You will allocate a bunch of new nodes and copy the data from the old ones to the new ones, but now you have to initialize neighbors pointers, so you need a map from old pointers to new pointers:

std::map<node*,node*> old_ptr_to_new_ptr;

这是一个很好的示例,其中池分配器很有用(我不会详细介绍如何将池分配器与std容器一起使用):许多小对象(地图节点)将被一起删除.

This is a good example where pool allocators are useful (I won't go into detail about how to use pool allocators with std containers): a lot of small objects (map nodes) which are going to be deleted all together.

这篇关于boost.pool如何实现对已分配内存的重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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