Coverage Report

Created: 2026-11-22 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5.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
/* Module Setup */
15
/****************/
16
#include "H5module.h" /* This source code file is part of the H5 module */
17
18
/***********/
19
/* Headers */
20
/***********/
21
#include "H5private.h"   /* Generic Functions                        */
22
#include "H5ACprivate.h" /* Metadata cache                           */
23
#include "H5CXprivate.h" /* API Contexts                             */
24
#include "H5Dprivate.h"  /* Datasets                                 */
25
#include "H5Eprivate.h"  /* Error handling                           */
26
#include "H5FLprivate.h" /* Free lists                               */
27
#include "H5FSprivate.h" /* File free space                          */
28
#include "H5Lprivate.h"  /* Links                                    */
29
#include "H5MMprivate.h" /* Memory management                        */
30
#include "H5Pprivate.h"  /* Property lists                           */
31
#include "H5PLprivate.h" /* Plugins                                  */
32
#include "H5SLprivate.h" /* Skip lists                               */
33
#include "H5Tprivate.h"  /* Datatypes                                */
34
#include "H5TSprivate.h" /* Threadsafety                             */
35
36
/****************/
37
/* Local Macros */
38
/****************/
39
40
/******************/
41
/* Local Typedefs */
42
/******************/
43
44
/********************/
45
/* Package Typedefs */
46
/********************/
47
48
/* Node for list of 'atclose' routines to invoke at library shutdown */
49
typedef struct H5_atclose_node_t {
50
    H5_atclose_func_t         func; /* Function to invoke */
51
    void                     *ctx;  /* Context to pass to function */
52
    struct H5_atclose_node_t *next; /* Pointer to next node in list */
53
} H5_atclose_node_t;
54
55
/********************/
56
/* Local Prototypes */
57
/********************/
58
static void H5__debug_mask(const char *);
59
#ifdef H5_HAVE_PARALLEL
60
static int H5__mpi_delete_cb(MPI_Comm comm, int keyval, void *attr_val, int *flag);
61
#endif /*H5_HAVE_PARALLEL*/
62
static herr_t H5_check_version(unsigned majnum, unsigned minnum, unsigned relnum);
63
64
/*********************/
65
/* Package Variables */
66
/*********************/
67
68
/* Package initialization variable */
69
bool H5_PKG_INIT_VAR = false;
70
71
/*****************************/
72
/* Library Private Variables */
73
/*****************************/
74
75
/* Library known incompatible minor versions; develop releases are incompatible
76
 * by design. 999 is entered for testing an exception as a minor version that
77
 * will never occur.  Any released minor version found to be truly incompatible
78
 * (this should never happen) should be added to the list with 999.  999 alone
79
 * in the list indicates that there are no incompatible minor versions. */
80
static const unsigned VERS_MINOR_EXCEPTIONS[] = {999};
81
/* The size should be set to the number of minor version exceptions in the list. */
82
static const unsigned VERS_MINOR_EXCEPTIONS_SIZE = 1;
83
84
/* Library init / term status (global) */
85
bool H5_libinit_g = false; /* Library hasn't been initialized */
86
bool H5_libterm_g = false; /* Library isn't being shutdown */
87
88
char        H5_lib_vers_info_g[] = H5_VERS_INFO;
89
static bool H5_dont_atexit_g     = false;
90
H5_debug_t  H5_debug_g; /* debugging info */
91
92
/*******************/
93
/* Local Variables */
94
/*******************/
95
96
/* Linked list of registered 'atclose' functions to invoke at library shutdown */
97
static H5_atclose_node_t *H5_atclose_head = NULL;
98
99
/* Declare a free list to manage the H5_atclose_node_t struct */
100
H5FL_DEFINE_STATIC(H5_atclose_node_t);
101
102
/*--------------------------------------------------------------------------
103
NAME
104
    H5__init_package -- Initialize interface-specific information
105
USAGE
106
    herr_t H5__init_package()
107
RETURNS
108
    Non-negative on success/Negative on failure
109
DESCRIPTION
110
    Initializes any interface-specific data or routines.
111
--------------------------------------------------------------------------*/
112
herr_t
113
H5__init_package(void)
114
1
{
115
1
    herr_t ret_value = SUCCEED; /* Return value */
116
117
1
    FUNC_ENTER_PACKAGE
118
119
    /* Run the library initialization routine, if it hasn't already ran */
120
1
    if (!H5_INIT_GLOBAL && !H5_TERM_GLOBAL)
121
1
        if (H5_init_library() < 0)
122
0
            HGOTO_ERROR(H5E_LIB, H5E_CANTINIT, FAIL, "unable to initialize library");
123
124
1
done:
125
1
    FUNC_LEAVE_NOAPI(ret_value)
126
1
} /* end H5__init_package() */
127
128
/*--------------------------------------------------------------------------
129
 * NAME
130
 *   H5_init_library -- Initialize library-global information
131
 * USAGE
132
 *    herr_t H5_init_library()
133
 *
134
 * RETURNS
135
 *    Non-negative on success/Negative on failure
136
 *
137
 * DESCRIPTION
138
 *    Initializes any library-global data or routines.
139
 *
140
 *--------------------------------------------------------------------------
141
 */
