CXXR (C++ R)
Allocator.hpp
Go to the documentation of this file.
1 /* The following code is adapted from an example taken from the book
2  * "The C++ Standard Library - A Tutorial and Reference"
3  * by Nicolai M. Josuttis, Addison-Wesley, 1999
4  *
5  * (C) Copyright Nicolai M. Josuttis 1999.
6  * Permission to copy, use, modify, sell and distribute this software
7  * is granted provided this copyright notice appears in all copies.
8  * This software is provided "as is" without express or implied
9  * warranty, and with no claim as to its suitability for any purpose.
10  */
11 
12 /* Adaptations Copyright Andrew Runnalls 2007, under the same terms as
13  * the original copyright above.
14  */
15 
20 #ifndef ALLOCATOR_HPP
21 #define ALLOCATOR_HPP 1
22 
23 #include <limits>
24 #include "CXXR/MemoryBank.hpp"
25 
26 namespace CXXR {
41  template <typename T>
42  class Allocator {
43  public:
44  // type definitions
45  typedef T value_type;
46  typedef T* pointer;
47  typedef const T* const_pointer;
48  typedef T& reference;
49  typedef const T& const_reference;
50  typedef std::size_t size_type;
51  typedef std::ptrdiff_t difference_type;
52 
53  // rebind allocator to type U
54  template <class U>
55  struct rebind {
56  typedef Allocator<U> other;
57  };
58 
59  // return address of values
60  pointer address (reference value) const {
61  return &value;
62  }
63  const_pointer address (const_reference value) const {
64  return &value;
65  }
66 
67  /* constructors and destructor
68  * - nothing to do because the allocator has no state
69  */
70  Allocator() throw() {
71  }
72  Allocator(const Allocator&) throw() {
73  }
74  template <class U>
75  Allocator (const Allocator<U>&) throw() {
76  }
77  ~Allocator() throw() {
78  }
79 
80  // return maximum number of elements that can be allocated
81  size_type max_size () const throw() {
82  return std::numeric_limits<std::size_t>::max() / sizeof(T);
83  }
84 
85  // allocate but don't initialize num elements of type T
86  pointer allocate (size_type num, const void* /*hint*/ = 0) {
87  return static_cast<pointer>(MemoryBank::allocate(num*sizeof(T)));
88  }
89 
90  // initialize elements of allocated storage p with value value
91  void construct (pointer p, const T& value) {
92  // initialize memory with placement new
93  new (p) T(value);
94  }
95 
96  // destroy elements of initialized storage p
97  void destroy (pointer p) {
98  // destroy objects by calling their destructor
99  p->~T();
100  }
101 
102  // deallocate storage p of deleted elements
103  void deallocate (pointer p, size_type num) {
104  MemoryBank::deallocate(p, num*sizeof(T));
105  }
106  };
107 
108  // return that all specializations of this allocator are interchangeable
109  template <typename T1, typename T2>
110  bool operator== (const Allocator<T1>&,
111  const Allocator<T2>&) throw() {
112  return true;
113  }
114  template <typename T1, typename T2>
115  bool operator!= (const Allocator<T1>&,
116  const Allocator<T2>&) throw() {
117  return false;
118  }
119 }
120 
121 #endif // ALLOCATOR_HPP