0

如何使用boost pool或其他类似的分配器技术从固定的内存块分配连续的 512 字节扇区。我正在尝试在 C++17 中模拟内存文件系统。

我假设在幕后,当用户创建指定长度的新文件时,可以使用单独的文件系统元数据对象将文件名与此内存块/池中的根指针/长度节点相关联。

如果在小于当前长度的指定偏移量处附加或截断文件,则需要从上述根节点添加或删除额外的链表节点。碎片整理会很好但可选。鉴于最小扇区分配大小为 512 字节,如果扇区未完全使用,小分配/附加到文件可能不需要添加额外的链表节点。

我不确定我上面描述的内容是否作为一个简单的库存在,或者提升池是否按原样为我做这件事。我只想分配字节块,而不是对象数组。

4

1 回答 1

0

Perhaps you want an array of bitset:

#include <bitset>

int main() {
    std::bitset<8> bytes[512];
}

It is an array of bitsets of 8 bytes.

A std::bitset is a fixed size array of bools (0 and 1 for true and false), so since a byte is 8 bits, we need a std::bitset of 8 bytes, and we declare this like this:

std::bitset<8>

Then, since we want 512 bytes, we make an array of bitsets, like so:

    std::bitset<8> bytes[512];

Then, you can have std::vectors of these or something.

You could also have a bitset of 4096 bits (512 bytes where each is 8 bits), like so:

std::bitset<4096> bytes;

You could then have a dynamic container containing there, like a std::vector.

As for Boost Pool, there is a great tutorial here: https://theboostcpplibraries.com/boost.pool.

于 2021-04-29T22:26:53.057 回答