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: 2025-01-11 12:25:42 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.14