This is a temporary fix to the memory alignment problems on some
platforms. In the main TNG repository the whole I/O system is
rewritten to address this problem. This fix is to avoid the
problems without making too large changes.
There are also some pointers that were not reset after memory
was freed. This is also fixed in here.
This commit does not correspond to any commit in the TNG
repository.
Fixes #1542, #1546, #1547 and #1559.
Change-Id: I90a6406cccbc43fd57d4423c2b661019cf7763e8
int nalgo;
int new_len;
int *alt_algo = 0;
int nalgo;
int new_len;
int *alt_algo = 0;
- char *dest, *temp;
- int64_t algo_find_n_frames;
+ char *dest, *temp, *temp_data_contents;
+ int64_t algo_find_n_frames, compressed_len;
unsigned long offset;
float f_precision;
double d_precision;
unsigned long offset;
float f_precision;
double d_precision;
f_precision = 1/(float)tng_data->compression_precision;
d_precision = 1/tng_data->compression_precision;
f_precision = 1/(float)tng_data->compression_precision;
d_precision = 1/tng_data->compression_precision;
+ compressed_len = block->block_contents_size - (int64_t)((char *)start_pos - (char *)block->block_contents);
+ temp_data_contents = malloc(compressed_len);
+ if(!temp_data_contents)
+ {
+ fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
+ compressed_len, __FILE__, __LINE__);
+ return(TNG_CRITICAL);
+ }
+
+ memcpy(temp_data_contents, (char *)start_pos, compressed_len);
+
if(block->id == TNG_TRAJ_POSITIONS)
{
/* If there is only one frame in this frame set and there might be more
if(block->id == TNG_TRAJ_POSITIONS)
{
/* If there is only one frame in this frame set and there might be more
alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_pos_float_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_pos_float_find_algo((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision,
0, alt_algo,
(int)n_frames,
f_precision,
0, alt_algo,
- dest = tng_compress_pos_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_pos_find_algo((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision,
0, alt_algo,
(int)n_frames,
d_precision,
0, alt_algo,
sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_pos_float_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_pos_float_find_algo((float *)temp_data_contents, (int)n_particles,
(int)algo_find_n_frames,
f_precision,
0, tng_data->
(int)algo_find_n_frames,
f_precision,
0, tng_data->
if(algo_find_n_frames < n_frames)
{
if(algo_find_n_frames < n_frames)
{
- dest = tng_compress_pos_float(start_pos, (int)n_particles,
+ dest = tng_compress_pos_float((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision,
0, tng_data->compress_algo_pos,
(int)n_frames,
f_precision,
0, tng_data->compress_algo_pos,
- dest = tng_compress_pos_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_pos_find_algo((double *)temp_data_contents, (int)n_particles,
(int)algo_find_n_frames,
d_precision,
0, tng_data->
(int)algo_find_n_frames,
d_precision,
0, tng_data->
if(algo_find_n_frames < n_frames)
{
if(algo_find_n_frames < n_frames)
{
- dest = tng_compress_pos(start_pos, (int)n_particles,
+ dest = tng_compress_pos((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision, 0,
tng_data->compress_algo_pos,
(int)n_frames,
d_precision, 0,
tng_data->compress_algo_pos,
{
if(type == TNG_FLOAT_DATA)
{
{
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_pos_float(start_pos, (int)n_particles,
+ dest = tng_compress_pos_float((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision, 0,
tng_data->compress_algo_pos, &new_len);
}
else
{
(int)n_frames,
f_precision, 0,
tng_data->compress_algo_pos, &new_len);
}
else
{
- dest = tng_compress_pos(start_pos, (int)n_particles,
+ dest = tng_compress_pos((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision, 0,
tng_data->compress_algo_pos,
(int)n_frames,
d_precision, 0,
tng_data->compress_algo_pos,
alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
alt_algo=malloc(nalgo * sizeof *tng_data->compress_algo_pos);
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_vel_float_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_vel_float_find_algo((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision,
0, alt_algo,
(int)n_frames,
f_precision,
0, alt_algo,
- dest = tng_compress_vel_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_vel_find_algo((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision,
0, alt_algo,
(int)n_frames,
d_precision,
0, alt_algo,
if(type == TNG_FLOAT_DATA)
{
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_vel_float_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_vel_float_find_algo((float *)temp_data_contents, (int)n_particles,
(int)algo_find_n_frames,
f_precision,
0, tng_data->
(int)algo_find_n_frames,
f_precision,
0, tng_data->
&new_len);
if(algo_find_n_frames < n_frames)
{
&new_len);
if(algo_find_n_frames < n_frames)
{
- dest = tng_compress_vel_float(start_pos, (int)n_particles,
+ dest = tng_compress_vel_float((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision,
0, tng_data->compress_algo_vel,
(int)n_frames,
f_precision,
0, tng_data->compress_algo_vel,
- dest = tng_compress_vel_find_algo(start_pos, (int)n_particles,
+ dest = tng_compress_vel_find_algo((double *)temp_data_contents, (int)n_particles,
(int)algo_find_n_frames,
d_precision,
0, tng_data->
(int)algo_find_n_frames,
d_precision,
0, tng_data->
&new_len);
if(algo_find_n_frames < n_frames)
{
&new_len);
if(algo_find_n_frames < n_frames)
{
- dest = tng_compress_vel(start_pos, (int)n_particles,
+ dest = tng_compress_vel((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision,
0, tng_data->compress_algo_vel,
(int)n_frames,
d_precision,
0, tng_data->compress_algo_vel,
{
if(type == TNG_FLOAT_DATA)
{
{
if(type == TNG_FLOAT_DATA)
{
- dest = tng_compress_vel_float(start_pos, (int)n_particles,
+ dest = tng_compress_vel_float((float *)temp_data_contents, (int)n_particles,
(int)n_frames,
f_precision,
0, tng_data->
(int)n_frames,
f_precision,
0, tng_data->
- dest = tng_compress_vel(start_pos, (int)n_particles,
+ dest = tng_compress_vel((double *)temp_data_contents, (int)n_particles,
(int)n_frames,
d_precision,
0, tng_data->
(int)n_frames,
d_precision,
0, tng_data->
else
{
fprintf(stderr, "TNG library: Can only compress positions and velocities using TNG-MF1 algorithms.\n");
else
{
fprintf(stderr, "TNG library: Can only compress positions and velocities using TNG-MF1 algorithms.\n");
+ free(temp_data_contents);
block->block_contents_size = new_len + offset;
block->block_contents_size = new_len + offset;
+ free(temp_data_contents);
+
temp = realloc(block->block_contents, block->block_contents_size);
if(!temp)
{
temp = realloc(block->block_contents, block->block_contents_size);
if(!temp)
{
void *start_pos,
const unsigned long uncompressed_len)
{
void *start_pos,
const unsigned long uncompressed_len)
{
+ char *temp, *temp_data_contents;
double *d_dest = 0;
float *f_dest = 0;
unsigned long offset;
double *d_dest = 0;
float *f_dest = 0;
unsigned long offset;
+ temp_data_contents = malloc(uncompressed_len);
+ if(!temp_data_contents)
+ {
+ fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
+ uncompressed_len, __FILE__, __LINE__);
+ return(TNG_CRITICAL);
+ }
+
+ memcpy(temp_data_contents, (char *)start_pos, uncompressed_len);
+
if(type == TNG_FLOAT_DATA)
{
f_dest = malloc(uncompressed_len);
if(type == TNG_FLOAT_DATA)
{
f_dest = malloc(uncompressed_len);
{
fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
uncompressed_len, __FILE__, __LINE__);
{
fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
uncompressed_len, __FILE__, __LINE__);
+ free(temp_data_contents);
- result = tng_compress_uncompress_float(start_pos, f_dest);
+ result = tng_compress_uncompress_float(temp_data_contents, f_dest);
{
fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
uncompressed_len, __FILE__, __LINE__);
{
fprintf(stderr, "TNG library: Cannot allocate memory (%lu bytes). %s: %d\n",
uncompressed_len, __FILE__, __LINE__);
+ free(temp_data_contents);
- result = tng_compress_uncompress(start_pos, d_dest);
+ result = tng_compress_uncompress(temp_data_contents, d_dest);
}
if(result == 1)
{
fprintf(stderr, "TNG library: Cannot uncompress TNG compressed block.\n");
}
if(result == 1)
{
fprintf(stderr, "TNG library: Cannot uncompress TNG compressed block.\n");
+ free(temp_data_contents);
}
fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
block->block_contents_size, __FILE__, __LINE__);
}
fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n",
block->block_contents_size, __FILE__, __LINE__);
+ free(temp_data_contents);
block->block_contents = temp;
block->block_contents = temp;
+ free(temp_data_contents);
if(d_dest)
{
free(d_dest);
if(d_dest)
{
free(d_dest);
*n = tng_data->n_trajectory_frame_sets = cnt;
*frame_set = orig_frame_set;
*n = tng_data->n_trajectory_frame_sets = cnt;
*frame_set = orig_frame_set;
+ /* The mapping block in the original frame set has been freed when reading
+ * other frame sets. */
+ frame_set->mappings = 0;
+ frame_set->n_mapping_blocks = 0;
fseek(tng_data->input_file,
(long)tng_data->first_trajectory_frame_set_input_file_pos,
fseek(tng_data->input_file,
(long)tng_data->first_trajectory_frame_set_input_file_pos,