Coverage Report

Created: 2025-11-03 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5UC.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Reference counting buffer algorithms.
15
 *
16
 * These are used for various internal buffers which are shared.
17
 *
18
 * The module used to be H5RC, but changed to H5UC because of
19
 * conflicting requirement for the use of H5RC.
20
 *
21
 */
22
23
#include "H5private.h"   /* Generic Functions                    */
24
#include "H5Eprivate.h"  /* Error handling         */
25
#include "H5FLprivate.h" /* Free lists                           */
26
#include "H5UCprivate.h" /* Reference-counted buffers            */
27
28
/* Private typedefs & structs */
29
30
/* Declare a free list to manage the H5UC_t struct */
31
H5FL_DEFINE_STATIC(H5UC_t);
32
33
/*--------------------------------------------------------------------------
34
 NAME
35
    H5UC_create
36
 PURPOSE
37
    Create a reference counted object
38
 USAGE
39
    H5UC_t *H5UC_create(o,free)
40
        const void *o;          IN: Object to initialize ref-counted object with
41
        H5UC_free_func_t free;  IN: Function to call when ref-count drop to zero
42
43
 RETURNS
44
    Returns a pointer to a new ref-counted object on success, NULL on failure.
45
 DESCRIPTION
46
    Create a reference counted object.  The object is not duplicated, it is
47
    assumed to be owned by the reference counted object now and will be freed
48
    with the 'free' function when the reference count drops to zero.
49
 GLOBAL VARIABLES
50
 COMMENTS, BUGS, ASSUMPTIONS
51
 EXAMPLES
52
 REVISION LOG
53
--------------------------------------------------------------------------*/
54
H5UC_t *
55
H5UC_create(void *o, H5UC_free_func_t free_func)
56
2
{
57
2
    H5UC_t *ret_value; /* Return value */
58
59
2
    FUNC_ENTER_NOAPI(NULL)
60
61
    /* Sanity check */
62
2
    assert(o);
63
2
    assert(free_func);
64
65
    /* Allocate ref-counted string structure */
66
2
    if (NULL == (ret_value = H5FL_MALLOC(H5UC_t)))
67
0
        HGOTO_ERROR(H5E_RS, H5E_NOSPACE, NULL, "memory allocation failed");
68
69
    /* Set the internal fields */
70
2
    ret_value->o         = o;
71
2
    ret_value->n         = 1;
72
2
    ret_value->free_func = free_func;
73
74
2
done:
75
2
    FUNC_LEAVE_NOAPI(ret_value)
76
2
} /* end H5UC_create() */
77
78
/*--------------------------------------------------------------------------
79
 NAME
80
    H5UC_decr
81
 PURPOSE
82
    Decrement the reference count for a ref-counted object
83
 USAGE
84
    herr_t H5UC_decr(rc)
85
        H5UC_t *rc;             IN: Ref-counted object to decrement count for
86
87
 RETURNS
88
    SUCCEED/FAIL
89
 DESCRIPTION
90
    Decrements the reference count for a ref-counted object, calling the
91
    object's free function if ref-count drops to zero.
92
 GLOBAL VARIABLES
93
 COMMENTS, BUGS, ASSUMPTIONS
94
 EXAMPLES
95
 REVISION LOG
96
--------------------------------------------------------------------------*/
97
herr_t
98
H5UC_decr(H5UC_t *rc)
99
4
{
100
4
    herr_t ret_value = SUCCEED; /* Return value */
101
102
4
    FUNC_ENTER_NOAPI(FAIL)
103
104
    /* Sanity check */
105
4
    assert(rc);
106
4
    assert(rc->o);
107
4
    assert(rc->n > 0);
108
4
    assert(rc->free_func);
109
110
    /* Decrement reference count */
111
4
    rc->n--;
112
113
    /* Check if we should delete this object now */
114
4
    if (rc->n == 0) {
115
2
        if ((rc->free_func)(rc->o) < 0) {
116
0
            rc = H5FL_FREE(H5UC_t, rc);
117
0
            HGOTO_ERROR(H5E_RS, H5E_CANTFREE, FAIL, "memory release failed");
118
0
        } /* end if */
119
2
        rc = H5FL_FREE(H5UC_t, rc);
120
2
    } /* end if */
121
122
4
done:
123
4
    FUNC_LEAVE_NOAPI(ret_value)
124
4
} /* end H5UC_decr() */