36 #ifndef MEMORY_BUFFER_THREAD_HEADER
37 #define MEMORY_BUFFER_THREAD_HEADER
43 #ifdef USE_SSE_INTRINSICS
48 #include <emmintrin.h>
50 #include <pmmintrin.h>
58 class Memory_aligned {
60 inline void *
operator new (
size_t s) {
61 void * p = _mm_malloc (s, 16);
63 throw std::bad_alloc();
68 inline void *
operator new[] (
size_t s) {
69 void * p = _mm_malloc (s, 16);
71 throw std::bad_alloc();
76 inline void operator delete (
void * p) {
80 inline void operator delete[] (
void * p) {
85 class Memory_buffer_thread :
public Memory_aligned {
89 static Memory_buffer_thread* ptr_to_instance;
92 static volatile char ptr_to_instance_is_valid;
93 static unsigned int bufSize;
94 Memory_buffer_thread(Memory_buffer_thread
const &);
96 Memory_buffer_thread() {}
99 static Memory_buffer_thread& instance();
101 void get_buffer(
size_t size, T* & buffer)
const {
102 if (
sizeof(T) * size > bufSize)
103 throw std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
104 "Allocated buffer smaller than requested "
108 for (
int ind = 0; ind <= omp_get_level(); ind++) {
109 int tmp = omp_get_ancestor_thread_num(ind);
110 threadID = threadID > tmp ? threadID : tmp;
114 throw std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
116 if ((
unsigned)threadID >= buffers.size())
117 throw std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
118 "thread id larger than number of buffers");
119 buffer = (T*)buffers[threadID];
121 void init_buffers(
unsigned int const nThreads);
122 ~Memory_buffer_thread();
124 std::vector<char*> buffers;