Sources Pipelines Documentation

Allocators  
fallback_allocator.ipp
1 // Copyright (c) 2016 Lukasz Laszko
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #pragma once
6 
7 #include "memory_block.hpp"
8 
9 
10 namespace boost { namespace memory {
11 
12 template <typename primary_allocator, typename secondary_allocator>
14 {
15  auto block = primary_.allocate(size);
16  if (block.address != nullptr)
17  return block;
18  else
19  return secondary_.allocate(size);
20 }
21 
22 template <typename primary_allocator, typename secondary_allocator>
24 {
25  if (primary_.owns(block))
26  primary_.deallocate(block);
27  else if (secondary_.owns(block))
28  secondary_.deallocate(block);
29 }
30 
31 template <typename primary_allocator, typename secondary_allocator>
33 {
34  return primary_.owns(block) || secondary_.owns(block);
35 }
36 
37 template <typename primary_allocator, typename secondary_allocator>
39 {
40  return primary_;
41 }
42 
43 template <typename primary_allocator, typename secondary_allocator>
45 {
46  return secondary_;
47 }
48 
49 } }
50 
Allocates with secondary allocator, if allocation with primary allocator fails.
bool owns(memory_block &block)
Examines if the given block is owned by this allocator.
void deallocate(memory_block &block)
Deallocates the given memory_block.
Represents an allocated memory block.
memory_block allocate(std::size_t size)
Allocates a memory_block with either primary_allocator or secondary_allocator.