LCOV - code coverage report
Current view: top level - src/core - raii.h (source / functions) Coverage Total Hit
Test: coverage-functional.info Lines: 100.0 % 17 17
Test Date: 2026-04-20 19:54:24 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /* SPDX-License-Identifier: GPL-3.0-or-later */
       2              : /* Copyright 2026 Peter Csaszar */
       3              : 
       4              : #ifndef RAII_H
       5              : #define RAII_H
       6              : 
       7              : #include <stdlib.h>
       8              : #include <stdio.h>
       9              : #include <dirent.h>
      10              : 
      11              : /**
      12              :  * Cleanup functions for GNU RAII attributes.
      13              :  * These are called when a variable with the __attribute__((cleanup(func))) goes out of scope.
      14              :  */
      15              : 
      16         4683 : static inline void free_ptr(void *ptr) {
      17         4683 :     void **p = (void **)ptr;
      18         4683 :     if (*p) {
      19         4683 :         free(*p);
      20         4683 :         *p = NULL;
      21              :     }
      22         4683 : }
      23              : 
      24          653 : static inline void fclose_ptr(void *ptr) {
      25          653 :     FILE **p = (FILE **)ptr;
      26          653 :     if (*p) {
      27          617 :         fclose(*p);
      28          617 :         *p = NULL;
      29              :     }
      30          653 : }
      31              : 
      32            2 : static inline void closedir_ptr(DIR **p) {
      33            2 :     if (p && *p) {
      34            1 :         closedir(*p);
      35            1 :         *p = NULL;
      36              :     }
      37            2 : }
      38              : 
      39              : #define RAII_STRING __attribute__((cleanup(free_ptr)))
      40              : #define RAII_FILE __attribute__((cleanup(fclose_ptr)))
      41              : #define RAII_DIR __attribute__((cleanup(closedir_ptr)))
      42              : 
      43              : /* To avoid circular dependencies with Config, we use a generic cleanup for it 
      44              :  * but it must be defined in each file that uses it or we use a macro. */
      45              : #define RAII_WITH_CLEANUP(func) __attribute__((cleanup(func)))
      46              : 
      47              : #endif // RAII_H
        

Generated by: LCOV version 2.0-1