Skip to content

Commit

Permalink
converting to malloc instead of VLAs to move from stack to heap
Browse files Browse the repository at this point in the history
  • Loading branch information
edhartnett committed Mar 7, 2019
1 parent 163da37 commit fc2fc45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/clib/pioc_sc.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ PIO_Offset GCDblocksize(int arrlen, const PIO_Offset *arr_in)
PIO_Offset bsize; /* Size of the block. */
PIO_Offset bsizeg; /* Size of gap block. */
PIO_Offset blklensum; /* Sum of all block lengths. */
PIO_Offset del_arr[arrlen - 1]; /* Array of deltas between adjacent elements in arr_in. */
PIO_Offset loc_arr[arrlen - 1];
PIO_Offset *del_arr; /* Array of deltas between adjacent elements in arr_in. */
PIO_Offset *loc_arr;

/* Check inputs. */
pioassert(arrlen > 0 && arr_in, "invalid input", __FILE__, __LINE__);

/* Allocate arrays. */
if (!(loc_arr = malloc(sizeof(PIO_Offset) * (arrlen - 1))))
return PIO_ENOMEM;
if (!(del_arr = malloc(sizeof(PIO_Offset) * (arrlen - 1))))
return PIO_ENOMEM;

/* Count the number of contiguous blocks in arr_in. If any if
these blocks is of size 1, we are done and can return.
Otherwise numtimes is the number of blocks. */
Expand Down Expand Up @@ -226,6 +232,9 @@ PIO_Offset GCDblocksize(int arrlen, const PIO_Offset *arr_in)
bsize = lgcd(bsize, arr_in[0]);
}

free(loc_arr);
free(del_arr);

return bsize;
}

Expand Down
21 changes: 17 additions & 4 deletions tests/cunit/test_perf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,16 @@ int test_darray(int iosysid, int ioid, int num_flavors, int *flavor, int my_rank
PIO_Offset arraylen = EXPECTED_MAPLEN;
int int_fillvalue = NC_FILL_INT;
void *fillvalue = NULL;
int test_data[arraylen];
int test_data2[arraylen];
int test_data_in[arraylen];
int *test_data;
int *test_data2;
int *test_data_in;

if (!(test_data = malloc(sizeof(int) * arraylen)))
ERR(PIO_ENOMEM);
if (!(test_data2 = malloc(sizeof(int) * arraylen)))
ERR(PIO_ENOMEM);
if (!(test_data_in = malloc(sizeof(int) * arraylen)))
ERR(PIO_ENOMEM);

/* Initialize some data. */
for (int f = 0; f < arraylen; f++)
Expand All @@ -146,7 +153,8 @@ int test_darray(int iosysid, int ioid, int num_flavors, int *flavor, int my_rank
for (int fmt = 0; fmt < num_flavors; fmt++)
{
/* Create the filename. */
sprintf(filename, "data_%s_iotype_%d.nc", TEST_NAME, flavor[fmt]);
/* sprintf(filename, "data_%s_iotype_%d.nc", TEST_NAME, flavor[fmt]); */
sprintf(filename, "data__iotype_.nc");

/* Create the netCDF output file. */
if ((ret = PIOc_createfile(iosysid, &ncid, &flavor[fmt], filename, PIO_CLOBBER)))
Expand Down Expand Up @@ -223,6 +231,11 @@ int test_darray(int iosysid, int ioid, int num_flavors, int *flavor, int my_rank
if ((ret = PIOc_closefile(ncid2)))
ERR(ret);
}

free(test_data);
free(test_data2);
free(test_data_in);

return PIO_NOERR;
}

Expand Down

0 comments on commit fc2fc45

Please sign in to comment.