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