Commit ef08ba93 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

replaced the vlist locked flag by two other flags

parent 06fffaed
......@@ -3,6 +3,10 @@
* Version 1.7.1 released
* using CGRIBEX library version 1.7.4
2015-11-23 Nathanael Huebbe
* replaced the vlist locked flag by two other flags
2015-11-17 Uwe Schulzweida
* cdfCopyRecord: use MEMTYPE_FLOAT for DATATYPE_FLT32
......
......@@ -538,17 +538,19 @@ streamOpenID(const char *filename, char filemode, int filetype,
streamptr->fileID = fileID;
if ( filemode == 'r' )
{
int vlistID = vlistCreate();
if ( vlistID < 0 ) return(CDI_ELIMIT);
streamptr->vlistID = vlistID;
/* cdiReadByteorder(streamID); */
status = cdiInqContents(streamptr);
if ( status < 0 ) return (status);
vlist_t *vlistptr = vlist_to_pointer(streamptr->vlistID);
vlistptr->ntsteps = streamptr->ntsteps;
}
{
int vlistID = vlistCreate();
if ( vlistID < 0 ) return(CDI_ELIMIT);
cdiVlistMakeInternal(vlistID);
streamptr->vlistID = vlistID;
/* cdiReadByteorder(streamID); */
status = cdiInqContents(streamptr);
if ( status < 0 ) return (status);
vlist_t *vlistptr = vlist_to_pointer(streamptr->vlistID);
vlistptr->ntsteps = streamptr->ntsteps;
cdiVlistMakeImmutable(vlistID);
}
}
return (streamID);
......@@ -593,11 +595,13 @@ static int streamOpenA(const char *filename, const char *filemode, int filetype)
streamptr->fileID = fileID;
streamptr->vlistID = vlistCreate();
cdiVlistMakeInternal(streamptr->vlistID);
/* cdiReadByteorder(streamID); */
status = cdiInqContents(streamptr);
if ( status < 0 ) return (status);
vlistptr = vlist_to_pointer(streamptr->vlistID);
vlistptr->ntsteps = (int)cdiInqTimeSize(streamID);
if(!strcmp(filemode, "r")) cdiVlistMakeImmutable(streamptr->vlistID);
{
void (*streamCloseDelegate)(stream_t *streamptr, int recordBufIsToBeDeleted)
......@@ -1023,8 +1027,7 @@ void streamClose(int streamID)
taxisDestroy(vlistInqTaxis(vlistID));
}
vlist_unlock(vlistID);
vlistDestroy(vlistID);
cdiVlistDestroy_(vlistID);
}
stream_delete_entry(streamptr);
......@@ -1420,6 +1423,10 @@ off_t streamNvals(int streamID)
@Description
The function @func{streamDefVlist} defines the variable list of a stream.
To safeguard against errors by modifying the wrong vlist object,
this function makes the passed vlist object immutable.
All further vlist changes have to use the vlist object returned by streamInqVlist().
@EndFunction
*/
void streamDefVlist(int streamID, int vlistID)
......@@ -1436,7 +1443,12 @@ cdiStreamDefVlist_(int streamID, int vlistID)
stream_t *streamptr = stream_to_pointer(streamID);
if ( streamptr->vlistID == CDI_UNDEFID )
cdiStreamSetupVlist(streamptr, vlistDuplicate(vlistID));
{
int vlistCopy = vlistDuplicate(vlistID);
cdiVlistMakeInternal(vlistCopy);
cdiVlistMakeImmutable(vlistID);
cdiStreamSetupVlist(streamptr, vlistCopy);
}
else
Warning("vlist already defined for %s!", streamptr->filename);
}
......@@ -1548,7 +1560,6 @@ cdiStreamSetupVlist(stream_t *streamptr, int vlistID)
void
cdiStreamSetupVlist_(stream_t *streamptr, int vlistID)
{
vlist_lock(vlistID);
int nvars = vlistNvars(vlistID);
streamptr->vlistID = vlistID;
for (int varID = 0; varID < nvars; varID++ )
......
......@@ -93,7 +93,8 @@ vlist_t *vlist_to_pointer(int vlistID)
static
void vlist_init_entry(vlist_t *vlistptr)
{
vlistptr->locked = 0;
vlistptr->immutable = 0;
vlistptr->internal = 0;
vlistptr->self = CDI_UNDEFID;
vlistptr->nvars = 0;
vlistptr->vars = NULL;
......@@ -158,32 +159,22 @@ static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
{
int vlistID2 = vlistptr2->self;
int vlist2internal = vlistptr2->internal;
memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
vlistptr2->internal = vlist2internal; //the question who's responsible to destroy the vlist is tied to its containing memory region, so we retain this flag
vlistptr2->immutable = 0; //this is a copy, so it's mutable, independent of whether the original is mutable or not
vlistptr2->atts.nelems = 0;
vlistptr2->self = vlistID2;
}
void vlist_lock(int vlistID)
void cdiVlistMakeInternal(int vlistID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if ( !vlistptr->locked )
{
vlistptr->locked += 1;
reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
}
vlist_to_pointer(vlistID)->internal = 1;
}
void vlist_unlock(int vlistID)
void cdiVlistMakeImmutable(int vlistID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if ( vlistptr->locked )
{
vlistptr->locked -= 1;
reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
}
vlist_to_pointer(vlistID)->immutable = 1;
}
/*
......@@ -277,12 +268,25 @@ void vlistDestroy(int vlistID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if ( vlistptr->locked != 0 )
Warning("Destroying of a locked object (vlistID=%d) failed!", vlistID);
if ( vlistptr->internal )
Warning("Attempt to destroy an internal vlist object by the user (vlistID=%d).", vlistID);
else
vlist_delete(vlistptr);
}
// destroy an internal vlist object
void cdiVlistDestroy_(int vlistID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
if(!vlistptr->internal)
Warning("Destroying a vlist object that is owned by the user.\n"
"This is most likely because of a missing vlistDestroy() in the application code.\n"
"If that's not the case, and you are absolutely certain about it, please report the bug.");
vlist_delete(vlistptr);
}
static
void var_copy_entries(var_t *var2, var_t *var1)
{
......@@ -1517,6 +1521,7 @@ void vlistUnpack(char * buf, int size, int *position, int originNamespace,
xassert(!force_id || p->self == targetID);
if (!force_id)
targetID = p->self;
cdiVlistMakeInternal(p->self);
p->taxisID = namespaceAdaptKey(tempbuf[2], originNamespace);
p->tableID = tempbuf[3];
p->instID = namespaceAdaptKey(tempbuf[4], originNamespace);
......
......@@ -116,7 +116,8 @@ var_t;
typedef struct
{
int locked;
unsigned immutable : 1; //set when a vlist is passed to streamDefVlist() to safeguard against modifications of the wrong vlist object
unsigned internal : 1; //set if this vlist has been created by CDI itself, and must not be destroyed by the user, consequently
int self;
int nvars; /* number of variables */
int ngrids;
......@@ -138,6 +139,8 @@ vlist_t;
vlist_t *vlist_to_pointer(int vlistID);
void cdiVlistMakeInternal(int vlistID);
void cdiVlistMakeImmutable(int vlistID);
void vlistCheckVarID(const char *caller, int vlistID, int varID);
const char *vlistInqVarNamePtr(int vlistID, int varID);
const char *vlistInqVarLongnamePtr(int vlistID, int varID);
......@@ -147,6 +150,7 @@ void vlistDestroyVarName(int vlistID, int varID);
void vlistDestroyVarLongname(int vlistID, int varID);
void vlistDestroyVarStdname(int vlistID, int varID);
void vlistDestroyVarUnits(int vlistID, int varID);
void cdiVlistDestroy_(int vlistID);
void vlistDefVarTsteptype(int vlistID, int varID, int tsteptype);
int vlistInqVarMissvalUsed(int vlistID, int varID);
int vlistHasTime(int vlistID);
......@@ -167,9 +171,6 @@ void vlistInqVarDimorder(int vlistID, int varID, int (*outDimorder)[3]);
int vlist_att_compare(vlist_t *a, int varIDA, vlist_t *b, int varIDB, int attnum);
void vlist_lock(int vlistID);
void vlist_unlock(int vlistID);
void resize_opt_grib_entries(var_t *var, int nentries);
......
......@@ -1768,9 +1768,10 @@ void vlistDefVarIntKey(int vlistID, int varID, const char *name, int value)
if (vlistptr == NULL) Error("Internal error!");
int idx;
if ( vlistptr->locked != 0 )
Error("User defined vlist object (vlistID=%d) isn't allowed!\n"
"Need a CDI internal vlist object from streamInqVlist(streamID).", vlistID);
if ( vlistptr->immutable )
Error("vlistDefVarIntKey() was called on an immutable vlist object (vlistID = %d)\n"
"Either call vlistDefVarIntKey() before passing the vlist object to streamDefVlist(),\n"
"or use the stream-internal vlist by calling streamInqVlist().", vlistID);
for ( idx=0; idx<vlistptr->vars[varID].opt_grib_nentries; idx++)
if ( (strcmp(name, vlistptr->vars[varID].opt_grib_kvpair[idx].keyword) == 0 ) &&
......@@ -1830,9 +1831,10 @@ void vlistDefVarDblKey(int vlistID, int varID, const char *name, double value)
if (vlistptr == NULL) Error("Internal error!");
int idx;
if ( vlistptr->locked != 0 )
Error("User defined vlist object (vlistID=%d) isn't allowed!\n"
"Need a CDI internal vlist object from streamInqVlist(streamID).", vlistID);
if ( vlistptr->immutable )
Error("vlistDefVarDblKey() was called on an immutable vlist object (vlistID = %d)\n"
"Either call vlistDefVarIntKey() before passing the vlist object to streamDefVlist(),\n"
"or use the stream-internal vlist by calling streamInqVlist().", vlistID);
for ( idx=0; idx<vlistptr->vars[varID].opt_grib_nentries; idx++)
if ( (strcmp(name, vlistptr->vars[varID].opt_grib_kvpair[idx].keyword) == 0 ) &&
......
......@@ -218,6 +218,7 @@ static int modelRun(MPI_Comm comm)
streamID = streamOpenWrite("example.grb", FILETYPE_GRB);
if ( streamID < 0 ) xabort ( "Could not open file" );
defineStream ( streamID, vlistID );
vlistDestroy(temp.id1);
vlistDestroy(temp.id2);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment