Skip to content

Commit

Permalink
ckp
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisHeimbigner committed Dec 3, 2024
1 parent 48111e6 commit c6dbbc4
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 117 deletions.
39 changes: 24 additions & 15 deletions libdispatch/dinstance_intern.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Currently two operations are defined:
#include "nc4dispatch.h"
#include "ncoffsets.h"
#include "ncbytes.h"
#include "nclog.h"

#undef REPORT
#undef DEBUG
Expand Down Expand Up @@ -76,8 +77,11 @@ NC_reclaim_data(NC* nc, nc_type xtype, void* memory, size_t count)
NC_TYPE_INFO_T* utype = NULL;

assert(nc != NULL);
assert((memory == NULL && count == 0) || (memory != NULL || count > 0));
/* If memory is NULL, ignore count */
assert(memory == NULL || (memory != NULL && count > 0));

if(memory == NULL) goto done;

/* Process atomic types */

/* Optimize: Vector of fixed size atomic types (always the case for netcdf-3)*/
Expand Down Expand Up @@ -118,7 +122,7 @@ NC_reclaim_data(NC* nc, nc_type xtype, void* memory, size_t count)
#endif

done:
return stat;
return NCTHROW(stat);
}

#ifdef USE_NETCDF4
Expand All @@ -128,7 +132,8 @@ NC_reclaim_data(NC* nc, nc_type xtype, void* memory, size_t count)
static int
reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance)
{
int i,stat = NC_NOERR;
int stat = NC_NOERR;
int i;
nc_type basetypeid;
NC_TYPE_INFO_T* basetype = NULL;
size_t nfields;
Expand All @@ -154,7 +159,7 @@ reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance)
if(basetypeid == NC_STRING) {
if(vlen->len > 0 && vlen->p != NULL) {
char** slist = (char**)vlen->p; /* vlen instance is a vector of string pointers */
for(i=0;i<vlen->len;i++) {if(slist[i] != NULL) {free(slist[i]);slist[i] = NULL;}}
for(i=0;i<(int)vlen->len;i++) {if(slist[i] != NULL) {free(slist[i]);slist[i] = NULL;}}
}
goto out;
}
Expand All @@ -167,7 +172,7 @@ reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance)
if((stat = NC_type_alignment_internal(file,basetypeid,basetype,&alignment))) goto done;;
vinstance.memory = (char*)vlen->p; /* use char* so we can do pointer arithmetic */
vinstance.memory = (void*)NC_read_align((uintptr_t)vinstance.memory,alignment);
for(i=0;i<vlen->len;i++) {
for(i=0;i<(int)vlen->len;i++) {
if((stat=reclaim_datar(file,basetype,vinstance))) goto done; /* reclaim one basetype instance */
vinstance.memory += basetype->size; /* move to next base instance */
}
Expand Down Expand Up @@ -217,7 +222,7 @@ reclaim_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position instance)
} else {stat = NC_EBADTYPE; goto done;}

done:
return stat;
return NCTHROW(stat);
}
#endif

Expand Down Expand Up @@ -323,7 +328,7 @@ NC_copy_data(NC* nc, nc_type xtype, const void* memory, size_t count, void* copy
#endif

done:
return stat;
return NCTHROW(stat);
}

