Skip to content

Commit

Permalink
Create 2D arrays on the heap in a different way (#1169)
Browse files Browse the repository at this point in the history
* Create 2D arrays on the heap by malloc'ing `struct { TYPE arr[ROWS][COLS];
}`.  This avoids the double-indirection through pointers and the
additional memory of H5TEST_ALLOCATE_2D_ARRAY().

This change will safely quiet the cast warning that PR #1129 was
intended to fix.

* Committing clang-format changes

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
gnuoyd and github-actions[bot] authored Nov 23, 2021
1 parent b9e4dee commit 462e9a3
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 437 deletions.
59 changes: 9 additions & 50 deletions test/h5test.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,60 +172,19 @@ H5TEST_DLLVAR MPI_Info h5_io_info_g; /* MPI INFO object for IO */
#define H5_EXCLUDE_MULTIPART_DRIVERS 0x01
#define H5_EXCLUDE_NON_MULTIPART_DRIVERS 0x02

/* Macros to create and fill 2D arrays with a single heap allocation.
* These can be used to replace large stack and global arrays which raise
* warnings.
*
* The macros make a single heap allocation large enough to hold all the
* pointers and the data elements. The first part of the allocation holds
* the pointers, and the second part holds the data as a contiguous block
* in row-major order.
*
* To pass the data block to calls like H5Dread(), pass a pointer to the
* first array element as the data pointer (e.g., array[0] in a 2D array).
*
* The fill macro just fills the array with an increasing count value.
*
* Usage:
*
* int **array;
*
* H5TEST_ALLOCATE_2D_ARRAY(array, int, 5, 10);
*
* H5TEST_FILL_2D_ARRAY(array, int, 5, 10);
*
* (do stuff)
*
* HDfree(array);
/* Fill an array on the heap with an increasing count value. BUF
* is expected to point to a `struct { TYPE arr[...][...]; }`.
*/
#define H5TEST_ALLOCATE_2D_ARRAY(ARR, TYPE, DIMS_I, DIMS_J) \
do { \
/* Prefix with h5taa to avoid shadow warnings */ \
size_t h5taa_pointers_size = 0; \
size_t h5taa_data_size = 0; \
int h5taa_i; \
\
h5taa_pointers_size = (DIMS_I) * sizeof(TYPE *); \
h5taa_data_size = (DIMS_I) * (DIMS_J) * sizeof(TYPE); \
\
ARR = (TYPE **)HDmalloc(h5taa_pointers_size + h5taa_data_size); \
\
ARR[0] = (TYPE *)(ARR + (DIMS_I)); \
\
for (h5taa_i = 1; h5taa_i < (DIMS_I); h5taa_i++) \
ARR[h5taa_i] = ARR[h5taa_i - 1] + (DIMS_J); \
} while (0)

#define H5TEST_FILL_2D_ARRAY(ARR, TYPE, DIMS_I, DIMS_J) \
#define H5TEST_FILL_2D_HEAP_ARRAY(BUF, TYPE) \
do { \
/* Prefix with h5tfa to avoid shadow warnings */ \
int h5tfa_i = 0; \
int h5tfa_j = 0; \
TYPE h5tfa_count = 0; \
size_t h5tfa_i = 0; \
size_t h5tfa_j = 0; \
TYPE h5tfa_count = 0; \
\
for (h5tfa_i = 0; h5tfa_i < (DIMS_I); h5tfa_i++) \
for (h5tfa_j = 0; h5tfa_j < (DIMS_J); h5tfa_j++) { \
ARR[h5tfa_i][h5tfa_j] = h5tfa_count; \
for (h5tfa_i = 0; h5tfa_i < NELMTS((BUF)->arr); h5tfa_i++) \
for (h5tfa_j = 0; h5tfa_j < NELMTS((BUF)->arr[0]); h5tfa_j++) { \
(BUF)->arr[h5tfa_i][h5tfa_j] = h5tfa_count; \
h5tfa_count++; \
} \
} while (0)
Expand Down
Loading

0 comments on commit 462e9a3

Please sign in to comment.