/src/netcdf-c/libdispatch/dglobal.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata |
3 | | See LICENSE.txt for license information. |
4 | | */ |
5 | | |
6 | | #include "config.h" |
7 | | #include <stdlib.h> |
8 | | #include <stdio.h> |
9 | | #include <string.h> |
10 | | |
11 | | #include "netcdf.h" |
12 | | #include "ncglobal.h" |
13 | | #include "nclist.h" |
14 | | #include "ncuri.h" |
15 | | #include "ncrc.h" |
16 | | #include "ncs3sdk.h" |
17 | | |
18 | | /**************************************************/ |
19 | | /* Global State constants and state */ |
20 | | |
21 | | /* The singleton global state object */ |
22 | | static NCglobalstate* nc_globalstate = NULL; |
23 | | |
24 | | /* Forward */ |
25 | | static int NC_createglobalstate(void); |
26 | | |
27 | | /** \defgroup global_state Global state functions. */ |
28 | | /** \{ |
29 | | |
30 | | \ingroup global_state |
31 | | */ |
32 | | |
33 | | /* NCglobal state management */ |
34 | | |
35 | | static int |
36 | | NC_createglobalstate(void) |
37 | 1 | { |
38 | 1 | int stat = NC_NOERR; |
39 | 1 | const char* tmp = NULL; |
40 | | |
41 | 1 | if(nc_globalstate == NULL) { |
42 | 1 | nc_globalstate = calloc(1,sizeof(NCglobalstate)); |
43 | 1 | if(nc_globalstate == NULL) {stat = NC_ENOMEM; goto done;} |
44 | | /* Initialize struct pointers */ |
45 | 1 | if((nc_globalstate->rcinfo = calloc(1,sizeof(struct NCRCinfo)))==NULL) |
46 | 0 | {stat = NC_ENOMEM; goto done;} |
47 | 1 | if((nc_globalstate->rcinfo->entries = nclistnew())==NULL) |
48 | 0 | {stat = NC_ENOMEM; goto done;} |
49 | 1 | if((nc_globalstate->rcinfo->s3profiles = nclistnew())==NULL) |
50 | 0 | {stat = NC_ENOMEM; goto done;} |
51 | 1 | memset(&nc_globalstate->chunkcache,0,sizeof(struct ChunkCache)); |
52 | 1 | } |
53 | | |
54 | | /* Get environment variables */ |
55 | 1 | if(getenv(NCRCENVIGNORE) != NULL) |
56 | 0 | nc_globalstate->rcinfo->ignore = 1; |
57 | 1 | tmp = getenv(NCRCENVRC); |
58 | 1 | if(tmp != NULL && strlen(tmp) > 0) |
59 | 0 | nc_globalstate->rcinfo->rcfile = strdup(tmp); |
60 | | /* Initialize chunk cache defaults */ |
61 | 1 | nc_globalstate->chunkcache.size = DEFAULT_CHUNK_CACHE_SIZE; /**< Default chunk cache size. */ |
62 | 1 | nc_globalstate->chunkcache.nelems = DEFAULT_CHUNKS_IN_CACHE; /**< Default chunk cache number of elements. */ |
63 | 1 | nc_globalstate->chunkcache.preemption = DEFAULT_CHUNK_CACHE_PREEMPTION; /**< Default chunk cache preemption. */ |
64 | | |
65 | 1 | done: |
66 | 1 | return stat; |
67 | 1 | } |
68 | | |
69 | | /* Get global state */ |
70 | | NCglobalstate* |
71 | | NC_getglobalstate(void) |
72 | 11 | { |
73 | 11 | if(nc_globalstate == NULL) |
74 | 1 | NC_createglobalstate(); |
75 | 11 | return nc_globalstate; |
76 | 11 | } |
77 | | |
78 | | void |
79 | | NC_freeglobalstate(void) |
80 | 1 | { |
81 | 1 | NCglobalstate* gs = nc_globalstate; |
82 | 1 | if(gs != NULL) { |
83 | 1 | nullfree(gs->tempdir); |
84 | 1 | nullfree(gs->home); |
85 | 1 | nullfree(gs->cwd); |
86 | 1 | memset(&gs->chunkcache,0,sizeof(struct ChunkCache)); |
87 | 1 | NC_clearawsparams(&gs->aws); |
88 | 1 | if(gs->rcinfo) { |
89 | 1 | NC_rcclear(gs->rcinfo); |
90 | 1 | free(gs->rcinfo); |
91 | 1 | } |
92 | 1 | nclistfree(gs->pluginpaths); |
93 | 1 | free(gs); |
94 | 1 | nc_globalstate = NULL; |
95 | 1 | } |
96 | 1 | } |
97 | | |
98 | | /** \} */ |
99 | | |
100 | | void |
101 | | NC_clearawsparams(struct GlobalAWS* aws) |
102 | 1 | { |
103 | 1 | nullfree(aws->default_region); |
104 | 1 | nullfree(aws->config_file); |
105 | 1 | nullfree(aws->profile); |
106 | 1 | nullfree(aws->access_key_id); |
107 | | nullfree(aws->secret_access_key); |
108 | 1 | memset(aws,0,sizeof(struct GlobalAWS)); |
109 | 1 | } |
110 | | |