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

Replaced malloced arrays by Varray.

parent 226f0d4f
Pipeline #4886 failed with stages
in 15 minutes and 39 seconds
......@@ -20,7 +20,6 @@
#include <cdi.h>
#include "cdo_options.h"
#include "dmemory.h"
#include "process_int.h"
#include "cdo_cdi_wrapper.h"
#include "printinfo.h"
......@@ -143,21 +142,19 @@ define_level(dsets_t *pfi, int nlev)
if (nz)
{
double *zvals = (double *) Malloc(nz * sizeof(double));
Varray<double> zvals(nz);
get_dim_vals(pfi, zvals, nz, 2);
get_dim_vals(pfi, zvals.data(), nz, 2);
if (nz == 1 && IS_EQUAL(zvals[0], 0))
zaxisID = zaxisCreate(ZAXIS_SURFACE, nz);
else
{
if (nlev > 0 && nlev < nz) nz = nlev;
if (pfi->zrflg) rev_vals(zvals, nz);
if (pfi->zrflg) rev_vals(zvals.data(), nz);
zaxisID = zaxisCreate(ZAXIS_GENERIC, nz);
}
zaxisDefLevels(zaxisID, zvals);
Free(zvals);
zaxisDefLevels(zaxisID, zvals.data());
}
else
{
......@@ -178,23 +175,17 @@ Importbinary(void *process)
size_t nmiss = 0, n_nan;
int ivar;
int varID = -1, levelID, tsID;
size_t gridsize;
int datatype;
dsets_t pfi;
int64_t vdate;
int vtime;
int tcur, told, fnum;
int tmin = 0, tmax = 0;
char *ch = nullptr;
int nlevels;
int e, flag;
size_t rc, recsize;
int recoffset;
char *rec = nullptr;
size_t rc;
struct dt dtim, dtimi;
double missval;
double fmin, fmax;
double *array;
double sfclevel = 0;
cdoInitialize(process);
......@@ -222,8 +213,8 @@ Importbinary(void *process)
const auto vlistID = vlistCreate();
int *var_zaxisID = (int *) Malloc(nvars * sizeof(int));
int *var_dfrm = (int *) Malloc(nrecs * sizeof(int));
Varray<int> var_zaxisID(nvars);
Varray<int> var_dfrm(nrecs);
std::vector<RecordInfo> recList(nrecs);
int recID = 0;
......@@ -327,19 +318,15 @@ Importbinary(void *process)
const auto streamID = cdoOpenWrite(1);
cdoDefVlist(streamID, vlistID);
gridsize = pfi.dnum[0] * pfi.dnum[1];
if (pfi.flt64)
recoffset = pfi.xyhdr * 8;
else
recoffset = pfi.xyhdr * 4;
const size_t gridsize = pfi.dnum[0] * pfi.dnum[1];
int recoffset = pfi.xyhdr * (pfi.flt64 ? 8 : 4);
if (pfi.seqflg) recoffset += 4;
// recsize = pfi.gsiz*4;
recsize = pfi.gsiz * 8;
rec = (char *) Malloc(recsize);
size_t recsize = pfi.gsiz * 8;
Varray<char> rec(recsize);
array = (double *) Malloc(gridsize * sizeof(double));
Varray<double> array(gridsize);
/*
if (pfi.tmplat)
......@@ -425,7 +412,6 @@ Importbinary(void *process)
cdoAbort("Could not open file: %s", ch);
}
}
// if (pfi.tmplat) gree(ch, "312"); //US: ch is a local stack variable, which is not memory allocated by malloc()
/* file header */
if (pfi.fhdr > 0) fseeko(pfi.infile, pfi.fhdr, SEEK_SET);
......@@ -442,8 +428,8 @@ Importbinary(void *process)
for (tsID = tmin - 1; tsID < tmax; ++tsID)
{
gr2t(pfi.grvals[3], (gadouble)(tsID + 1), &dtim);
vdate = cdiEncodeDate(dtim.yr, dtim.mo, dtim.dy);
vtime = cdiEncodeTime(dtim.hr, dtim.mn, 0);
const auto vdate = cdiEncodeDate(dtim.yr, dtim.mo, dtim.dy);
const auto vtime = cdiEncodeTime(dtim.hr, dtim.mn, 0);
if (Options::cdoVerbose)
cdoPrint(" Reading timestep: %3d %s %s", tsID + 1, dateToString(vdate).c_str(), timeToString(vtime).c_str());
......@@ -451,7 +437,7 @@ Importbinary(void *process)
taxisDefVtime(taxisID, vtime);
cdoDefTimestep(streamID, tsID);
for (int recID = 0; recID < nrecs; ++recID)
for (recID = 0; recID < nrecs; ++recID)
{
/* record size depends on data type */
if (var_dfrm[recID] == 1)
......@@ -467,30 +453,30 @@ Importbinary(void *process)
recsize = pfi.flt64 ? pfi.gsiz * 8 : pfi.gsiz * 4;
}
rc = fread(rec, 1, recsize, pfi.infile);
rc = fread(rec.data(), 1, recsize, pfi.infile);
if (rc < recsize) cdoAbort("I/O error reading record=%d of timestep=%d!", recID + 1, tsID + 1);
/* convert */
if (var_dfrm[recID] == 1)
{
unsigned char *carray = (unsigned char *) (rec + recoffset);
unsigned char *carray = (unsigned char *) &rec[recoffset];
for (i = 0; i < gridsize; ++i) array[i] = (double) carray[i];
}
else if (var_dfrm[recID] == 2)
{
unsigned short *sarray = (unsigned short *) (rec + recoffset);
unsigned short *sarray = (unsigned short *) &rec[recoffset];
if (pfi.bswap) gabswp2(sarray, gridsize);
for (i = 0; i < gridsize; ++i) array[i] = (double) sarray[i];
}
else if (var_dfrm[recID] == -2)
{
short *sarray = (short *) (rec + recoffset);
short *sarray = (short *) &rec[recoffset];
if (pfi.bswap) gabswp2(sarray, gridsize);
for (i = 0; i < gridsize; ++i) array[i] = (double) sarray[i];
}
else if (var_dfrm[recID] == 4)
{
int *iarray = (int *) (rec + recoffset);
int *iarray = (int *) &rec[recoffset];
if (pfi.bswap) gabswp(iarray, gridsize);
for (i = 0; i < gridsize; ++i) array[i] = (double) iarray[i];
}
......@@ -498,13 +484,13 @@ Importbinary(void *process)
{
if (pfi.flt64)
{
double *darray = (double *) (rec + recoffset);
double *darray = (double *) &rec[recoffset];
if (pfi.bswap) gabswp(darray, gridsize);
for (i = 0; i < gridsize; ++i) array[i] = darray[i];
}
else
{
float *farray = (float *) (rec + recoffset);
float *farray = (float *) &rec[recoffset];
if (pfi.bswap) gabswp(farray, gridsize);
for (i = 0; i < gridsize; ++i) array[i] = (double) farray[i];
}
......@@ -529,8 +515,8 @@ Importbinary(void *process)
}
else
{
if (array[i] < fmin) fmin = array[i];
if (array[i] > fmax) fmax = array[i];
fmin = std::min(fmin, array[i]);
fmax = std::max(fmax, array[i]);
}
}
/*
......@@ -541,7 +527,7 @@ Importbinary(void *process)
varID = recList[recID].varID;
levelID = recList[recID].levelID;
cdoDefRecord(streamID, varID, levelID);
cdoWriteRecord(streamID, array, nmiss);
cdoWriteRecord(streamID, array.data(), nmiss);
}
}
......@@ -559,11 +545,6 @@ Importbinary(void *process)
zaxisDestroy(zaxisID);
taxisDestroy(taxisID);
Free(array);
Free(rec);
if (var_zaxisID) Free(var_zaxisID);
if (var_dfrm) Free(var_dfrm);
if (pfi.infile) fclose(pfi.infile);
cdoFinish();
......
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