Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/foonathan/memory

STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.
https://github.com/foonathan/memory

allocator c-plus-plus memory memory-allocation

Last synced: about 7 hours ago
JSON representation

STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.

Awesome Lists containing this project

README

        

# memory

![Project Status](https://img.shields.io/endpoint?url=https%3A%2F%2Fwww.jonathanmueller.dev%2Fproject%2Fmemory%2Findex.json)
![Build Status](https://github.com/foonathan/memory/workflows/Main%20CI/badge.svg)
[![Code Coverage](https://codecov.io/gh/foonathan/memory/branch/master/graph/badge.svg?token=U6wnInlamY)](https://codecov.io/gh/foonathan/memory)

The C++ STL allocator model has various flaws. For example, they are fixed to a certain type, because they are almost necessarily required to be templates. So you can't easily share a single allocator for multiple types. In addition, you can only get a copy from the containers and not the original allocator object. At least with C++11 they are allowed to be stateful and so can be made object not instance based. But still, the model has many flaws.
Over the course of the years many solutions have been proposed, for example [EASTL]. This library is another. But instead of trying to change the STL, it works with the current implementation.

## Features

New allocator concepts:

* a `RawAllocator` that is similar to an `Allocator` but easier to use and write
* a `BlockAllocator` that is an allocator for huge memory blocks

Several implementations:

* `heap_/malloc_/new_allocator`
* virtual memory allocators
* allocator using a static memory block located on the stack
* memory stack, `iteration_allocator`
* different memory pools
* a portable, improved `alloca()` in the form of `temporary_allocator`
* facilities for joint memory allocations: share a big memory block for the object
and all dynamic memory allocations for its members

Adapters, wrappers and storage classes:

* incredible powerful `allocator_traits` allowing `Allocator`s as `RawAllocator`s
* `std_allocator` to make a `RawAllocator` an `Allocator` again
* adapters for the memory resource TS
* `allocator_deleter` classes for smart pointers
* (optionally type-erased) `allocator_reference` and other storage classes
* memory tracking wrapper

In addition:

* container node size debuggers that obtain information about the node size of an STL container at compile-time to specify node sizes for pools
* debugging options for leak checking, double-free checks or buffer overflows
* customizable error handling routines that can work with exceptions disabled
* everything except the STL adapters works on a freestanding environment

## Basic example

```cpp
#include
#include
#include

#include // vector, list, list_node_size
#include // memory_pool
#include // allocate_unique
#include // static_allocator_storage, static_block_allocator
#include // temporary_allocator

// alias namespace foonathan::memory as memory for easier access
#include

template
void merge_sort(BiIter begin, BiIter end);

int main()
{
using namespace memory::literals;

// a memory pool RawAllocator
// allocates a memory block - initially 4KiB - and splits it into chunks of list_node_size::value big
// list_node_size::value is the size of each node of a std::list
memory::memory_pool<> pool(memory::list_node_size::value, 4_KiB);

// just an alias for std::list>
// a std::list using a memory_pool
// std_allocator stores a reference to a RawAllocator and provides the Allocator interface
memory::list> list(pool);
list.push_back(3);
list.push_back(2);
list.push_back(1);

for (auto e : list)
std::cout << e << ' ';
std::cout << '\n';

merge_sort(list.begin(), list.end());

for (auto e : list)
std::cout << e << ' ';
std::cout << '\n';

// allocate a std::unique_ptr using the pool
// memory::allocate_shared is also available
auto ptr = memory::allocate_unique(pool, *list.begin());
std::cout << *ptr << '\n';

// static storage of size 4KiB
memory::static_allocator_storage<4096u> storage;

// a memory pool again but this time with a BlockAllocator
// this controls the internal allocations of the pool itself
// we need to specify the first template parameter giving the type of the pool as well
// (node_pool is the default)
// we use a static_block_allocator that uses the static storage above
// all allocations will use a memory block on the stack
using static_pool_t = memory::memory_pool;
static_pool_t static_pool(memory::unordered_set_node_size::value, 4096u, storage);

// again, just an alias for std::unordered_set, std::equal_to, memory::std_allocator
// see why I wrote these? :D
// now we have a hash set that lives on the stack!
memory::unordered_set set(static_pool);

set.insert(3);
set.insert(2);
set.insert(3); // running out of stack memory is properly handled, of course

for (auto e : set)
std::cout << e << ' ';
std::cout << '\n';
}

// naive implementation of merge_sort using temporary memory allocator
template
void merge_sort(BiIter begin, BiIter end)
{
using value_type = typename std::iterator_traits::value_type;

auto distance = std::distance(begin, end);
if (distance <= 1)
return;

auto mid = begin;
std::advance(mid, distance / 2);

// an allocator for temporary memory
// is similar to alloca() but uses its own stack
// this stack is thread_local and created on the first call to this function
// as soon as the allocator object goes out of scope, everything allocated through it, will be freed
auto alloc = memory::temporary_allocator();

// alias for std::vector>
// a std::vector using a temporary_allocator
memory::vector first(begin, mid, alloc),
second(mid, end, alloc);

merge_sort(first.begin(), first.end());
merge_sort(second.begin(), second.end());
std::merge(first.begin(), first.end(), second.begin(), second.end(), begin);
}
```

See `example/` for more.

## Installation

This library can be used as [CMake] subdirectory.
It is tested on GCC 4.8-5.0, Clang 3.5 and Visual Studio 2013. Newer versions should work too.

1. Fetch it, e.g. using [git submodules] `git submodule add https://github.com/foonathan/memory ext/memory` and `git submodule update --init --recursive`.

2. Call `add_subdirectory(ext/memory)` or whatever your local path is to make it available in CMake.

3. Simply call `target_link_libraries(your_target PUBLIC foonathan_memory)` to link this library and setups the include search path and compilation options.

You can also install the library:

1. Run `cmake -DCMAKE_BUILD_TYPE="buildtype" -DFOONATHAN_MEMORY_BUILD_EXAMPLES=OFF -DFOONATHAN_MEMORY_BUILD_TESTS=OFF .` inside the library sources.

2. Run `cmake --build . -- install` to install the library under `${CMAKE_INSTALL_PREFIX}`.

3. Repeat 1 and 2 for each build type/configuration you want to have (like `Debug`, `RelWithDebInfo` and `Release` or custom names).

To use an installed library:

4. Call `find_package(foonathan_memory major.minor REQUIRED)` to find the library.

5. Call `target_link_libraries(your_target PUBLIC foonathan_memory)` to link to the library and setup all required options.

See https://memory.foonathan.net/md_doc_installation.html for a detailed guide.

## Building foonathan-memory - Using vcpkg

You can download and install foonathan-memory using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install foonathan-memory

The foonathan-memory port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.

## Documentation

Full documentation can be found at https://memory.foonathan.net/.

A tutorial is also available at https://memory.foonathan.net/md_doc_tutorial.html.

## RawAllocator

Below is the interface required for a `RawAllocator`, everything optional is marked:

```cpp
struct raw_allocator
{
using is_stateful = std::integral_constant; // optional, defaults to std::is_empty

void* allocate_node(std::size_t size, std::size_t alignment); // required, allocation function
void deallocate_node(void *node, std::size_t size, std::size_t alignment) noexcept; // required, deallocation function

void* allocate_array(std::size_t count, std::size_t size, std::size_t alignment); // optional, forwards to node version
void deallocate_array(void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept; // optional, forwards to node version

std::size_t max_node_size() const; // optional, returns maximum value
std::size_t max_array_size() const; // optional, forwards to max_node_size()
std::size_t max_alignment() const; // optional, returns maximum fundamental alignment, i.e. alignof(std::max_align_t)
};
```

A `RawAllocator` only needs to be moveable, all `Allocator` classes are `RawAllocators` too.
Classes not providing the interface can specialize the `allocator_traits`, read more about [writing allocators here](https://memory.foonathan.net/md_doc_writing_allocators.html) or about the technical details of the [concept here](https://memory.foonathan.net/md_doc_concepts.html).

## Acknowledgements

This project is greatly supported by my [patrons](https://patreon.com/foonathan).
In particular thanks to the individual supporters:

* Kaido Kert

And big thanks to the contributors as well:

* @Guekka
* @Manu343726
* @MiguelCompany
* @asobhy-qnx
* @bfierz
* @cho3
* @gabyx
* @j-carl
* @kaidokert
* @maksqwe
* @moazzamak
* @myd7349
* @nicolastagliani
* @quattrinili
* @razr
* @roehling
* @seanyen
* @wtsnyder
* @zhouchengming1
* @jwdevel

[EASTL]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html
[CMake]: www.cmake.org
[git submodules]: http://git-scm.com/docs/git-submodule
[foonathan/compatibility]: hptts://github.com/foonathan/compatibility