Rumba C++ SDK
BufferAllocator.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2018
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include <assert.h>
18 #include <memory>
19 
20 namespace maquina
21 {
22 
24 template<class T>
26 {
27 public:
28  using size_type = typename std::allocator<T>::size_type;
29  using value_type = typename std::allocator<T>::value_type;
30  using difference_type = typename std::allocator<T>::difference_type;
31  using pointer = typename std::allocator<T>::pointer;
32  using const_pointer = typename std::allocator<T>::const_pointer;
34  using const_reference = typename std::allocator<T>::const_reference;
36 
37  BufferAllocator() = delete;
38 
42  BufferAllocator(T* ptr, size_type size) : _pointer(ptr), _size(size) {}
43 
44  T* allocate( size_type n, const void * hint = 0 )
45  {
46  if(_free && n <= _size)
47  {
48  _free = false;
49  return _pointer;
50  }
51  _use_heap = true;
52  return _fall_back.allocate(n, hint);
53  }
54 
55  void deallocate( T* p, std::size_t n )
56  {
57  if(_pointer == p)
58  {
59  assert(!_free);
60  _free = true;
61  }
62  else
63  _fall_back.deallocate(p, n);
64  }
65 
67  bool use_heap() const
68  {
69  return _use_heap;
70  }
71 private:
72  T* _pointer;
73  size_type _size;
74  bool _free = true;
75  bool _use_heap = false;
76  std::allocator<T> _fall_back;
77 };
78 
79 }
typename std::allocator< T >::difference_type difference_type
Definition: BufferAllocator.h:30
MAQUINA_EXPORT Node reference(Node &root, const std::wstring &filepath, const std::string &reference_root_name="")
Reference the content of a file into the project.
typename std::allocator< T >::size_type size_type
Definition: BufferAllocator.h:28
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
BufferAllocator(T *ptr, size_type size)
Creates a BufferAllocator.
Definition: BufferAllocator.h:42
typename std::allocator< T >::const_pointer const_pointer
Definition: BufferAllocator.h:32
typename std::allocator< T >::pointer pointer
Definition: BufferAllocator.h:31
T * allocate(size_type n, const void *hint=0)
Definition: BufferAllocator.h:44
bool use_heap() const
Return true if the heap allocator has been used by this allocator. Return false if only the buffer ha...
Definition: BufferAllocator.h:67
typename std::allocator< T >::reference reference
Definition: BufferAllocator.h:33
void deallocate(T *p, std::size_t n)
Definition: BufferAllocator.h:55
std::true_type propagate_on_container_move_assignment
Definition: BufferAllocator.h:35
Allocator to use with std::vector. Allocates memory first in the user provided buffer and then using ...
Definition: BufferAllocator.h:25
typename std::allocator< T >::const_reference const_reference
Definition: BufferAllocator.h:34
typename std::allocator< T >::value_type value_type
Definition: BufferAllocator.h:29