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