Sources Pipelines Documentation

Allocators  
stack_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 "null_allocator.hpp"
8 
9 
10 
11 namespace boost { namespace memory {
12 
13 template <
14  std::size_t capacity,
15  std::size_t alignment>
17 {
18  auto end = reserved_block_ + capacity;
19  auto next = align_forward<alignment>(current_ + size);
20  if (next <= end)
21  {
22  memory_block block { current_, size };
23  current_ = next;
24 
25  return block;
26  }
27  else
28  {
29  return null_block;
30  }
31 }
32 
33 template <
34  std::size_t capacity,
35  std::size_t alignment>
37 {
38  auto previous = align_backward<alignment>(current_ - block.size);
39  if (previous == block.address)
40  {
41  current_ = previous;
42  block = { nullptr, 0ul };
43  }
44 }
45 
46 template <
47  std::size_t capacity,
48  std::size_t alignment>
49 inline bool stack_allocator<capacity, alignment>::owns(memory_block& block) const noexcept
50 {
51  return &reserved_block_ <= block.address && block.address <= &reserved_block_ + capacity;
52 }
53 
54 } }
55 
bool owns(memory_block &block) const noexcept
Determines if the block has been allocated with this allocator.
void deallocate(memory_block &block)
Deallocates the block if possible.
memory_block allocate(std::size_t size)
Allocated a block of the requested size on the stack.
std::size_t size
Block&#39;s size.
Represents an allocated memory block.