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