Allocating byte arrays of memory from fixed memory block

447 views Asked by At

How can I use boost pool or some other similar allocator technique to allocate contiguous 512 byte sectors from a fixed block of memory. I am trying to emulate a memory filesystem in C++17.

I assume that under the covers, when a user creates a new file of a specified length, a separate filesystem metadata object could be used to associate the filename with a root pointer/length node within this memory block/pool.

If the file is appended or truncated at a specified offset less than the current length, additional linked list nodes would need to be added or removed from the root node mentioned above. Defragmentation would be nice but optional. Given that the minimum sector allocation size is 512 bytes, small allocations/appends to the file may not require additional linked list nodes to be added if the sector is not fully used.

I am not sure if what I described above exists as a simple library or if the boost pool does this for me as is. I just want to allocated blocks of bytes, not arrays of objects.

1

There are 1 answers

3
AudioBubble On

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.