Skip to content

Commit

Permalink
Reduce stack pressure (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Sep 30, 2024
1 parent e851138 commit 6b56b91
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion console/nii_dicom.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern "C" {
#define kCPUsuf " " // unknown CPU
#endif

#define kDCMdate "v1.0.20240928"
#define kDCMdate "v1.0.20240930"
#define kDCMvers kDCMdate " " kJP2suf kLSsuf kCCsuf kCPUsuf

static const int kMaxEPI3D = 1024; // maximum number of EPI images in Siemens Mosaic
Expand Down
15 changes: 9 additions & 6 deletions console/nii_dicom_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8758,13 +8758,15 @@ int saveDcm2Nii(int nConvert, struct TDCMsort dcmSort[], struct TDICOMdata dcmLi
int ret = EXIT_SUCCESS;
// check for repeated echoes - count unique number of echoes
// code below checks for multi-echoes - not required if maxNumberOfEchoes reported in PARREC
//reduce stack pressure use malloc, not "int echoNum[kMaxDTI4D];"
int *echoNum = (int *)malloc(kMaxDTI4D * sizeof(int));
// issue867 use malloc, not "int echoNum[kMaxDTI4D];"
// issue867 nVol is known and typically far smaller than kMaxDTI4D
int nVol = dcmList[indx].xyzDim[4];
int *echoNum = (int *)malloc(nVol * sizeof(int));
int echo = 1;
for (int i = 0; i < dcmList[indx].xyzDim[4]; i++)
for (int i = 0; i < nVol; i++)
echoNum[i] = 0;
echoNum[0] = 1;
for (int i = 1; i < dcmList[indx].xyzDim[4]; i++) {
for (int i = 1; i < nVol; i++) {
for (int j = 0; j < i; j++)
if (dti4D->TE[i] == dti4D->TE[j])
echoNum[i] = echoNum[j];
Expand All @@ -8773,7 +8775,6 @@ int saveDcm2Nii(int nConvert, struct TDCMsort dcmSort[], struct TDICOMdata dcmLi
echoNum[i] = echo;
}
}
free(echoNum);
if (echo > 1)
dcmList[indx].isMultiEcho = true;
// check for repeated volumes
Expand All @@ -8795,7 +8796,8 @@ int saveDcm2Nii(int nConvert, struct TDCMsort dcmSort[], struct TDICOMdata dcmLi
}
}
// bvec/bval saved for each series (real, phase, magnitude, imaginary) https://github.com/rordenlab/dcm2niix/issues/219
// Allocate memory for dti4Ds on the heap
// issue867 Allocate memory for dti4Ds on the heap
// issue867 TODO sizeof(TDTI4D) = 10mb, this could be reduced if elements reduced from kMaxDTI4D
TDTI4D *dti4Ds = (TDTI4D *)malloc(sizeof(TDTI4D));
if (dti4Ds == NULL) {
perror("Failed to allocate memory for dti4Ds");
Expand Down Expand Up @@ -8885,6 +8887,7 @@ int saveDcm2Nii(int nConvert, struct TDCMsort dcmSort[], struct TDICOMdata dcmLi
}
// Free the heap-allocated memory
free(dti4Ds);
free(echoNum);
return ret;
} // saveDcm2Nii()

Expand Down

0 comments on commit 6b56b91

Please sign in to comment.