Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #ifndef __PALIGNED_ALLOCATOR_H__ | ||
8 | #define __PALIGNED_ALLOCATOR_H__ | ||
9 | |||
10 | #include <new> | ||
11 | #include <limits> | ||
12 | #include <iostream> | ||
13 | #include <vector> | ||
14 | |||
15 | #include <malloc.h> | ||
16 | |||
17 | #include "phoenix_template_alloc.h" | ||
18 | |||
19 | ///@brief Custom allocator to align memory of std::vector | ||
20 | template<class T> | ||
21 | struct PAlignedAllocator{ | ||
22 | ///Value of the type of the current allocator (used by std::vector to avoid ambiguities if we use multiple template) | ||
23 | typedef T value_type; | ||
24 | PAlignedAllocator () = default; //Declare the current constructor as default constructor | ||
25 | |||
26 | ///Copy constructor from U type | ||
27 | template<class U> | ||
28 | constexpr PAlignedAllocator (const PAlignedAllocator <U>&) noexcept {} | ||
29 | |||
30 | ///Do the memory allocation | ||
31 | /** @param n : number of element to be allocated | ||
32 | * @return pointer to the allocated memory | ||
33 | * This function is nodiscard, so return value cannot be ignored | ||
34 | * We have to remove nodiscard when we make it static (and it works with std::vector) | ||
35 | */ | ||
36 | 1 | static /*[[nodiscard]]*/ T* allocate(std::size_t n){ | |
37 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→7) taken 1 times.
|
1 | if(n > std::numeric_limits<std::size_t>::max() / sizeof(T)){ |
38 | ✗ | throw std::bad_array_new_length(); | |
39 | } | ||
40 | 1 | T* p = phoenix_template_alloc_aligned1d<T>(n); | |
41 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→10) not taken.
|
1 | if(p != NULL){ |
42 | 1 | return p; | |
43 | } | ||
44 | ✗ | throw std::bad_alloc(); | |
45 | } | ||
46 | ///Free the allocated memory | ||
47 | /** @param p : pointer to be freed | ||
48 | * @param n : number of element allocated in p | ||
49 | */ | ||
50 | 1 | static void deallocate(T* p, std::size_t n) noexcept{ | |
51 | 1 | phoenix_freeAlignedVector(p); | |
52 | 1 | } | |
53 | }; | ||
54 | |||
55 | |||
56 | |||
57 | #endif | ||
58 | |||
59 |