移动 C++对象,尤其是 stl 容器,到特定的内存位置

发布于 2024-08-21 12:35:55 字数 1102 浏览 4 评论 0原文

我正在与一个内存管理器合作,有时需要对内存进行碎片整理。基本上,我将遍历内存管理器分配的对象列表并重新定位它们:

class A {
  SomeClass* data; // This member is allocated by the special manager
};

for(... each instance of A ...) 
    a.data = memory_manager.relocate(a.data);

memory_manager.relocate()memcpy() 数据内容移动到新位置,并返回指针。

虽然它对于 memcpy() C++ 类来说通常不是惯用的,但在这种情况下,考虑到我控制将与内存管理器一起使用的(少数)类的实现,它似乎是一个有用的解决方案。

问题是其中一个类使用 std::map ,就我而言,这是一个不透明的类。我当然不认为我可以memcpy()它。在任何情况下我可能都无法使用 std::map 。据我所知,它可以分配几块内存。

我能想到的最好的解决方法很简单。由于碎片内存管理器会将新的分配放在更有利的位置,因此我所需要做的就是重新分配它,然后删除旧的:

for(... each instance of A ...) {
    stl::map<something>* tmp = a.the_map;
    a.the_map = new stl::map<something>(tmp);
    delete tmp;
}



无论如何,这让我想知道:

C++ 是否具有将对象移动/复制到特定内存位置的语义或习惯用法?

是否可以将 stl 容器的内容移动到特定的内存位置?


编辑:虽然我没有指出,但我显然会将分配器参数传递给 std::map。根据我得到的信息丰富的答案,我意识到我在最初的问题中发布的解决方法可能是减少碎片的唯一方法。通过使用映射的复制构造函数(和分配器模板参数),映射使用的所有内存都将被正确地重新分配。

正如评论所指出的,这主要是一个理论问题。内存碎片很少是值得担心的事情。

I am working with a memory manager that, on occasion, wants to defragment memory. Basically, I will go through a list of objects allocated by the memory manager and relocate them:

class A {
  SomeClass* data; // This member is allocated by the special manager
};

for(... each instance of A ...) 
    a.data = memory_manager.relocate(a.data);

memory_manager.relocate() will memcpy() the contents of data to a new location, and return the pointer.

Although it's generally not idiomatic to memcpy() C++ classes, it seems to be a useful solution in this case, considering that I control the implementation of the (few) classes that will be used with the memory manager.

The problem is that one of those classes uses std::map, which is an opaque class as far as I am concerned. I certainly don't imagine I can memcpy() it. I may not be able to use std::map in any case. For all I know it could allocate several pieces of memory.

The best workaround I can think of is simple enough. Due to the fact that the fragmented memory manager will put new allocations at more beneficial locations, all I need to do is allocate it anew and then delete the old:

for(... each instance of A ...) {
    stl::map<something>* tmp = a.the_map;
    a.the_map = new stl::map<something>(tmp);
    delete tmp;
}

In any case, this lead me to wonder:

Does C++ have semantics or idioms to move/copy an object into a specific memory location?

Is it possible to move the contents of an stl container to a specific memory location?

Edit: Although I didn't point it out, I would obviously pass an allocator parameter to std::map. Based on the informative answers I got, I realize the workaround I posted in my initial question would probably be the only way to reduce fragmentation. By using the map's copy constructor (and the allocator template parameter), all memory used by the map would be properly re-allocated.

As a comment pointed out, this is mostly a theoretical problem. Memory fragmentation is rarely something to worry about.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离线来电— 2024-08-28 12:35:55

每次插入新的键值对时,映射都会分配一个节点来存储它。分配如何进行的详细信息由映射使用的分配器决定。

默认情况下,当您像 std::map 中那样创建映射时,将使用默认分配器,它会在堆上创建节点(即使用 new/ 删除)。

您不希望这样,因此您必须创建一个自定义分配器类,该类根据内存管理器的指示创建节点。

创建分配器类并不简单。 这段代码展示了如何做到这一点,你必须根据您自己的需要进行调整。

一旦你有了你的分配器类(假设你称之为MemManagerAllocator),你必须将你的映射定义为std::map,然后像使用普通地图一样使用它。

就我个人而言,我需要有一个非常严重的内存碎片问题才能陷入所有这些麻烦。

Everytime you insert a new key,value pair the map will allocate a node to store it. The details of how this allocation takes place are determined by the allocator that the map uses.

