summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tng_io.h8
-rw-r--r--src/lib/tng_io.c65
2 files changed, 45 insertions, 28 deletions
diff --git a/include/tng_io.h b/include/tng_io.h
index 16ba849..c9172cd 100644
--- a/include/tng_io.h
+++ b/include/tng_io.h
@@ -3236,7 +3236,7 @@ tng_function_status DECLSPECDLLEXPORT tng_util_time_of_frame_get
const int64_t frame_nr,
double *time);
-/**
+/*
* @brief High-level function for getting the molecules in the mol system.
* @param tng_data is the trajectory containing the mol system.
* @param n_mols is set to the number of molecules in the system.
@@ -4629,9 +4629,11 @@ tng_function_status DECLSPECDLLEXPORT tng_util_compression_current_frame_get
* @param next_frame will be set to the next frame with data.
* @param n_data_blocks_in_next_frame is set to the number of data blocks with
* data for next_frame.
- * @param data_block_ids_in_next_frame is an array (of length
+ * @param data_block_ids_in_next_frame is set to an array (of length
* n_data_blocks_in_next_frame) that lists the data block IDs with data for
- * next_frame.
+ * next_frame. The array is created by this function allocated.
+ * The memory must be freed by the client afterwards or
+ * there will be a memory leak.
* @pre \code tng_data != 0 \endcode The trajectory container (tng_data)
* must be initialised before using it.
* @pre \code next_frame != 0 \endcode The pointer to the next frame must not
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c
index abb0592..658e474 100644
--- a/src/lib/tng_io.c
+++ b/src/lib/tng_io.c
@@ -8171,7 +8171,7 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_atom_of_index_get
{
tng_chain_t chain;
tng_molecule_t molecule;
-
+
(void) tng_data;
TNG_ASSERT(residue, "TNG library: residue must not be a NULL pointer.");
TNG_ASSERT(atom, "TNG library: atom must not be a NULL pointer.");
@@ -8183,13 +8183,13 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_atom_of_index_get
}
chain = residue->chain;
molecule = chain->molecule;
-
+
if(index + residue->atoms_offset >= molecule->n_atoms)
{
*atom = 0;
return(TNG_FAILURE);
}
-
+
*atom = &molecule->atoms[residue->atoms_offset + index];
return(TNG_SUCCESS);
}
@@ -17777,10 +17777,12 @@ tng_function_status DECLSPECDLLEXPORT tng_util_next_frame_present_data_blocks_fi
TNG_ASSERT(tng_data, "TNG library: Trajectory container not properly setup.");
TNG_ASSERT(next_frame, "TNG library: The pointer to the next frame must not be NULL.");
- TNG_ASSERT(data_block_ids_in_next_frame, "TNG library: The pointer to the list of data block IDs must not be NULL.");
+ TNG_ASSERT(data_block_ids_in_next_frame == 0, "TNG library: The pointer to the list of data block IDs must be NULL.");
+
if(n_requested_data_block_ids)
{
TNG_ASSERT(requested_data_block_ids, "TNG library: If the number of requested data blocks is > 0 then the array of data block IDs must not be NULL.");
+ *data_block_ids_in_next_frame = malloc(sizeof(int64_t) * n_requested_data_block_ids);
}
frame_set = &tng_data->current_trajectory_frame_set;
@@ -17854,18 +17856,25 @@ tng_function_status DECLSPECDLLEXPORT tng_util_next_frame_present_data_blocks_fi
{
*n_data_blocks_in_next_frame += 1;
}
- temp = realloc(*data_block_ids_in_next_frame, sizeof(int64_t) *
- (*n_data_blocks_in_next_frame));
- if(!temp)
+ if(n_requested_data_block_ids > 0)
{
- printf("TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
- sizeof(int64_t) * (*n_data_blocks_in_next_frame),
- __FILE__, __LINE__);
- free(*data_block_ids_in_next_frame);
- *data_block_ids_in_next_frame = 0;
- return(TNG_CRITICAL);
+ temp = realloc(*data_block_ids_in_next_frame, sizeof(int64_t) *
+ (*n_data_blocks_in_next_frame));
+ if(!temp)
+ {
+ printf("TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ sizeof(int64_t) * (*n_data_blocks_in_next_frame),
+ __FILE__, __LINE__);
+ free(*data_block_ids_in_next_frame);
+ *data_block_ids_in_next_frame = 0;
+ return(TNG_CRITICAL);
+ }
+ *data_block_ids_in_next_frame = temp;
+ }
+ else
+ {
+ TNG_ASSERT(*n_data_blocks_in_next_frame <= n_requested_data_block_ids, "TNG library: Array of data block IDs out of bounds");
}
- *data_block_ids_in_next_frame = temp;
*data_block_ids_in_next_frame[*n_data_blocks_in_next_frame-1] = block_id;
@@ -17925,19 +17934,25 @@ tng_function_status DECLSPECDLLEXPORT tng_util_next_frame_present_data_blocks_fi
{
*n_data_blocks_in_next_frame += 1;
}
- temp = realloc(*data_block_ids_in_next_frame, sizeof(int64_t) *
- (*n_data_blocks_in_next_frame));
- if(!temp)
+ if(n_requested_data_block_ids > 0)
{
- printf("TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
- sizeof(int64_t) * (*n_data_blocks_in_next_frame),
- __FILE__, __LINE__);
- free(*data_block_ids_in_next_frame);
- *data_block_ids_in_next_frame = 0;
- return(TNG_CRITICAL);
+ temp = realloc(*data_block_ids_in_next_frame, sizeof(int64_t) *
+ (*n_data_blocks_in_next_frame));
+ if(!temp)
+ {
+ printf("TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ sizeof(int64_t) * (*n_data_blocks_in_next_frame),
+ __FILE__, __LINE__);
+ free(*data_block_ids_in_next_frame);
+ *data_block_ids_in_next_frame = 0;
+ return(TNG_CRITICAL);
+ }
+ *data_block_ids_in_next_frame = temp;
+ }
+ else
+ {
+ TNG_ASSERT(*n_data_blocks_in_next_frame <= n_requested_data_block_ids, "TNG library: Array of data block IDs out of bounds");
}
- *data_block_ids_in_next_frame = temp;
-
*data_block_ids_in_next_frame[*n_data_blocks_in_next_frame-1] = block_id;
min_diff = frame_diff;
contact: Jan Huwald // Impressum