Line data Source code
1 : #include <cassert> 2 : #include <iostream> 3 : 4 : #include <libsakura/sakura.h> 5 : 6 : #include <casacore/casa/Logging/LogIO.h> 7 : #include <casacore/casa/Logging/LogOrigin.h> 8 : 9 : namespace casa { 10 : template<typename T> 11 4 : inline SakuraAlignedArray<T>::SakuraAlignedArray(size_t num_data) : 12 4 : num_data_(num_data) { 13 : #if !defined(NDEBUG) 14 8 : casacore::LogIO logger(casacore::LogOrigin("SakuraAlignedArray", "SakuraAlignedArray", WHERE)); 15 4 : logger << casacore::LogIO::DEBUGGING << "Constructing SakuraAlignedArray..." << casacore::LogIO::POST; 16 : #endif 17 : 18 4 : initialize(); 19 : 20 : #if !defined(NDEBUG) 21 4 : logger << casacore::LogIO::DEBUGGING << " Initial Address = " << storage_ << casacore::LogIO::POST; 22 4 : logger << casacore::LogIO::DEBUGGING << " Aligned Address = " << data_ << casacore::LogIO::POST; 23 : #endif 24 4 : } 25 : 26 : template<typename T> 27 0 : inline SakuraAlignedArray<T>::SakuraAlignedArray(casacore::Vector<T> const &in_vector) : 28 0 : num_data_(in_vector.nelements()) { 29 : #if !defined(NDEBUG) 30 0 : casacore::LogIO logger(casacore::LogOrigin("SakuraAlignedArray", "SakuraAlignedArray", WHERE)); 31 0 : logger << casacore::LogIO::DEBUGGING << "Constructing SakuraAlignedArray..." << casacore::LogIO::POST; 32 : #endif 33 : 34 0 : initialize(); 35 : 36 0 : T *ptr = data_; 37 0 : for (size_t i = 0; i < num_data_; ++i) { 38 0 : ptr[i] = in_vector(i); 39 : } 40 : 41 : #if !defined(NDEBUG) 42 0 : logger << casacore::LogIO::DEBUGGING << " Initial Address = " << storage_ << casacore::LogIO::POST; 43 0 : logger << casacore::LogIO::DEBUGGING << " Aligned Address = " << data_ << casacore::LogIO::POST; 44 : #endif 45 0 : } 46 : 47 : template<typename T> 48 4 : inline void SakuraAlignedArray<T>::initialize() { 49 4 : storage_ = nullptr; 50 4 : data_ = nullptr; 51 : 52 4 : size_t size_required = sizeof(T) * num_data_; 53 4 : size_t size_of_arena = size_required + LIBSAKURA_SYMBOL(GetAlignment)() - 1; 54 4 : storage_ = malloc(size_of_arena); 55 4 : if (storage_ == nullptr) { 56 0 : data_ = nullptr; 57 0 : throw std::bad_alloc(); 58 : } 59 4 : data_ = reinterpret_cast<T *>(LIBSAKURA_SYMBOL(AlignAny)( 60 : size_of_arena, storage_, size_required)); 61 4 : assert(data_ != nullptr); 62 4 : assert(LIBSAKURA_SYMBOL(IsAligned)(data_)); 63 4 : } 64 : 65 : template<typename T> 66 4 : inline SakuraAlignedArray<T>::~SakuraAlignedArray() { 67 : #if !defined(NDEBUG) 68 8 : casacore::LogIO logger(casacore::LogOrigin("SakuraAlignedArray", "~SakuraAlignedArray", WHERE)); 69 4 : logger << casacore::LogIO::DEBUGGING << "Destructing SakuraAlignedArray..." << casacore::LogIO::POST; 70 : #endif 71 : 72 4 : free(storage_); 73 4 : } 74 : 75 : } // End of casa namespace.