142
herr_t
143
H5_init_library(void)
144
2
{
145
2
    herr_t ret_value = SUCCEED;
146
147
2
    FUNC_ENTER_NOAPI(FAIL)
148
149
    /* Run the library initialization routine, if it hasn't already run */
150
2
    if (H5_INIT_GLOBAL || H5_TERM_GLOBAL)
151
1
        HGOTO_DONE(SUCCEED);
152
153
    /* Check library version */
154
    /* (Will abort() on failure) */
155
1
    H5_check_version(H5_VERS_MAJOR, H5_VERS_MINOR, H5_VERS_RELEASE);
156
157
    /* Set the 'library initialized' flag as early as possible, to avoid
158
     * possible re-entrancy.
159
     */
160
1
    H5_INIT_GLOBAL = true;
161
162
    /* Make sure we picked a good type for ssize_t if it wasn't present */
163
1
    HDcompile_assert(sizeof(size_t) == sizeof(ssize_t));
164
165
#ifdef H5_HAVE_PARALLEL
166
    {
167
        int mpi_initialized;
168
        int mpi_finalized;
169
        int mpi_code;
170
171
        MPI_Initialized(&mpi_initialized);
172
        MPI_Finalized(&mpi_finalized);
173
174
        /* add an attribute on MPI_COMM_SELF to call H5_term_library
175
           when it is destroyed, i.e. on MPI_Finalize */
176
        if (mpi_initialized && !mpi_finalized) {
177
            int key_val;
178
179
            if (MPI_SUCCESS != (mpi_code = MPI_Comm_create_keyval(
180
                                    MPI_COMM_NULL_COPY_FN, (MPI_Comm_delete_attr_function *)H5__mpi_delete_cb,
181
                                    &key_val, NULL)))
182
                HMPI_GOTO_ERROR(FAIL, "MPI_Comm_create_keyval failed", mpi_code)
183
184
            if (MPI_SUCCESS != (mpi_code = MPI_Comm_set_attr(MPI_COMM_SELF, key_val, NULL)))
185
                HMPI_GOTO_ERROR(FAIL, "MPI_Comm_set_attr failed", mpi_code)
186
187
            if (MPI_SUCCESS != (mpi_code = MPI_Comm_free_keyval(&key_val)))
188
                HMPI_GOTO_ERROR(FAIL, "MPI_Comm_free_keyval failed", mpi_code)
189
        }
190
    }
191
#endif /*H5_HAVE_PARALLEL*/
192
193
    /*
194
     * Make sure the package information is updated.
195
     */
196
1
    memset(&H5_debug_g, 0, sizeof H5_debug_g);
197
1
    H5_debug_g.pkg[H5_PKG_A].name  = "a";
198
1
    H5_debug_g.pkg[H5_PKG_AC].name = "ac";
199
1
    H5_debug_g.pkg[H5_PKG_B].name  = "b";
200
1
    H5_debug_g.pkg[H5_PKG_D].name  = "d";
201
1
    H5_debug_g.pkg[H5_PKG_E].name  = "e";
202
1
    H5_debug_g.pkg[H5_PKG_F].name  = "f";
203
1
    H5_debug_g.pkg[H5_PKG_G].name  = "g";
204
1
    H5_debug_g.pkg[H5_PKG_HG].name = "hg";
205
1
    H5_debug_g.pkg[H5_PKG_HL].name = "hl";
206
1
    H5_debug_g.pkg[H5_PKG_I].name  = "i";
207
1
    H5_debug_g.pkg[H5_PKG_M].name  = "m";
208
1
    H5_debug_g.pkg[H5_PKG_MF].name = "mf";
209
1
    H5_debug_g.pkg[H5_PKG_MM].name = "mm";
210
1
    H5_debug_g.pkg[H5_PKG_O].name  = "o";
211
1
    H5_debug_g.pkg[H5_PKG_P].name  = "p";
212
1
    H5_debug_g.pkg[H5_PKG_PL].name = "pl";
213
1
    H5_debug_g.pkg[H5_PKG_S].name  = "s";
214
1
    H5_debug_g.pkg[H5_PKG_T].name  = "t";
215
1
    H5_debug_g.pkg[H5_PKG_V].name  = "v";
216
1
    H5_debug_g.pkg[H5_PKG_VL].name = "vl";
217
1
    H5_debug_g.pkg[H5_PKG_Z].name  = "z";
218
219
    /*
220
     * Install atexit() library cleanup routines unless the H5dont_atexit()
221
     * has been called.  Once we add something to the atexit() list it stays
222
     * there permanently, so we set H5_dont_atexit_g after we add it to prevent
223
     * adding it again later if the library is closed and reopened.
224
     */
225
1
    if (!H5_dont_atexit_g) {
226
227
#ifdef H5_HAVE_THREADSAFE_API
228
        /* Clean up thread resources.
229
         *
230
         * This must be pushed before the library cleanup code so it's
231
         * executed in LIFO order (i.e., last).
232
         */
233
        (void)atexit(H5TS_term_package);
234
#endif /* H5_HAVE_THREADSAFE_API */
235
236
        /* Normal library termination code */
237
1
        (void)atexit(H5_term_library);
238
239
1
        H5_dont_atexit_g = true;
240
1
    } /* end if */
241
242
    /*
243
     * Initialize interfaces that use macros of the form "(H5OPEN <var>)", so
244
     * that the variable returned through the macros has been initialized.
245
     * Also initialize some interfaces that might not be able to initialize
246
     * themselves soon enough.
247
     *
248
     * Interfaces returning variables through a macro: H5E, H5FD, H5O, H5P, H5T
249
     *
250
     * The link interface needs to be initialized so that the external link
251
     *   class is registered.
252
     *
253
     * The FS module needs to be initialized as a result of the fix for HDFFV-10160:
254
     *   It might not be initialized during normal file open.
255
     *   When the application does not close the file, routines in the module might
256
     *   be called via H5_term_library() when shutting down the file.
257
     *
258
     * The dataspace interface needs to be initialized so that future IDs for
259
     *   dataspaces work.
260
     *
261
     * The VFD & VOL interfaces need to be initialized before the H5P interface
262
     *   so that the default VFD and default VOL connector are ready for the
263
     *   default FAPL.
264
     *
265
     */
266
1
    if (H5E_init() < 0)
267
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize error interface");
268
1
    if (H5FD_init() < 0)
269
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize VFL interface");
270
1
    if (H5VL_init_phase1() < 0)
271
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize vol interface");
272
1
    if (H5P_init_phase1() < 0)
273
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize property list interface");
274
1
    if (H5L_init() < 0)
275
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize link interface");
276
1
    if (H5O_init() < 0)
277
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize object interface");
278
1
    if (H5FS_init() < 0)
279
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize FS interface");
280
1
    if (H5S_init() < 0)
281
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize dataspace interface");
282
1
    if (H5T_init() < 0)
283
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize datatype interface");
284
285
    /* Finish initializing interfaces that depend on the interfaces above */
286
1
    if (H5P_init_phase2() < 0)
287
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize property list interface");
288
1
    if (H5VL_init_phase2() < 0)
289
0
        HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, FAIL, "unable to initialize vol interface");
290
291
    /* Debugging? */
292
1
    H5__debug_mask("-all");
293
1
    H5__debug_mask(getenv("HDF5_DEBUG"));
294
295
2
done:
296
2
    FUNC_LEAVE_NOAPI(ret_value)
297
2
} /* end H5_init_library() */
298
299
/*-------------------------------------------------------------------------
300
 * Function:    H5_term_library
301
 *
302
 * Purpose:    Terminate interfaces in a well-defined order due to
303
 *        dependencies among the interfaces, then terminate
304
 *        library-specific data.
305
 *
306
 * Return:    void
307
 *
308
 *-------------------------------------------------------------------------
309
 */
