简体   繁体   English

适用于mempool /内存分配器的布局? (哪种算法)

[英]Proper layout for an an mempool/memory allocator? (which algorithm)

Hello, I'm thinking about trying to expand my skills by trying some things I've never done before. 您好,我正在考虑尝试通过尝试一些我以前从未做过的事情来扩展我的技能。 One thing that has always mystified me a bit is memory allocators and memory pools. 让我有点困惑的一件事是内存分配器和内存池。 What I want to do is take a block of memory and only allocate the memory from the system once. 我想要做的是占用一块内存,只从系统中分配一次内存。 I have that currently set up, the memory is an array of bytes (or chars) that is 65535 for my testing purpose. 我有当前设置,内存是一个字节数组(或字符串),为我的测试目的是65535。

I have two algorithms that I've considered using. 我有两个算法,我考虑过使用它。

First is an algorithm where the entire block of data is appended with the amount of memory left over, and a pointer (or rather an offset) to the first allocated block (or the header of the block), then each allocation is preceded by the size of the allocation, and the previous and next allocation, so I can release the allocation easily. 首先是一种算法,其中整个数据块附加了剩余的内存量,以及指向第一个分配块(或块的头部)的指针(或者更确切地说是偏移量),然后每个分配前面都有分配的大小,以及上一个和下一个分配,所以我可以轻松地释放分配。 I then can generate the largest and smallest blocks by looking at the space after the current allocations. 然后,我可以通过查看当前分配后的空间来生成最大和最小的块。

The other option I have is to add an second offset before the allocated memory and make that point to the first unallocated block, then each unallocated block also has the Previous and Next allocation, along with a size so that I can easily find a place where my next allocation can be placed. 我有另一个选择是在分配的内存之前添加第二个偏移量,并将该点指向第一个未分配的块,然后每个未分配的块也具有上一个和下一个分配,以及一个大小,以便我可以轻松找到一个位置我的下一个分配可以放置。

The issue is I can't tell which is "proper". 问题是我不知道哪个是“正确的”。 Assume we'll have variable size allocations (but most will be at a size that this isn't that much overhead.) The first will be slower to get the largest and smallest possible blocks, but I can store those and manipulate them if necessary to avoid regenerating them. 假设我们将有可变大小的分配(但是大多数将是这个开销没有那么多的大小。)第一个将获得最大和最小可能的块更慢,但我可以存储它们并在必要时操作它们避免再生它们。 However the second would take a longer time to deallocate (due to having to find which deallocator is next to which allocator) and not necessarily give any benefits to allocation. 然而,第二个将需要更长的时间来解除分配(由于必须找到哪个解除分配器在哪个分配器旁边)并且不一定给分配带来任何好处。 In fact it will require more specialized code for the case where I have less than 6 bytes left over (2 bytes for size, 2 for prev offset, 2 for next offset). 事实上,对于我剩下少于6个字节的情况,它将需要更专门的代码(大小为2个字节,prev偏移为2,下一个偏移为2)。

My gut tells me the first would be far superior, but something about the second is enticing. 我的直觉告诉我,第一个会更优越,但关于第二个的东西是诱人的。 Any opinions? 任何意见? Or is there a far easier solution? 还是有一个更容易的解决方案?

The problem with this type of approach is handling fragmentation (and hence how to coalesce adjoining free chunks of memory). 这种方法的问题是处理碎片(因此如何合并相邻的空闲内存块)。 Look at this question: Designing and coding a non-fragmentizing static memory pool , discusses a different approach... 看看这个问题: 设计和编码非碎片化的静态内存池 ,讨论一种不同的方法......

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

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