Skip to content

alecbz/mmap-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mmap-allocator

A C++ allocator backed by a memory-mapped file.

Usage

You can create a new allocator via the mm::Allocator::New method:

unique_ptr<mm::Allocator<int>> alloc = mm::Allocator<int>::New("test.storage");

Space can be allocated via allocate and deallocate:

int* data = alloc->allocate(10);  // space for 10 ints
for (int i = 0; i < 10; ++i) {
  data[i] = i * i;
}
alloc->deallocate(data, 10);

STL Containers

Most standard containers can be passed in a custom allocator to use. Given an allocator:

unique_ptr<mm::Allocator<int>> alloc = mm::Allocator<int>::New("test.storage");

We can construct a std::vector that utilizes it like so:

std::vector<int, mm::Allocator<int>> vec(*alloc);

For convenience, some STL containers have been typdefed:

mm::vector<int> vec(*alloc);  // same as above

Setting a default file

You can set a default storage location for all mm::Allocators via the mm::SetDefault method.

mm::SetDefault("test.storage");

Now all mm::Allocators constructed via the default constructor will use the "test.storage" file. (Using Allocator::New will stil utilize whatever storage you specify).

This is convenient because most standard containers attempt to construct default allocators when not passed one explicitly. Thus, instead of

unique_ptr<mm::Allocator<int>> alloc = mm::Allocator<int>::New("test.storage");
mm::vector<int> vec(*alloc);

We can do:

mm::SetDefault("test.storage");
mm::vector<int> vec;

mm::SetDefault does new an object, which can be freed via mm::FreeDefault().

Testing

tests/mmap_allocator_test.cc is a gtest unit test. With gtest installed, do:

cd tests
make
./mmap_allocator_test

to run the test.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages