Analytics Template Library
 All Classes Namespaces Functions Variables Pages
short_alloc.hpp
1 #ifndef SHORT_ALLOC_H
2 #define SHORT_ALLOC_H
3 
4 #include <cstddef>
5 #include <cassert>
6 
7 template <std::size_t N>
8 class arena
9 {
10  static const std::size_t alignment = 16;
11  alignas(alignment) char buf_[N];
12  char* ptr_;
13 
14  bool
15  pointer_in_buffer(char* p) noexcept
16  {return buf_ <= p && p <= buf_ + N;}
17 
18  public:
19  arena() noexcept : ptr_(buf_) {}
20  ~arena() {ptr_ = nullptr;}
21  arena(const arena&) = delete;
22  arena& operator=(const arena&) = delete;
23 
24  char* allocate(std::size_t n);
25  void deallocate(char* p, std::size_t n) noexcept;
26 
27  static constexpr std::size_t size() {return N;}
28  std::size_t used() const {return static_cast<std::size_t>(ptr_ - buf_);}
29  void reset() {ptr_ = buf_;}
30  };
31 
32  template <std::size_t N>
33  char*
34  arena<N>::allocate(std::size_t n)
35  {
36  assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
37  if (buf_ + N - ptr_ >= n)
38  {
39  char* r = ptr_;
40  ptr_ += n;
41  return r;
42  }
43  return static_cast<char*>(::operator new(n));
44  }
45 
46  template <std::size_t N>
47  void
48  arena<N>::deallocate(char* p, std::size_t n) noexcept
49  {
50  assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
51  if (pointer_in_buffer(p))
52  {
53  if (p + n == ptr_)
54  ptr_ = p;
55  }
56  else
57  ::operator delete(p);
58  }
59 
60  template <class T, std::size_t N>
62  {
63  arena<N>& a_;
64  public:
65  typedef T value_type;
66 
67  public:
68  template <class _Up> struct rebind {typedef short_alloc<_Up, N> other;};
69 
70  short_alloc(arena<N>& a) noexcept : a_(a) {}
71  template <class U>
72  short_alloc(const short_alloc<U, N>& a) noexcept
73  : a_(a.a_) {}
74  short_alloc(const short_alloc&) = default;
75  short_alloc& operator=(const short_alloc&) = delete;
76 
77  T* allocate(std::size_t n)
78  {
79  return reinterpret_cast<T*>(a_.allocate(n*sizeof(T)));
80  }
81  void deallocate(T* p, std::size_t n) noexcept
82  {
83  a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
84  }
85 
86  template <class T1, std::size_t N1, class U, std::size_t M>
87  friend
88  bool
89  operator==(const short_alloc<T1, N1>& x, const short_alloc<U, M>& y) noexcept;
90 
91  template <class U, std::size_t M> friend class short_alloc;
92  };
93 
94  template <class T, std::size_t N, class U, std::size_t M>
95  inline
96  bool
97  operator==(const short_alloc<T, N>& x, const short_alloc<U, M>& y) noexcept
98  {
99  return N == M && &x.a_ == &y.a_;
100  }
101 
102  template <class T, std::size_t N, class U, std::size_t M>
103  inline
104  bool
105  operator!=(const short_alloc<T, N>& x, const short_alloc<U, M>& y) noexcept
106  {
107  return !(x == y);
108  }
109 
110 #endif // SHORT_ALLOC_H
Definition: short_alloc.hpp:8
const int operator!=(const BigFloat< T > &lhs, const BigFloat< T > &rhs)
Definition: BigFloat.hpp:435
Definition: short_alloc.hpp:61
Definition: short_alloc.hpp:68
const int operator==(const BigFloat< T > &lhs, const BigFloat< T > &rhs)
Definition: BigFloat.hpp:423