310
void
311
H5_term_library(void)
312
1
{
313
1
    int         pending, ntries = 0, n;
314
1
    size_t      at = 0;
315
1
    char        loop[1024];
316
1
    H5E_auto2_t func;
317
1
    H5CX_node_t api_ctx = {{0}, NULL}; /* API context node to push */
318
319
    /* Acquire the API lock */
320
    H5_API_SETUP_PUBLIC_API_VARS
321
    H5_API_LOCK
322
323
    /* Don't do anything if the library is already closed */
324
1
    if (!H5_INIT_GLOBAL)
325
0
        goto done;
326
327
    /* Indicate that the library is being shut down */
328
1
    H5_TERM_GLOBAL = true;
329
330
    /* Push the API context without checking for errors */
331
1
    H5CX_push(&api_ctx);
332
333
    /* Check if we should display error output */
334
1
    (void)H5E_get_default_auto_func(&func);
335
336
    /* Iterate over the list of 'atclose' callbacks that have been registered */
337
1
    if (H5_atclose_head) {
338
0
        H5_atclose_node_t *curr_atclose; /* Current 'atclose' node */
339
340
        /* Iterate over all 'atclose' nodes, making callbacks */
341
0
        curr_atclose = H5_atclose_head;
342
0
        while (curr_atclose) {
343
0
            H5_atclose_node_t *tmp_atclose; /* Temporary pointer to 'atclose' node */
344
345
            /* Prepare & restore library for user callback */
346
0
            H5_BEFORE_USER_CB_NOCHECK
347
0
                {
348
                    /* Invoke callback, providing context */
349
0
                    (*curr_atclose->func)(curr_atclose->ctx);
350
0
                }
351
0
            H5_AFTER_USER_CB_NOCHECK
352
353
            /* Advance to next node and free this one */
354
0
            tmp_atclose  = curr_atclose;
355
0
            curr_atclose = curr_atclose->next;
356
0
            H5FL_FREE(H5_atclose_node_t, tmp_atclose);
357
0
        } /* end while */
358
359
        /* Reset list head, in case library is re-initialized */
360
0
        H5_atclose_head = NULL;
361
0
    } /* end if */
362
363
    /*
364
     * Terminate each interface. The termination functions return a positive
365
     * value if they do something that might affect some other interface in a
366
     * way that would necessitate some cleanup work in the other interface.
367
     */
368
1
#define DOWN(F)                                                                                              \
369
161
    (((n = H5##F##_term_package()) && (at + 8) < sizeof loop)                                                \
370
161
         ? (sprintf(loop + at, "%s%s", (at ? "," : ""), #F), at += strlen(loop + at), n)                     \
371
161
         : ((n > 0 && (at + 5) < sizeof loop) ? (sprintf(loop + at, "..."), at += strlen(loop + at), n)      \
372
150
                                              : n))
373
374
9
    do {
375
9
        pending = 0;
376
377
        /* Try to organize these so the "higher" level components get shut
378
         * down before "lower" level components that they might rely on. -QAK
379
         */
380
381
        /* Close the event sets first, so that all asynchronous operations
382
         * complete before anything else attempts to shut down.
383
         */
384
9
        pending += DOWN(ES);
385
386
        /* Close down the user-facing interfaces, after the event sets */
387
9
        if (pending == 0) {
388
            /* Close the interfaces dependent on others */
389
9
            pending += DOWN(L);
390
391
            /* Close the "top" of various interfaces (IDs, etc) but don't shut
392
             * down the whole interface yet, so that the object header messages
393
             * get serialized correctly for entries in the metadata cache and the
394
             * symbol table entry in the superblock gets serialized correctly, etc.
395
             * all of which is performed in the 'F' shutdown.
396
             */
397
9
            pending += DOWN(A_top);
398
9
            pending += DOWN(D_top);
399
9
            pending += DOWN(G_top);
400
9
            pending += DOWN(M_top);
401
9
            pending += DOWN(S_top);
402
9
            pending += DOWN(T_top);
403
9
        } /* end if */
404
405
        /* Don't shut down the file code until objects in files are shut down */
406
9
        if (pending == 0)
407
8
            pending += DOWN(F);
408
409
        /* Don't shut down the property list code until all objects that might
410
         * use property lists are shut down */
411
9
        if (pending == 0)
412
8
            pending += DOWN(P);
413
414
        /* Wait to shut down the "bottom" of various interfaces until the
415
         * files are closed, so pieces of the file can be serialized
416
         * correctly.
417
         */
418
9
        if (pending == 0) {
419
            /* Shut down the "bottom" of the attribute, dataset, group,
420
             * dataspace, and datatype interfaces, fully closing
421
             * out the interfaces now.
422
             */
423
6
            pending += DOWN(A);
424
6
            pending += DOWN(D);
425
6
            pending += DOWN(G);
426
6
            pending += DOWN(M);
427
6
            pending += DOWN(S);
428
6
            pending += DOWN(T);
429
6
        } /* end if */
430
431
        /* Don't shut down "low-level" components until "high-level" components
432
         * have successfully shut down.  This prevents property lists and IDs
433
         * from being closed "out from underneath" of the high-level objects
434
         * that depend on them. -QAK
435
         */
436
9
        if (pending == 0) {
437
6
            pending += DOWN(AC);
438
            /* Shut down the "pluggable" interfaces, before the plugin framework */
439
6
            pending += DOWN(Z);
440
6
            pending += DOWN(FD);
441
6
            pending += DOWN(VL);
442
            /* Don't shut down the plugin code until all "pluggable" interfaces (Z, FD, PL) are shut down */
443
6
            if (pending == 0)
444
4
                pending += DOWN(PL);
445
            /* Don't shut down the error code until other APIs which use it are shut down */
446
6
            if (pending == 0)
447
3
                pending += DOWN(E);
448
            /* Don't shut down the ID code until other APIs which use them are shut down */
449
6
            if (pending == 0)
450
2
                pending += DOWN(I);
451
            /* Don't shut down the skip list code until everything that uses it is down */
452
6
            if (pending == 0)
453
2
                pending += DOWN(SL);
454
            /* Don't shut down the free list code until everything that uses it is down */
455
6
            if (pending == 0)
456
1
                pending += DOWN(FL);
457
            /* Don't shut down the API context code until _everything_ else is down */
458
6
            if (pending == 0)
459
1
                pending += DOWN(CX);
460
6
        } /* end if */
461
9
    } while (pending && ntries++ < 100);
462
463
1
    if (pending) {
464
        /* Only display the error message if the user is interested in them. */
465
0
        if (func) {
466
0
            fprintf(stderr, "HDF5: infinite loop closing library\n");
467
0
            fprintf(stderr, "      %s\n", loop);
468
#ifndef NDEBUG
469
            abort();
470
#endif
471
0
        }
472
0
    }
473
474
    /* Free open debugging streams */
475
1
    while (H5_debug_g.open_stream) {
476
0
        H5_debug_open_stream_t *tmp_open_stream;
477
478
0
        tmp_open_stream = H5_debug_g.open_stream;
479
0
        (void)fclose(H5_debug_g.open_stream->stream);
480
0
        H5_debug_g.open_stream = H5_debug_g.open_stream->next;
481
0
        (void)H5MM_free(tmp_open_stream);
482
0
    } /* end while */
483
484
    /* Reset flag indicating that the library is being shut down */
485
1
    H5_TERM_GLOBAL = false;
486
487
    /* Mark library as closed */
488
1
    H5_INIT_GLOBAL = false;
489
490
    /* Don't pop the API context (i.e. H5CX_pop), since it's been shut down already */
491
492
1
done:
493
    /* Release API lock */
494
    H5_API_UNLOCK
495
496
1
    return;
497
1
} /* end H5_term_library() */
498
499
/*-------------------------------------------------------------------------
500
 * Function:    H5dont_atexit
501
 *
502
 * Purpose:    Indicates that the library is not to clean up after itself
503
 *        when the application exits by calling exit() or returning
504
 *        from main().  This function must be called before any other
505
 *        HDF5 function or constant is used or it will have no effect.
506
 *
507
 *        If this function is used then certain memory buffers will not
508
 *        be de-allocated nor will open files be flushed automatically.
509
 *        The application may still call H5close() explicitly to
510
 *        accomplish these things.
511
 *
512
 * Return:    Success:    non-negative
513
 *
514
 *        Failure:    negative if this function is called more than
515
 *                once or if it is called too late.
516
 *
517
 *-------------------------------------------------------------------------
518
 */
519
herr_t
520
H5dont_atexit(void)
521
0
{
522
0
    herr_t ret_value = SUCCEED; /* Return value */
523
524
0
    FUNC_ENTER_API_NOINIT_NOERR
525
526
0
    if (H5_dont_atexit_g)
527
0
        ret_value = FAIL;
528
0
    else
529
0
        H5_dont_atexit_g = true;
530
531
0
    FUNC_LEAVE_API_NOERR(ret_value)
532
0
} /* end H5dont_atexit() */
533
534
/*-------------------------------------------------------------------------
535
 * Function:    H5garbage_collect
536
 *
537
 * Purpose:    Walks through all the garbage collection routines for the
538
 *        library, which are supposed to free any unused memory they have
539
 *        allocated.
540
 *
541
 *      These should probably be registered dynamically in a linked list of
542
 *          functions to call, but there aren't that many right now, so we
543
 *          hard-wire them...
544
 *
545
 * Return:    Success:    non-negative
546
 *
547
 *        Failure:    negative
548
 *
549
 *-------------------------------------------------------------------------
550
 */
551
herr_t
552
H5garbage_collect(void)
553
0
{
554
0
    herr_t ret_value = SUCCEED;
555
556
0
    FUNC_ENTER_API(FAIL)
557
558
    /* Call the garbage collection routines in the library */
559
0
    if (H5FL_garbage_coll() < 0)
560
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGC, FAIL, "can't garbage collect objects");
561
562
0
done:
563
0
    FUNC_LEAVE_API(ret_value)
564
0
} /* end H5garbage_collect() */
565
566
/*-------------------------------------------------------------------------
567
 * Function:    H5set_free_list_limits
568
 *
569
 * Purpose:    Sets limits on the different kinds of free lists.  Setting a value
570
 *      of -1 for a limit means no limit of that type.  These limits are global
571
 *      for the entire library.  Each "global" limit only applies to free lists
572
 *      of that type, so if an application sets a limit of 1 MB on each of the
573
 *      global lists, up to 3 MB of total storage might be allocated (1MB on
574
 *      each of regular, array and block type lists).
575
 *
576
 *      The settings for block free lists are duplicated to factory free lists.
577
 *      Factory free list limits cannot be set independently currently.
578
 *
579
 * Parameters:
580
 *  int reg_global_lim;  IN: The limit on all "regular" free list memory used
581
 *  int reg_list_lim;    IN: The limit on memory used in each "regular" free list
582
 *  int arr_global_lim;  IN: The limit on all "array" free list memory used
583
 *  int arr_list_lim;    IN: The limit on memory used in each "array" free list
584
 *  int blk_global_lim;  IN: The limit on all "block" free list memory used
585
 *  int blk_list_lim;    IN: The limit on memory used in each "block" free list
586
 *
587
 * Return:    Success:    non-negative
588
 *
589
 *        Failure:    negative
590
 *
591
 *-------------------------------------------------------------------------
592
 */
593
herr_t
594
H5set_free_list_limits(int reg_global_lim, int reg_list_lim, int arr_global_lim, int arr_list_lim,
595
                       int blk_global_lim, int blk_list_lim)
596
0
{
597
0
    herr_t ret_value = SUCCEED;
598
599
0
    FUNC_ENTER_API(FAIL)
600
601
    /* Call the free list function to actually set the limits */
602
0
    if (H5FL_set_free_list_limits(reg_global_lim, reg_list_lim, arr_global_lim, arr_list_lim, blk_global_lim,
603
0
                                  blk_list_lim, blk_global_lim, blk_list_lim) < 0)
604
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTSET, FAIL, "can't set garbage collection limits");
605
606
0
done:
607
0
    FUNC_LEAVE_API(ret_value)
608
0
} /* end H5set_free_list_limits() */
609
610
/*-------------------------------------------------------------------------
611
 * Function:    H5get_free_list_sizes
612
 *
613
 * Purpose:    Gets the current size of the different kinds of free lists that
614
 *    the library uses to manage memory.  The free list sizes can be set with
615
 *    H5set_free_list_limits and garbage collected with H5garbage_collect.
616
 *      These lists are global for the entire library.
617
 *
618
 * Parameters:
619
 *  size_t *reg_size;    OUT: The current size of all "regular" free list memory used
620
 *  size_t *arr_size;    OUT: The current size of all "array" free list memory used
621
 *  size_t *blk_size;    OUT: The current size of all "block" free list memory used
622
 *  size_t *fac_size;    OUT: The current size of all "factory" free list memory used
623
 *
624
 * Return:    Success:    non-negative
625
 *        Failure:    negative
626
 *
627
 *-------------------------------------------------------------------------
628
 */
629
herr_t
630
H5get_free_list_sizes(size_t *reg_size /*out*/, size_t *arr_size /*out*/, size_t *blk_size /*out*/,
631
                      size_t *fac_size /*out*/)
632
0
{
633
0
    herr_t ret_value = SUCCEED; /* Return value */
634
635
0
    FUNC_ENTER_API(FAIL)
636
637
    /* Call the free list function to actually get the sizes */
638
0
    if (H5FL_get_free_list_sizes(reg_size, arr_size, blk_size, fac_size) < 0)
639
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGET, FAIL, "can't get garbage collection sizes");
640
641
0
done:
642
0
    FUNC_LEAVE_API(ret_value)
643
0
} /* end H5get_free_list_sizes() */
644
645
/*-------------------------------------------------------------------------
646
 * Function:    H5__debug_mask
647
 *
648
 * Purpose:     Set runtime debugging flags according to the string S.  The
649
 *              string should contain file numbers and package names
650
 *              separated by other characters. A file number applies to all
651
 *              following package names up to the next file number. The
652
 *              initial file number is `2' (the standard error stream). Each
653
 *              package name can be preceded by a `+' or `-' to add or remove
654
 *              the package from the debugging list (`+' is the default). The
655
 *              special name `all' means all packages.
656
 *
657
 *              The name `trace' indicates that API tracing is to be turned
658
 *              on or off.
659
 *
660
 *              The name 'ttop' indicates that only top-level API calls
661
 *              should be shown. This also turns on tracing as if the
662
 *              'trace' word was shown.
663
 *
664
 * Return:      void
665
 *
666
 *-------------------------------------------------------------------------
667
 */
668
static void
669
H5__debug_mask(const char *s)
670
2
{
671
2
    FILE  *stream = stderr;
672
2
    char   pkg_name[32], *rest;
673
2
    size_t i;
674
2
    bool   clear;
675
676
3
    while (s && *s) {
677
678
1
        if (isalpha(*s) || '-' == *s || '+' == *s) {
679
680
            /* Enable or Disable debugging? */
681
1
            if ('-' == *s) {
682
1
                clear = true;
683
1
                s++;
684
1
            }
685
0
            else if ('+' == *s) {
686
0
                clear = false;
687
0
                s++;
688
0
            }
689
0
            else {
690
0
                clear = false;
691
0
            } /* end if */
692
693
            /* Get the name */
694
3
            for (i = 0; isalpha(*s); i++, s++)
695
3
                if (i < sizeof pkg_name)
696
3
                    pkg_name[i] = *s;
697
1
            pkg_name[MIN(sizeof(pkg_name) - 1, i)] = '\0';
698
699
            /* Trace, all, or one? */
700
1
            if (!strcmp(pkg_name, "trace")) {
701
0
                H5_debug_g.trace = clear ? NULL : stream;
702
0
            }
703
1
            else if (!strcmp(pkg_name, "ttop")) {
704
0
                H5_debug_g.trace = stream;
705
0
                H5_debug_g.ttop  = (bool)!clear;
706
0
            }
707
1
            else if (!strcmp(pkg_name, "ttimes")) {
708
0
                H5_debug_g.trace  = stream;
709
0
                H5_debug_g.ttimes = (bool)!clear;
710
0
            }
711
1
            else if (!strcmp(pkg_name, "all")) {
712
22
                for (i = 0; i < (size_t)H5_NPKGS; i++)
713
21
                    H5_debug_g.pkg[i].stream = clear ? NULL : stream;
714
1
            }
715
0
            else {
716
0
                for (i = 0; i < (size_t)H5_NPKGS; i++) {
717
0
                    if (!strcmp(H5_debug_g.pkg[i].name, pkg_name)) {
718
0
                        H5_debug_g.pkg[i].stream = clear ? NULL : stream;
719
0
                        break;
720
0
                    } /* end if */
721
0
                }     /* end for */
722
0
                if (i >= (size_t)H5_NPKGS)
723
0
                    fprintf(stderr, "HDF5_DEBUG: ignored %s\n", pkg_name);
724
0
            } /* end if-else */
725
1
        }
726
0
        else if (isdigit(*s)) {
727
0
            int                     fd = (int)strtol(s, &rest, 0);
728
0
            H5_debug_open_stream_t *open_stream;
729
730
0
            if ((stream = HDfdopen(fd, "w")) != NULL) {
731
0
                (void)HDsetvbuf(stream, NULL, _IOLBF, (size_t)0);
732
733
0
                if (NULL ==
734
0
                    (open_stream = (H5_debug_open_stream_t *)H5MM_malloc(sizeof(H5_debug_open_stream_t)))) {
735
0
                    (void)fclose(stream);
736
0
                    return;
737
0
                } /* end if */
738
739
0
                open_stream->stream    = stream;
740
0
                open_stream->next      = H5_debug_g.open_stream;
741
0
                H5_debug_g.open_stream = open_stream;
742
0
            } /* end if */
743
744
0
            s = rest;
745
0
        }
746
0
        else {
747
0
            s++;
748
0
        } /* end if-else */
749
1
    }     /* end while */
750
2
} /* end H5__debug_mask() */
751
752
#ifdef H5_HAVE_PARALLEL
753
754
/*-------------------------------------------------------------------------
755
 * Function:    H5__mpi_delete_cb
756
 *
757
 * Purpose:    Callback attribute on MPI_COMM_SELF to terminate the HDF5
758
 *              library when the communicator is destroyed, i.e. on MPI_Finalize.
759
 *
760
 * Return:    MPI_SUCCESS
761
 *
762
 *-------------------------------------------------------------------------
763
 */
764
static int
765
H5__mpi_delete_cb(MPI_Comm H5_ATTR_UNUSED comm, int H5_ATTR_UNUSED keyval, void H5_ATTR_UNUSED *attr_val,
766
                  int H5_ATTR_UNUSED *flag)
767
{
768
    H5_term_library();
769
    return MPI_SUCCESS;
770
}
771
#endif /*H5_HAVE_PARALLEL*/
772
773
/*-------------------------------------------------------------------------
774
 * Function:    H5get_libversion
775
 *
776
 * Purpose:    Returns the library version numbers through arguments. MAJNUM
777
 *        will be the major revision number of the library, MINNUM the
778
 *        minor revision number, and RELNUM the release revision number.
779
 *
780
 * Note:    When printing an HDF5 version number it should be printed as
781
 *
782
 *         printf("%u.%u.%u", maj, min, rel)        or
783
 *        printf("version %u.%u release %u", maj, min, rel)
784
 *
785
 * Return:    Non-negative on success/Negative on failure
786
 *
787
 *-------------------------------------------------------------------------
788
 */
789
herr_t
790
H5get_libversion(unsigned *majnum /*out*/, unsigned *minnum /*out*/, unsigned *relnum /*out*/)
791
0
{
792
0
    herr_t ret_value = SUCCEED;
793
794
0
    FUNC_ENTER_API(FAIL)
795
796
    /* Set the version information */
797
0
    if (majnum)
798
0
        *majnum = H5_VERS_MAJOR;
799
0
    if (minnum)
800
0
        *minnum = H5_VERS_MINOR;
801
0
    if (relnum)
802
0
        *relnum = H5_VERS_RELEASE;
803
804
0
done:
805
0
    FUNC_LEAVE_API(ret_value)
806
0
} /* end H5get_libversion() */
807
808
/*-------------------------------------------------------------------------
809
 * Function:    H5__check_version
810
 *
811
 * Purpose:     Internal routine which Verifies that the arguments match the
812
 *              version numbers compiled into the library.
813
 *
814
 *              Within major.minor.release version, the expectation
815
 *              is that all minor versions are compatible, exceptions to
816
 *              this rule must be added to the VERS_MINOR_EXCEPTIONS list.
817
 *
818
 * Return:      Success:    SUCCEED
819
 *              Failure:    abort()
820
 *
821
 *-------------------------------------------------------------------------
822
 */
823
#define VERSION_MISMATCH_WARNING                                                                             \
824
1
    "Warning! ***HDF5 library version mismatched error***\n"                                                 \
825
1
    "The HDF5 header files used to compile this application do not match\n"                                  \
826
1
    "the version used by the HDF5 library to which this application is linked.\n"                            \
827
1
    "Data corruption or segmentation faults may occur if the application continues.\n"                       \
828
1
    "This can happen when an application was compiled by one version of HDF5 but\n"                          \
829
1
    "linked with a different version of static or shared HDF5 library.\n"                                    \
830
1
    "You should recompile the application or check your shared library related\n"                            \
831
1
    "settings such as 'LD_LIBRARY_PATH'.\n"
832
#define MINOR_VERSION_MISMATCH_WARNING                                                                       \
833
1
    "Warning! ***HDF5 library minor version mismatched error***\n"                                           \
834
1
    "The HDF5 header files used to compile this application are not compatible with\n"                       \
835
1
    "the version used by the HDF5 library to which this application is linked.\n"                            \
836
1
    "Data corruption or segmentation faults may occur if the application continues.\n"                       \
837
1
    "This can happen when an application was compiled by one version of HDF5 but\n"                          \
838
1
    "linked with an incompatible version of static or shared HDF5 library.\n"                                \
839
1
    "You should recompile the application or check your shared library related\n"                            \
840
1
    "settings such as 'LD_LIBRARY_PATH'.\n"
841
#define MINOR_VERSION_FORWARD_COMPATIBLE_WARNING                                                             \
842
1
    "Warning! ***HDF5 library minor version forward compatibility error***\n"                                \
843
1
    "The HDF5 header files used to compile this application are from a newer\n"                              \
844
1
    "version of the HDF5 library than the one to which this application is linked.\n"                        \
845
1
    "Data corruption or segmentation faults may occur if the application continues.\n"                       \
846
1
    "This can happen when an application was compiled by a newer version of HDF5 but\n"                      \
847
1
    "linked with an older version of static or shared HDF5 library.\n"                                       \
848
1
    "You should recompile the application or check your shared library related\n"                            \
849
1
    "settings such as 'LD_LIBRARY_PATH'.\n"
850
851
static herr_t
852
H5_check_version(unsigned majnum, unsigned minnum, unsigned relnum)
853
1
{
854
1
    char                lib_str[256];
855
1
    char                substr[]                 = H5_VERS_SUBRELEASE;
856
1
    static bool         checked                  = false; /* If we've already checked the version info */
857
1
    static unsigned int disable_version_check    = 0;     /* Set if the version check should be disabled */
858
1
    static const char  *version_mismatch_warning = VERSION_MISMATCH_WARNING;
859
1
    static const char  *minor_version_mismatch_warning           = MINOR_VERSION_MISMATCH_WARNING;
860
1
    static const char  *minor_version_forward_compatible_warning = MINOR_VERSION_FORWARD_COMPATIBLE_WARNING;
861
1
    herr_t              ret_value                                = SUCCEED; /* Return value */
862
863
1
    FUNC_ENTER_NOAPI_NOINIT_NOERR
864
865
    /* Don't check again, if we already have */
866
1
    if (checked)
867
0
        HGOTO_DONE(SUCCEED);
868
869
1
    {
870
1
        const char *s; /* Environment string for disabling version check */
871
872
        /* Allow different versions of the header files and library? */
873
1
        s = getenv("HDF5_DISABLE_VERSION_CHECK");
874
875
1
        if (s && isdigit(*s))
876
0
            disable_version_check = (unsigned int)strtol(s, NULL, 0);
877
1
    }
878
879
    /* H5_VERS_MAJOR must match */
880
1
    if (H5_VERS_MAJOR != majnum) {
881
0
        switch (disable_version_check) {
882
0
            case 0:
883
0
                fprintf(stderr, "%s%s", version_mismatch_warning,
884
0
                        "You can, at your own risk, disable this warning by setting the environment\n"
885
0
                        "variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.\n"
886
0
                        "Setting it to 2 or higher will suppress the warning messages totally.\n");
887
                /* Mention the versions we are referring to */
888
0
                fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
889
0
                        (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
890
                /* Show library build settings if available */
891
0
                fprintf(stderr, "%s", H5build_settings);
892
893
                /* Bail out now. */
894
0
                fputs("Bye...\n", stderr);
895
0
                abort();
896
0
            case 1:
897
                /* continue with a warning */
898
                /* Note that the warning message is embedded in the format string.*/
899
0
                fprintf(stderr,
900
0
                        "%s'HDF5_DISABLE_VERSION_CHECK' "
901
0
                        "environment variable is set to %d, application will\n"
902
0
                        "continue at your own risk.\n",
903
0
                        version_mismatch_warning, disable_version_check);
904
                /* Mention the versions we are referring to */
905
0
                fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
906
0
                        (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
907
                /* Show library build settings if available */
908
0
                fprintf(stderr, "%s", H5build_settings);
909
0
                break;
910
0
            default:
911
                /* 2 or higher: continue silently */
912
0
                break;
913
0
        } /* end switch */
914
915
0
    } /* end if (H5_VERS_MAJOR != majnum) */
916
917
    /* H5_VERS_MINOR should be compatible, we will only add checks for exceptions */
918
    /* Library develop minor versions are incompatible by design */
919
1
    if (H5_VERS_MINOR != minnum) {
920
0
        for (unsigned i = 0; i < VERS_MINOR_EXCEPTIONS_SIZE; i++) {
921
            /* Check for incompatible headers or incompatible library */
922
0
            if (VERS_MINOR_EXCEPTIONS[i] == minnum || VERS_MINOR_EXCEPTIONS[i] == H5_VERS_MINOR) {
923
0
                switch (disable_version_check) {
924
0
                    case 0:
925
0
                        fprintf(stderr, "%s%s", minor_version_mismatch_warning,
926
0
                                "You can, at your own risk, disable this warning by setting the environment\n"
927
0
                                "variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.\n"
928
0
                                "Setting it to 2 or higher will suppress the warning messages totally.\n");
929
                        /* Mention the versions we are referring to */
930
0
                        fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
931
0
                                (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
932
933
                        /* Bail out now. */
934
0
                        fputs("Bye...\n", stderr);
935
0
                        abort();
936
0
                    case 1:
937
                        /* continue with a warning */
938
                        /* Note that the warning message is embedded in the format string.*/
939
0
                        fprintf(stderr,
940
0
                                "%s'HDF5_DISABLE_VERSION_CHECK' "
941
0
                                "environment variable is set to %d, application will\n"
942
0
                                "continue at your own risk.\n",
943
0
                                minor_version_mismatch_warning, disable_version_check);
944
                        /* Mention the versions we are referring to */
945
0
                        fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
946
0
                                (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
947
0
                        break;
948
0
                    default:
949
                        /* 2 or higher: continue silently */
950
0
                        break;
951
0
                } /* end switch */
952
953
0
            } /* end if */
954
955
0
        } /* end for */
956
957
        /* Check for forward compatibility usage. */
958
0
        if (minnum > H5_VERS_MINOR) {
959
0
            switch (disable_version_check) {
960
0
                case 0:
961
0
                    fprintf(stderr, "%s%s", minor_version_forward_compatible_warning,
962
0
                            "You can, at your own risk, disable this warning by setting the environment\n"
963
0
                            "variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.\n"
964
0
                            "Setting it to 2 or higher will suppress the warning messages totally.\n");
965
                    /* Mention the versions we are referring to */
966
0
                    fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
967
0
                            (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
968
969
                    /* Bail out now. */
970
0
                    fputs("Bye...\n", stderr);
971
0
                    abort();
972
0
                case 1:
973
                    /* continue with a warning */
974
                    /* Note that the warning message is embedded in the format string.*/
975
0
                    fprintf(stderr,
976
0
                            "%s'HDF5_DISABLE_VERSION_CHECK' "
977
0
                            "environment variable is set to %d, application will\n"
978
0
                            "continue at your own risk.\n",
979
0
                            minor_version_forward_compatible_warning, disable_version_check);
980
                    /* Mention the versions we are referring to */
981
0
                    fprintf(stderr, "Headers are %u.%u.%u, library is %u.%u.%u\n", majnum, minnum, relnum,
982
0
                            (unsigned)H5_VERS_MAJOR, (unsigned)H5_VERS_MINOR, (unsigned)H5_VERS_RELEASE);
983
0
                    break;
984
0
                default:
985
                    /* 2 or higher: continue silently */
986
0
                    break;
987
0
            } /* end switch */
988
0
        }
989
990
0
    } /* end if (H5_VERS_MINOR != minnum) */
991
992
    /* Indicate that the version check has been performed */
993
1
    checked = true;
994
995
1
    if (!disable_version_check) {
996
        /*
997
         * Verify if H5_VERS_INFO is consistent with the other version information.
998
         * Check only the first sizeof(lib_str) char.  Assume the information
999
         * will fit within this size or enough significance.
1000
         */
1001
1
        snprintf(lib_str, sizeof(lib_str), "HDF5 library version: %d.%d.%d%s%s", H5_VERS_MAJOR, H5_VERS_MINOR,
1002
1
                 H5_VERS_RELEASE, (*substr ? "-" : ""), substr);
1003
1004
1
        if (strcmp(lib_str, H5_lib_vers_info_g) != 0) {
1005
0
            fputs("Warning!  Library version information error.\n"
1006
0
                  "The HDF5 library version information are not "
1007
0
                  "consistent in its source code.\nThis is NOT a fatal error "
1008
0
                  "but should be corrected.  Setting the environment\n"
1009
0
                  "variable 'HDF5_DISABLE_VERSION_CHECK' to a value of 1 "
1010
0
                  "will suppress\nthis warning.\n",
1011
0
                  stderr);
1012
0
            fprintf(stderr,
1013
0
                    "Library version information are:\n"
1014
0
                    "H5_VERS_MAJOR=%d, H5_VERS_MINOR=%d, H5_VERS_RELEASE=%d, "
1015
0
                    "H5_VERS_SUBRELEASE=%s,\nH5_VERS_INFO=%s\n",
1016
0
                    H5_VERS_MAJOR, H5_VERS_MINOR, H5_VERS_RELEASE, H5_VERS_SUBRELEASE, H5_VERS_INFO);
1017
0
        } /* end if */
1018
1
    }
1019
1020
1
done:
1021
1
    FUNC_LEAVE_NOAPI(ret_value)
1022
1
} /* end H5__check_version() */
1023
1024
herr_t
1025
H5check_version(unsigned majnum, unsigned minnum, unsigned relnum)
1026
0
{
1027
0
    herr_t ret_value = SUCCEED; /* Return value */
1028
1029
0
    FUNC_ENTER_API_NOINIT_NOERR
1030
1031
    /* Call internal routine */
1032
    /* (Will abort() on failure) */
1033
0
    H5_check_version(majnum, minnum, relnum);
1034
1035
0
    FUNC_LEAVE_API_NOERR(ret_value)
1036
0
} /* end H5check_version() */
1037
1038
/*-------------------------------------------------------------------------
1039
 * Function:    H5open
1040
 *
1041
 * Purpose:     Initialize the library.  This is normally called
1042
 *              automatically, but if you find that an HDF5 library function
1043
 *              is failing inexplicably, then try calling this function
1044
 *              first.
1045
 *
1046
 * Return:    Non-negative on success/Negative on failure
1047
 *
1048
 *-------------------------------------------------------------------------
1049
 */
1050
herr_t
1051
H5open(void)
1052
0
{
1053
0
    herr_t ret_value = SUCCEED; /* Return value */
1054
1055
0
    FUNC_ENTER_API_NOPUSH(FAIL)
1056
1057
    /* all work is done by FUNC_ENTER() */
1058
1059
0
done:
1060
0
    FUNC_LEAVE_API_NOPUSH(ret_value)
1061
0
} /* end H5open() */
1062
1063
/*-------------------------------------------------------------------------
1064
 * Function:    H5atclose
1065
 *
1066
 * Purpose:    Register a callback for the library to invoke when it's
1067
 *        closing.  Callbacks are invoked in LIFO order.
1068
 *
1069
 * Return:    Non-negative on success/Negative on failure
1070
 *
1071
 *-------------------------------------------------------------------------
1072
 */
1073
herr_t
1074
H5atclose(H5_atclose_func_t func, void *ctx)
1075
0
{
1076
0
    H5_atclose_node_t *new_atclose;         /* New 'atclose' node */
1077
0
    herr_t             ret_value = SUCCEED; /* Return value */
1078
1079
0
    FUNC_ENTER_API(FAIL)
1080
1081
    /* Check arguments */
1082
0
    if (NULL == func)
1083
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL func pointer");
1084
1085
    /* Allocate space for the 'atclose' node */
1086
0
    if (NULL == (new_atclose = H5FL_MALLOC(H5_atclose_node_t)))
1087
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't allocate 'atclose' node");
1088
1089
    /* Set up 'atclose' node */
1090
0
    new_atclose->func = func;
1091
0
    new_atclose->ctx  = ctx;
1092
1093
    /* Connector to linked-list of 'atclose' nodes */
1094
0
    new_atclose->next = H5_atclose_head;
1095
0
    H5_atclose_head   = new_atclose;
1096
1097
0
done:
1098
0
    FUNC_LEAVE_API(ret_value)
1099
0
} /* end H5atclose() */
1100
1101
/*-------------------------------------------------------------------------
1102
 * Function:    H5close
1103
 *
1104
 * Purpose:    Terminate the library and release all resources.
1105
 *
1106
 * Return:    Non-negative on success/Negative on failure
1107
 *
1108
 *-------------------------------------------------------------------------
1109
 */
1110
herr_t
1111
H5close(void)
1112
0
{
1113
    /*
1114
     * Don't call normal FUNC_ENTER() since we don't want to initialize the
1115
     * whole library just to release it all right away.  It is safe to call
1116
     * this function for an uninitialized library.
1117
     */
1118
0
    FUNC_ENTER_API_NAMECHECK_ONLY
1119
1120
0
    H5_term_library();
1121
1122
0
    FUNC_LEAVE_API_NAMECHECK_ONLY(SUCCEED)
1123
0
} /* end H5close() */
1124
1125
/*-------------------------------------------------------------------------
1126
 * Function:    H5allocate_memory
1127
 *
1128
 * Purpose:        Allocate a memory buffer with the semantics of malloc().
1129
 *
1130
 *              NOTE: This function is intended for use with filter
1131
 *              plugins so that all allocation and free operations
1132
 *              use the same memory allocator. It is not intended for
1133
 *              use as a general memory allocator in applications.
1134
 *
1135
 * Parameters:
1136
 *
1137
 *      size:   The size of the buffer.
1138
 *
1139
 *      clear:  Whether or not to memset the buffer to 0.
1140
 *
1141
 * Return:
1142
 *
1143
 *      Success:    A pointer to the allocated buffer or NULL if the size
1144
 *                  parameter is zero.
1145
 *
1146
 *      Failure:    NULL (but may also be NULL w/ size 0!)
1147
 *
1148
 *-------------------------------------------------------------------------
1149
 */
1150
void *H5_ATTR_MALLOC
1151
H5allocate_memory(size_t size, bool clear)
1152
0
{
1153
0
    void *ret_value = NULL;
1154
1155
0
    FUNC_ENTER_API_NOINIT
1156
1157
0
    if (0 == size)
1158
0
        HGOTO_DONE(NULL);
1159
1160
0
    if (clear)
1161
0
        ret_value = H5MM_calloc(size);
1162
0
    else
1163
0
        ret_value = H5MM_malloc(size);
1164
1165
0
done:
1166
0
    FUNC_LEAVE_API_NOINIT(ret_value)
1167
0
} /* end H5allocate_memory() */
1168
1169
/*-------------------------------------------------------------------------
1170
 * Function:    H5resize_memory
1171
 *
1172
 * Purpose:        Resize a memory buffer with the semantics of realloc().
1173
 *
1174
 *              NOTE: This function is intended for use with filter
1175
 *              plugins so that all allocation and free operations
1176
 *              use the same memory allocator. It is not intended for
1177
 *              use as a general memory allocator in applications.
1178
 *
1179
 * Parameters:
1180
 *
1181
 *      mem:    The buffer to be resized.
1182
 *
1183
 *      size:   The size of the buffer.
1184
 *
1185
 * Return:
1186
 *
1187
 *      Success:    A pointer to the resized buffer.
1188
 *
1189
 *      Failure:    NULL (the input buffer will be unchanged)
1190
 *
1191
 *-------------------------------------------------------------------------
1192
 */
1193
void *
1194
H5resize_memory(void *mem, size_t size)
1195
0
{
1196
0
    void *ret_value = NULL;
1197
1198
0
    FUNC_ENTER_API_NOINIT
1199
1200
0
    ret_value = H5MM_realloc(mem, size);
1201
1202
0
    FUNC_LEAVE_API_NOINIT(ret_value)
1203
0
} /* end H5resize_memory() */
1204
1205
/*-------------------------------------------------------------------------
1206
 * Function:    H5free_memory
1207
 *
1208
 * Purpose:        Frees memory allocated by the library that it is the user's
1209
 *              responsibility to free.  Ensures that the same library
1210
 *              that was used to allocate the memory frees it.  Passing
1211
 *              NULL pointers is allowed.
1212
 *
1213
 * Return:        SUCCEED/FAIL
1214
 *
1215
 *-------------------------------------------------------------------------
1216
 */
1217
herr_t
1218
H5free_memory(void *mem)
1219
0
{
1220
0
    FUNC_ENTER_API_NOINIT
1221
1222
    /* At this time, it is impossible for this to fail. */
1223
0
    H5MM_xfree(mem);
1224
1225
0
    FUNC_LEAVE_API_NOINIT(SUCCEED)
1226
0
} /* end H5free_memory() */
1227
1228
/*-------------------------------------------------------------------------
1229
 * Function:    H5is_library_threadsafe
1230
 *
1231
 * Purpose:        Checks to see if the library was built with thread-safety
1232
 *              enabled.
1233
 *
1234
 * Return:        SUCCEED/FAIL
1235
 *
1236
 *-------------------------------------------------------------------------
1237
 */
1238
herr_t
1239
H5is_library_threadsafe(bool *is_ts /*out*/)
1240
0
{
1241
0
    herr_t ret_value = SUCCEED; /* Return value */
1242
1243
0
    FUNC_ENTER_API_NOINIT
1244
1245
0
    if (is_ts) {
1246
#ifdef H5_HAVE_THREADSAFE_API
1247
        *is_ts = true;
1248
#else  /* H5_HAVE_THREADSAFE_API */
1249
0
        *is_ts = false;
1250
0
#endif /* H5_HAVE_THREADSAFE_API */
1251
0
    }
1252
0
    else
1253
0
        ret_value = FAIL;
1254
1255
0
    FUNC_LEAVE_API_NOINIT(ret_value)
1256
0
} /* end H5is_library_threadsafe() */
1257
1258
/*-------------------------------------------------------------------------
1259
 * Function:    H5is_library_terminating
1260
 *
1261
 * Purpose:    Checks to see if the library is shutting down.
1262
 *
1263
 * Note:    Useful for plugins to detect when the library is terminating.
1264
 *        For example, a VOL connector could check if a "file close"
1265
 *        callback was the result of the library shutdown process, or
1266
 *        an API action from the application.
1267
 *
1268
 * Return:    SUCCEED/FAIL
1269
 *
1270
 *-------------------------------------------------------------------------
1271
 */
1272
herr_t
1273
H5is_library_terminating(bool *is_terminating /*out*/)
1274
0
{
1275
0
    herr_t ret_value = SUCCEED; /* Return value */
1276
1277
0
    FUNC_ENTER_API_NOINIT
1278
1279
0
    assert(is_terminating);
1280
1281
0
    if (is_terminating)
1282
0
        *is_terminating = H5_TERM_GLOBAL;
1283
0
    else
1284
0
        ret_value = FAIL;
1285
1286
0
    FUNC_LEAVE_API_NOINIT(ret_value)
1287
0
} /* end H5is_library_terminating() */
1288
1289
/*-------------------------------------------------------------------------
1290
 * Function:    H5_user_cb_prepare
1291
 *
1292
 * Purpose:     Prepares library before a user callback
1293
 *
1294
 * Return:      SUCCEED/FAIL
1295
 *
1296
 *-------------------------------------------------------------------------
1297
 */
1298
herr_t
1299
H5_user_cb_prepare(H5_user_cb_state_t *state)
1300
1.24M
{
1301
1.24M
    herr_t ret_value = SUCCEED; /* Return value */
1302
1303
1.24M
    FUNC_ENTER_NOAPI(FAIL)
1304
1305
    /* Prepare H5E package for user callback */
1306
1.24M
    if (H5E_user_cb_prepare(&state->h5e_state) < 0)
1307
0
        HGOTO_ERROR(H5E_LIB, H5E_CANTSET, FAIL, "unable to prepare H5E package for user callback");
1308
1309
#ifdef H5_HAVE_CONCURRENCY
1310
    /* Prepare H5TS package for user callback */
1311
    if (H5TS_user_cb_prepare() < 0)
1312
        HGOTO_ERROR(H5E_LIB, H5E_CANTSET, FAIL, "unable to prepare H5TS package for user callback");
1313
#endif /* H5_HAVE_THREADSAFE_API */
1314
1315
1.24M
done:
1316
1.24M
    FUNC_LEAVE_NOAPI(ret_value)
1317
1.24M
} /* end H5_user_cb_prepare() */
1318
1319
/*-------------------------------------------------------------------------
1320
 * Function:    H5_user_cb_restore
1321
 *
1322
 * Purpose:     Restores library after a user callback
1323
 *
1324
 * Return:      SUCCEED/FAIL
1325
 *
1326
 *-------------------------------------------------------------------------
1327
 */
1328
herr_t
1329
H5_user_cb_restore(const H5_user_cb_state_t *state)
1330
1.24M
{
1331
1.24M
    herr_t ret_value = SUCCEED; /* Return value */
1332
1333
1.24M
    FUNC_ENTER_NOAPI(FAIL)
1334
1335
    /* Restore H5E package after user callback */
1336
1.24M
    if (H5E_user_cb_restore(&state->h5e_state) < 0)
1337
0
        HGOTO_ERROR(H5E_LIB, H5E_CANTRESTORE, FAIL, "unable to restore H5E package after user callback");
1338
1339
#ifdef H5_HAVE_CONCURRENCY
1340
    /* Restore H5TS package after user callback */
1341
    if (H5TS_user_cb_restore() < 0)
1342
        HGOTO_ERROR(H5E_LIB, H5E_CANTRESTORE, FAIL, "unable to restore H5TS package after user callback");
1343
#endif /* H5_HAVE_THREADSAFE_API */
1344
1345
1.24M
done:
1346
1.24M
    FUNC_LEAVE_NOAPI(ret_value)
1347
1.24M
} /* end H5_user_cb_restore() */