LCOV - code coverage report
Current view: top level - core - memory.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.0 % 35 28
Test Date: 2026-03-04 10:22:18 Functions: 100.0 % 6 6

            Line data    Source code
       1              : #include "cfd/core/memory.h"
       2              : #include "cfd/core/cfd_status.h"
       3              : #include "cfd/core/logging.h"
       4              : 
       5              : #include <stdlib.h>
       6              : #include <string.h>
       7              : 
       8              : 
       9              : #ifdef _WIN32
      10              : #else
      11              : #include <unistd.h>
      12              : #endif
      13              : 
      14              : //=============================================================================
      15              : // MEMORY MANAGEMENT
      16              : //=============================================================================
      17              : 
      18          774 : void* cfd_malloc(size_t size) {
      19          774 :     void* ptr = malloc(size);
      20          774 :     if (ptr == NULL) {
      21            0 :         cfd_error("Memory allocation failed");
      22            0 :         cfd_set_error(CFD_ERROR_NOMEM, "Memory allocation failed");
      23              :     }
      24          774 :     return ptr;
      25              : }
      26              : 
      27       121798 : void* cfd_calloc(size_t count, size_t size) {
      28       121798 :     void* ptr = calloc(count, size);
      29       121798 :     if (ptr == NULL) {
      30            0 :         cfd_error("Memory allocation failed");
      31            0 :         cfd_set_error(CFD_ERROR_NOMEM, "Memory allocation failed");
      32              :     }
      33       121798 :     return ptr;
      34              : }
      35              : 
      36       123209 : void cfd_free(void* ptr) {
      37       123209 :     if (ptr != NULL) {
      38       122569 :         free(ptr);
      39              :     }
      40       123209 : }
      41              : 
      42              : // Aligned memory allocation functions for SIMD operations
      43         1884 : void* cfd_aligned_malloc(size_t size) {
      44         1884 :     void* ptr;
      45              : 
      46              : #ifndef _WIN32
      47              :     // Linux/Unix - use robust posix_memalign
      48         1884 :     size_t alignment = 32;  // 32-byte alignment for AVX2
      49              : 
      50              :     // Ensure alignment is valid (power of 2 and >= sizeof(void*))
      51         1884 :     if (alignment < sizeof(void*)) {
      52              :         alignment = sizeof(void*);
      53              :     }
      54              : 
      55              :     // Round alignment up to next power of 2 if needed
      56         1884 :     if ((alignment & (alignment - 1)) != 0) {
      57              :         size_t power = 1;
      58              :         while (power < alignment)
      59              :             power <<= 1;
      60              :         alignment = power;
      61              :     }
      62              : 
      63         1884 :     if (posix_memalign(&ptr, alignment, size) != 0) {
      64            0 :         cfd_error("Aligned memory allocation failed");
      65            0 :         cfd_set_error(CFD_ERROR_NOMEM, "Aligned memory allocation failed");
      66            0 :         return NULL;
      67              :     }
      68              : #else
      69              :     // Windows - use _aligned_malloc
      70              :     ptr = _aligned_malloc(size, 32);
      71              :     if (ptr == NULL) {
      72              :         cfd_error("Aligned memory allocation failed");
      73              :         cfd_set_error(CFD_ERROR_NOMEM, "Aligned memory allocation failed");
      74              :     }
      75              : #endif
      76              : 
      77              :     return ptr;
      78              : }
      79              : 
      80         1824 : void* cfd_aligned_calloc(size_t count, size_t size) {
      81         1824 :     size_t total_size = count * size;
      82         1824 :     void* ptr = cfd_aligned_malloc(total_size);
      83         1824 :     if (ptr != NULL) {
      84         1824 :         memset(ptr, 0, total_size);
      85              :     }
      86         1824 :     return ptr;
      87              : }
      88              : 
      89         1884 : void cfd_aligned_free(void* ptr) {
      90         1884 :     if (ptr != NULL) {
      91              : #ifndef _WIN32
      92         1884 :         free(ptr);
      93              : #else
      94              :         _aligned_free(ptr);
      95              : #endif
      96              :     }
      97         1884 : }
        

Generated by: LCOV version 2.0-1