From: <kin...@us...> - 2011-08-21 10:56:32
|
Revision: 5073 http://teem.svn.sourceforge.net/teem/?rev=5073&view=rev Author: kindlmann Date: 2011-08-21 10:56:26 +0000 (Sun, 21 Aug 2011) Log Message: ----------- various tweaks and memory leak fixes, inspired by Per I Mathisen Modified Paths: -------------- teem/trunk/src/gage/pvl.c teem/trunk/src/gage/st.c teem/trunk/src/nrrd/resampleNrrd.c teem/trunk/src/ten/chan.c teem/trunk/src/ten/fiberMethods.c teem/trunk/src/ten/tendEpireg.c Modified: teem/trunk/src/gage/pvl.c =================================================================== --- teem/trunk/src/gage/pvl.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/gage/pvl.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -60,6 +60,7 @@ static const char me[]="gagePerVolumeNew"; gagePerVolume *pvl; int ii; + airArray *mop; if (!( nin && kind )) { biffAddf(GAGE, "%s: got NULL pointer", me); @@ -69,11 +70,13 @@ biffAddf(GAGE, "%s: problem with given volume", me); return NULL; } - pvl = (gagePerVolume *)calloc(1, sizeof(gagePerVolume)); + pvl = AIR_CALLOC(1, gagePerVolume); if (!pvl) { biffAddf(GAGE, "%s: couldn't alloc gagePerVolume", me); return NULL; } + mop = airMopNew(); + airMopAdd(mop, pvl, airFree, airMopOnError); pvl->verbose = gageDefVerbose; pvl->kind = kind; GAGE_QUERY_RESET(pvl->query); @@ -84,12 +87,13 @@ } pvl->iv3 = pvl->iv2 = pvl->iv1 = NULL; pvl->lup = nrrdDLookup[nin->type]; - pvl->answer = (double *)calloc(gageKindTotalAnswerLength(kind), - sizeof(double)); - pvl->directAnswer = (double **)calloc(kind->itemMax+1, sizeof(double*)); + pvl->answer = AIR_CALLOC(gageKindTotalAnswerLength(kind), double); + airMopAdd(mop, pvl->answer, airFree, airMopOnError); + pvl->directAnswer = AIR_CALLOC(kind->itemMax+1, double*); + airMopAdd(mop, pvl->directAnswer, airFree, airMopOnError); if (!(pvl->answer && pvl->directAnswer)) { biffAddf(GAGE, "%s: couldn't alloc answer and directAnswer arrays", me); - return NULL; + airMopError(mop); return NULL; } for (ii=1; ii<=kind->itemMax; ii++) { pvl->directAnswer[ii] = pvl->answer + gageKindAnswerOffset(kind, ii); @@ -98,12 +102,13 @@ if (kind->pvlDataNew) { if (!(pvl->data = kind->pvlDataNew(kind))) { biffAddf(GAGE, "%s: double creating gagePerVolume data", me); - return NULL; + airMopError(mop); return NULL; } } else { pvl->data = NULL; } + airMopOkay(mop); return pvl; } @@ -118,30 +123,36 @@ static const char me[]="gagePerVolumeCopy"; gagePerVolume *nvl; int ii; + airArray *mop; - nvl = (gagePerVolume *)calloc(1, sizeof(gagePerVolume)); + nvl = AIR_CALLOC(1, gagePerVolume); if (!nvl) { biffAddf(GAGE, "%s: couldn't create new pervolume", me); return NULL; } + mop = airMopNew(); + airMopAdd(mop, nvl, airFree, airMopOnError); /* we should probably restrict ourselves to gage API calls, but given the constant state of gage construction, this seems much simpler. Pointers to per-pervolume-allocated arrays are fixed below */ memcpy(nvl, pvl, sizeof(gagePerVolume)); - nvl->iv3 = (double *)calloc(fd*fd*fd*nvl->kind->valLen, sizeof(double)); - nvl->iv2 = (double *)calloc(fd*fd*nvl->kind->valLen, sizeof(double)); - nvl->iv1 = (double *)calloc(fd*nvl->kind->valLen, sizeof(double)); - nvl->answer = (double *)calloc(gageKindTotalAnswerLength(nvl->kind), - sizeof(double)); - nvl->directAnswer = (double **)calloc(nvl->kind->itemMax+1, - sizeof(double*)); + nvl->iv3 = AIR_CALLOC(fd*fd*fd*nvl->kind->valLen, double); + nvl->iv2 = AIR_CALLOC(fd*fd*nvl->kind->valLen, double); + nvl->iv1 = AIR_CALLOC(fd*nvl->kind->valLen, double); + airMopAdd(mop, nvl->iv3, airFree, airMopOnError); + airMopAdd(mop, nvl->iv2, airFree, airMopOnError); + airMopAdd(mop, nvl->iv1, airFree, airMopOnError); + nvl->answer = AIR_CALLOC(gageKindTotalAnswerLength(nvl->kind), double); + airMopAdd(mop, nvl->answer, airFree, airMopOnError); + nvl->directAnswer = AIR_CALLOC(nvl->kind->itemMax+1, double*); + airMopAdd(mop, nvl->directAnswer, airFree, airMopOnError); if (!( nvl->iv3 && nvl->iv2 && nvl->iv1 && nvl->answer && nvl->directAnswer )) { biffAddf(GAGE, "%s: couldn't allocate all caches " "(fd=%u, valLen=%u, totAnsLen=%u, itemMax=%u)", me, fd, nvl->kind->valLen, gageKindTotalAnswerLength(nvl->kind), nvl->kind->itemMax); - return NULL; + airMopError(mop); return NULL; } for (ii=1; ii<=pvl->kind->itemMax; ii++) { nvl->directAnswer[ii] = nvl->answer + gageKindAnswerOffset(pvl->kind, ii); @@ -149,12 +160,15 @@ if (pvl->kind->pvlDataCopy) { if (!(nvl->data = pvl->kind->pvlDataCopy(pvl->kind, pvl->data))) { biffAddf(GAGE, "%s: double copying gagePerVolume data", me); - return NULL; + airMopError(mop); return NULL; } + /* HEY: pvlDataNix takes 2 arguments; so we ca't mop nvl->data, + so its a good thing that we created nvl->data last */ } else { nvl->data = NULL; } + airMopOkay(mop); return nvl; } Modified: teem/trunk/src/gage/st.c =================================================================== --- teem/trunk/src/gage/st.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/gage/st.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -176,29 +176,29 @@ ik0->parm[0] = iScale/zs; rad = AIR_MAX(rad, AIR_ROUNDUP(ik0->kernel->support(ik0->parm))); diam = 2*rad + 1; - ixw = (double*)calloc(diam, sizeof(double)); - iyw = (double*)calloc(diam, sizeof(double)); - izw = (double*)calloc(diam, sizeof(double)); + ixw = AIR_CALLOC(diam, double); + iyw = AIR_CALLOC(diam, double); + izw = AIR_CALLOC(diam, double); + airMopAdd(mop, ixw, airFree, airMopAlways); + airMopAdd(mop, iyw, airFree, airMopAlways); + airMopAdd(mop, izw, airFree, airMopAlways); if (!(ixw && iyw && izw)) { biffAddf(GAGE, "%s: couldn't allocate grad vector or weight buffers", me); airMopError(mop); return 1; } - airMopAdd(mop, ixw, airFree, airMopAlways); - airMopAdd(mop, iyw, airFree, airMopAlways); - airMopAdd(mop, izw, airFree, airMopAlways); /* the only reason that it is thread-safe to cache gageProbe results, without having the cache hang directly off the gageContext, is that we're doing all the probing for one context in one shot- producing an entirely volume of structure tensors with one function call */ - gradCache = (double*)calloc(3*GAGE_CACHE_LEN, sizeof(double)); - coordCache = (int*)calloc(3*GAGE_CACHE_LEN, sizeof(int)); + gradCache = AIR_CALLOC(3*GAGE_CACHE_LEN, double); + coordCache = AIR_CALLOC(3*GAGE_CACHE_LEN, int); + airMopAdd(mop, gradCache, airFree, airMopAlways); + airMopAdd(mop, coordCache, airFree, airMopAlways); if (!(gradCache && coordCache)) { biffAddf(GAGE, "%s: couldn't allocate caches", me); airMopError(mop); return 1; } - airMopAdd(mop, gradCache, airFree, airMopAlways); - airMopAdd(mop, coordCache, airFree, airMopAlways); for (ixi=0; ixi<GAGE_CACHE_LEN; ixi++) { coordCache[3*ixi + 0] = -1; coordCache[3*ixi + 1] = -1; @@ -229,7 +229,7 @@ } airMopAdd(mop, nout, (airMopper)nrrdEmpty, airMopOnError); - out = (double *)nout->data; + out = AIR_CAST(double *, nout->data); for (ozi=0; ozi<osz; ozi++) { fprintf(stderr, "%s: z = %d/%d\n", me, ozi+1, osz); for (oyi=0; oyi<osy; oyi++) { Modified: teem/trunk/src/nrrd/resampleNrrd.c =================================================================== --- teem/trunk/src/nrrd/resampleNrrd.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/nrrd/resampleNrrd.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -327,12 +327,16 @@ fprintf(stderr, "!%s(%d): dotLen = %d\n", me, d, dotLen); */ - weight = (nrrdResample_t*)calloc(sizeOut*dotLen, sizeof(nrrdResample_t)); - index = (int*)calloc(sizeOut*dotLen, sizeof(int)); - if (!(weight && index)) { - biffAddf(NRRD, "%s: can't allocate weight and index arrays", me); + weight = AIR_CALLOC(sizeOut*dotLen, nrrdResample_t); + if (!weight) { + biffAddf(NRRD, "%s: can't allocate weight array", me); *weightP = NULL; *indexP = NULL; return 0; } + index = AIR_CALLOC(sizeOut*dotLen, int); + if (!index) { + biffAddf(NRRD, "%s: can't allocate index arrays", me); + *weightP = NULL; *indexP = NULL; return 0; + } /* calculate sample locations and do first pass on indices */ halfLen = dotLen/2; Modified: teem/trunk/src/ten/chan.c =================================================================== --- teem/trunk/src/ten/chan.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/ten/chan.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -175,7 +175,7 @@ mop = airMopNew(); skipArr = airArrayNew((void**)skipP, skipNumP, sizeof(unsigned int), 16); airMopAdd(mop, skipArr, (airMopper)airArrayNix, airMopAlways); - skipLut = AIR_CAST(unsigned int*, calloc(dwiNum, sizeof(unsigned int))); + skipLut = AIR_CALLOC(dwiNum, unsigned int); airMopAdd(mop, skipLut, airFree, airMopAlways); if (!skipLut) { biffAddf(TEN, "%s: couldn't allocate skip LUT", me); @@ -679,16 +679,16 @@ biffAddf(TEN, "%s: trouble computing estimation matrix", me); airMopError(mop); return 1; } - vbuf = (double*)calloc(knownB0 ? DD-1 : DD, sizeof(double)); - dwi1 = (float*)calloc(DD, sizeof(float)); - dwi2 = (float*)calloc(knownB0 ? DD : DD+1, sizeof(float)); + vbuf = AIR_CALLOC(knownB0 ? DD-1 : DD, double); + dwi1 = AIR_CALLOC(DD, float); + dwi2 = AIR_CALLOC(knownB0 ? DD : DD+1, float); + airMopAdd(mop, vbuf, airFree, airMopAlways); + airMopAdd(mop, dwi1, airFree, airMopAlways); + airMopAdd(mop, dwi2, airFree, airMopAlways); if (!(vbuf && dwi1 && dwi2)) { biffAddf(TEN, "%s: couldn't allocate temp buffers", me); airMopError(mop); return 1; } - airMopAdd(mop, vbuf, airFree, airMopAlways); - airMopAdd(mop, dwi1, airFree, airMopAlways); - airMopAdd(mop, dwi2, airFree, airMopAlways); if (!AIR_EXISTS(thresh)) { airMopAdd(mop, ncrop=nrrdNew(), (airMopper)nrrdNuke, airMopAlways); airMopAdd(mop, nhist=nrrdNew(), (airMopper)nrrdNuke, airMopAlways); Modified: teem/trunk/src/ten/fiberMethods.c =================================================================== --- teem/trunk/src/ten/fiberMethods.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/ten/fiberMethods.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -56,7 +56,7 @@ tenFiberSingleNew() { tenFiberSingle *ret; - ret = AIR_CAST(tenFiberSingle *, calloc(1, sizeof(tenFiberSingle))); + ret = AIR_CALLOC(1, tenFiberSingle); if (ret) { tenFiberSingleInit(ret); } @@ -81,16 +81,19 @@ static const char me[]="_tenFiberContextCommonNew"; tenFiberContext *tfx; gageKind *kind; + airArray *mop; - if (!( tfx = (tenFiberContext *)calloc(1, sizeof(tenFiberContext)) )) { + if (!( tfx = AIR_CALLOC(1, tenFiberContext) )) { biffAddf(TEN, "%s: couldn't allocate new context", me); return NULL; } + mop = airMopNew(); + airMopAdd(mop, tfx, airFree, airMopOnError); if (useDwi) { Nrrd *ngrad=NULL, *nbmat=NULL; double bval=0; - unsigned int *skip, skipNum; + unsigned int *skip=NULL, skipNum; tfx->useDwi = AIR_TRUE; /* default fiber type */ @@ -98,19 +101,23 @@ if (tenDWMRIKeyValueParse(&ngrad, &nbmat, &bval, &skip, &skipNum, vol)) { biffAddf(TEN, "%s: trouble parsing DWI info", me ); - return NULL; + airMopError(mop); return NULL; } + airMopAdd(mop, ngrad, (airMopper)nrrdNuke, airMopOnError); + airMopAdd(mop, nbmat, (airMopper)nrrdNuke, airMopOnError); + airMopAdd(mop, skip, airFree, airMopOnError); if (skipNum) { biffAddf(TEN, "%s: sorry, can't do DWI skipping here", me); - return NULL; + airMopError(mop); return NULL; } kind = tenDwiGageKindNew(); + airMopAdd(mop, kind, (airMopper)tenDwiGageKindNix, airMopOnError); if (tenDwiGageKindSet(kind, thresh, soft, bval, valueMin, ngrad, NULL, ten1method, ten2method, 42)) { biffAddf(TEN, "%s: trouble setting DWI kind", me); - return NULL; + airMopError(mop); return NULL; } } else { /* it should be a tensor volume */ @@ -119,28 +126,31 @@ tfx->fiberType = tenFiberTypeUnknown; if (tenTensorCheck(vol, nrrdTypeUnknown, AIR_TRUE, AIR_TRUE)) { biffAddf(TEN, "%s: didn't get a tensor volume", me); - return NULL; + airMopError(mop); return NULL; } kind = tenGageKind; } - if ( !(tfx->gtx = gageContextNew()) - || !(tfx->pvl = gagePerVolumeNew(tfx->gtx, vol, kind)) - || (gagePerVolumeAttach(tfx->gtx, tfx->pvl)) ) { + tfx->gtx = gageContextNew(); + airMopAdd(mop, tfx->gtx, (airMopper)gageContextNix, airMopOnError); + tfx->pvl = gagePerVolumeNew(tfx->gtx, vol, kind); + airMopAdd(mop, tfx->pvl, (airMopper)gagePerVolumeNix, airMopOnError); + if (!( tfx->gtx && tfx->pvl && !gagePerVolumeAttach(tfx->gtx, tfx->pvl) )) { biffMovef(TEN, GAGE, "%s: gage trouble", me); - free(tfx); return NULL; + airMopError(mop); return NULL; } tfx->nin = vol; tfx->ksp = nrrdKernelSpecNew(); + airMopAdd(mop, tfx->ksp, (airMopper)nrrdKernelSpecNix, airMopOnError); if (nrrdKernelSpecParse(tfx->ksp, tenDefFiberKernel)) { biffMovef(TEN, NRRD, "%s: couldn't parse tenDefFiberKernel \"%s\"", me, tenDefFiberKernel); - return NULL; + airMopError(mop); return NULL; } if (tenFiberKernelSet(tfx, tfx->ksp->kernel, tfx->ksp->parm)) { biffAddf(TEN, "%s: couldn't set default kernel", me); - return NULL; + airMopError(mop); return NULL; } tfx->fiberProbeItem = 0; /* unknown for any gageKind */ /* looks to GK like GK says that we must set some stop criterion */ @@ -190,7 +200,8 @@ /* ... don't really see the point of initializing the ten2 stuff here; its properly done in tenFiberTraceSet() ... */ tfx->radius = AIR_NAN; - + + airMopOkay(mop); return tfx; } @@ -828,7 +839,7 @@ fprintf(stderr, "!%s: sorry, can't copy DWI contexts; bye.\n", me); exit(1); } - tfx = (tenFiberContext *)calloc(1, sizeof(tenFiberContext)); + tfx = AIR_CALLOC(1, tenFiberContext); memcpy(tfx, oldTfx, sizeof(tenFiberContext)); tfx->ksp = nrrdKernelSpecCopy(oldTfx->ksp); tfx->gtx = gageContextCopy(oldTfx->gtx); Modified: teem/trunk/src/ten/tendEpireg.c =================================================================== --- teem/trunk/src/ten/tendEpireg.c 2011-08-21 10:10:57 UTC (rev 5072) +++ teem/trunk/src/ten/tendEpireg.c 2011-08-21 10:56:26 UTC (rev 5073) @@ -146,16 +146,16 @@ } airMopAdd(mop, ngrad, (airMopper)nrrdNuke, airMopAlways); - nout3D = (Nrrd **)calloc(ninLen, sizeof(Nrrd *)); + nout3D = AIR_CALLOC(ninLen, Nrrd *); + airMopAdd(mop, nout3D, airFree, airMopAlways); nout4D = nrrdNew(); - buff = (char *)calloc(airStrlen(outS) + 10, sizeof(char)); + airMopAdd(mop, nout4D, (airMopper)nrrdNuke, airMopAlways); + buff = AIR_CALLOC(airStrlen(outS) + 10, char); + airMopAdd(mop, buff, airFree, airMopAlways); if (!( nout3D && nout4D && buff )) { fprintf(stderr, "%s: couldn't allocate buffers", me); airMopError(mop); return 1; } - airMopAdd(mop, nout4D, (airMopper)nrrdNuke, airMopAlways); - airMopAdd(mop, nout3D, airFree, airMopAlways); - airMopAdd(mop, buff, airFree, airMopAlways); for (ni=0; ni<ninLen; ni++) { nout3D[ni]=nrrdNew(); airMopAdd(mop, nout3D[ni], (airMopper)nrrdNuke, airMopAlways); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |