-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathblock_storage_allocator.hpp
More file actions
52 lines (43 loc) · 2 KB
/
Copy pathblock_storage_allocator.hpp
File metadata and controls
52 lines (43 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef FOONATHAN_ARRAY_BLOCK_STORAGE_ALLOCATOR_HPP_INCLUDED
#define FOONATHAN_ARRAY_BLOCK_STORAGE_ALLOCATOR_HPP_INCLUDED
#include <memory>
#include <foonathan/array/block_storage_heap.hpp>
namespace foonathan
{
namespace array
{
/// A `Heap` that uses the given `Allocator` for allocation.
///
/// The allocator will be rebound to [array::byte]().
/// Only `allocate()` and `deallocate()` will be called, all other functions ignored.
///
/// \note As the arguments of a `BlockStorage` will always propagate, so will the allocator,
/// regardless of the `propagate_XXX` settings.
template <class Allocator>
struct allocator_heap
{
using handle_type =
typename std::allocator_traits<Allocator>::template rebind_alloc<byte>;
static memory_block allocate(handle_type& handle, size_type size, size_type)
{
auto ptr = std::allocator_traits<handle_type>::allocate(handle, size);
return memory_block(ptr, size);
}
static void deallocate(handle_type& handle, memory_block&& block) noexcept
{
std::allocator_traits<handle_type>::deallocate(handle, block.begin(), block.size());
}
static size_type max_size(const handle_type& handle) noexcept
{
return std::allocator_traits<handle_type>::max_size(handle);
}
};
/// A `BlockStorage` that uses an [array::allocator_heap]() for allocation.
template <class Allocator, class GrowthPolicy>
using block_storage_allocator = block_storage_heap<allocator_heap<Allocator>, GrowthPolicy>;
} // namespace array
} // namespace foonathan
#endif // FOONATHAN_ARRAY_BLOCK_STORAGE_ALLOCATOR_HPP_INCLUDED