LCOV - code coverage report
Current view: top level - libs/mem/src - mem_data_readonly.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.2 % 26 25
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 85.0 % 20 17

             Branch data     Line data    Source code
       1                 :             : #include "mem.h"
       2                 :             : #include "mem_internal.h"
       3                 :             : 
       4                 :             : /**
       5                 :             :  * @brief Return a read-only typed pointer after verifying the element size
       6                 :             :  *
       7                 :             :  * This helper performs the same descriptor validation as
       8                 :             :  * @ref mem_data_writable, but returns a read-only pointer. Use it when the
       9                 :             :  * caller wants checked typed access and does not intend to modify the
      10                 :             :  * descriptor's storage through the returned pointer.
      11                 :             :  *
      12                 :             :  * The function never rewrites the descriptor. It either returns the existing
      13                 :             :  * storage pointer as `const void *`, or reports why the descriptor is not safe
      14                 :             :  * to read and returns `NULL`
      15                 :             :  *
      16                 :             :  * @param memory_structure Pointer to a descriptor
      17                 :             :  * @param expected_single_element_size Expected element size in bytes, usually `sizeof(T)`
      18                 :             :  * @return Read-only data pointer on success. `NULL` when the descriptor is invalid or the element size does not match
      19                 :             :  */
      20                 :        1107 : const void *mem_data_readonly(
      21                 :             :         const memory *memory_structure,
      22                 :             :         size_t       expected_single_element_size)
      23                 :             : {
      24                 :        1107 :         size_t current_payload_bytes = 0;
      25                 :             : 
      26         [ +  + ]:        1107 :         if(memory_structure == NULL)
      27                 :             :         {
      28                 :           1 :                 report("Memory management; Descriptor is NULL");
      29                 :           1 :                 return(NULL);
      30                 :             :         }
      31                 :             : 
      32         [ +  + ]:        1106 :         if(memory_structure->single_element_size == 0)
      33                 :             :         {
      34                 :           1 :                 report("Memory management; Descriptor element size is zero (uninitialized)");
      35                 :           1 :                 return(NULL);
      36                 :             :         }
      37                 :             : 
      38   [ +  +  +  + ]:        1105 :         if(memory_structure->data == NULL && memory_structure->actually_allocated_bytes > 0)
      39                 :             :         {
      40                 :           1 :                 report("Memory management; Descriptor has reserved bytes with NULL data pointer");
      41                 :           1 :                 return(NULL);
      42                 :             :         }
      43                 :             : 
      44   [ +  -  +  + ]:        1104 :         if(memory_structure->length > 0 && memory_structure->data == NULL)
      45                 :             :         {
      46                 :           1 :                 report("Memory management; Descriptor has non-zero length with NULL data pointer");
      47                 :           1 :                 return(NULL);
      48                 :             :         }
      49                 :             : 
      50                 :        1103 :         Return guarded_status = mem_guarded_byte_size(
      51                 :             :                 memory_structure,
      52                 :        1103 :                 memory_structure->length,
      53                 :             :                 &current_payload_bytes);
      54                 :             : 
      55         [ -  + ]:        1103 :         if((TRIUMPH & guarded_status) == 0)
      56                 :             :         {
      57                 :           0 :                 return(NULL);
      58                 :             :         }
      59                 :             : 
      60         [ +  - ]:        1103 :         if(memory_structure->length > 0 &&
      61         [ +  + ]:        1103 :                 memory_structure->actually_allocated_bytes < current_payload_bytes)
      62                 :             :         {
      63                 :           1 :                 report("Memory management; Descriptor reserve is smaller than logical payload");
      64                 :           1 :                 return(NULL);
      65                 :             :         }
      66                 :             : 
      67         [ +  + ]:        1102 :         if(memory_structure->single_element_size != expected_single_element_size)
      68                 :             :         {
      69                 :           1 :                 report("Memory management; Expected %zu bytes but descriptor uses %zu",expected_single_element_size,memory_structure->single_element_size);
      70                 :           1 :                 return(NULL);
      71                 :             :         }
      72                 :             : 
      73                 :        1101 :         return(memory_structure->data);
      74                 :             : }
        

Generated by: LCOV version 2.0-1