Skip to content

Commit

Permalink
Decouple htsFile::fnidx from its source and let HTSlib manage its mem…
Browse files Browse the repository at this point in the history
…ory.
  • Loading branch information
valeriuo committed Apr 6, 2020
1 parent 75fdb1c commit a934f21
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ int hts_close(htsFile *fp)
hts_idx_destroy(fp->idx);
free(fp->fn);
free(fp->fn_aux);
free(fp->fnidx);
free(fp->line.s);
free(fp);
errno = save;
Expand Down
2 changes: 1 addition & 1 deletion htslib/hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ typedef struct {
void *state; // format specific state information
htsFormat format;
hts_idx_t *idx;
const char *fnidx;
char *fnidx;
struct sam_hdr_t *bam_header;
} htsFile;

Expand Down
4 changes: 3 additions & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ int bam_index_build(const char *fn, int min_shift)
// Initialise fp->idx for the current format type.
// This must be called after the header has been written but no other data.
int sam_idx_init(htsFile *fp, sam_hdr_t *h, int min_shift, const char *fnidx) {
fp->fnidx = fnidx;
fp->fnidx = strdup(fnidx);
if (!fp->fnidx) return -1;

if (fp->format.format == bam || fp->format.format == bcf ||
(fp->format.format == sam && fp->format.compression == bgzf)) {
int n_lvls, fmt = HTS_FMT_CSI;
Expand Down
6 changes: 4 additions & 2 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3295,7 +3295,8 @@ static int vcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fn
fp->idx = NULL;
return -1;
}
fp->fnidx = fnidx;
fp->fnidx = strdup(fnidx);
if (!fp->fnidx) return -1;

return 0;
}
Expand All @@ -3315,7 +3316,8 @@ int bcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fnidx) {

fp->idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls);
if (!fp->idx) return -1;
fp->fnidx = fnidx;
fp->fnidx = strdup(fnidx);
if (!fp->fnidx) return -1;

return 0;
}
Expand Down

0 comments on commit a934f21

Please sign in to comment.