Coverage Report

Created: 2026-04-09 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/netcdf-c/liblib/nc_initialize.c
Line
Count
Source
1
/*********************************************************************
2
 *   Copyright 2018, UCAR/Unidata
3
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 *********************************************************************/
5
6
/**
7
 * @file
8
 * Library initialization and finalization.
9
 *
10
 * These functions handle the initialization and finalization of the
11
 * netCDF library, including all dispatch tables and external
12
 * libraries.
13
 */
14
15
#include "config.h"
16
17
#ifdef USE_PARALLEL
18
#include <mpi.h>
19
#endif
20
21
#include "ncdispatch.h"
22
23
#ifdef USE_NETCDF4
24
#include "nc4internal.h"
25
#endif
26
27
#ifdef USE_HDF5
28
#include "hdf5internal.h"
29
extern int NC_HDF5_initialize(void);
30
extern int NC_HDF5_finalize(void);
31
#endif
32
33
/*
34
 * NETCDF_ENABLE_DAP is the top-level option that enables DAP support.
35
 * Setting it implies NETCDF_ENABLE_DAP2; NETCDF_ENABLE_DAP4 is also
36
 * set when HDF5 support is available. The extern declarations here
37
 * use the protocol-specific macros, while nc_initialize() below
38
 * guards the NCD2_initialize() call with the parent NETCDF_ENABLE_DAP
39
 * macro since DAP2 is always enabled when DAP is enabled.
40
 */
41
#ifdef NETCDF_ENABLE_DAP2
42
extern int NCD2_initialize(void);
43
extern int NCD2_finalize(void);
44
#endif
45
46
#ifdef NETCDF_ENABLE_DAP4
47
extern int NCD4_initialize(void);
48
extern int NCD4_finalize(void);
49
#endif
50
51
52
#ifdef USE_PNETCDF
53
extern int NCP_initialize(void);
54
extern int NCP_finalize(void);
55
#endif
56
57
#ifdef USE_HDF4
58
extern int NC_HDF4_initialize(void);
59
extern int NC_HDF4_finalize(void);
60
#endif
61
62
#ifdef NETCDF_ENABLE_S3
63
EXTERNL int NC_s3sdkinitialize(void);
64
EXTERNL int NC_s3sdkfinalize(void);
65
#endif
66
67
#ifdef _MSC_VER
68
#include <io.h>
69
#include <fcntl.h>
70
#endif
71
72
int NC_initialized = 0;
73
int NC_finalized = 1;
74
75
#ifdef NETCDF_ENABLE_ATEXIT_FINALIZE
76
/* Provide the void function to give to atexit() */
77
static void
78
finalize_atexit(void)
79
1
{
80
1
    (void)nc_finalize();
81
1
}
82
#endif
83
84
/**
85
 * Initialize the netCDF library.
86
 *
87
 * This function sets up all internal dispatch tables and initializes
88
 * any external libraries required by the enabled protocols (e.g.,
89
 * HDF5, DAP2, DAP4, PnetCDF, NCZarr). It is called automatically on
90
 * first use of the library, so most users do not need to call it
91
 * directly.
92
 *
93
 * It is safe to call this function more than once; subsequent calls
94
 * are no-ops.
95
 *
96
 * @return ::NC_NOERR No error.
97
 * @return ::NC_EXXX An error occurred during initialization.
98
 * @see nc_finalize
99
 */
