Line data Source code
1 : #include "HostStandardResource.h" 2 : 3 : #include "BitUtil.h" 4 : #include "Util.h" 5 : 6 : #include <memory> 7 : #include <cstdlib> 8 : #include <cstring> 9 : #include <algorithm> 10 : 11 : namespace elsa::mr 12 : { 13 : MemoryResource HostStandardResource::make() 14 118 : { 15 118 : return std::shared_ptr<MemResInterface>(new HostStandardResource(), 16 118 : [](HostStandardResource* p) { delete p; }); 17 118 : } 18 : 19 : void* HostStandardResource::allocate(size_t size, size_t alignment) 20 95388 : { 21 95388 : if (size == 0) { 22 3 : ++size; 23 3 : } 24 95388 : if (!detail::isPowerOfTwo(alignment)) { 25 12 : throw std::bad_alloc(); 26 12 : } 27 : 28 : #if defined(__APPLE__) 29 : // On OsX, the implementation of std::aligned_alloc requires a minimal alignment 30 : // of 16 31 : alignment = std::max(std::max<size_t>(sizeof(void*), 16), alignment); 32 : #else 33 : // posix_memalign requires a minimal alignment of sizeof(void *) 34 95376 : alignment = std::max(sizeof(void*), alignment); 35 95376 : #endif 36 : 37 : // std::aligned_alloc requires size as a multiple of alignment 38 95376 : size = detail::alignUp(size, alignment); 39 95376 : void* ptr = std::aligned_alloc(alignment, size); 40 95376 : if (unlikely(!ptr)) { 41 0 : throw std::bad_alloc(); 42 0 : } 43 95376 : return ptr; 44 95376 : } 45 : 46 : void HostStandardResource::deallocate(void* ptr, size_t size, size_t alignment) noexcept 47 95376 : { 48 95376 : static_cast<void>(size); 49 95376 : static_cast<void>(alignment); 50 95376 : std::free(ptr); 51 95376 : } 52 : 53 : bool HostStandardResource::tryResize(void* ptr, size_t size, size_t alignment, 54 : size_t newSize) noexcept 55 287 : { 56 287 : static_cast<void>(ptr); 57 287 : static_cast<void>(size); 58 287 : static_cast<void>(alignment); 59 287 : static_cast<void>(newSize); 60 287 : return false; 61 287 : } 62 : } // namespace elsa::mr