CbmRoot
std_alloc.h
Go to the documentation of this file.
1 #ifndef STD_ALLOC_H
2 #define STD_ALLOC_H
3 // ---------------------- Allocator for using STL ------------------------
4 
5 #include "xmmintrin.h"
6 #include <limits>
7 #include <vector>
8 
9 
10 namespace nsL1 {
11 
12  // #define DEBUG_nsL1
13 
14  template<class T>
15  class SimdAlloc {
16  public:
17  // type definitions
18  typedef T value_type;
19  typedef T* pointer;
20  typedef const T* const_pointer;
21  typedef T& reference;
22  typedef const T& const_reference;
23  typedef std::size_t size_type;
24  typedef std::ptrdiff_t difference_type;
25 
26  // rebind allocator to type U
27  template<class U>
28  struct rebind {
30  };
31 
32  // return address of values
33  pointer address(reference value) const { return &value; }
34  const_pointer address(const_reference value) const { return &value; }
35 
36  /* constructors and destructor
37  * - nothing to do because the allocator has no state
38  */
39  SimdAlloc() throw() {}
40  SimdAlloc(const SimdAlloc&) throw() {}
41  template<class U>
42  SimdAlloc(const SimdAlloc<U>&) throw() {}
43  ~SimdAlloc() throw() {}
44 
45  // return maximum number of elements that can be allocated
46  size_type max_size() const throw() {
47  return std::numeric_limits<std::size_t>::max() / sizeof(T);
48  }
49 
50  // allocate but don't initialize num elements of type T
51  pointer allocate(size_type num, const void* = 0) {
52 // print message and allocate memory with global new
53 #ifdef DEBUG_nsL1
54  std::cerr << "Allocator: allocate " << num << " element(s)"
55  << " of size " << sizeof(T) << std::endl;
56 #endif // DEBUG_nsL1
57  pointer ret =
58  reinterpret_cast<pointer>(/*T::*/ operator new(num * sizeof(T)));
59 #ifdef DEBUG_nsL1
60  std::cerr << " allocated at: " << (void*) ret << std::endl;
61 #endif // DEBUG_nsL1
62  return ret;
63  }
64 
65  // initialize elements of allocated storage p with value value
66  void construct(pointer p, const T& value) {
67  // initialize memory with placement new
68 #ifdef DEBUG_nsL1
69  std::cerr << "Allocator: construct "
70  << p /*<< " " << value*/ << std::endl;
71 #endif // DEBUG_nsL1
72  new (p) T(value);
73 // p = reinterpret_cast<pointer>( operator new(sizeof(T), p) );
74 // *p = value;
75 #ifdef DEBUG_nsL1
76  std::cerr << "done." << std::endl;
77 #endif // DEBUG_nsL1
78  }
79 
80  // destroy elements of initialized storage p
81  void destroy(pointer p) {
82  // destroy objects by calling their destructor
83 #ifdef DEBUG_nsL1
84  std::cerr << "Allocator: destroy " << p << std::endl;
85 #endif // DEBUG_nsL1
86  p->~T();
87 #ifdef DEBUG_nsL1
88  std::cerr << "done." << std::endl;
89 #endif // DEBUG_nsL1
90  }
91 
92  // deallocate storage p of deleted elements
93  void deallocate(pointer p, size_type num) {
94  // print message and deallocate memory with global delete
95 #ifdef DEBUG_nsL1
96  std::cerr << "Allocator: deallocate " << num << " element(s)"
97  << " of size " << sizeof(T) << " at: " << static_cast<void*>(p)
98  << std::endl;
99 #endif // DEBUG_nsL1
100  /*T::*/ operator delete(static_cast<void*>(p), num * sizeof(T));
101 #ifdef DEBUG_nsL1
102  std::cerr << "done." << std::endl;
103 #endif // DEBUG_nsL1
104  }
105 
106 
107  void* operator new(size_t size, void* ptr) {
108  return ::operator new(size, ptr);
109  }
110  void* operator new[](size_t size, void* ptr) {
111  return ::operator new(size, ptr);
112  }
113  void* operator new(size_t size) { return _mm_malloc(size, 16); }
114  void* operator new[](size_t size) { return _mm_malloc(size, 16); }
115  void operator delete(void* ptr, size_t) { _mm_free(ptr); }
116  void operator delete[](void* ptr, size_t) { _mm_free(ptr); }
117  }; // SimdAlloc
118 
119  // return that all specializations of this allocator are interchangeable
120  template<class T1, class T2>
121  bool operator==(const SimdAlloc<T1>&, const SimdAlloc<T2>&) throw() {
122  return true;
123  };
124  template<class T1, class T2>
125  bool operator!=(const SimdAlloc<T1>&, const SimdAlloc<T2>&) throw() {
126  return false;
127  };
128 
129  template<typename T>
130  struct vector {
131  vector() {};
132  virtual ~vector() {};
133 
134  typedef std::vector<T> TStd;
135  // typedef std::vector<T > TSimd;
136  typedef std::vector<T, SimdAlloc<T>> TSimd;
137  };
138 
140 }; // namespace nsL1
141 
142 template<typename T>
143 struct nsL1vector :
144  public nsL1::vector<T> // just for use std::vector simultaniosly
145 {};
146 
147 #endif
nsL1
Definition: L1/vectors/PSEUDO_F32vec1.h:144
nsL1::operator==
bool operator==(const SimdAlloc< T1 > &, const SimdAlloc< T2 > &)
Definition: std_alloc.h:121
nsL1::SimdAlloc::allocate
pointer allocate(size_type num, const void *=0)
Definition: std_alloc.h:51
nsL1::SimdAlloc::SimdAlloc
SimdAlloc()
Definition: std_alloc.h:39
_mm_free
#define _mm_free
Definition: LxCA.cxx:319
nsL1::SimdAlloc::~SimdAlloc
~SimdAlloc()
Definition: std_alloc.h:43
nsL1::vector::~vector
virtual ~vector()
Definition: std_alloc.h:132
nsL1::SimdAlloc::destroy
void destroy(pointer p)
Definition: std_alloc.h:81
nsL1::SimdAlloc::SimdAlloc
SimdAlloc(const SimdAlloc &)
Definition: std_alloc.h:40
nsL1::SimdAlloc::construct
void construct(pointer p, const T &value)
Definition: std_alloc.h:66
nsL1::SimdAlloc::size_type
std::size_t size_type
Definition: std_alloc.h:23
nsL1::SimdAlloc::const_reference
const T & const_reference
Definition: std_alloc.h:22
_mm_malloc
#define _mm_malloc(X, Y)
Definition: LxCA.cxx:318
nsL1::SimdAlloc::rebind::other
SimdAlloc< U > other
Definition: std_alloc.h:29
nsL1::SimdAlloc::SimdAlloc
SimdAlloc(const SimdAlloc< U > &)
Definition: std_alloc.h:42
nsL1::SimdAlloc::difference_type
std::ptrdiff_t difference_type
Definition: std_alloc.h:24
nsL1::SimdAlloc::const_pointer
const T * const_pointer
Definition: std_alloc.h:20
nsL1::SimdAlloc::deallocate
void deallocate(pointer p, size_type num)
Definition: std_alloc.h:93
nsL1::SimdAlloc::rebind
Definition: std_alloc.h:28
nsL1::SimdAlloc::address
pointer address(reference value) const
Definition: std_alloc.h:33
nsL1::vector_fvec
nsL1::vector< fvec >::TSimd vector_fvec
Definition: L1/vectors/PSEUDO_F32vec1.h:151
nsL1::vector::TSimd
std::vector< T > TSimd
Definition: L1/vectors/PSEUDO_F32vec1.h:148
nsL1::vector::vector
vector()
Definition: std_alloc.h:131
nsL1::SimdAlloc::pointer
T * pointer
Definition: std_alloc.h:19
nsL1::operator!=
bool operator!=(const SimdAlloc< T1 > &, const SimdAlloc< T2 > &)
Definition: std_alloc.h:125
nsL1::SimdAlloc::max_size
size_type max_size() const
Definition: std_alloc.h:46
nsL1::SimdAlloc::address
const_pointer address(const_reference value) const
Definition: std_alloc.h:34
nsL1::vector::TStd
std::vector< T > TStd
Definition: std_alloc.h:132
nsL1::SimdAlloc::value_type
T value_type
Definition: std_alloc.h:18
max
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:36
nsL1::vector
Definition: L1/vectors/PSEUDO_F32vec1.h:146
nsL1::SimdAlloc
Definition: std_alloc.h:15
nsL1vector
Definition: L1/vectors/PSEUDO_F32vec1.h:157
nsL1::SimdAlloc::reference
T & reference
Definition: std_alloc.h:21
nsL1::vector::TSimd
std::vector< T, SimdAlloc< T > > TSimd
Definition: std_alloc.h:136