Coverage Report

Created: 2026-02-23 06:05

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
150
{
57
150
    H5UC_t *ret_value; /* Return value */
58
59
150
    FUNC_ENTER_NOAPI(NULL)
60
61
    /* Sanity check */
62
150
    assert(o);
63
150
    assert(free_func);
64
65
    /* Allocate ref-counted string structure */
66
150
    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
150
    ret_value->o         = o;
71
150
    ret_value->n         = 1;
72
150
    ret_value->free_func = free_func;
73
74
150
done:
75
150
    FUNC_LEAVE_NOAPI(ret_value)
76
150
} /* 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
195
{
100
195
    herr_t ret_value = SUCCEED; /* Return value */
101
102
195
    FUNC_ENTER_NOAPI(FAIL)
103
104
    /* Sanity check */
105
195
    assert(rc);
106
195
    assert(rc->o);
107
195
    assert(rc->n > 0);
108
195
    assert(rc->free_func);
109
110
    /* Decrement reference count */
111
195
    rc->n--;
112
113
    /* Check if we should delete this object now */
114
195
    if (rc->n == 0) {
115
150
        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
150
        rc = H5FL_FREE(H5UC_t, rc);
120
150
    } /* end if */
121
122
195
done:
123
195
    FUNC_LEAVE_NOAPI(ret_value)
124
195
} /* end H5UC_decr() */