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