Commit d23bba22 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Re-use memory allocated in stream open for reading.

parent 110e199c
......@@ -464,6 +464,7 @@ void gribapiGetGrid(grib_handle *gh, grid_t *grid)
}
*/
grid_init(grid);
cdiGridTypeInit(grid, gridtype, 0);
size_t datasize;
FAIL_ON_GRIB_ERROR(grib_get_size, gh, "values", &datasize);
......
......@@ -4711,16 +4711,16 @@ gridPack(void * voidP, void * packBuffer, int packBufferSize,
#undef GRID_STR_SERIALIZE
struct varDefGridSearchState
struct gridCompareSearchState
{
int resIDValue;
const grid_t *queryKey;
};
static enum cdiApplyRet
varDefGridSearch(int id, void *res, void *data)
gridCompareSearch(int id, void *res, void *data)
{
struct varDefGridSearchState *state = (struct varDefGridSearchState*)data;
struct gridCompareSearchState *state = (struct gridCompareSearchState*)data;
(void)res;
if (gridCompare(id, state->queryKey) == 0)
{
......@@ -4731,7 +4731,7 @@ varDefGridSearch(int id, void *res, void *data)
return CDI_APPLY_GO_ON;
}
int varDefGrid(int vlistID, const grid_t *grid, int mode)
struct addIffNewRes cdiVlistAddGridIfNew(int vlistID, grid_t *grid, int mode)
{
/*
mode: 0 search in vlist and grid table
......@@ -4763,10 +4763,10 @@ int varDefGrid(int vlistID, const grid_t *grid, int mode)
if ( ! griddefined )
{
struct varDefGridSearchState query;
struct gridCompareSearchState query;
query.queryKey = grid;// = { .queryKey = grid };
if ((gridglobdefined
= (cdiResHFilterApply(&gridOps, varDefGridSearch, &query)
= (cdiResHFilterApply(&gridOps, gridCompareSearch, &query)
== CDI_APPLY_STOP)))
gridID = query.resIDValue;
......@@ -4781,12 +4781,17 @@ int varDefGrid(int vlistID, const grid_t *grid, int mode)
if ( ! griddefined )
{
if ( ! gridglobdefined ) gridID = gridGenerate(grid);
if ( ! gridglobdefined )
{
grid->self = gridID = reshPut(grid, &gridOps);
gridComplete(grid);
}
vlistptr->gridIDs[ngrids] = gridID;
vlistptr->ngrids++;
}
return (gridID);
return (struct addIffNewRes){ .Id = gridID,
.isNew = !griddefined && !gridglobdefined };
}
const struct gridVirtTable cdiGridVtable
......
......@@ -103,6 +103,8 @@ struct grid_t {
void grid_init(grid_t *gridptr);
void
cdiGridTypeInit(grid_t *gridptr, int gridtype, int size);
void grid_free(grid_t *gridptr);
grid_t *gridID2Ptr(int gridID);
extern const struct gridVirtTable cdiGridVtable;
......@@ -126,7 +128,13 @@ gridUnpack(char * unpackBuffer, int unpackBufferSize,
int * unpackBufferPos, int originNamespace, void *context,
int force_id);
int varDefGrid(int vlistID, const grid_t *grid, int mode);
struct addIffNewRes
{
int Id;
int isNew;
};
struct addIffNewRes cdiVlistAddGridIfNew(int vlistID, grid_t *grid, int mode);
#endif
/*
......
......@@ -4450,9 +4450,6 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
int i;
int nvatts;
int skipvar;
grid_t *restrict grid;
grid_t proj;
int gridindex;
char name[CDI_MAX_NAME];
int iatt;
int ltwarn = TRUE;
......@@ -4464,7 +4461,7 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
{
if ( ncvars[ncvarid].isvar && ncvars[ncvarid].gridID == UNDEFID )
{
int xdimids[2] = {-1,-1}, ydimids[2] = {-1,-1};
int xdimids[2] = {-1,-1}, ydimids[2] = {-1,-1};
int xdimid = -1, ydimid = -1;
int xvarid = -1, yvarid = -1;
int islon = 0, islat = 0;
......@@ -4472,6 +4469,8 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
size_t size = 0, np = 0;
size_t xsize = 0, ysize = 0;
double xinc = 0, yinc = 0;
struct addIffNewRes projAdded = { .Id = CDI_UNDEFID, .isNew = 0 },
gridAdded = { .Id = CDI_UNDEFID, .isNew = 0 };
int ndims = ncvars[ncvarid].ndims;
for ( i = 0; i < ndims; i++ )
......@@ -4541,9 +4540,12 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
if ( ncvars[ncvarid].gridtype == UNDEFID || ncvars[ncvarid].gridtype == GRID_GENERIC )
if ( xdimid != UNDEFID && xdimid == ydimid && nydims == 0 ) ncvars[ncvarid].gridtype = GRID_UNSTRUCTURED;
grid = (grid_t *)Malloc(sizeof (*grid));
grid_t *restrict grid = (grid_t *)Malloc(sizeof (*grid));
grid_init(grid);
grid_init(&proj);
cdiGridTypeInit(grid, ncvars[ncvarid].gridtype, 0);
grid_t *restrict proj = (grid_t *)Malloc(sizeof (*proj));
grid_init(proj);
cdiGridTypeInit(grid, GRID_PROJECTION, 0);
grid->prec = DATATYPE_FLT64;
grid->trunc = ncvars[ncvarid].truncation;
......@@ -4928,8 +4930,8 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
grid->type = GRID_LCC2;
else if ( strcmp(attstring, "lambert_cylindrical_equal_area") == 0 )
{
proj.type = GRID_PROJECTION;
proj.name = strdup(attstring);
proj->type = GRID_PROJECTION;
proj->name = strdup(attstring);
}
}
else if ( strcmp(attname, "earth_radius") == 0 )
......@@ -5027,7 +5029,7 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
}
#if defined (PROJECTION_TEST)
if ( proj.type == GRID_PROJECTION )
if ( proj->type == GRID_PROJECTION )
{
if ( grid->type == GRID_GENERIC )
{
......@@ -5036,9 +5038,9 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
if ( grid->type == GRID_CURVILINEAR )
{
proj.size = grid->size;
proj.xsize = grid->xsize;
proj.ysize = grid->ysize;
proj->size = grid->size;
proj->xsize = grid->xsize;
proj->ysize = grid->ysize;
}
// grid->proj = gridGenerate(proj);
......@@ -5050,18 +5052,22 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
Message("grid: type = %d, size = %d, nx = %d, ny %d",
grid->type, grid->size, grid->xsize, grid->ysize);
Message("proj: type = %d, size = %d, nx = %d, ny %d",
proj.type, proj.size, proj.xsize, proj.ysize);
proj->type, proj->size, proj->xsize, proj->ysize);
}
#if defined (PROJECTION_TEST)
if ( proj.type == GRID_PROJECTION )
if ( proj->type == GRID_PROJECTION )
{
ncvars[ncvarid].gridID = varDefGrid(vlistID, &proj, 1);
projAdded = cdiVlistAddGridIfNew(vlistID, proj, 1);
ncvars[ncvarid].gridID = projAdded.Id;
copy_numeric_projatts(ncvars[ncvarid].gridID, ncvars[ncvarid].gmapid, ncvars[ncvarid].ncid);
}
else
#endif
ncvars[ncvarid].gridID = varDefGrid(vlistID, grid, 1);
{
gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 1);
ncvars[ncvarid].gridID = gridAdded.Id;
}
if ( grid->type == GRID_UNSTRUCTURED )
{
......@@ -5070,7 +5076,7 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
if ( ncvars[ncvarid].chunked ) grid_set_chunktype(grid, &ncvars[ncvarid]);
gridindex = vlistGridIndex(vlistID, ncvars[ncvarid].gridID);
int gridindex = vlistGridIndex(vlistID, ncvars[ncvarid].gridID);
streamptr->xdimID[gridindex] = xdimid;
streamptr->ydimID[gridindex] = ydimid;
if ( xdimid == -1 && ydimid == -1 && grid->size == 1 )
......@@ -5136,9 +5142,16 @@ void define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nva
}
}
grid_free(grid);
Free(grid);
grid_free(&proj);
if (!gridAdded.isNew)
{
grid_free(grid);
Free(grid);
}
if (!projAdded.isNew)
{
grid_free(proj);
Free(proj);
}
}
}
}
......
......@@ -168,6 +168,7 @@ void cgribexGetGrid(stream_t *streamptr, int *isec2, double *fsec2, int *isec4,
}
grid_init(grid);
cdiGridTypeInit(grid, gridtype, 0);
switch (gridtype)
{
case GRID_LONLAT:
......@@ -386,8 +387,9 @@ void cgribexAddRecord(stream_t * streamptr, int param, int *isec1, int *isec2, d
cgribexGetGrid(streamptr, isec2, fsec2, isec4, grid, iret);
int gridID = varDefGrid(vlistID, grid, 0);
Free(grid);
struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
int gridID = gridAdded.Id;
if (!gridAdded.isNew) Free(grid);
int zaxistype = grib1ltypeToZaxisType(ISEC1_LevelType);
......
......@@ -218,14 +218,14 @@ void extAddRecord(stream_t *streamptr, int param, int level, int xysize,
grid_t *grid = (grid_t *)Malloc(sizeof (*grid));
grid_init(grid);
grid->type = GRID_GENERIC;
grid->size = xysize;
cdiGridTypeInit(grid, GRID_GENERIC, xysize);
grid->xsize = xysize;
grid->ysize = 0;
grid->xvals = NULL;
grid->yvals = NULL;
int gridID = varDefGrid(vlistID, grid, 0);
Free(grid);
struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
int gridID = gridAdded.Id;
if (!gridAdded.isNew) Free(grid);
/*
if ( level == 0 ) leveltype = ZAXIS_SURFACE;
else leveltype = ZAXIS_GENERIC;
......
......@@ -462,8 +462,9 @@ void gribapiAddRecord(stream_t * streamptr, int param, grib_handle *gh,
grid_t *grid = (grid_t *)Malloc(sizeof (*grid));
gribapiGetGrid(gh, grid);
int gridID = varDefGrid(vlistID, grid, 0);
Free(grid);
struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
int gridID = gridAdded.Id;
if (!gridAdded.isNew) Free(grid);
int zaxistype = gribapiGetZaxisType(gribEditionNumber(gh), leveltype1);
......
......@@ -674,8 +674,7 @@ void iegAddRecord(stream_t *streamptr, int param, int *pdb, int *gdb, double *vc
grid_t *grid = (grid_t *)Malloc(sizeof (*grid));
grid_init(grid);
grid->type = gridtype;
grid->size = IEG_G_NumLon(gdb)*IEG_G_NumLat(gdb);
cdiGridTypeInit(grid, gridtype, IEG_G_NumLon(gdb)*IEG_G_NumLat(gdb));
grid->xsize = IEG_G_NumLon(gdb);
grid->ysize = IEG_G_NumLat(gdb);
grid->xinc = 0;
......@@ -747,8 +746,9 @@ void iegAddRecord(stream_t *streamptr, int param, int *pdb, int *gdb, double *vc
grid->angle = 0;
}
int gridID = varDefGrid(vlistID, grid, 0);
Free(grid);
struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
int gridID = gridAdded.Id;
if (!gridAdded.isNew) Free(grid);
int leveltype = iegGetZaxisType(IEG_P_LevelType(pdb));
......
......@@ -211,14 +211,14 @@ void srv_add_record(stream_t *streamptr, int param, int level, int xsize, int ys
grid_t *grid = (grid_t *)Malloc(sizeof (*grid));
grid_init(grid);
grid->type = GRID_GENERIC;
grid->size = xsize*ysize;
cdiGridTypeInit(grid, GRID_GENERIC, xsize*ysize);
grid->xsize = xsize;
grid->ysize = ysize;
grid->xvals = NULL;
grid->yvals = NULL;
int gridID = varDefGrid(vlistID, grid, 0);
Free(grid);
struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
int gridID = gridAdded.Id;
if (!gridAdded.isNew) Free(grid);
/*
if ( level == 0 ) leveltype = ZAXIS_SURFACE;
else leveltype = ZAXIS_GENERIC;
......
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