Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ shlib-exports-*.txt
/test/sam
/test/ref_cache/*.tmp.*
/test/tabix/*.tmp.*
/test/test_alloc
/test/test-bcf-sr
/test/test-bcf-translate
/test/test-bcf_set_variant_type
Expand Down
80 changes: 43 additions & 37 deletions Makefile

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions annot-tsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <string.h>
#include <strings.h>
#include "htslib/hts.h"
#include "htslib/hts_alloc.h"
#include "htslib/hts_defs.h"
#include "htslib/khash_str2int.h"
#include "htslib/kstring.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ static inline void nbp_add(nbp_t *nbp, hts_pos_t beg, hts_pos_t end)
if ( nbp->n >= nbp->m )
{
nbp->m += 2;
nbp->regs = realloc(nbp->regs, nbp->m*sizeof(*nbp->regs));
nbp->regs = hts_realloc_p(nbp->regs, sizeof(*nbp->regs), nbp->m);
if ( !nbp->regs ) error("Out of memory, failed to allocate %zu bytes\n",nbp->m*sizeof(*nbp->regs));
}
nbp->regs[nbp->n - 2] = NBP_SET_BEG(beg);
Expand Down Expand Up @@ -203,7 +204,7 @@ cols_t *cols_split(const char *line, cols_t *cols, char delim)
if ( cols->n > cols->m )
{
cols->m += 10;
cols->off = realloc(cols->off, sizeof(*cols->off)*cols->m);
cols->off = hts_realloc_p(cols->off, sizeof(*cols->off), cols->m);
if ( !cols->off ) error("Out of memory, failed to allocate %zu bytes\n",sizeof(*cols->off)*cols->m);
}
cols->off[ cols->n - 1 ] = ss;
Expand Down Expand Up @@ -253,7 +254,7 @@ void cols_append(cols_t *cols, char *str)
if ( cols->n > cols->m )
{
cols->m++;
cols->off = realloc(cols->off,sizeof(*cols->off)*cols->m);
cols->off = hts_realloc_p(cols->off, sizeof(*cols->off), cols->m);
if ( !cols->off ) error("Out of memory, failed to allocate %zu bytes\n",sizeof(*cols->off)*cols->m);
}
cols->off[cols->n-1] = str;
Expand Down Expand Up @@ -479,7 +480,7 @@ static int read_next_line(dat_t *dat)

void sanity_check_columns(char *fname, hdr_t *hdr, cols_t *cols, int **col2idx, int force)
{
*col2idx = (int*)malloc(sizeof(int)*cols->n);
*col2idx = hts_malloc_p(sizeof(int), cols->n);
if ( !*col2idx ) error("Out of memory, failed to allocate %zu bytes\n",sizeof(int)*cols->n);
int i, idx;
for (i=0; i<cols->n; i++)
Expand Down
10 changes: 7 additions & 3 deletions bcf_sr_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "bcf_sr_sort.h"
#include "htslib/khash_str2int.h"
#include "htslib/kbitset.h"
#include "htslib/hts_alloc.h"

// Variant types and pair-wise compatibility of their combinations, see bcf_sr_init_scores()
#define SR_REF 1
Expand Down Expand Up @@ -311,7 +312,9 @@ static char *grp_create_key(sr_sort_t *srt)
if ( i>0 ) srt->charp[i][-1] = 0;
}
qsort(srt->charp, srt->noff, sizeof(*srt->charp), cmpstringp);
char *ret = (char*) malloc(srt->str.l + 1), *ptr = ret;
char *ret = hts_malloc_ps(sizeof(*ret), srt->str.l, 1), *ptr = ret;
if (!ret)
return NULL;
for (i=0; i<srt->noff; i++)
{
int len = strlen(srt->charp[i]);
Expand Down Expand Up @@ -461,7 +464,7 @@ static int bcf_sr_sort_set(bcf_srs_t *readers, sr_sort_t *srt, const char *chr,
int mvcf = var->mvcf;
var->nvcf++;
hts_expand0(int*, var->nvcf, var->mvcf, var->vcf);
if ( mvcf != var->mvcf ) var->rec = (bcf1_t **) realloc(var->rec,sizeof(bcf1_t*)*var->mvcf);
if ( mvcf != var->mvcf ) var->rec = hts_realloc_p(var->rec, sizeof(bcf1_t*), var->mvcf);
var->vcf[var->nvcf-1] = ireader;
var->rec[var->nvcf-1] = line;

Expand Down Expand Up @@ -600,7 +603,8 @@ int bcf_sr_sort_next(bcf_srs_t *readers, sr_sort_t *srt, const char *chr, hts_po
srt->sr = readers;
if ( srt->nsr < readers->nreaders )
{
srt->vcf_buf = (vcf_buf_t*) realloc(srt->vcf_buf,readers->nreaders*sizeof(vcf_buf_t));
srt->vcf_buf = hts_realloc_p(srt->vcf_buf, sizeof(vcf_buf_t),
readers->nreaders);
memset(srt->vcf_buf + srt->nsr, 0, sizeof(vcf_buf_t)*(readers->nreaders - srt->nsr));
if ( srt->msr < srt->nsr ) srt->msr = srt->nsr;
}
Expand Down
15 changes: 9 additions & 6 deletions bgzf.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "htslib/bgzf.h"
#include "htslib/hfile.h"
#include "htslib/thread_pool.h"
#include "htslib/hts_alloc.h"
#include "htslib/hts_endian.h"
#include "cram/pooled_alloc.h"
#include "hts_internal.h"
Expand Down Expand Up @@ -204,7 +205,7 @@ int bgzf_idx_push(BGZF *fp, hts_idx_t *hidx, int tid, hts_pos_t beg, hts_pos_t e

if (ic->nentries >= ic->mentries) {
int new_sz = ic->mentries ? ic->mentries*2 : 1024;
if (!(e = realloc(ic->e, new_sz * sizeof(*ic->e)))) {
if (!(e = hts_realloc_p(ic->e, sizeof(*ic->e), new_sz))) {
pthread_mutex_unlock(&mt->idx_m);
return -1;
}
Expand Down Expand Up @@ -391,7 +392,7 @@ static BGZF *bgzf_read_init(hFILE *hfpr, const char *filename)
if (fp == NULL) return NULL;

fp->is_write = 0;
fp->uncompressed_block = malloc(2 * BGZF_MAX_BLOCK_SIZE);
fp->uncompressed_block = hts_malloc_p(2, BGZF_MAX_BLOCK_SIZE);
if (fp->uncompressed_block == NULL) { free(fp); return NULL; }
fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;
fp->is_compressed = (n==18 && magic[0]==0x1f && magic[1]==0x8b);
Expand Down Expand Up @@ -455,7 +456,7 @@ static BGZF *bgzf_write_init(const char *mode)
}
fp->is_compressed = 1;

fp->uncompressed_block = malloc(2 * BGZF_MAX_BLOCK_SIZE);
fp->uncompressed_block = hts_malloc_p(2, BGZF_MAX_BLOCK_SIZE);
if (fp->uncompressed_block == NULL) goto mem_fail;
fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;

Expand Down Expand Up @@ -1019,7 +1020,7 @@ int bgzf_read_block(BGZF *fp)

if (!j || j->errcode == BGZF_ERR_MT) {
if (!fp->mt->free_block) {
fp->uncompressed_block = malloc(2 * BGZF_MAX_BLOCK_SIZE);
fp->uncompressed_block = hts_malloc_p(2, BGZF_MAX_BLOCK_SIZE);
if (fp->uncompressed_block == NULL) return -1;
fp->compressed_block = (char *)fp->uncompressed_block + BGZF_MAX_BLOCK_SIZE;
} // else it's already allocated with malloc, maybe even in-use.
Expand Down Expand Up @@ -1417,7 +1418,8 @@ static void *bgzf_mt_writer(void *vp) {
{
fp->idx->moffs = fp->idx->noffs;
kroundup32(fp->idx->moffs);
fp->idx->offs = (bgzidx1_t*) realloc(fp->idx->offs, fp->idx->moffs*sizeof(bgzidx1_t));
fp->idx->offs = hts_realloc_p(fp->idx->offs, sizeof(bgzidx1_t),
fp->idx->moffs);
if ( !fp->idx->offs ) goto err;
}
fp->idx->offs[ fp->idx->noffs-1 ].uaddr = fp->idx->offs[ fp->idx->noffs-2 ].uaddr + j->uncomp_len;
Expand Down Expand Up @@ -2358,7 +2360,8 @@ int bgzf_index_add_block(BGZF *fp)
{
fp->idx->moffs = fp->idx->noffs;
kroundup32(fp->idx->moffs);
fp->idx->offs = (bgzidx1_t*) realloc(fp->idx->offs, fp->idx->moffs*sizeof(bgzidx1_t));
fp->idx->offs = hts_realloc_p(fp->idx->offs, sizeof(bgzidx1_t),
fp->idx->moffs);
if ( !fp->idx->offs ) return -1;
}
fp->idx->offs[ fp->idx->noffs-1 ].uaddr = fp->idx->ublock_addr;
Expand Down
4 changes: 3 additions & 1 deletion bgzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <sys/time.h>
#include "htslib/bgzf.h"
#include "htslib/hts.h"
#include "htslib/hts_alloc.h"
#include "htslib/hfile.h"

#ifdef _WIN32
Expand Down Expand Up @@ -352,7 +353,8 @@ int main(int argc, char **argv)
fp = bgzf_open("-", out_mode);
else
{
char *name = malloc(strlen(argv[optind]) + 5);
char *name = hts_malloc_ps(sizeof(*name),
strlen(argv[optind]), 5);
strcpy(name, argv[optind]);
strcat(name, ".gz");
fp = bgzf_open(name, is_forced? out_mode : out_mode_exclusive);
Expand Down
27 changes: 15 additions & 12 deletions cram/cram_codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../fuzz_settings.h"
#endif

#include "../htslib/hts_alloc.h"
#include "../htslib/hts_endian.h"

#if defined(HAVE_EXTERNAL_LIBHTSCODECS)
Expand Down Expand Up @@ -268,12 +269,14 @@ static int store_bits_MSB(cram_block *block, uint64_t val, int nbits) {
if (block->byte+8 >= block->alloc) {
if (block->byte) {
block->alloc *= 2;
block->data = realloc(block->data, block->alloc + 8);
block->data = hts_realloc_ps(block->data, sizeof(*block->data),
block->alloc, 8);
if (!block->data)
return -1;
} else {
block->alloc = 1024;
block->data = realloc(block->data, block->alloc + 8);
block->data = hts_realloc_ps(block->data, sizeof(*block->data),
block->alloc, 8);
if (!block->data)
return -1;
block->data[0] = 0; // initialise first byte of buffer
Expand Down Expand Up @@ -1975,7 +1978,7 @@ int cram_xdelta_encode_int(cram_slice *slice, cram_codec *c,

int cram_xdelta_encode_char(cram_slice *slice, cram_codec *c,
char *in, int in_size) {
char *dat = malloc(in_size*5);
char *dat = hts_malloc_p(5, in_size);
if (!dat)
return -1;
char *cp = dat, *cp_end = dat + in_size*5;
Expand Down Expand Up @@ -2269,7 +2272,7 @@ int cram_xrle_encode_flush(cram_codec *c) {
c->u.e_xrle.to_flush_size = BLOCK_SIZE(c->out);
}

out_len = malloc(c->u.e_xrle.to_flush_size+8);
out_len = hts_malloc_ps(sizeof(*out_len), c->u.e_xrle.to_flush_size, 8);
if (!out_len)
return -1;

Expand Down Expand Up @@ -3122,7 +3125,7 @@ int cram_huffman_encode_store(cram_codec *c, cram_block *b, char *prefix,
*
* Therefore 6*ncodes + 5 + 5 + 1 + 5 is max memory
*/
char *tmp = malloc(6*c->u.e_huffman.nvals+16);
char *tmp = hts_malloc_pse(6, c->u.e_huffman.nvals, 0, 16);
char *tp = tmp, *tpend = tmp+6*c->u.e_huffman.nvals+16;

