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