LCOV - code coverage report
Current view: top level - src - file_buffer_memory.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 11 11
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            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          158 : size_t file_buffer_memory(void)
      22              : {
      23              :         // Default value is 1MB buffer. Is it too big for embedded and IoT?
      24          158 :         const size_t buffer_size = 1024*1024;
      25              : 
      26              :         // Number of actually free pages
      27          158 :         long pages = -1;
      28              : 
      29              : #ifdef _SC_AVPHYS_PAGES
      30          158 :         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              : #endif
      35              : 
      36              :         /* Page size in bytes */
      37          158 :         long page_size = sysconf(_SC_PAGESIZE);
      38              : 
      39          158 :         if(pages == -1 || page_size == -1)
      40              :         {
      41            2 :                 return(buffer_size);
      42              :         }
      43              : 
      44              :         // Only 1% of available RAM
      45          156 :         size_t avail_bytes = (size_t)pages * (size_t)page_size;
      46              : 
      47          156 :         size_t one_percent = avail_bytes / 100;
      48              : 
      49          156 :         slog(TRACE,"Bytes that can be allocated for the file buffer: %s\n",bkbmbgbtbpbeb(one_percent));
      50              : 
      51          156 :         return(one_percent);
      52              : }
        

Generated by: LCOV version 2.0-1