Allocator Builder
Policy Based C++ Template Allocator Library
 All Classes Functions Variables Enumerations Enumerator Groups Pages
mallocator.hpp
1 //
3 // Copyright 2014 Felix Petriconi
4 //
5 // License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0
6 //
7 // Authors: http://petriconi.net, Felix Petriconi
8 //
10 #pragma once
11 
12 #include "allocator_base.hpp"
13 #include "internal/reallocator.hpp"
14 
15 namespace alb {
16  namespace v_100 {
22  class mallocator {
23  public:
24  static constexpr bool supports_truncated_deallocation = false;
25  static constexpr unsigned alignment = 4;
26 
34  block allocate(size_t n) noexcept {
35  block result;
36 
37  if (n == 0) {
38  return result;
39  }
40  auto p = ::malloc(n);
41  if (p != nullptr) {
42  result.ptr = p;
43  result.length = n;
44  return result;
45  }
46  return result;
47  }
48 
55  bool reallocate(block &b, size_t n) noexcept {
56  if (internal::is_reallocation_handled_default(*this, b, n)) {
57  return true;
58  }
59 
60  block reallocatedBlock(::realloc(b.ptr, n), n);
61 
62  if (reallocatedBlock) {
63  b = reallocatedBlock;
64  return true;
65  }
66  return false;
67  }
68 
73  void deallocate(block &b) noexcept
74  {
75  if (b) {
76  ::free(b.ptr);
77  b.reset();
78  }
79  }
80  };
81  }
82  using namespace v_100;
83 }
void deallocate(block &b) noexcept
Definition: mallocator.hpp:73
block allocate(size_t n) noexcept
Definition: mallocator.hpp:34
size_t length
This describes the length of the reserved bytes.
void * ptr
This points to the start address of the described memory block.
bool reallocate(block &b, size_t n) noexcept
Definition: mallocator.hpp:55