File-backed allocator for STL containers

File-backed allocator for STL containers

The STL is renowned for its modular design, allowing universal algorithms to be applied to a wide range of containers. Even the behaviour of a container is not set in stone and can be customized, including its memory management.

While there are many different types of allocators, let's focus on one in particular—memory allocation from a file. Sound crazy? Welcome to C++.

The concept is straightforward: map a file to the process memory and allocate memory blocks from that file instead of from the heap. As a result, if an element of the container is modified, those changes are written directly to disk. The overall picture looks like this:


But first of all, I have to say that this option is useful only for a limited number of applications where the persistence of objects is required.

I was able to find an allocator called mmap-allocator, which I’ll use for demonstration purposes. The code is a bit outdated and likely has some bugs, but it serves well for illustrating the concept.

In my example, I declare a vector to store a user-defined type and use the mmap-allocator with it. Then, I push several elements into the container, and we’ll observe what happens.

The data stored in the container should be a POD (Plain Old Data) object, without any references or pointers inside, so a vector of string is not an option here:


Then, the allocator can be created along with the vector:


After the program execution, the resulting file test.storage contains the following content:

So all data has been written to the disk without any extra modifications to the container's behavior.

Of course, in such a trivial demonstration, there are some blind spots. For example, data cannot be read back when the program starts up. This is a reasonable concern, and solving it requires additional logic, such as a factory method to construct and initialize the vector with the content of the mapped file.

Let's sum up:

  • Using a file to allocate memory can be implemented for STL containers without modifying them.
  • It may be necessary to store additional meta-information about the current status of the container, such as free and occupied blocks and the current size.
  • This approach is suitable for a limited number of applications.
  • There is a strong limitation on data types: they should be POD (Plain Old Data) types without references or pointers.
  • Constructing a container from an existing file requires extra logic and meta-data.

要查看或添加评论,请登录

Nikolai Kutiavin的更多文章

  • Golang generic implementation for Producer-Consumer

    Golang generic implementation for Producer-Consumer

    Producer-consumer design pattern is well-known in software development. In a few words, one object produces values, and…

    6 条评论
  • Factory Method in Python with blackjack and ?h?o?o?k?e?r?s? decorators.

    Factory Method in Python with blackjack and ?h?o?o?k?e?r?s? decorators.

    Design patterns help organize code in a better way, improving readability and maintainability. Many design patterns can…

    8 条评论
  • Non-blocking synchronization for std::vector

    Non-blocking synchronization for std::vector

    In my previous post, I described how to protect a std::vector using std::mutex. It was straightforward.

    13 条评论
  • Unit-test in C++: what should you know.

    Unit-test in C++: what should you know.

    Unit tests are important for a single reason - they prove that a single component works as expected in isolation. If a…

    9 条评论
  • C++ transactional memory

    C++ transactional memory

    Sometimes I feel like an archaeologist, and today I’ll share one of my findings: an old proposal for the C++ standard…

    24 条评论
  • Behavior vs Data concurrency protection

    Behavior vs Data concurrency protection

    Multithreading is one of the hottest topics in C++. However, the most crucial question isn't just about running code…

  • Why preprocessor directives are evil

    Why preprocessor directives are evil

    I'll start with a simple quiz: Do you think the code below is correct? Does it make more sense now? Preprocessor…

    42 条评论
  • Perl with classes

    Perl with classes

    Originally developed as a procedural language, Perl was later adapted to support modern Object-Oriented Programming…

    4 条评论
  • r-value reference: when and why?

    r-value reference: when and why?

    C++ has two commonly used types of references: l-value and r-value. An l-value reference points to an object with a…

    6 条评论
  • Network transport protocol: reliability and message-orientation out of the box

    Network transport protocol: reliability and message-orientation out of the box

    If I asked you to name a reliable network protocol for the transport layer, what would be the first one that comes to…

    4 条评论

社区洞察

其他会员也浏览了