100
101
int
102
nc_initialize()
103
1
{
104
1
    int stat = NC_NOERR;
105
106
1
    if(NC_initialized) return NC_NOERR;
107
1
    NC_initialized = 1;
108
1
    NC_finalized = 0;
109
110
    /* Do general initialization */
111
1
    if((stat = NCDISPATCH_initialize())) goto done;
112
113
    /* Initialize each active protocol */
114
1
    if((stat = NC3_initialize())) goto done;
115
#ifdef NETCDF_ENABLE_DAP
116
    if((stat = NCD2_initialize())) goto done;
117
#endif
118
#ifdef NETCDF_ENABLE_DAP4
119
    if((stat = NCD4_initialize())) goto done;
120
#endif
121
#ifdef USE_PNETCDF
122
    if((stat = NCP_initialize())) goto done;
123
#endif
124
1
#ifdef USE_NETCDF4
125
1
    if((stat = NC4_initialize())) goto done;
126
1
#endif /* USE_NETCDF4 */
127
#ifdef USE_HDF5
128
    if((stat = NC_HDF5_initialize())) goto done;
129
#endif
130
#ifdef USE_HDF4
131
    if((stat = NC_HDF4_initialize())) goto done;
132
#endif
133
#ifdef NETCDF_ENABLE_S3
134
    if((stat = NC_s3sdkinitialize())) goto done;
135
#endif
136
1
#ifdef NETCDF_ENABLE_NCZARR
137
1
    if((stat = NCZ_initialize())) goto done;
138
1
#endif
139
140
1
#ifdef NETCDF_ENABLE_ATEXIT_FINALIZE
141
    /* Use atexit() to invoke nc_finalize */
142
1
    if(atexit(finalize_atexit))
143
0
  fprintf(stderr,"atexit failed\n");
144
1
#endif
145
146
1
done:
147
1
    return stat;
148
1
}
149
150
/**
151
 * Finalize the netCDF library.
152
 *
153
 * This function releases all internal resources and finalizes any
154
 * external libraries initialized by nc_initialize(). After calling
155
 * this function, the library may be re-initialized by calling
156
 * nc_initialize() again.
157
 *
158
 * If ::NETCDF_ENABLE_ATEXIT_FINALIZE is set, this function is
159
 * registered with atexit() and called automatically at program exit.
160
 *
161
 * It is safe to call this function more than once; subsequent calls
162
 * are no-ops.
163
 *
164
 * @return ::NC_NOERR No error.
165
 * @return ::NC_EXXX An error occurred during finalization.
166
 * @see nc_initialize
167
 */
168
169
int
170
nc_finalize(void)
171
1
{
172
1
    int stat = NC_NOERR;
173
1
    int failed = stat;
174
175
1
    if(NC_finalized) goto done;
176
1
    NC_initialized = 0;
177
1
    NC_finalized = 1;
178
179
    /* Finalize each active protocol */
180
181
#ifdef NETCDF_ENABLE_DAP2
182
    if((stat = NCD2_finalize())) failed = stat;
183
#endif
184
#ifdef NETCDF_ENABLE_DAP4
185
    if((stat = NCD4_finalize())) failed = stat;
186
#endif
187
188
#ifdef USE_PNETCDF
189
    if((stat = NCP_finalize())) failed = stat;
190
#endif
191
192
#ifdef USE_HDF4
193
    if((stat = NC_HDF4_finalize())) failed = stat;
194
#endif /* USE_HDF4 */
195
196
1
#ifdef USE_NETCDF4
197
1
    if((stat = NC4_finalize())) failed = stat;
198
1
#endif /* USE_NETCDF4 */
199
200
#ifdef USE_HDF5
201
    if((stat = NC_HDF5_finalize())) failed = stat;
202
#endif
203
204
1
#ifdef NETCDF_ENABLE_NCZARR
205
1
    if((stat = NCZ_finalize())) failed = stat;
206
1
#endif
207
208
#ifdef NETCDF_ENABLE_S3
209
    if((stat = NC_s3sdkfinalize())) failed = stat;
210
#endif
211
212
1
    if((stat = NC3_finalize())) failed = stat;
213
214
    /* Do general finalization */
215
1
    if((stat = NCDISPATCH_finalize())) failed = stat;
216
217
1
done:
218
1
    if(failed) fprintf(stderr,"nc_finalize failed: %d\n",failed);
219
1
    return failed;
220
1
}