2018-05-25 04:43:00 +08:00
|
|
|
/* Copyright 2005-2018, University Corporation for Atmospheric
|
|
|
|
* Research. See the COPYRIGHT file for copying and redistribution
|
|
|
|
* conditions. */
|
2018-05-25 04:27:16 +08:00
|
|
|
/**
|
|
|
|
* @file @internal This file is part of netcdf-4, a netCDF-like
|
|
|
|
* interface for HDF5, or a HDF5 backend for netCDF, depending on your
|
|
|
|
* point of view.
|
|
|
|
*
|
|
|
|
* This file handles the nc4 user-defined type functions
|
|
|
|
* (i.e. compound and opaque types).
|
|
|
|
*
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-07-16 18:50:15 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
2018-07-12 21:05:21 +08:00
|
|
|
#include "hdf5internal.h"
|
2023-11-27 19:36:03 +08:00
|
|
|
#include <stddef.h>
|
2018-05-25 04:27:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Determine if two types are equal.
|
|
|
|
*
|
|
|
|
* @param ncid1 First file/group ID.
|
|
|
|
* @param typeid1 First type ID.
|
|
|
|
* @param ncid2 Second file/group ID.
|
|
|
|
* @param typeid2 Second type ID.
|
|
|
|
* @param equalp Pointer that will get 1 if the two types are equal.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_EBADTYPE Type not found.
|
|
|
|
* @return ::NC_EINVAL Invalid type.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
extern int
|
|
|
|
NC4_inq_type_equal(int ncid1, nc_type typeid1, int ncid2,
|
|
|
|
nc_type typeid2, int *equalp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
NC_GRP_INFO_T *grpone, *grptwo;
|
|
|
|
NC_TYPE_INFO_T *type1, *type2;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
LOG((2, "nc_inq_type_equal: ncid1 0x%x typeid1 %d ncid2 0x%x typeid2 %d",
|
|
|
|
ncid1, typeid1, ncid2, typeid2));
|
|
|
|
|
|
|
|
/* Check input. */
|
|
|
|
if(equalp == NULL) return NC_NOERR;
|
|
|
|
|
|
|
|
if (typeid1 <= NC_NAT || typeid2 <= NC_NAT)
|
|
|
|
return NC_EINVAL;
|
|
|
|
|
|
|
|
/* If one is atomic, and the other user-defined, the types are not
|
|
|
|
* equal. */
|
|
|
|
if ((typeid1 <= NC_STRING && typeid2 > NC_STRING) ||
|
|
|
|
(typeid2 <= NC_STRING && typeid1 > NC_STRING))
|
|
|
|
{
|
|
|
|
*equalp = 0;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If both are atomic types, the answer is easy. */
|
|
|
|
if (typeid1 <= NUM_ATOMIC_TYPES)
|
|
|
|
{
|
|
|
|
if (typeid1 == typeid2)
|
|
|
|
*equalp = 1;
|
|
|
|
else
|
|
|
|
*equalp = 0;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not atomic types - so find type1 and type2 information. */
|
|
|
|
if ((retval = nc4_find_nc4_grp(ncid1, &grpone)))
|
|
|
|
return retval;
|
2023-11-27 19:36:03 +08:00
|
|
|
if (!(type1 = nclistget(grpone->nc4_info->alltypes, (size_t)typeid1)))
|
2019-02-19 20:18:25 +08:00
|
|
|
return NC_EBADTYPE;
|
|
|
|
if ((retval = nc4_find_nc4_grp(ncid2, &grptwo)))
|
|
|
|
return retval;
|
2023-11-27 19:36:03 +08:00
|
|
|
if (!(type2 = nclistget(grptwo->nc4_info->alltypes, (size_t)typeid2)))
|
2019-02-19 20:18:25 +08:00
|
|
|
return NC_EBADTYPE;
|
|
|
|
|
|
|
|
/* Are the two types equal? */
|
|
|
|
{
|
|
|
|
hid_t hid1, hid2;
|
|
|
|
|
|
|
|
/* Get the HDF5 types from the HDF5-specific type info. */
|
|
|
|
assert(type1->format_type_info && type2->format_type_info);
|
|
|
|
hid1 = ((NC_HDF5_TYPE_INFO_T *)type1->format_type_info)->native_hdf_typeid;
|
|
|
|
hid2 = ((NC_HDF5_TYPE_INFO_T *)type2->format_type_info)->native_hdf_typeid;
|
|
|
|
|
|
|
|
/* Ask HDF5 if the types are equal. */
|
|
|
|
if ((retval = H5Tequal(hid1, hid2)) < 0)
|
|
|
|
return NC_EHDFERR;
|
|
|
|
*equalp = 1 ? retval : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal This internal function adds a new user defined type to
|
|
|
|
* the metadata of a group of an open file.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param size Size in bytes of new type.
|
|
|
|
* @param name Name of new type.
|
|
|
|
* @param base_typeid Base type ID.
|
|
|
|
* @param type_class NC_VLEN, NC_ENUM, or NC_STRING
|
|
|
|
* @param typeidp Pointer that gets new type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTNC4 User types in netCDF-4 files only.
|
|
|
|
* @return ::NC_EINVAL Bad size.
|
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ESTRICTNC3 Cannot define user types in classic model.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
add_user_type(int ncid, size_t size, const char *name, nc_type base_typeid,
|
|
|
|
nc_type type_class, nc_type *typeidp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
NC_FILE_INFO_T *h5;
|
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_TYPE_INFO_T *type;
|
|
|
|
NC_HDF5_TYPE_INFO_T *hdf5_type;
|
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* Check and normalize the name. */
|
|
|
|
if ((retval = nc4_check_name(name, norm_name)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
LOG((2, "%s: ncid 0x%x size %d name %s base_typeid %d ",
|
|
|
|
__FUNCTION__, ncid, size, norm_name, base_typeid));
|
|
|
|
|
|
|
|
/* Find group metadata. */
|
|
|
|
if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
|
|
|
|
return retval;
|
|
|
|
assert(h5 && grp);
|
|
|
|
|
|
|
|
/* User types cannot be defined with classic model flag. */
|
|
|
|
if (h5->cmode & NC_CLASSIC_MODEL)
|
|
|
|
return NC_ESTRICTNC3;
|
|
|
|
|
|
|
|
/* Turn on define mode if it is not on. */
|
|
|
|
if (!(h5->cmode & NC_INDEF))
|
|
|
|
if ((retval = NC4_redef(ncid)))
|
|
|
|
return retval;
|
|
|
|
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
/* No size is provided for vlens; use the size of nc_vlen_t */
|
|
|
|
if (type_class == NC_VLEN)
|
|
|
|
size = sizeof(nc_vlen_t);
|
|
|
|
|
|
|
|
/* No size is provided for enums, get it from the base type. */
|
|
|
|
else if(type_class == NC_ENUM)
|
2019-02-19 20:18:25 +08:00
|
|
|
{
|
|
|
|
if ((retval = nc4_get_typelen_mem(grp->nc4_info, base_typeid, &size)))
|
|
|
|
return retval;
|
|
|
|
}
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
|
|
|
|
/* Else better be defined */
|
2019-02-19 20:18:25 +08:00
|
|
|
else if (size <= 0)
|
|
|
|
return NC_EINVAL;
|
|
|
|
|
|
|
|
/* Check that this name is not in use as a var, grp, or type. */
|
|
|
|
if ((retval = nc4_check_dup_name(grp, norm_name)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Add to our list of types. */
|
|
|
|
if ((retval = nc4_type_list_add(grp, size, norm_name, &type)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Allocate storage for HDF5-specific type info. */
|
|
|
|
if (!(hdf5_type = calloc(1, sizeof(NC_HDF5_TYPE_INFO_T))))
|
|
|
|
return NC_ENOMEM;
|
|
|
|
type->format_type_info = hdf5_type;
|
|
|
|
|
|
|
|
/* Remember info about this type. */
|
|
|
|
type->nc_type_class = type_class;
|
Improve performance of the nc_reclaim_data and nc_copy_data functions.
re: Issue https://github.com/Unidata/netcdf-c/issues/2685
re: PR https://github.com/Unidata/netcdf-c/pull/2179
As noted in PR https://github.com/Unidata/netcdf-c/pull/2179,
the old code did not allow for reclaiming instances of types,
nor for properly copying them. That PR provided new functions
capable of reclaiming/copying instances of arbitrary types.
However, as noted by Issue https://github.com/Unidata/netcdf-c/issues/2685, using these
most general functions resulted in a significant performance
degradation, even for common cases.
This PR attempts to mitigate the cost of using the general
reclaim/copy functions in two ways.
First, the previous functions operating at the top level by
using ncid and typeid arguments. These functions were augmented
with equivalent versions that used the netcdf-c library internal
data structures to allow direct access to needed information.
These new functions are used internally to the library.
The second mitigation involves optimizing the internal functions
by providing early tests for common cases. This avoids
unnecessary recursive function calls.
The overall result is a significant improvement in speed by a
factor of roughly twenty -- your mileage may vary. These
optimized functions are still not as fast as the original (more
limited) functions, but they are getting close. Additional optimizations are
possible. But the cost is a significant "uglification" of the
code that I deemed a step too far, at least for now.
## Misc. Changes
1. Added a test case to check the proper reclamation/copy of complex types.
2. Found and fixed some places where nc_reclaim/copy should have been used.
3. Replaced, in the netcdf-c library, (almost all) occurrences of nc_reclaim_copy with calls to NC_reclaim/copy. This plus the optimizations is the primary speed-up mechanism.
4. In DAP4, the metadata is held in a substrate in-memory file; this required some changes so that the reclaim/copy code accessed that substrate dispatcher rather than the DAP4 dispatcher.
5. Re-factored and isolated the code that computes if a type is (transitively) variable-sized or not.
6. Clean up the reclamation code in ncgen; adding the use of nc_reclaim exposed some memory problems.
2023-05-21 07:11:25 +08:00
|
|
|
switch(type_class) {
|
|
|
|
case NC_ENUM:
|
2019-02-19 20:18:25 +08:00
|
|
|
type->u.e.base_nc_typeid = base_typeid;
|
|
|
|
type->u.e.enum_member = nclistnew();
|
Improve performance of the nc_reclaim_data and nc_copy_data functions.
re: Issue https://github.com/Unidata/netcdf-c/issues/2685
re: PR https://github.com/Unidata/netcdf-c/pull/2179
As noted in PR https://github.com/Unidata/netcdf-c/pull/2179,
the old code did not allow for reclaiming instances of types,
nor for properly copying them. That PR provided new functions
capable of reclaiming/copying instances of arbitrary types.
However, as noted by Issue https://github.com/Unidata/netcdf-c/issues/2685, using these
most general functions resulted in a significant performance
degradation, even for common cases.
This PR attempts to mitigate the cost of using the general
reclaim/copy functions in two ways.
First, the previous functions operating at the top level by
using ncid and typeid arguments. These functions were augmented
with equivalent versions that used the netcdf-c library internal
data structures to allow direct access to needed information.
These new functions are used internally to the library.
The second mitigation involves optimizing the internal functions
by providing early tests for common cases. This avoids
unnecessary recursive function calls.
The overall result is a significant improvement in speed by a
factor of roughly twenty -- your mileage may vary. These
optimized functions are still not as fast as the original (more
limited) functions, but they are getting close. Additional optimizations are
possible. But the cost is a significant "uglification" of the
code that I deemed a step too far, at least for now.
## Misc. Changes
1. Added a test case to check the proper reclamation/copy of complex types.
2. Found and fixed some places where nc_reclaim/copy should have been used.
3. Replaced, in the netcdf-c library, (almost all) occurrences of nc_reclaim_copy with calls to NC_reclaim/copy. This plus the optimizations is the primary speed-up mechanism.
4. In DAP4, the metadata is held in a substrate in-memory file; this required some changes so that the reclaim/copy code accessed that substrate dispatcher rather than the DAP4 dispatcher.
5. Re-factored and isolated the code that computes if a type is (transitively) variable-sized or not.
6. Clean up the reclamation code in ncgen; adding the use of nc_reclaim exposed some memory problems.
2023-05-21 07:11:25 +08:00
|
|
|
break;
|
|
|
|
case NC_OPAQUE:
|
|
|
|
break;
|
|
|
|
case NC_VLEN:
|
|
|
|
type->u.v.base_nc_typeid = base_typeid;
|
|
|
|
break;
|
|
|
|
case NC_COMPOUND:
|
2019-02-19 20:18:25 +08:00
|
|
|
type->u.c.field = nclistnew();
|
Improve performance of the nc_reclaim_data and nc_copy_data functions.
re: Issue https://github.com/Unidata/netcdf-c/issues/2685
re: PR https://github.com/Unidata/netcdf-c/pull/2179
As noted in PR https://github.com/Unidata/netcdf-c/pull/2179,
the old code did not allow for reclaiming instances of types,
nor for properly copying them. That PR provided new functions
capable of reclaiming/copying instances of arbitrary types.
However, as noted by Issue https://github.com/Unidata/netcdf-c/issues/2685, using these
most general functions resulted in a significant performance
degradation, even for common cases.
This PR attempts to mitigate the cost of using the general
reclaim/copy functions in two ways.
First, the previous functions operating at the top level by
using ncid and typeid arguments. These functions were augmented
with equivalent versions that used the netcdf-c library internal
data structures to allow direct access to needed information.
These new functions are used internally to the library.
The second mitigation involves optimizing the internal functions
by providing early tests for common cases. This avoids
unnecessary recursive function calls.
The overall result is a significant improvement in speed by a
factor of roughly twenty -- your mileage may vary. These
optimized functions are still not as fast as the original (more
limited) functions, but they are getting close. Additional optimizations are
possible. But the cost is a significant "uglification" of the
code that I deemed a step too far, at least for now.
## Misc. Changes
1. Added a test case to check the proper reclamation/copy of complex types.
2. Found and fixed some places where nc_reclaim/copy should have been used.
3. Replaced, in the netcdf-c library, (almost all) occurrences of nc_reclaim_copy with calls to NC_reclaim/copy. This plus the optimizations is the primary speed-up mechanism.
4. In DAP4, the metadata is held in a substrate in-memory file; this required some changes so that the reclaim/copy code accessed that substrate dispatcher rather than the DAP4 dispatcher.
5. Re-factored and isolated the code that computes if a type is (transitively) variable-sized or not.
6. Clean up the reclamation code in ncgen; adding the use of nc_reclaim exposed some memory problems.
2023-05-21 07:11:25 +08:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if((retval=NC4_set_varsize(type))) return retval;
|
2019-02-19 20:18:25 +08:00
|
|
|
|
|
|
|
/* Return the typeid to the user. */
|
|
|
|
if (typeidp)
|
|
|
|
*typeidp = type->hdr.id;
|
|
|
|
|
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Create a compound type.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param size Gets size in bytes of one element of type.
|
|
|
|
* @param name Name of the type.
|
|
|
|
* @param typeidp Gets the type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ENOTNC4 User types in netCDF-4 files only.
|
|
|
|
* @return ::NC_EINVAL Bad size.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ESTRICTNC3 Cannot define user types in classic model.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
return add_user_type(ncid, size, name, 0, NC_COMPOUND, typeidp);
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Insert a named field into a compound type.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param name Name of the type.
|
|
|
|
* @param offset Offset of field.
|
|
|
|
* @param field_typeid Field type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_insert_compound(int ncid, nc_type typeid1, const char *name, size_t offset,
|
|
|
|
nc_type field_typeid)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
return nc_insert_array_compound(ncid, typeid1, name, offset,
|
|
|
|
field_typeid, 0, NULL);
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Insert a named array into a compound type.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param name Name of the array field.
|
|
|
|
* @param offset Offset in bytes.
|
|
|
|
* @param field_typeid Type of field.
|
|
|
|
* @param ndims Number of dims for field.
|
|
|
|
* @param dim_sizesp Array of dim sizes.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
extern int
|
|
|
|
NC4_insert_array_compound(int ncid, int typeid1, const char *name,
|
|
|
|
size_t offset, nc_type field_typeid,
|
|
|
|
int ndims, const int *dim_sizesp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_TYPE_INFO_T *type;
|
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
LOG((2, "nc_insert_array_compound: ncid 0x%x, typeid %d name %s "
|
|
|
|
"offset %d field_typeid %d ndims %d", ncid, typeid1,
|
|
|
|
name, offset, field_typeid, ndims));
|
|
|
|
|
|
|
|
/* Check and normalize the name. */
|
|
|
|
if ((retval = nc4_check_name(name, norm_name)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Find file metadata. */
|
|
|
|
if ((retval = nc4_find_nc4_grp(ncid, &grp)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Find type metadata. */
|
|
|
|
if ((retval = nc4_find_type(grp->nc4_info, typeid1, &type)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Did the user give us a good compound type typeid? */
|
|
|
|
if (!type || type->nc_type_class != NC_COMPOUND)
|
|
|
|
return NC_EBADTYPE;
|
|
|
|
|
|
|
|
/* If this type has already been written to the file, you can't
|
|
|
|
* change it. */
|
|
|
|
if (type->committed)
|
|
|
|
return NC_ETYPDEFINED;
|
|
|
|
|
|
|
|
/* Insert new field into this type's list of fields. */
|
|
|
|
if ((retval = nc4_field_list_add(type, norm_name, offset, field_typeid,
|
|
|
|
ndims, dim_sizesp)))
|
|
|
|
return retval;
|
|
|
|
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
/* See if this changes from fixed size to variable size */
|
Improve performance of the nc_reclaim_data and nc_copy_data functions.
re: Issue https://github.com/Unidata/netcdf-c/issues/2685
re: PR https://github.com/Unidata/netcdf-c/pull/2179
As noted in PR https://github.com/Unidata/netcdf-c/pull/2179,
the old code did not allow for reclaiming instances of types,
nor for properly copying them. That PR provided new functions
capable of reclaiming/copying instances of arbitrary types.
However, as noted by Issue https://github.com/Unidata/netcdf-c/issues/2685, using these
most general functions resulted in a significant performance
degradation, even for common cases.
This PR attempts to mitigate the cost of using the general
reclaim/copy functions in two ways.
First, the previous functions operating at the top level by
using ncid and typeid arguments. These functions were augmented
with equivalent versions that used the netcdf-c library internal
data structures to allow direct access to needed information.
These new functions are used internally to the library.
The second mitigation involves optimizing the internal functions
by providing early tests for common cases. This avoids
unnecessary recursive function calls.
The overall result is a significant improvement in speed by a
factor of roughly twenty -- your mileage may vary. These
optimized functions are still not as fast as the original (more
limited) functions, but they are getting close. Additional optimizations are
possible. But the cost is a significant "uglification" of the
code that I deemed a step too far, at least for now.
## Misc. Changes
1. Added a test case to check the proper reclamation/copy of complex types.
2. Found and fixed some places where nc_reclaim/copy should have been used.
3. Replaced, in the netcdf-c library, (almost all) occurrences of nc_reclaim_copy with calls to NC_reclaim/copy. This plus the optimizations is the primary speed-up mechanism.
4. In DAP4, the metadata is held in a substrate in-memory file; this required some changes so that the reclaim/copy code accessed that substrate dispatcher rather than the DAP4 dispatcher.
5. Re-factored and isolated the code that computes if a type is (transitively) variable-sized or not.
6. Clean up the reclamation code in ncgen; adding the use of nc_reclaim exposed some memory problems.
2023-05-21 07:11:25 +08:00
|
|
|
if((retval=NC4_recheck_varsize(type,field_typeid))) return retval;
|
2019-02-19 20:18:25 +08:00
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Opaque type. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Create an opaque type. Provide a size and a name.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param datum_size Size in bytes of a datum.
|
|
|
|
* @param name Name of new vlen type.
|
|
|
|
* @param typeidp Pointer that gets new type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ENOTNC4 User types in netCDF-4 files only.
|
|
|
|
* @return ::NC_EINVAL Bad size.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ESTRICTNC3 Cannot define user types in classic model.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_def_opaque(int ncid, size_t datum_size, const char *name,
|
|
|
|
nc_type *typeidp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
return add_user_type(ncid, datum_size, name, 0, NC_OPAQUE, typeidp);
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Define a variable length type.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param name Name of new vlen type.
|
|
|
|
* @param base_typeid Base type of vlen.
|
|
|
|
* @param typeidp Pointer that gets new type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ENOTNC4 User types in netCDF-4 files only.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ESTRICTNC3 Cannot define user types in classic model.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_def_vlen(int ncid, const char *name, nc_type base_typeid,
|
|
|
|
nc_type *typeidp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
return add_user_type(ncid, 0, name, base_typeid, NC_VLEN, typeidp);
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Create an enum type. Provide a base type and a name. At
|
|
|
|
* the moment only ints are accepted as base types.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param base_typeid Base type of vlen.
|
|
|
|
* @param name Name of new vlen type.
|
|
|
|
* @param typeidp Pointer that gets new type ID.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTNC4 User types in netCDF-4 files only.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @return ::NC_EMAXNAME Name is too long.
|
|
|
|
* @return ::NC_EBADNAME Name breaks netCDF name rules.
|
2018-06-12 21:01:50 +08:00
|
|
|
* @return ::NC_ESTRICTNC3 Cannot define user types in classic model.
|
2018-05-25 04:27:16 +08:00
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_def_enum(int ncid, nc_type base_typeid, const char *name,
|
|
|
|
nc_type *typeidp)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
return add_user_type(ncid, 0, name, base_typeid, NC_ENUM, typeidp);
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Insert a identifier value into an enum type. The value
|
|
|
|
* must fit within the size of the enum type, the identifier size must
|
|
|
|
* be <= NC_MAX_NAME.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param identifier Name of this enum value.
|
|
|
|
* @param value Value of enum.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_EBADTYPE Type not found.
|
|
|
|
* @return ::NC_ETYPDEFINED Type already defined.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_insert_enum(int ncid, nc_type typeid1, const char *identifier,
|
|
|
|
const void *value)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_TYPE_INFO_T *type;
|
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
int retval;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
LOG((2, "nc_insert_enum: ncid 0x%x, typeid %d identifier %s value %d", ncid,
|
|
|
|
typeid1, identifier, value));
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* Check and normalize the name. */
|
|
|
|
if ((retval = nc4_check_name(identifier, norm_name)))
|
|
|
|
return retval;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* Find file metadata. */
|
|
|
|
if ((retval = nc4_find_nc4_grp(ncid, &grp)))
|
|
|
|
return retval;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* Find type metadata. */
|
|
|
|
if ((retval = nc4_find_type(grp->nc4_info, typeid1, &type)))
|
|
|
|
return retval;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* Did the user give us a good enum typeid? */
|
|
|
|
if (!type || type->nc_type_class != NC_ENUM)
|
|
|
|
return NC_EBADTYPE;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* If this type has already been written to the file, you can't
|
|
|
|
* change it. */
|
|
|
|
if (type->committed)
|
|
|
|
return NC_ETYPDEFINED;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
/* Insert new field into this type's list of fields. */
|
|
|
|
if ((retval = nc4_enum_member_add(type, type->size,
|
|
|
|
norm_name, value)))
|
|
|
|
return retval;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Insert one element into an already allocated vlen array
|
|
|
|
* element.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param vlen_element The VLEN element to insert.
|
|
|
|
* @param len Length of element in bytes.
|
|
|
|
* @param data Element data.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_put_vlen_element(int ncid, int typeid1, void *vlen_element,
|
|
|
|
size_t len, const void *data)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
nc_vlen_t *tmp = (nc_vlen_t*)vlen_element;
|
|
|
|
tmp->len = len;
|
|
|
|
tmp->p = (void *)data;
|
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Insert one element into an already allocated vlen array
|
|
|
|
* element.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param vlen_element The VLEN element to insert.
|
|
|
|
* @param len Length of element in bytes.
|
|
|
|
* @param data Element data.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_get_vlen_element(int ncid, int typeid1, const void *vlen_element,
|
|
|
|
size_t *len, void *data)
|
|
|
|
{
|
2019-02-19 20:18:25 +08:00
|
|
|
const nc_vlen_t *tmp = (nc_vlen_t*)vlen_element;
|
|
|
|
int type_size = 4;
|
2018-05-25 04:27:16 +08:00
|
|
|
|
2019-02-19 20:18:25 +08:00
|
|
|
*len = tmp->len;
|
|
|
|
memcpy(data, tmp->p, tmp->len * type_size);
|
|
|
|
return NC_NOERR;
|
2018-05-25 04:27:16 +08:00
|
|
|
}
|