By default when you create a map as in std::map<K,V> the default allocator is used, which creates nodes on the heap (i.e., with new/delete).

You don't want that, so you'll have to create a custom allocator class that creates nodes as dictated by your memory manager.

Creating an allocator class is not trivial. This code shows how it can be done, you'll have to adapt it to your own needs.

Once you have your allocator class (let's say you call it MemManagerAllocator) you'll have to define your map as std::map<K, V, MemManagerAllocator> and then use it like you would use a regular map.

Personally, I would need to have a really bad problem of memory fragmentation to go into all that trouble.

想念有你 2024-08-28 12:35:55

您可以使用新的展示位置吗?

void* adress = new void[size_of(*old_map_ptr)]; // or wherever you want it in your memory
map<type> new_map* = new (adress) map<type>(*old_map_ptr);
delete old_map_ptr;

You could use placement new?

void* adress = new void[size_of(*old_map_ptr)]; // or wherever you want it in your memory
map<type> new_map* = new (adress) map<type>(*old_map_ptr);
delete old_map_ptr;
随心而道 2024-08-28 12:35:55

C++ 对象的内容将分散在堆周围。 STL 对象(例如容器或字符串等)可能会将其元数据存储在堆栈上(如果不将其放入动态内存中)...但是内容分散在堆周围。

跟踪对象的所有元素以及对这些元素的所有引用将是一项艰巨的任务。该语言必须提供某种形式的“钩子”或事件来跟踪记忆块之间的关系。

所以不,你不能只 memcpy 任意 STL 对象。

Afaik 重写 new 并不是万能药,它是一个“进程全局”概念,因此尝试将消息本地化到线程-对象-分配对是不可能的。这种本地化将使您能够将内存分组在一起。

您可以编写自己的容器,显式地使用自定义内存分配器(之所以显式,是因为您提供了必要的对象标识信息),并且有一些自定义分配器。

The contents of a C++ object will be scattered around the heap. A STL object for example a container or string etc might store its meta-data on your stack (if you don't put it in dynamic memory) ... however the contents are scattered arround the heap.

It will be a hard task to keep track of all the elements of an object, and all the references to these elements. The language has to provide some form of "hooks" or events to enable tracking the relationships between chunks of memories.

So no, you can't just memcpy a arbitrary STL object.

Afaik overriding new is not a panacea, it is a "process global" concept so trying to localize the news to a thread-object-allocation pair is not possible. And this localization would enable you to group the memory together.

You could write your own containers that used a custom memory allocator explicitly (explicit because your provide the necessary object identity information), and there are some custom allocators out there.

白衬杉格子梦 2024-08-28 12:35:55

不,C++ 仅自行管理内存,但您可以通过实现 std::map::allocator 来帮助 stl 来完成此操作,这将按照您的方式完成,因此您不需要 memcpy 它。

No, C++ manages memory solely by itself, but you can help stl to do it by implementing std::map::allocator, which will do it all your way so you won't need memcpy it.

还如梦归 2024-08-28 12:35:55

您没有提到您正在为哪个平台编码,也没有提到这是否重要。但在 Linux 和 GCC 中,C++ 默认分配器是通过 malloc 实现的。

malloc 可以被覆盖并替换为您自己的内部分配器。

我将按如下方式实现此malloc

在一般情况下,只需遵循原始malloc。无论何时,您都可以更改 malloc 函数的行为,以返回指向您已准备好的特殊内存区域的指针。

因此,在创建这个特定的 std::map 之前,您需要给您的 malloc 一个提示。 (通过全局变量或其他一些通信方式。)

更新:alemjerus 建议实现 std::map::allocator,这是在同一主题上更干净的方式,也与平台无关。

You did not mention which platform you are coding for or if that is allowed to matter. But in Linux and the GCC, the C++ default allocator is implemented with malloc.

malloc can be overridden and replaced with your own internal allocator.

I would implement this malloc as follows:

in the general case, just defer to original malloc. Whenever you want to, you can change the behavior of the malloc function to return pointers to this special memory area you have prepared.

So, before creating this particular std::map you would give your malloc a hint. (Through a global variable or some other means of communication.)

Update: alemjerus suggested implementing std::map::allocator which is a much cleaner way on the same theme, also platform agnostic.

不喜欢何必死缠烂打 2024-08-28 12:35:55

您可以使用 placement new 来定义基本 std::map 对象所在的位置。但大部分空间将由 std::map 在堆上分配。

You can use placement new to define where the base std::map object lives. But the majority of the space will be allocated on the heap, by std::map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文