#ifdef USE_NETCDF4
Expand Down Expand Up @@ -380,7 +385,7 @@ copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position d
char** dststrvec = NULL;
if((dststrvec = (void*)malloc(copycount))==NULL) {stat = NC_ENOMEM; goto done;}
dstvlens->p = (void*)dststrvec;
for(i=0;i<srcvlens->len;i++) {
for(i=0;i<(int)srcvlens->len;i++) {
if((dststrvec[i] = strdup(srcstrvec[i]))==NULL) {stat = NC_ENOMEM; goto done;}
}
goto done;
Expand All @@ -406,7 +411,7 @@ copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position d
dstvlens->p = vdst.memory; /* don't lose it */
vsrc.memory = (void*)NC_read_align((uintptr_t)vsrc.memory,alignment);
vdst.memory = (void*)NC_read_align((uintptr_t)vdst.memory,alignment);
for(i=0;i<srcvlens->len;i++) {
for(i=0;i<(int)srcvlens->len;i++) {
if((stat=copy_datar(file,basetype,vsrc,vdst))) goto done;
vsrc.memory += basetype->size;
vdst.memory += basetype->size;
Expand Down Expand Up @@ -441,7 +446,7 @@ copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position d
if(field->nc_typeid == NC_STRING) {
char** srcstrvec = (char**)src.memory;
char** dststrvec = (char**)dst.memory;
for(i=0;i<arraycount;i++)
for(i=0;i<(int)arraycount;i++)
{if(srcstrvec[i] != NULL) {dststrvec[i] = strdup(srcstrvec[i]);} else {dststrvec[i] = NULL;}}
continue; /* move to next field */
}
Expand All @@ -454,7 +459,7 @@ copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position d
}

/* Remaining case; field type is variable type */
for(i=0;i<arraycount;i++) {
for(i=0;i<(int)arraycount;i++) {
if((stat = copy_datar(file, basetype, fsrc, fdst))) goto done;
fsrc.memory += basetype->size;
fdst.memory += basetype->size;
Expand All @@ -465,7 +470,7 @@ copy_datar(NC_FILE_INFO_T* file, NC_TYPE_INFO_T* utype, Position src, Position d
} else {stat = NC_EBADTYPE; goto done;}

done:
return stat;
return NCTHROW(stat);
}
#endif

Expand Down Expand Up @@ -531,7 +536,7 @@ NC_type_alignment_internal(NC_FILE_INFO_T* file, nc_type xtype, NC_TYPE_INFO_T*
Why was this here?
if(stat == NC_NOERR && align == 0) stat = NC_EINVAL;
#endif
return stat;
return NCTHROW(stat);
}
#endif

Expand All @@ -546,11 +551,15 @@ NC_reclaim_data_all(NC* nc, nc_type xtypeid, void* memory, size_t count)
int stat = NC_NOERR;

assert(nc != NULL);
/* If memory is NULL, ignore count */
assert(memory == NULL || (memory != NULL && count > 0));

if(memory == NULL) goto done;
stat = NC_reclaim_data(nc,xtypeid,memory,count);
if(stat == NC_NOERR && memory != NULL)
{free(memory); memory = NULL;}
return stat;
done:
return NCTHROW(stat);
}

/* Alternate entry point: includes recovering the top-level memory */
Expand Down Expand Up @@ -597,7 +606,7 @@ NC_copy_data_all(NC* nc, nc_type xtype, const void* memory, size_t count, void**
#endif
if(copyp) {*copyp = copy; copy = NULL;}
done:
return stat;
return NCTHROW(stat);
}

/* Alternate entry point: includes recovering the top-level memory */
Expand Down
1 change: 1 addition & 0 deletions libnczarr/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ztype.c \
zutil.c \
zvar.c \
zwalk.c \
znc4.c \
zdebug.c \
zformat.h \
zarr.h \
Expand Down
2 changes: 1 addition & 1 deletion libnczarr/zarr.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ EXTERNL void NCZ_setatts_read(NC_OBJ* container);
EXTERNL int NCZ_decodesizet64vec(const NCjson* jshape, size_t* rankp, size64_t* shapes);
EXTERNL int NCZ_decodesizetvec(const NCjson* jshape, size_t* rankp, size_t* shapes);
EXTERNL int NCZ_uniquedimname(NC_FILE_INFO_T* file, NC_GRP_INFO_T* parent, NCZ_DimInfo* dimdata, NC_DIM_INFO_T** dimp, NCbytes* dimname);
EXTERNL int NCZ_computeattrdata(NC_FILE_INFO_T* file, struct NCZ_AttrInfo* att);
EXTERNL int NCZ_computeattrdata(NC_FILE_INFO_T* file, const NCjson* jdata, struct NCZ_AttrInfo* att);
EXTERNL int NCZ_dictgetalt(const NCjson* jdict, const NCjson** jvaluep, ...);
EXTERNL int NCZ_dictgetalt2(const NCjson* jdict, const NCjson** jvaluep, const char* name1, const char* name2); /* common case */
EXTERNL int NCZ_getnczarrkey(NC_FILE_INFO_T* file, struct ZOBJ* jsonz, const char* name, const NCjson** jncxxxp);
Expand Down
9 changes: 4 additions & 5 deletions libnczarr/zattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,9 @@ NCZ_set_dual_obj_data(NC_FILE_INFO_T* file, NC_OBJ* object, const char* name, Du
switch(which) {
case DA_FILLVALUE:
assert(len == 1);
if((stat = NC_reclaim_data_all(file->controller,tid,var->fill_value,1))) goto done;
if((stat = NC_reclaim_data_all(file->controller,tid,var->fill_value,len))) goto done;
var->fill_value = NULL;
if((stat = NC_copy_data_all(file->controller,tid,data,1,&var->fill_value))) goto done;
if((stat = NC_copy_data_all(file->controller,tid,data,len,&var->fill_value))) goto done;
break;
case DA_MAXSTRLEN:
assert(len == 1);
Expand Down Expand Up @@ -970,16 +970,15 @@ This is essentially Version 2|3 agnostic because the
data part of an attribute is (currently) the same for both versions.
*/
int
NCZ_computeattrdata(NC_FILE_INFO_T* file, struct NCZ_AttrInfo* ainfo)
NCZ_computeattrdata(NC_FILE_INFO_T* file, const NCjson* jdata, struct NCZ_AttrInfo* ainfo)
{
int stat = NC_NOERR;
NCbytes* buf = ncbytesnew();
NCjson* jtext = NULL;
int isjson = 0; /* 1 => attribute value is neither scalar nor array of scalars */
int reclaimvalues = 0;
const NCjson* jdata = ainfo->jdata;

ZTRACE(3,"typehint=%d typeid=%d values=|%s|",ainfo->typehint,ainfo->nctype,NCJtotext(ainfo->jdata));
ZTRACE(3,"typehint=%d typeid=%d values=|%s|",ainfo->typehint,ainfo->nctype,NCJtotext(jdata));

/* See if this is a simple vector (or scalar) of atomic types vs more complex json */
isjson = (ainfo->nctype == NC_JSON || NCZ_iscomplexjson(ainfo->name,jdata));
Expand Down
66 changes: 0 additions & 66 deletions libnczarr/zfill.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,72 +43,6 @@ static struct DFALTFILL {
/**************************************************/
/**************************************************/

#if 0
/* (over-) write the NC_VAR_INFO_T.fill_value; always make copy of fillvalue argument.
Takes no_fill flag into account. Sync with attribute
*/
int
NCZ_set_fill_value(NC_FILE_INFO_T* file, NC_VAR_INFO_T* var, int no_fill, const void* fillvalue)
{
int stat = NC_NOERR;
size_t size;
nc_type tid = var->type_info->hdr.id;

if(no_fill) {
stat = NCZ_disable_fill(file,var);
goto done;
}

if ((stat = nc4_get_typelen_mem(file, tid, &size))) goto done;
assert(size);

/* Reclaim any existing var->fill_value */
if(var->fill_value != NULL) {
if((stat = NC_reclaim_data_all(file->controller,tid,var->fill_value,1))) goto done;
var->fill_value = NULL;
}
if(fillvalue == NULL) {/* use default fill value */
/* initialize the fill_value to the default */
if((stat = NCZ_set_fill_value(file,var,var->no_fill,NCZ_getdfaltfillvalue(var->type_info->hdr.id)))) goto done;
var->fill_val_changed = 0;
} else {
/* overwrite the fill value */
assert(var->fill_value == NULL);
if((stat = NC_copy_data_all(file->controller,tid,fillvalue,1,&var->fill_value))) goto done;
var->fill_val_changed = 1;
}
var->no_fill = NC_FILL;

stat = NCZ_reclaim_fill_chunk(((NCZ_VAR_INFO_T*)var->format_var_info)->cache); /* Reclaim any existing fill_chunk */

done:
return THROW(stat);
}

/* (over-) write/create the _FillValue attribute; always makes copy of NC_VAR_INFO_T.fill_value.
Takes no_fill flag into account. Does not sync with NC_VAR_INFO_T.fill_value.
*/
int
NCZ_set_fill_att(NC_FILE_INFO_T* file, NC_VAR_INFO_T* var, NC_ATT_INFO_T* att, int no_fill, const void* fillvalue)
{
int stat = NC_NOERR;
if(no_fill) {
stat = NCZ_fillvalue_disable(file,var);
} else {
if(att == NULL) {
int isnew = 0;
if((stat = NCZ_getattr(file, (NC_OBJ*)var, NC_FillValue, var->type_info->hdr.id, &att, &isnew))) goto done;
}
assert(att != NULL && strcmp(att->hdr.name,NC_FillValue)==0); /* Verify */
if((stat = NCZ_sync_dual_att(file,(NC_OBJ*)var,NC_FillValue,DA_FILLVALUE,FIXATT))) goto done;
var->no_fill = NC_FALSE;
var->fill_val_changed = 1;
}

done:
return THROW(stat);
}
#endif

/* Turn off var.no_fill and var.fill_value. Sync with attribute */
int
Expand Down
19 changes: 7 additions & 12 deletions libnczarr/zformat2.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static int ZF2_encode_xarray(NC_FILE_INFO_T* file, size_t rank, NC_DIM_INFO_T**
static int decode_grp_dims(NC_FILE_INFO_T* file, NC_GRP_INFO_T* grp, const NCjson* jdims, NClist* dimdefs);
static int dtype2nctype(NC_FILE_INFO_T* file, const char* dtype, int isattr, nc_type* nctypep, int* endianp, size_t* typelenp);
static int nctype2dtype(NC_FILE_INFO_T* file, nc_type nctype, int endianness, size_t typesize, char** dtypep, char** dattrtypep);
static int computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const NCjson* jtypes, const NCjson* jainfo, struct NCZ_AttrInfo* ainfo);
static int computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const NCjson* jtypes, const NCjson* jdata, struct NCZ_AttrInfo* ainfo);

/**************************************************/
/* Format dispatch table */
Expand Down Expand Up @@ -517,17 +517,14 @@ ZF2_decode_var(NC_FILE_INFO_T* file, NC_VAR_INFO_T* var, struct ZOBJ* zobj, NCli
NCZ_clearAttrInfo(file,&ainfo);
ainfo.name = NC_FillValue;
ainfo.nctype = vtype;
ainfo.jdata = jvalue;
if((stat = NCZ_computeattrdata(file,&ainfo))) goto done;
if((stat = NCZ_computeattrdata(file,jvalue,&ainfo))) goto done;
/* Create var->fill_value */
assert(ainfo.nctype == vtype);
if((stat = NCZ_set_dual_obj_data(file,(NC_OBJ*)var,NC_FillValue,DA_FILLVALUE,ainfo.datalen,ainfo.data))) goto done;
/* propagate to _FillValue attribute */
if((stat = NCZ_sync_dual_att(file,(NC_OBJ*)var,NC_FillValue,DA_FILLVALUE,FIXATT))) goto done;
/* reclaim ainfo.data */
if((stat = NC_reclaim_data_all(file->controller,ainfo.nctype,ainfo.data,ainfo.datalen))) goto done;
ainfo.datalen = 0;
ainfo.data = NULL;
/* clear+reclaim ainfo */
NCZ_clearAttrInfo(file,&ainfo);
}
}

Expand Down Expand Up @@ -1656,7 +1653,7 @@ dtype2nctype(NC_FILE_INFO_T* file, const char* dtype, int isattr, nc_type* nctyp
Extract type and data for an attribute from json
*/
static int
computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const NCjson* jtypes, const NCjson* jainfo, struct NCZ_AttrInfo* ainfo)
computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const NCjson* jtypes, const NCjson* jdata, struct NCZ_AttrInfo* ainfo)
{
int stat = NC_NOERR;
int purezarr = 0;
Expand All @@ -1670,13 +1667,11 @@ computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const
TESTPUREZARR;

ainfo->name = aname;
/* Save the attribute data */
ainfo->jdata = jainfo;

/* Infer the attribute data's type */
if(purezarr || jtypes == NULL) {
ainfo->nctype = NC_NAT;
if((stat = NCZ_inferattrtype(ainfo->name,typehint,ainfo->jdata,&ainfo->nctype))) goto done;
if((stat = NCZ_inferattrtype(ainfo->name,typehint,jdata,&ainfo->nctype))) goto done;
} else {
/* Search the jtypes for the type of this attribute */
ainfo->nctype = NC_NAT;
Expand All @@ -1685,7 +1680,7 @@ computeattrinfo(NC_FILE_INFO_T* file, nc_type typehint, const char* aname, const
if((stat=dtype2nctype(file,NCJstring(jatype),ISATTR,&ainfo->nctype,&ainfo->endianness,&ainfo->typelen))) goto done;
if(ainfo->nctype >= N_NCZARR_TYPES) {stat = NC_EINTERNAL; goto done;}
}
if((stat = NCZ_computeattrdata(file,ainfo))) goto done;
if((stat = NCZ_computeattrdata(file,jdata,ainfo))) goto done;

done:
return ZUNTRACEX(THROW(stat),"typeid=%d typelen=%d len=%u",ainfo->nctype,ainfo->typelen,ainfo->len);
Expand Down
4 changes: 2 additions & 2 deletions libnczarr/zinternal.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ NCZ_ensure_fill_value(NC_VAR_INFO_T *var)
return NC_NOERR;

/* If the user has set a fill_value for this var, use, otherwise find the default fill value. */
assert(var->no_fill == NC_FILL);
if(var->fill_value == NULL) {
NC_FILE_INFO_T* file = var->container->nc4_info;
nc_type vartid = var->type_info->hdr.id;
if((stat = NC_copy_data_all(file->controller,vartid,NCZ_getdfaltfillvalue(vartid),1,&var->fill_value))) goto done;
void* dfalt = NCZ_getdfaltfillvalue(vartid);
if((stat = NCZ_set_dual_obj_data(file,(NC_OBJ*)var,NC_FillValue,DA_FILLVALUE,1,dfalt))) goto done;
/* synchronize to attribute */
if((stat = NCZ_sync_dual_att(file,(NC_OBJ*)var,NC_FillValue,DA_FILLVALUE,FIXATT))) goto done;
}
Expand Down
1 change: 0 additions & 1 deletion libnczarr/zinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ typedef struct NCZ_DimDecl {
/* Parsed Attribute info */
struct NCZ_AttrInfo {
const char* name;
const NCjson* jdata;
nc_type nctype;
size_t typelen;
int endianness;
Expand Down
Loading

0 comments on commit c6dbbc4

Please sign in to comment.