Allocators

Allocators provides a handle to override the memory management done by the operating systems. In this blog, I will incrementally write about the details of allocators.

Allocators

In one of the talk at CppCon, a speaker mentioned allocators are a handle to heap.

Most of the STL containers store their data on the heap instead of the stack. As an example, I will explain how vector works superficially, only highlighting the aspects needed for talking about the allocators. From what is shown in the below figure, it can be seen that the data a vector represents is stored in the heap section of the program memory.

Working of vector

This can be achieved by the simple default usage, as described below. This calls in for a dynamic (heap) allocation by calling the new/malloc function.

vector<int> v (50, 0);

However, STL containers also give an option for creating the allocation in the memory that the client wants to allocate in. If this path is taken, the responsibility of managing the memory is on the client. A small sample program to demonstrate how this is done is shown here.

Creating Shared Memory Interface

The concept of allocators is a powerful tool that helps precisely controlling the memory management. This section explains one use-case of custom allocators in which multiple processes share the same memory to store their containers’ data. This can reduce the overhead involved transferring the data between multiple consumers and producers.

#include <sys/ipc.h>
#include <sys/shm.h>

void * setup_shm()
{
    int shmid = shmget(777, 100, 0644 | IPC_CREAT); // request 100 bytes

    void * shared_mem = shmat(shmid, (void*)(0), 0);

    if (shared_mem == (char *) (-1)) perror("error creating shared mem\n");

    return shared_mem;
}

Allocators to shared memory

To use the shared memory for multiple containers (vectors), present in multiple containers, the allocator needs to passed with the shared memory address obtained in the previous sections.

Allocators to shared memory

A simple working code for using shared memory as a custom allocator with a vector is given here.

Now with the sharing in place, there needs to be additional care taken for synchronization of the data shared by multiple processes.

Written on June 29, 2020