CARMA C++
FftwAllocator.h
Go to the documentation of this file.
1 
10 #ifndef CARMA_UTIL_FFTWALLOCATOR_H
11 #define CARMA_UTIL_FFTWALLOCATOR_H
12 
13 #include <fftw3.h>
14 
15 #include <cstddef>
16 #include <limits>
17 
18 namespace carma {
19 
20 namespace util {
21 
29  template <class T>
30  class FftwAllocator {
31  public:
32 
33  // Josuttis is my homeboy (i.e. this is copied from section 15.4)
34 
35  // type definitions
36  typedef size_t size_type;
37  typedef ptrdiff_t difference_type;
38  typedef T * pointer;
39  typedef const T * const_pointer;
40  typedef T & reference;
41  typedef const T & const_reference;
42  typedef T value_type;
43 
44  // rebind allocator to type U
45  template <class U>
46  struct rebind {
47  typedef FftwAllocator<U> other;
48  };
49 
50  // return address of values
51  pointer address ( reference value ) const {
52  return &value;
53  }
54 
55  const_pointer address( const_reference value ) const {
56  return &value;
57  }
58 
63  FftwAllocator( ) throw ( ) {
64  }
65 
66  FftwAllocator( const FftwAllocator &) throw ( ) {
67  }
68 
69  template <class U>
70  FftwAllocator( const FftwAllocator<U> & ) throw ( ) {
71  }
72 
73  ~FftwAllocator( ) throw ( ) {
74  }
75 
76  // return maximum number of elements that can be allocated
77  size_type max_size( ) const throw ( ) {
78  return std::numeric_limits<size_t>::max( ) / sizeof( T );
79  }
80 
81  // allocate but don't initialize num elements of type T
82  pointer allocate( size_type num,
83  const void * hint = 0 ) {
84  // allocate memory with fftw_alloc
85  return static_cast<pointer>( ::fftw_malloc( num * sizeof( T ) ) );
86  }
87 
88  // initialize elements of allocated storage p with value value
89  void construct( pointer p, const T & value ) {
90  // initialized memory with placement new
91  new ( static_cast<void *>( p ) ) T( value );
92  }
93 
94  // destroy elements of initialized storage p
95  void destroy( pointer p ) {
96  // destroy objects by calling their destructor
97  p->~T( );
98  }
99 
100  // deallocate storage p of deleted elements
101  void deallocate( pointer p, size_type num ) {
102  // deallocate memory with fftw_free
103  fftw_free( static_cast<void *>( p ) );
104  }
105 
106  }; // class FftwAllocator
107 
108  // return that all specializations of this allocator are interchangeable
109  template <class T1, class T2>
110  bool operator==( const FftwAllocator<T1> &,
111  const FftwAllocator<T2> & ) throw ( ) {
112  return true;
113  }
114 
115  template <class T1, class T2>
116  bool operator!=( const FftwAllocator<T1> &,
117  const FftwAllocator<T2> & ) throw ( ) {
118  return false;
119  }
120 
121 } // namespace util
122 } // namespace carma
123 #endif
Allocator for memory for use by FFTW.
Definition: FftwAllocator.h:30
FftwAllocator()
Constructors and destructor.
Definition: FftwAllocator.h:63