if (!tmp)
Expand Down Expand Up @@ -3196,10 +3199,10 @@ cram_codec *cram_huffman_encode_init(cram_stats *st,
continue;
if (nvals >= vals_alloc) {
vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
new_vals = realloc(vals, vals_alloc * sizeof(int));
new_vals = hts_realloc_p(vals, sizeof(*vals), vals_alloc);
if (!new_vals) goto nomem;
vals = new_vals;
new_freqs = realloc(freqs, vals_alloc * sizeof(int));
new_freqs = hts_realloc_p(freqs, sizeof(*freqs), vals_alloc);
if (!new_freqs) goto nomem;
freqs = new_freqs;
}
Expand All @@ -3218,10 +3221,10 @@ cram_codec *cram_huffman_encode_init(cram_stats *st,
continue;
if (nvals >= vals_alloc) {
vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
new_vals = realloc(vals, vals_alloc * sizeof(int));
new_vals = hts_realloc_p(vals, sizeof(*vals), vals_alloc);
if (!new_vals) goto nomem;
vals = new_vals;
new_freqs = realloc(freqs, vals_alloc * sizeof(int));
new_freqs = hts_realloc_p(freqs, sizeof(*freqs), vals_alloc);
if (!new_freqs) goto nomem;
freqs = new_freqs;
}
Expand All @@ -3236,10 +3239,10 @@ cram_codec *cram_huffman_encode_init(cram_stats *st,

assert(nvals > 0);

new_freqs = realloc(freqs, 2*nvals*sizeof(*freqs));
new_freqs = hts_realloc_p(freqs, 2 * sizeof(*freqs), nvals);
if (!new_freqs) goto nomem;
freqs = new_freqs;
lens = calloc(2*nvals, sizeof(*lens));
lens = calloc(nvals, 2 * sizeof(*lens));
if (!lens) goto nomem;

/* Inefficient, use pointers to form chain so we can insert and maintain
Expand Down Expand Up @@ -3280,7 +3283,7 @@ cram_codec *cram_huffman_encode_init(cram_stats *st,


/* Sort, need in a struct */
if (!(codes = malloc(nvals * sizeof(*codes))))
if (!(codes = hts_malloc_p(sizeof(*codes), nvals)))
goto nomem;
for (i = 0; i < nvals; i++) {
codes[i].symbol = vals[i];
Expand Down
15 changes: 8 additions & 7 deletions cram/cram_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "cram.h"
#include "os.h"
#include "../htslib/hts.h"
#include "../htslib/hts_alloc.h"
#include "../htslib/hfile.h"

//Whether CIGAR has just M or uses = and X to indicate match and mismatch
Expand Down Expand Up @@ -178,7 +179,7 @@ cram_block_compression_hdr *cram_decode_compression_header(cram_fd *fd,
free(hdr);
return NULL;
}
if (!(hdr->landmark = malloc(hdr->num_landmarks * sizeof(int32_t)))) {
if (!(hdr->landmark = hts_malloc_p(sizeof(int32_t), hdr->num_landmarks))) {
free(hdr);
return NULL;
}
Expand Down Expand Up @@ -629,7 +630,7 @@ int cram_dependent_data_series(cram_fd *fd,
return 0;
}

block_used = calloc(s->hdr->num_blocks+1, sizeof(int));
block_used = hts_calloc_ps(sizeof(*block_used), s->hdr->num_blocks, 1);
if (!block_used)
return -1;

Expand Down Expand Up @@ -1007,7 +1008,7 @@ cram_block_slice_hdr *cram_decode_slice_header(cram_fd *fd, cram_block *b) {
free(hdr);
return NULL;
}
hdr->block_content_ids = malloc(hdr->num_content_ids * sizeof(int32_t));
hdr->block_content_ids = hts_malloc_p(sizeof(int32_t), hdr->num_content_ids);
if (!hdr->block_content_ids) {
free(hdr);
return NULL;
Expand Down Expand Up @@ -1162,7 +1163,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,

if (ncigar+2 >= cigar_alloc) {
cigar_alloc = cigar_alloc ? cigar_alloc*2 : 1024;
if (!(cigar = realloc(s->cigar, cigar_alloc * sizeof(*cigar))))
if (!(cigar = hts_realloc_p(s->cigar, sizeof(*cigar), cigar_alloc)))
return -1;
s->cigar = cigar;
}
Expand Down Expand Up @@ -1775,7 +1776,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,

if (ncigar+1 >= cigar_alloc) {
cigar_alloc = cigar_alloc ? cigar_alloc*2 : 1024;
if (!(cigar = realloc(s->cigar, cigar_alloc * sizeof(*cigar))))
if (!(cigar = hts_realloc_p(s->cigar, sizeof(*cigar), cigar_alloc)))
return -1;
s->cigar = cigar;
}
Expand Down Expand Up @@ -1805,7 +1806,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,
if (cig_len) {
if (ncigar >= cigar_alloc) {
cigar_alloc = cigar_alloc ? cigar_alloc*2 : 1024;
if (!(cigar = realloc(s->cigar, cigar_alloc * sizeof(*cigar))))
if (!(cigar = hts_realloc_p(s->cigar, sizeof(*cigar), cigar_alloc)))
return -1;
s->cigar = cigar;
}
Expand Down Expand Up @@ -2400,7 +2401,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,

if (s->crecs)
free(s->crecs);
if (!(s->crecs = malloc(s->hdr->num_records * sizeof(*s->crecs))))
if (!(s->crecs = hts_malloc_p(sizeof(*s->crecs), s->hdr->num_records)))
return -1;

ref_id = s->hdr->ref_seq_id;
Expand Down
Loading
Loading