forked from Unidata/netcdf-c
/
nc4internal.c
1591 lines (1335 loc) · 41.7 KB
/
nc4internal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file \internal
Internal netcdf-4 functions.
This file contains functions internal to the netcdf4 library. None of
the functions in this file are exposed in the exetnal API. These
functions all relate to the manipulation of netcdf-4's in-memory
buffer of metadata information, i.e. the linked list of NC
structs.
Copyright 2003-2011, University Corporation for Atmospheric
Research. See the COPYRIGHT file for copying and redistribution
conditions.
*/
#include "config.h"
#include "nc4internal.h"
#include "nc.h" /* from libsrc */
#include "ncdispatch.h" /* from libdispatch */
#include "H5DSpublic.h"
#include <utf8proc.h>
#define MEGABYTE 1048576
#undef DEBUGH5
#ifdef DEBUGH5
/* Provide a catchable error reporting function */
static herr_t
h5catch(void* ignored)
{
H5Eprint(NULL);
return 0;
}
#endif
/* These are the default chunk cache sizes for HDF5 files created or
* opened with netCDF-4. */
extern size_t nc4_chunk_cache_size;
extern size_t nc4_chunk_cache_nelems;
extern float nc4_chunk_cache_preemption;
/* This is to track opened HDF5 objects to make sure they are
* closed. */
#ifdef EXTRA_TESTS
extern int num_spaces;
#endif /* EXTRA_TESTS */
#ifdef LOGGING
/* This is the severity level of messages which will be logged. Use
severity 0 for errors, 1 for important log messages, 2 for less
important, etc. */
int nc_log_level = -1;
#endif /* LOGGING */
int nc4_hdf5_initialized = 0;
/* Provide a wrapper for H5Eset_auto */
static herr_t
set_auto(void* func, void *client_data)
{
#ifdef DEBUGH5
return H5Eset_auto2(H5E_DEFAULT,(H5E_auto2_t)h5catch,client_data);
#else
return H5Eset_auto2(H5E_DEFAULT,(H5E_auto2_t)func,client_data);
#endif
}
/*
Provide a function to do any necessary initialization
of the HDF5 library.
*/
void
nc4_hdf5_initialize(void)
{
if (set_auto(NULL, NULL) < 0)
LOG((0, "Couldn't turn off HDF5 error messages!"));
LOG((1, "HDF5 error messages have been turned off."));
nc4_hdf5_initialized = 1;
}
/* Check and normalize and name. */
int
nc4_check_name(const char *name, char *norm_name)
{
char *temp;
int retval;
/* Check the length. */
if (strlen(name) > NC_MAX_NAME)
return NC_EMAXNAME;
/* Make sure this is a valid netcdf name. This should be done
* before the name is normalized, because it gives better error
* codes for bad utf8 strings. */
if ((retval = NC_check_name(name)))
return retval;
/* Normalize the name. */
if (!(temp = (char *)utf8proc_NFC((const unsigned char *)name)))
return NC_EINVAL;
strcpy(norm_name, temp);
free(temp);
return NC_NOERR;
}
/* Given a varid, return the maximum length of a dimension using dimid */
static int
find_var_dim_max_length(NC_GRP_INFO_T *grp, int varid, int dimid, size_t *maxlen)
{
hid_t datasetid = 0, spaceid = 0;
NC_VAR_INFO_T *var;
hsize_t *h5dimlen = NULL, *h5dimlenmax = NULL;
int d, dataset_ndims = 0;
int retval = NC_NOERR;
*maxlen = 0;
/* Find this var. */
for (var = grp->var; var; var = var->l.next)
if (var->varid == varid)
break;
if (!var)
return NC_ENOTVAR;
/* If the var hasn't been created yet, its size is 0. */
if (!var->created)
{
*maxlen = 0;
}
else
{
/* Get the number of records in the dataset. */
if ((retval = nc4_open_var_grp2(grp, var->varid, &datasetid)))
BAIL(retval);
if ((spaceid = H5Dget_space(datasetid)) < 0)
BAIL(NC_EHDFERR);
#ifdef EXTRA_TESTS
num_spaces++;
#endif
/* If it's a scalar dataset, it has length one. */
if (H5Sget_simple_extent_type(spaceid) == H5S_SCALAR)
{
*maxlen = (var->dimids && var->dimids[0] == dimid) ? 1 : 0;
}
else
{
/* Check to make sure ndims is right, then get the len of each
dim in the space. */
if ((dataset_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
BAIL(NC_EHDFERR);
if (dataset_ndims != var->ndims)
BAIL(NC_EHDFERR);
if (!(h5dimlen = malloc(dataset_ndims * sizeof(hsize_t))))
BAIL(NC_ENOMEM);
if (!(h5dimlenmax = malloc(dataset_ndims * sizeof(hsize_t))))
BAIL(NC_ENOMEM);
if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid,
h5dimlen, h5dimlenmax)) < 0)
BAIL(NC_EHDFERR);
LOG((5, "find_var_dim_max_length: varid %d len %d max: %d",
varid, (int)h5dimlen[0], (int)h5dimlenmax[0]));
for (d=0; d<dataset_ndims; d++) {
if (var->dimids[d] == dimid) {
*maxlen = *maxlen > h5dimlen[d] ? *maxlen : h5dimlen[d];
}
}
}
}
exit:
if (spaceid > 0 && H5Sclose(spaceid) < 0)
BAIL2(NC_EHDFERR);
#ifdef EXTRA_TESTS
num_spaces--;
#endif
if (h5dimlen) free(h5dimlen);
if (h5dimlenmax) free(h5dimlenmax);
return retval;
}
/* Given an NC pointer, add the necessary stuff for a
* netcdf-4 file. */
int
nc4_nc4f_list_add(NC *nc, const char *path, int mode)
{
NC_HDF5_FILE_INFO_T *h5;
assert(nc && !NC4_DATA(nc) && path);
/* We need to malloc and
initialize the substructure NC_HDF_FILE_INFO_T. */
if (!(h5 = calloc(1, sizeof(NC_HDF5_FILE_INFO_T))))
return NC_ENOMEM;
NC4_DATA_SET(nc,h5);
h5->controller = nc;
/* Hang on to cmode, and note that we're in define mode. */
h5->cmode = mode | NC_INDEF;
/* The next_typeid needs to be set beyond the end of our atomic
* types. */
h5->next_typeid = NC_FIRSTUSERTYPEID;
/* There's always at least one open group - the root
* group. Allocate space for one group's worth of information. Set
* its hdf id, name, and a pointer to it's file structure. */
return nc4_grp_list_add(&(h5->root_grp), h5->next_nc_grpid++,
NULL, nc, NC_GROUP_NAME, NULL);
}
/* Given an ncid, find the relevant group and return a pointer to it,
* return an error of this is not a netcdf-4 file (or if strict nc3 is
* turned on for this file.) */
int
nc4_find_nc4_grp(int ncid, NC_GRP_INFO_T **grp)
{
NC_HDF5_FILE_INFO_T* h5;
NC *f = nc4_find_nc_file(ncid,&h5);
if(f == NULL) return NC_EBADID;
/* No netcdf-3 files allowed! */
if (!h5) return NC_ENOTNC4;
assert(h5->root_grp);
/* This function demands netcdf-4 files without strict nc3
* rules.*/
if (h5->cmode & NC_CLASSIC_MODEL) return NC_ESTRICTNC3;
/* If we can't find it, the grp id part of ncid is bad. */
if (!(*grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK))))
return NC_EBADID;
return NC_NOERR;
}
/* Given an ncid, find the relevant group and return a pointer to it,
* also set a pointer to the nc4_info struct of the related file. For
* netcdf-3 files, *h5 will be set to NULL. */
int
nc4_find_grp_h5(int ncid, NC_GRP_INFO_T **grpp, NC_HDF5_FILE_INFO_T **h5p)
{
NC_HDF5_FILE_INFO_T *h5;
NC_GRP_INFO_T *grp;
NC *f = nc4_find_nc_file(ncid,&h5);
if(f == NULL) return NC_EBADID;
if (h5) {
assert(h5->root_grp);
/* If we can't find it, the grp id part of ncid is bad. */
if (!(grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK))))
return NC_EBADID;
h5 = (grp)->nc4_info;
assert(h5);
} else {
h5 = NULL;
grp = NULL;
}
if(h5p) *h5p = h5;
if(grpp) *grpp = grp;
return NC_NOERR;
}
int
nc4_find_nc_grp_h5(int ncid, NC **nc, NC_GRP_INFO_T **grpp,
NC_HDF5_FILE_INFO_T **h5p)
{
NC_GRP_INFO_T *grp;
NC_HDF5_FILE_INFO_T* h5;
NC *f = nc4_find_nc_file(ncid,&h5);
if(f == NULL) return NC_EBADID;
*nc = f;
if (h5) {
assert(h5->root_grp);
/* If we can't find it, the grp id part of ncid is bad. */
if (!(grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK))))
return NC_EBADID;
h5 = (grp)->nc4_info;
assert(h5);
} else {
h5 = NULL;
grp = NULL;
}
if(h5p) *h5p = h5;
if(grpp) *grpp = grp;
return NC_NOERR;
}
/* Recursively hunt for a group id. */
NC_GRP_INFO_T *
nc4_rec_find_grp(NC_GRP_INFO_T *start_grp, int target_nc_grpid)
{
NC_GRP_INFO_T *g, *res;
assert(start_grp);
/* Is this the group we are searching for? */
if (start_grp->nc_grpid == target_nc_grpid)
return start_grp;
/* Shake down the kids. */
if (start_grp->children)
for (g = start_grp->children; g; g = g->l.next)
if ((res = nc4_rec_find_grp(g, target_nc_grpid)))
return res;
/* Can't find it. Fate, why do you mock me? */
return NULL;
}
/* Given an ncid and varid, get pointers to the group and var
* metadata. */
int
nc4_find_g_var_nc(NC *nc, int ncid, int varid,
NC_GRP_INFO_T **grp, NC_VAR_INFO_T **var)
{
NC_HDF5_FILE_INFO_T* h5 = NC4_DATA(nc);
/* Find the group info. */
assert(grp && var && h5 && h5->root_grp);
*grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK));
/* It is possible for *grp to be NULL. If it is,
return an error. */
if(*grp == NULL)
return NC_ENOTVAR;
/* Find the var info. */
for ((*var) = (*grp)->var; (*var); (*var) = (*var)->l.next)
if ((*var)->varid == varid)
break;
if (!(*var))
return NC_ENOTVAR;
return NC_NOERR;
}
/* Find a dim in a grp (or parents). */
int
nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim,
NC_GRP_INFO_T **dim_grp)
{
NC_GRP_INFO_T *g, *dg = NULL;
int finished = 0;
assert(grp && dim);
/* Find the dim info. */
for (g = grp; g && !finished; g = g->parent)
for ((*dim) = g->dim; (*dim); (*dim) = (*dim)->l.next)
if ((*dim)->dimid == dimid)
{
dg = g;
finished++;
break;
}
/* If we didn't find it, return an error. */
if (!(*dim))
return NC_EBADDIM;
/* Give the caller the group the dimension is in. */
if (dim_grp)
*dim_grp = dg;
return NC_NOERR;
}
/* Find a var (by name) in a grp. */
int
nc4_find_var(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var)
{
assert(grp && var && name);
/* Find the var info. */
for ((*var) = grp->var; (*var); (*var) = (*var)->l.next)
if (0 == strcmp(name, (*var)->name))
break;
return NC_NOERR;
}
/* Recursively hunt for a HDF type id. */
NC_TYPE_INFO_T *
nc4_rec_find_hdf_type(NC_GRP_INFO_T *start_grp, hid_t target_hdf_typeid)
{
NC_GRP_INFO_T *g;
NC_TYPE_INFO_T *type, *res;
htri_t equal;
assert(start_grp);
/* Does this group have the type we are searching for? */
for (type = start_grp->type; type; type = type->l.next)
{
if ((equal = H5Tequal(type->native_hdf_typeid ? type->native_hdf_typeid : type->hdf_typeid, target_hdf_typeid)) < 0)
return NULL;
if (equal)
return type;
}
/* Shake down the kids. */
if (start_grp->children)
for (g = start_grp->children; g; g = g->l.next)
if ((res = nc4_rec_find_hdf_type(g, target_hdf_typeid)))
return res;
/* Can't find it. Fate, why do you mock me? */
return NULL;
}
/* Recursively hunt for a netCDF type by name. */
NC_TYPE_INFO_T *
nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name)
{
NC_GRP_INFO_T *g;
NC_TYPE_INFO_T *type, *res;
assert(start_grp);
/* Does this group have the type we are searching for? */
for (type = start_grp->type; type; type = type->l.next)
if (!strcmp(type->name, name))
return type;
/* Search subgroups. */
if (start_grp->children)
for (g = start_grp->children; g; g = g->l.next)
if ((res = nc4_rec_find_named_type(g, name)))
return res;
/* Can't find it. Oh, woe is me! */
return NULL;
}
/* Recursively hunt for a netCDF type id. */
NC_TYPE_INFO_T *
nc4_rec_find_nc_type(const NC_GRP_INFO_T *start_grp, nc_type target_nc_typeid)
{
NC_TYPE_INFO_T *type;
assert(start_grp);
/* Does this group have the type we are searching for? */
for (type = start_grp->type; type; type = type->l.next)
if (type->nc_typeid == target_nc_typeid)
return type;
/* Shake down the kids. */
if (start_grp->children)
{
NC_GRP_INFO_T *g;
for (g = start_grp->children; g; g = g->l.next)
{
NC_TYPE_INFO_T *res;
if ((res = nc4_rec_find_nc_type(g, target_nc_typeid)))
return res;
}
}
/* Can't find it. Fate, why do you mock me? */
return NULL;
}
/* Use a netCDF typeid to find a type in a type_list. */
int
nc4_find_type(const NC_HDF5_FILE_INFO_T *h5, nc_type typeid, NC_TYPE_INFO_T **type)
{
if (typeid < 0 || !type)
return NC_EINVAL;
*type = NULL;
/* Atomic types don't have associated NC_TYPE_INFO_T struct, just
* return NOERR. */
if (typeid <= NC_STRING)
return NC_NOERR;
/* Find the type. */
if(!(*type = nc4_rec_find_nc_type(h5->root_grp, typeid)))
return NC_EBADTYPID;
return NC_NOERR;
}
/* Find the actual length of a dim by checking the length of that dim
* in all variables that use it, in grp or children. **len must be
* initialized to zero before this function is called. */
int
nc4_find_dim_len(NC_GRP_INFO_T *grp, int dimid, size_t **len)
{
NC_GRP_INFO_T *g;
NC_VAR_INFO_T *var;
int retval;
assert(grp && len);
LOG((3, "nc4_find_dim_len: grp->name %s dimid %d", grp->name, dimid));
/* If there are any groups, call this function recursively on
* them. */
for (g = grp->children; g; g = g->l.next)
if ((retval = nc4_find_dim_len(g, dimid, len)))
return retval;
/* For all variables in this group, find the ones that use this
* dimension, and remember the max length. */
for (var = grp->var; var; var = var->l.next)
{
size_t mylen;
/* Find max length of dim in this variable... */
if ((retval = find_var_dim_max_length(grp, var->varid, dimid, &mylen)))
return retval;
**len = **len > mylen ? **len : mylen;
}
return NC_NOERR;
}
/* Given a group, find an att. */
int
nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum,
NC_ATT_INFO_T **att)
{
NC_VAR_INFO_T *var;
NC_ATT_INFO_T *attlist = NULL;
assert(grp && grp->name);
LOG((4, "nc4_find_grp_att: grp->name %s varid %d name %s attnum %d",
grp->name, varid, name, attnum));
/* Get either the global or a variable attribute list. */
if (varid == NC_GLOBAL)
attlist = grp->att;
else
{
for(var = grp->var; var; var = var->l.next)
{
if (var->varid == varid)
{
attlist = var->att;
break;
}
}
if (!var)
return NC_ENOTVAR;
}
/* Now find the attribute by name or number. If a name is provided,
* ignore the attnum. */
if(attlist)
for (*att = attlist; *att; *att = (*att)->l.next) {
if (name && (*att)->name && !strcmp((*att)->name, name))
return NC_NOERR;
if (!name && (*att)->attnum == attnum)
return NC_NOERR;
}
/* If we get here, we couldn't find the attribute. */
return NC_ENOTATT;
}
/* Given an ncid, varid, and name or attnum, find and return pointer
to NC_ATT_INFO_T metadata. */
int
nc4_find_nc_att(int ncid, int varid, const char *name, int attnum,
NC_ATT_INFO_T **att)
{
NC_GRP_INFO_T *grp;
NC_HDF5_FILE_INFO_T *h5;
NC_VAR_INFO_T *var;
NC_ATT_INFO_T *attlist = NULL;
int retval;
LOG((4, "nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
ncid, varid, name, attnum));
/* Find info for this file and group, and set pointer to each. */
if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
return retval;
assert(grp && h5);
/* Get either the global or a variable attribute list. */
if (varid == NC_GLOBAL)
attlist = grp->att;
else
{
for(var = grp->var; var; var = var->l.next)
{
if (var->varid == varid)
{
attlist = var->att;
break;
}
}
if (!var)
return NC_ENOTVAR;
}
/* Now find the attribute by name or number. If a name is provided, ignore the attnum. */
for (*att = attlist; *att; *att = (*att)->l.next)
if ((name && !strcmp((*att)->name, name)) ||
(!name && (*att)->attnum == attnum))
return NC_NOERR;
/* If we get here, we couldn't find the attribute. */
return NC_ENOTATT;
}
/* Given an id, walk the list and find the appropriate
NC. */
NC*
nc4_find_nc_file(int ext_ncid, NC_HDF5_FILE_INFO_T** h5p)
{
NC* nc;
int stat;
stat = NC_check_id(ext_ncid,&nc);
if(stat != NC_NOERR)
nc = NULL;
if(nc)
if(h5p) *h5p = (NC_HDF5_FILE_INFO_T*)nc->dispatchdata;
return nc;
}
/* Add object to the end of a list. */
static void
obj_list_add(NC_LIST_NODE_T **list, NC_LIST_NODE_T *obj)
{
/* Go to the end of the list and set the last one to point at object,
* or, if the list is empty, our new object becomes the list. */
if(*list)
{
NC_LIST_NODE_T *o;
for (o = *list; o; o = o->next)
if (!o->next)
break;
o->next = obj;
obj->prev = o;
}
else
*list = obj;
}
/* Remove object from a list. */
static void
obj_list_del(NC_LIST_NODE_T **list, NC_LIST_NODE_T *obj)
{
/* Remove the var from the linked list. */
if(*list == obj)
*list = obj->next;
else
((NC_LIST_NODE_T *)obj->prev)->next = obj->next;
if(obj->next)
((NC_LIST_NODE_T *)obj->next)->prev = obj->prev;
}
/* Add to the end of a var list. Return a pointer to the newly
* added var. */
int
nc4_var_list_add(NC_VAR_INFO_T **list, NC_VAR_INFO_T **var)
{
NC_VAR_INFO_T *new_var;
/* Allocate storage for new variable. */
if (!(new_var = calloc(1, sizeof(NC_VAR_INFO_T))))
return NC_ENOMEM;
/* These are the HDF5-1.8.4 defaults. */
new_var->chunk_cache_size = nc4_chunk_cache_size;
new_var->chunk_cache_nelems = nc4_chunk_cache_nelems;
new_var->chunk_cache_preemption = nc4_chunk_cache_preemption;
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)new_var);
/* Set the var pointer, if one was given */
if (var)
*var = new_var;
return NC_NOERR;
}
/* Add to the beginning of a dim list. */
int
nc4_dim_list_add(NC_DIM_INFO_T **list, NC_DIM_INFO_T **dim)
{
NC_DIM_INFO_T *new_dim;
if (!(new_dim = calloc(1, sizeof(NC_DIM_INFO_T))))
return NC_ENOMEM;
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)new_dim);
/* Set the dim pointer, if one was given */
if (dim)
*dim = new_dim;
return NC_NOERR;
}
/* Add to the end of an att list. */
int
nc4_att_list_add(NC_ATT_INFO_T **list, NC_ATT_INFO_T **att)
{
NC_ATT_INFO_T *new_att;
if (!(new_att = calloc(1, sizeof(NC_ATT_INFO_T))))
return NC_ENOMEM;
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)new_att);
/* Set the attribute pointer, if one was given */
if (att)
*att = new_att;
return NC_NOERR;
}
/* Add to the end of a group list. Can't use 0 as a new_nc_grpid -
* it's reserverd for the root group. */
int
nc4_grp_list_add(NC_GRP_INFO_T **list, int new_nc_grpid,
NC_GRP_INFO_T *parent_grp, NC *nc,
char *name, NC_GRP_INFO_T **grp)
{
NC_GRP_INFO_T *new_grp;
LOG((3, "%s: new_nc_grpid %d name %s ", __func__, new_nc_grpid, name));
/* Get the memory to store this groups info. */
if (!(new_grp = calloc(1, sizeof(NC_GRP_INFO_T))))
return NC_ENOMEM;
/* Fill in this group's information. */
new_grp->nc_grpid = new_nc_grpid;
new_grp->parent = parent_grp;
if (!(new_grp->name = strdup(name)))
{
free(new_grp);
return NC_ENOMEM;
}
new_grp->nc4_info = NC4_DATA(nc);
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)new_grp);
/* Set the group pointer, if one was given */
if (grp)
*grp = new_grp;
return NC_NOERR;
}
/* Names for groups, variables, and types must not be the same. This
* function checks that a proposed name is not already in
* use. Normalzation of UTF8 strings should happen before this
* function is called. */
int
nc4_check_dup_name(NC_GRP_INFO_T *grp, char *name)
{
NC_TYPE_INFO_T *type;
NC_GRP_INFO_T *g;
NC_VAR_INFO_T *var;
uint32_t hash;
/* Any types of this name? */
for (type = grp->type; type; type = type->l.next)
if (!strcmp(type->name, name))
return NC_ENAMEINUSE;
/* Any child groups of this name? */
for (g = grp->children; g; g = g->l.next)
if (!strcmp(g->name, name))
return NC_ENAMEINUSE;
/* Any variables of this name? */
hash = hash_fast(name, strlen(name));
for (var = grp->var; var; var = var->l.next)
if (var->hash == hash && !strcmp(var->name, name))
return NC_ENAMEINUSE;
return NC_NOERR;
}
/* Add to the end of a type list. */
int
nc4_type_list_add(NC_GRP_INFO_T *grp, size_t size, const char *name,
NC_TYPE_INFO_T **type)
{
NC_TYPE_INFO_T *new_type;
/* Allocate memory for the type */
if (!(new_type = calloc(1, sizeof(NC_TYPE_INFO_T))))
return NC_ENOMEM;
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)(&grp->type), (NC_LIST_NODE_T *)new_type);
/* Remember info about this type. */
new_type->nc_typeid = grp->nc4_info->next_typeid++;
new_type->size = size;
if (!(new_type->name = strdup(name)))
return NC_ENOMEM;
/* Increment the ref. count on the type */
new_type->rc++;
/* Return a pointer to the new type, if requested */
if (type)
*type = new_type;
return NC_NOERR;
}
/* Add to the end of a compound field list. */
int
nc4_field_list_add(NC_FIELD_INFO_T **list, int fieldid, const char *name,
size_t offset, hid_t field_hdf_typeid, hid_t native_typeid,
nc_type xtype, int ndims, const int *dim_sizesp)
{
NC_FIELD_INFO_T *field;
/* Name has already been checked and UTF8 normalized. */
if (!name)
return NC_EINVAL;
/* Allocate storage for this field information. */
if (!(field = calloc(1, sizeof(NC_FIELD_INFO_T))))
return NC_ENOMEM;
/* Store the information about this field. */
field->fieldid = fieldid;
if (!(field->name = strdup(name)))
{
free(field);
return NC_ENOMEM;
}
field->hdf_typeid = field_hdf_typeid;
field->native_hdf_typeid = native_typeid;
field->nc_typeid = xtype;
field->offset = offset;
field->ndims = ndims;
if (ndims)
{
int i;
if (!(field->dim_size = malloc(ndims * sizeof(int))))
{
free(field->name);
free(field);
return NC_ENOMEM;
}
for (i = 0; i < ndims; i++)
field->dim_size[i] = dim_sizesp[i];
}
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)field);
return NC_NOERR;
}
/* Add a member to an enum type. */
int
nc4_enum_member_add(NC_ENUM_MEMBER_INFO_T **list, size_t size,
const char *name, const void *value)
{
NC_ENUM_MEMBER_INFO_T *member;
/* Name has already been checked. */
assert(name && size > 0 && value);
LOG((4, "%s: size %d name %s", __func__, size, name));
/* Allocate storage for this field information. */
if (!(member = calloc(1, sizeof(NC_ENUM_MEMBER_INFO_T))))
return NC_ENOMEM;
if (!(member->value = malloc(size))) {
free(member);
return NC_ENOMEM;
}
if (!(member->name = strdup(name))) {
free(member->value);
free(member);
return NC_ENOMEM;
}
/* Store the value for this member. */
memcpy(member->value, value, size);
/* Add object to list */
obj_list_add((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)member);
return NC_NOERR;
}
/* Delete a field from a field list, and nc_free the memory. */
static void
field_list_del(NC_FIELD_INFO_T **list, NC_FIELD_INFO_T *field)
{
/* Take this field out of the list. */
obj_list_del((NC_LIST_NODE_T **)list, (NC_LIST_NODE_T *)field);
/* Free some stuff. */
if (field->name)
free(field->name);
if (field->dim_size)
free(field->dim_size);
/* Nc_Free the memory. */
free(field);
}
/* Free allocated space for type information. */
int
nc4_type_free(NC_TYPE_INFO_T *type)
{
/* Decrement the ref. count on the type */
assert(type->rc);
type->rc--;
/* Release the type, if the ref. count drops to zero */
if (0 == type->rc)
{
/* Close any open user-defined HDF5 typeids. */
if (type->hdf_typeid && H5Tclose(type->hdf_typeid) < 0)
return NC_EHDFERR;
if (type->native_hdf_typeid && H5Tclose(type->native_hdf_typeid) < 0)
return NC_EHDFERR;
/* Free the name. */
if (type->name)
free(type->name);
/* Class-specific cleanup */
switch (type->nc_type_class)
{
case NC_COMPOUND:
{
NC_FIELD_INFO_T *field;
/* Delete all the fields in this type (there will be some if its a
* compound). */
field = type->u.c.field;
while (field)
{
NC_FIELD_INFO_T *f = field->l.next;
field_list_del(&type->u.c.field, field);
field = f;
}
}
break;
case NC_ENUM:
{
NC_ENUM_MEMBER_INFO_T *enum_member;
/* Delete all the enum_members, if any. */
enum_member = type->u.e.enum_member;
while (enum_member)
{
NC_ENUM_MEMBER_INFO_T *em = enum_member->l.next;
free(enum_member->value);
free(enum_member->name);
free(enum_member);
enum_member = em;
}
if (H5Tclose(type->u.e.base_hdf_typeid) < 0)
return NC_EHDFERR;
}
break;
case NC_VLEN:
if (H5Tclose(type->u.v.base_hdf_typeid) < 0)
return NC_EHDFERR;
default:
break;
}
/* Release the memory. */
free(type);
}