LCOV - code coverage report
Current view: top level - src/Tools/System - memory.cpp (source / functions) Hit Total Coverage
Test: streampu_clean.info Lines: 10 10 100.0 %
Date: 2024-07-31 15:48:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "Tools/System/memory.hpp"
       2             : #include <cstdlib>
       3             : 
       4             : namespace spu
       5             : {
       6             : namespace tools
       7             : {
       8             : 
       9             : static void*
      10      139690 : mem_alloc_aligned(std::size_t alignment, std::size_t size)
      11             : {
      12      139690 :     void* ptr = nullptr;
      13             : #if __cplusplus >= 201703L
      14             :     ptr = std::aligned_alloc(alignment, size);
      15             : #elif defined _POSIX_C_SOURCE && (_POSIX_C_SOURCE >= 200112L)
      16      139690 :     if (posix_memalign(&ptr, alignment, size) != 0) ptr = nullptr;
      17             : #else
      18             :     {
      19             :         if (alignment < sizeof(void*))
      20             :         {
      21             :             alignment = sizeof(void*);
      22             :         }
      23             :         void* raw_ptr = malloc(size + alignment);
      24             :         uintptr_t addr = (uintptr_t)raw_ptr;
      25             :         addr = (addr & ~(alignment - 1)) + alignment;
      26             :         *(void**)(addr - sizeof(void*)) = raw_ptr;
      27             :         ptr = (void*)addr;
      28             :     }
      29             : #endif
      30      139690 :     return ptr;
      31             : }
      32             : 
      33             : void*
      34      139690 : mem_alloc(std::size_t size)
      35             : {
      36      139690 :     std::size_t alignment = 64; // default alignment for up to 512-bit vectors
      37             :     // XXX: for large enough buffers (heuristics TBD), we could
      38             :     // make the alignment a page size to allow for CPU/GPU Shared
      39             :     // Virtual Memory (SVM) configurations
      40      139690 :     return mem_alloc_aligned(alignment, size);
      41             : }
      42             : 
      43             : void
      44      139690 : mem_free(void* ptr)
      45             : {
      46             : #if (__cplusplus >= 201703L) || (defined _POSIX_C_SOURCE && (_POSIX_C_SOURCE >= 200112L))
      47      139690 :     free(ptr);
      48             : #else
      49             :     {
      50             :         uintptr_t addr = (uintptr_t)ptr;
      51             :         void* raw_ptr = *(void**)(addr - sizeof(void*));
      52             :         free(raw_ptr);
      53             :     }
      54             : #endif
      55      139690 : }
      56             : 
      57             : }
      58             : }

Generated by: LCOV version 1.14