LCOV - code coverage report
Current view: top level - elsa/storage/memory_resource - HostStandardResource.cpp (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 30 32 93.8 %
Date: 2024-05-16 04:22:26 Functions: 5 5 100.0 %

          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       95380 :     {
      21       95380 :         if (size == 0) {
      22           3 :             ++size;
      23           3 :         }
      24       95380 :         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       95368 :         alignment = std::max(sizeof(void*), alignment);
      35       95368 : #endif
      36             : 
      37             :         // std::aligned_alloc requires size as a multiple of alignment
      38       95368 :         size = detail::alignUp(size, alignment);
      39       95368 :         void* ptr = std::aligned_alloc(alignment, size);
      40       95368 :         if (unlikely(!ptr)) {
      41           0 :             throw std::bad_alloc();
      42           0 :         }
      43       95368 :         return ptr;
      44       95368 :     }
      45             : 
      46             :     void HostStandardResource::deallocate(void* ptr, size_t size, size_t alignment) noexcept
      47       95368 :     {
      48       95368 :         static_cast<void>(size);
      49       95368 :         static_cast<void>(alignment);
      50       95368 :         std::free(ptr);
      51       95368 :     }
      52             : 
      53             :     bool HostStandardResource::tryResize(void* ptr, size_t size, size_t alignment,
      54             :                                          size_t newSize) noexcept
      55         296 :     {
      56         296 :         static_cast<void>(ptr);
      57         296 :         static_cast<void>(size);
      58         296 :         static_cast<void>(alignment);
      59         296 :         static_cast<void>(newSize);
      60         296 :         return false;
      61         296 :     }
      62             : } // namespace elsa::mr

Generated by: LCOV version 1.14