LCOV - code coverage report
Current view: top level - src - file_buffer_memory.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 12 12
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 100.0 % 4 4

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : #ifdef TESTITALL
       4                 :             : // In test builds route sysconf() through a test hook to avoid overriding
       5                 :             : // the libc symbol globally (ASan/UBSan may rely on it).
       6                 :             : long testitall_sysconf(int name);
       7                 :             : #define sysconf(name) testitall_sysconf(name)
       8                 :             : #endif
       9                 :             : 
      10                 :             : /**
      11                 :             :  * @brief Determines the maximum amount of memory that can be allocated for the buffer.
      12                 :             :  *
      13                 :             :  * This function estimates how much memory can be allocated for a buffer based on
      14                 :             :  * available physical memory. It defaults to 1MB if system calls fail.
      15                 :             :  *
      16                 :             :  * @note The function assumes that only 1% of available RAM should be used for the buffer.
      17                 :             :  *       It may not be suitable for embedded or IoT devices with constrained memory.
      18                 :             :  *
      19                 :             :  * @return The maximum buffer size in bytes. Defaults to 1MB if system information is unavailable.
      20                 :             :  */
      21                 :         237 : size_t file_buffer_memory(void)
      22                 :             : {
      23                 :             :         // Default value is 1MB buffer. Is it too big for embedded and IoT?
      24                 :         237 :         const size_t buffer_size = 1024*1024;
      25                 :             : 
      26                 :             :         // Number of actually free pages
      27                 :             :         long pages;
      28                 :             : 
      29                 :             : #ifdef _SC_AVPHYS_PAGES
      30                 :         237 :         pages = sysconf(_SC_AVPHYS_PAGES);
      31                 :             : #elif defined(_SC_PHYS_PAGES)
      32                 :             :         // Fallback for platforms without _SC_AVPHYS_PAGES — use total pages
      33                 :             :         pages = sysconf(_SC_PHYS_PAGES);
      34                 :             : #else
      35                 :             :         pages = -1;
      36                 :             : #endif
      37                 :             : 
      38         [ +  + ]:         237 :         if(pages == -1)
      39                 :             :         {
      40                 :           1 :                 return(buffer_size);
      41                 :             :         }
      42                 :             : 
      43                 :             :         /* Page size in bytes */
      44                 :         236 :         long page_size = sysconf(_SC_PAGESIZE);
      45                 :             : 
      46         [ +  + ]:         236 :         if(page_size == -1)
      47                 :             :         {
      48                 :           1 :                 return(buffer_size);
      49                 :             :         }
      50                 :             : 
      51                 :             :         // Only 1% of available RAM
      52                 :         235 :         size_t avail_bytes = (size_t)pages * (size_t)page_size;
      53                 :             : 
      54                 :         235 :         size_t one_percent = avail_bytes / 100;
      55                 :             : 
      56                 :         235 :         slog(TRACE,"Bytes that can be allocated for the file buffer: %s\n",bkbmbgbtbpbeb(one_percent,FULL_VIEW));
      57                 :             : 
      58                 :         235 :         return(one_percent);
      59                 :             : }
        

Generated by: LCOV version 2.0-1