Skip to content

Commit 09058db

Browse files
committed
Use a single pointer for offsets storage
it's cleaner and easier to handle in the cython wrapper. I also reworded the comments to be better understandable.
1 parent ffb1798 commit 09058db

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

package/MDAnalysis/lib/formats/include/xdrfile_xtc.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern "C" {
5050
extern int read_xtc_natoms(char *fn, int *natoms);
5151

5252
/* Seek through trajectory counting and indexing frames */
53-
extern int read_xtc_n_frames(char *fn, int *n_frames, int64_t **offsets);
53+
extern int read_xtc_n_frames(char *fn, int *n_frames, int64_t *offsets);
5454

5555
/* Read one frame of an open xtc file */
5656
extern int read_xtc(XDRFILE *xd, int natoms, int *step, float *time, matrix box,
@@ -66,11 +66,17 @@ extern int write_xtc(XDRFILE *xd, int natoms, int step, float time, matrix box,
6666
#define XTC_SHORTHEADER_SIZE (20 + DIM * DIM * 4)
6767
/* Short XTCs store each coordinate as a 32-bit float. */
6868
#define XTC_SHORT_BYTESPERATOM 12
69-
/* XTC header fields until frame bytes: *** only for trajectories of more than 9
70-
* atoms! *** */
71-
/* magic natoms step time DIM*DIM_box_vecs natoms prec DIM_min_xyz DIM_max_xyz
72-
* smallidx */
73-
#define XTC_HEADER_SIZE (DIM * DIM * 4 + DIM * 2 + 46)
69+
/* Number of bytes until we can read the frame bytes in a frame, for frames
70+
* containing more then 9 atoms.
71+
*
72+
* This header is composed of the read header and the coordinate section header.
73+
* The 'real' header contains the following entries: magic, natoms, step, time.
74+
* The coordinate section header contains: 3x3 box, natoms, prec, 3 min_xyz, 3
75+
* max_xyz, smallidx.
76+
* Each of these entries is 4 byte long. In total that is 22
77+
* entries. So we need to skip 88 bytes.
78+
*/
79+
#define XTC_HEADER_SIZE 88
7480

7581
#ifdef __cplusplus
7682
}

package/MDAnalysis/lib/formats/src/xdrfile_xtc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int read_xtc(XDRFILE *xd, int natoms, int *step, float *time, matrix box,
113113
return exdrOK;
114114
}
115115

116-
int read_xtc_n_frames(char *fn, int *n_frames, int64_t **offsets) {
116+
int read_xtc_n_frames(char *fn, int *n_frames, int64_t *offsets) {
117117
XDRFILE *xd;
118118
int framebytes, natoms, step;
119119
float time;
@@ -141,10 +141,10 @@ int read_xtc_n_frames(char *fn, int *n_frames, int64_t **offsets) {
141141
*n_frames = filesize / framebytes; /* Should we complain if framesize
142142
doesn't divide filesize? */
143143
/* Allocate memory for the frame index array */
144-
if ((*offsets = (int64_t *)malloc(sizeof(int64_t) * (*n_frames))) == NULL)
144+
if ((offsets = malloc(sizeof(int64_t) * (*n_frames))) == NULL)
145145
return exdrNOMEM;
146146
for (i = 0; i < *n_frames; i++) {
147-
(*offsets)[i] = i * framebytes;
147+
offsets[i] = i * framebytes;
148148
}
149149
return exdrOK;
150150
} else /* No easy way out. We must iterate. */
@@ -167,16 +167,16 @@ int read_xtc_n_frames(char *fn, int *n_frames, int64_t **offsets) {
167167
est_nframes += est_nframes / 5;
168168

169169
/* Allocate memory for the frame index array */
170-
if ((*offsets = (int64_t *)malloc(sizeof(int64_t) * est_nframes)) == NULL) {
170+
if ((offsets = malloc(sizeof(int64_t) * est_nframes)) == NULL) {
171171
xdrfile_close(xd);
172172
return exdrNOMEM;
173173
}
174-
(*offsets)[0] = 0L;
174+
offsets[0] = 0L;
175175
*n_frames = 1;
176176
while (1) {
177177
if (xdr_seek(xd, (int64_t)(framebytes + XTC_HEADER_SIZE), SEEK_CUR) !=
178178
exdrOK) {
179-
free(*offsets);
179+
free(offsets);
180180
xdrfile_close(xd);
181181
return exdrNR;
182182
}
@@ -186,14 +186,14 @@ int read_xtc_n_frames(char *fn, int *n_frames, int64_t **offsets) {
186186
/* Check if we need to enlarge array */
187187
if (*n_frames == est_nframes) {
188188
est_nframes += est_nframes / 5 + 1; // Increase in 20% stretches
189-
if ((*offsets = realloc(*offsets, sizeof(int64_t) * est_nframes)) ==
189+
if ((offsets = realloc(offsets, sizeof(int64_t) * est_nframes)) ==
190190
NULL) {
191-
free(*offsets);
191+
free(offsets);
192192
xdrfile_close(xd);
193193
return exdrNOMEM;
194194
}
195195
}
196-
(*offsets)[*n_frames] =
196+
offsets[*n_frames] =
197197
xdr_tell(xd) - 4L - (int64_t)(XTC_HEADER_SIZE); // Account for the
198198
// header and the
199199
// nbytes bytes we

0 commit comments

Comments
 (0)