LinearAllocator.hpp
1 #ifndef CPP3DS_LINEARALLOCATOR_HPP
2 #define CPP3DS_LINEARALLOCATOR_HPP
3 
4 #include <3ds.h>
5 #include <bits/c++allocator.h>
6 #if __cplusplus >= 201103L
7 #include <type_traits>
8 #endif
9 
10 namespace cpp3ds {
11 
12  template<typename T>
13  class LinearAllocator: public std::__allocator_base<T>
14  {
15  public:
16  typedef size_t size_type;
17  typedef ptrdiff_t difference_type;
18  typedef T* pointer;
19  typedef const T* const_pointer;
20  typedef T& reference;
21  typedef const T& const_reference;
22  typedef T value_type;
23 
24  template<typename T1>
25  struct rebind
26  { typedef LinearAllocator<T1> other; };
27 
28  #if __cplusplus >= 201103L
29  // _GLIBCXX_RESOLVE_LIB_DEFECTS
30  // 2103. std::allocator propagate_on_container_move_assignment
31  typedef std::true_type propagate_on_container_move_assignment;
32  #endif
33 
34  LinearAllocator() throw() { }
35 
36  LinearAllocator(const LinearAllocator& __a) throw()
37  : std::__allocator_base<T>(__a) { }
38 
39  template<typename T1>
40  LinearAllocator(const LinearAllocator<T1>&) throw() { }
41 
42  ~LinearAllocator() throw() { }
43 
44  // NB: __n is permitted to be 0. The C++ standard says nothing
45  // about what the return value is when __n == 0.
46  pointer
47  allocate(size_type __n, const void* = 0)
48  {
49  if (__n > this->max_size())
50  std::__throw_bad_alloc();
51  return static_cast<T*>(linearAlloc(__n * sizeof(T)));
52  }
53 
54  // __p is not permitted to be a null pointer.
55  void
56  deallocate(pointer __p, size_type)
57  {
58  linearFree(__p);
59  }
60 
61  size_type
62  max_size() const
63  {
64  return size_t(-1) / sizeof(T);
65  }
66 
67  // Inherit everything else.
68  };
69 
70 } // namespace cpp3ds
71 
72 #endif // CPP3DS_LINEARALLOCATOR_HPP
73 
74 
83 ////////////////////////////////////////////////////////////
This allocator class is useful for when you want to use a STL container (e.g.