LCOV - code coverage report
Current view: top level - src/core - raii.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 17 17
Test Date: 2026-04-20 19:54:22 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        10547 : static inline void free_ptr(void *ptr) {
      17        10547 :     void **p = (void **)ptr;
      18        10547 :     if (*p) {
      19        10545 :         free(*p);
      20        10545 :         *p = NULL;
      21              :     }
      22        10547 : }
      23              : 
      24         1433 : static inline void fclose_ptr(void *ptr) {
      25         1433 :     FILE **p = (FILE **)ptr;
      26         1433 :     if (*p) {
      27         1344 :         fclose(*p);
      28         1344 :         *p = NULL;
      29              :     }
      30         1433 : }
      31              : 
      32            9 : static inline void closedir_ptr(DIR **p) {
      33            9 :     if (p && *p) {
      34            5 :         closedir(*p);
      35            5 :         *p = NULL;
      36              :     }
      37            9 : }
      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