I am working on a mmap-allocator that allows vectors to use memory from
a memory-mapped file. The goal is to have vectors that use storage that
are directly in the virtual memory mapped by mmap. Our problem is to
improve reading of really large files (>10GB) into memory with no copy
overhead, therefore I need this custom allocator.
So far I have the skeleton of a custom allocator
(which derives from std::allocator), I think it is a good starting
point to write own allocators. Feel free to use this piece of code
in whatever way you want:
#include <memory>
#include <stdio.h>
namespace mmap_allocator_namespace
{
// See StackOverflow replies to this answer for important commentary about inheriting from std::allocator before replicating this code.
template <typename T>
class mmap_allocator: public std::allocator<T>
{
public:
typedef size_t size_type;
typedef T* pointer;
typedef const T* const_pointer;
template<typename _Tp1>
struct rebind
{
typedef mmap_allocator<_Tp1> other;
};
pointer allocate(size_type n, const void *hint=0)
{
fprintf(stderr, "Alloc %d bytes.\n", n*sizeof(T));
return std::allocator<T>::allocate(n, hint);
}
void deallocate(pointer p, size_type n)
{
fprintf(stderr, "Dealloc %d bytes (%p).\n", n*sizeof(T), p);
return std::allocator<T>::deallocate(p, n);
}
mmap_allocator() throw(): std::allocator<T>() { fprintf(stderr, "Hello allocator!\n"); }
mmap_allocator(const mmap_allocator &a) throw(): std::allocator<T>(a) { }
template <class U>
mmap_allocator(const mmap_allocator<U> &a) throw(): std::allocator<T>(a) { }
~mmap_allocator() throw() { }
};
}
To use this, declare an STL container as follows:
using namespace std;
using namespace mmap_allocator_namespace;
vector<int, mmap_allocator<int> > int_vec(1024, 0, mmap_allocator<int>());
It can be used for example to log whenever memory is allocated. What is neccessary
is the rebind struct, else the vector container uses the superclasses allocate/deallocate
methods.
Update: The memory mapping allocator is now available at https://github.com/johannesthoma/mmap_allocator and is LGPL. Feel free to use it for your projects.
boost::pool
andboost::interprocess
– Ionia