From 752ce69b78bdc34746affa3f32a53c10b5d06285 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Sun, 16 Dec 2012 10:05:32 +0000 Subject: [PATCH 1/8] Added more wrappers for functions that make system calls Added the following wrappers that check the results of system calls and exit non-zero if something went wrong. This is so bwa can be more robust against system failures (e.g. IO errors from remote storage, or running out of memory). The existing and new wrappers have also been modified so that they no longer try to dump core on failure. In most cases the resulting core files are not useful (especially if bwa was compiled with optimization turned on) so just pollute whatever directories they got written to. Wrappers for memory allocation functions: xcalloc xmalloc xrealloc xstrdup New wrappers for IO functions: err_fread_noeof (also dies on EOF) err_gzread err_fseek err_rewind err_ftell --- utils.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++------- utils.h | 29 ++++++++++-- 2 files changed, 143 insertions(+), 20 deletions(-) diff --git a/utils.c b/utils.c index 8c1ad7e..bc39bf5 100644 --- a/utils.c +++ b/utils.c @@ -41,33 +41,44 @@ FILE *err_xopen_core(const char *func, const char *fn, const char *mode) if (strcmp(fn, "-") == 0) return (strstr(mode, "r"))? stdin : stdout; if ((fp = fopen(fn, mode)) == 0) { - fprintf(stderr, "[%s] fail to open file '%s'. Abort!\n", func, fn); - abort(); + err_fatal(func, "fail to open file '%s' : %s", fn, strerror(errno)); } return fp; } FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp) { if (freopen(fn, mode, fp) == 0) { - fprintf(stderr, "[%s] fail to open file '%s': ", func, fn); - perror(NULL); - fprintf(stderr, "Abort!\n"); - abort(); + err_fatal(func, "fail to open file '%s' : %s", fn, strerror(errno)); } return fp; } gzFile err_xzopen_core(const char *func, const char *fn, const char *mode) { gzFile fp; - if (strcmp(fn, "-") == 0) - return gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode); + if (strcmp(fn, "-") == 0) { + fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode); + /* According to zlib.h, this is the only reason gzdopen can fail */ + if (!fp) err_fatal(func, "Out of memory"); + return fp; + } if ((fp = gzopen(fn, mode)) == 0) { - fprintf(stderr, "[%s] fail to open file '%s'. Abort!\n", func, fn); - abort(); + err_fatal(func, "fail to open file '%s' : %s", fn, errno ? strerror(errno) : "Out of memory"); } return fp; } + void err_fatal(const char *header, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "[%s] ", header); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + va_end(args); + exit(EXIT_FAILURE); +} + +void err_fatal_core(const char *header, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -78,7 +89,13 @@ void err_fatal(const char *header, const char *fmt, ...) abort(); } -void err_fatal_simple_core(const char *func, const char *msg) +void _err_fatal_simple(const char *func, const char *msg) +{ + fprintf(stderr, "[%s] %s\n", func, msg); + exit(EXIT_FAILURE); +} + +void _err_fatal_simple_core(const char *func, const char *msg) { fprintf(stderr, "[%s] %s Abort!\n", func, msg); abort(); @@ -89,11 +106,55 @@ size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) size_t ret = fwrite(ptr, size, nmemb, stream); if (ret != nmemb) { - err_fatal_simple_core("fwrite", strerror(errno)); + _err_fatal_simple("fwrite", strerror(errno)); } return ret; } +size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret = fread(ptr, size, nmemb, stream); + if (ret != nmemb) + { + _err_fatal_simple("fread", ferror(stream) ? strerror(errno) : "Unexpected end of file"); + } + return ret; +} + +int err_gzread(gzFile file, void *ptr, unsigned int len) +{ + int ret = gzread(file, ptr, len); + + if (ret < 0) + { + int errnum = 0; + const char *msg = gzerror(file, &errnum); + _err_fatal_simple("gzread", Z_ERRNO == errnum ? strerror(errno) : msg); + } + + return ret; +} + +int err_fseek(FILE *stream, long offset, int whence) +{ + int ret = fseek(stream, offset, whence); + if (0 != ret) + { + _err_fatal_simple("fseek", strerror(errno)); + } + return ret; +} + +long err_ftell(FILE *stream) +{ + long ret = ftell(stream); + if (-1 == ret) + { + _err_fatal_simple("ftell", strerror(errno)); + } + return ret; +} + int err_printf(const char *format, ...) { va_list arg; @@ -106,7 +167,7 @@ int err_printf(const char *format, ...) if (done < 0) { - err_fatal_simple_core("vfprintf(stdout)", strerror(saveErrno)); + _err_fatal_simple("vfprintf(stdout)", strerror(saveErrno)); } return done; } @@ -123,7 +184,7 @@ int err_fprintf(FILE *stream, const char *format, ...) if (done < 0) { - err_fatal_simple_core("vfprintf", strerror(saveErrno)); + _err_fatal_simple("vfprintf", strerror(saveErrno)); } return done; } @@ -133,7 +194,7 @@ int err_fflush(FILE *stream) int ret = fflush(stream); if (ret != 0) { - err_fatal_simple_core("fflush", strerror(errno)); + _err_fatal_simple("fflush", strerror(errno)); } return ret; } @@ -143,11 +204,52 @@ int err_fclose(FILE *stream) int ret = fclose(stream); if (ret != 0) { - err_fatal_simple_core("fclose", strerror(errno)); + _err_fatal_simple("fclose", strerror(errno)); } return ret; } +void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func) +{ + void *p = calloc(nmemb, size); + if (NULL == p) + { + err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", nmemb * size, file, line, strerror(errno)); + } + return p; +} + +void *err_malloc(size_t size, const char *file, unsigned int line, const char *func) +{ + void *p = malloc(size); + if (NULL == p) + { + err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", size, file, line, strerror(errno)); + } + return p; +} + +void *err_realloc(void *ptr, size_t size, const char *file, unsigned int line, const char *func) +{ + void *p = realloc(ptr, size); + if (NULL == p) + { + err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", size, file, line, strerror(errno)); + } + return p; +} + +char *err_strdup(const char *s, const char *file, unsigned int line, const char *func) +{ + char *p = strdup(s); + + if (NULL == p) + { + err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", strlen(s), file, line, strerror(errno)); + } + return p; +} + double cputime() { struct rusage r; diff --git a/utils.h b/utils.h index b6839e9..c6cfc81 100644 --- a/utils.h +++ b/utils.h @@ -40,22 +40,38 @@ -#define err_fatal_simple(msg) err_fatal_simple_core(__func__, msg) +#define err_fatal_simple(msg) _err_fatal_simple(__func__, msg) +#define err_fatal_simple_core(msg) _err_fatal_simple_core(__func__, msg) #define xopen(fn, mode) err_xopen_core(__func__, fn, mode) #define xreopen(fn, mode, fp) err_xreopen_core(__func__, fn, mode, fp) #define xzopen(fn, mode) err_xzopen_core(__func__, fn, mode) -#define xassert(cond, msg) if ((cond) == 0) err_fatal_simple_core(__func__, msg) + +#define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg) + +#define xcalloc(n, s) err_calloc( (n), (s), __FILE__, __LINE__, __func__) +#define xmalloc(s) err_malloc( (s), __FILE__, __LINE__, __func__) +#define xrealloc(p, s) err_realloc((p), (s), __FILE__, __LINE__, __func__) +#define xstrdup(s) err_strdup( (s), __FILE__, __LINE__, __func__) + #ifdef __cplusplus extern "C" { #endif - void err_fatal(const char *header, const char *fmt, ...); - void err_fatal_simple_core(const char *func, const char *msg); + void err_fatal(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); + void err_fatal_core(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); + void _err_fatal_simple(const char *func, const char *msg) ATTRIBUTE((noreturn)); + void _err_fatal_simple_core(const char *func, const char *msg) ATTRIBUTE((noreturn)); FILE *err_xopen_core(const char *func, const char *fn, const char *mode); FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp); gzFile err_xzopen_core(const char *func, const char *fn, const char *mode); size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); + size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream); + + int err_gzread(gzFile file, void *ptr, unsigned int len); + int err_fseek(FILE *stream, long offset, int whence); +#define err_rewind(FP) err_fseek((FP), 0, SEEK_SET) + long err_ftell(FILE *stream); int err_fprintf(FILE *stream, const char *format, ...) ATTRIBUTE((format(printf, 2, 3))); int err_printf(const char *format, ...) @@ -63,6 +79,11 @@ extern "C" { int err_fflush(FILE *stream); int err_fclose(FILE *stream); + void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func); + void *err_malloc(size_t size, const char *file, unsigned int line, const char *func); + void *err_realloc(void *ptr, size_t size, const char *file, unsigned int line, const char *func); + char *err_strdup(const char *s, const char *file, unsigned int line, const char *func); + double cputime(); double realtime(); From b081ac9b8b16c2a57d9090784dc86dfbd48fad12 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Sun, 16 Dec 2012 10:34:57 +0000 Subject: [PATCH 2/8] Use wrapper functions to catch system errors Use the wrapper functions in utils.c plus a few extra bits of error checking code to catch system errors and exit non-zero when they occur. --- Makefile | 47 ++++++++++++++++------- bamlite.c | 38 +++++++++++-------- bamlite.h | 7 ++-- bntseq.c | 101 +++++++++++++++++++++++++++++-------------------- bwa.c | 28 +++++++------- bwape.c | 40 ++++++++++---------- bwase.c | 48 +++++++++++------------ bwaseqio.c | 26 ++++++------- bwt.c | 6 +-- bwt_gen.c | 43 +++++++++++---------- bwt_lite.c | 11 +++--- bwtaln.c | 14 +++---- bwtgap.c | 11 +++--- bwtindex.c | 10 ++--- bwtio.c | 54 +++++++++++++------------- bwtmisc.c | 38 +++++++++---------- bwtsw2_aux.c | 74 +++++++++++++++++------------------- bwtsw2_chain.c | 7 ++-- bwtsw2_core.c | 29 +++++++------- bwtsw2_pair.c | 7 ++-- cs2nt.c | 3 +- fastmap.c | 13 ++++--- is.c | 6 ++- khash.h | 13 ++++--- kseq.h | 13 ++++--- ksort.h | 5 ++- kstring.c | 3 +- kstring.h | 5 ++- ksw.c | 9 +++-- kvec.h | 8 ++-- simple_dp.c | 12 +++--- stdaln.c | 37 +++++++++--------- 32 files changed, 411 insertions(+), 355 deletions(-) diff --git a/Makefile b/Makefile index 6f388f2..b8fa824 100644 --- a/Makefile +++ b/Makefile @@ -31,19 +31,38 @@ bwa:libbwa.a $(AOBJS) main.o libbwa.a:$(LOBJS) $(AR) -csru $@ $(LOBJS) -bwa.o:bwa.h - -QSufSort.o:QSufSort.h - -bwt.o:bwt.h -bwtio.o:bwt.h -bwtaln.o:bwt.h bwtaln.h kseq.h -bntseq.o:bntseq.h -bwtgap.o:bwtgap.h bwtaln.h bwt.h - -bwtsw2_core.o:bwtsw2.h bwt.h bwt_lite.h stdaln.h -bwtsw2_aux.o:bwtsw2.h bwt.h bwt_lite.h stdaln.h -bwtsw2_main.o:bwtsw2.h - clean: rm -f gmon.out *.o a.out $(PROG) *~ *.a + +QSufSort.o: QSufSort.h +bamlite.o: bamlite.h utils.h +bntseq.o: bntseq.h kseq.h main.h utils.h +bwa.o: bntseq.h bwa.h bwt.h bwtaln.h bwtgap.h stdaln.h utils.h +bwape.o: bntseq.h bwase.h bwt.h bwtaln.h khash.h ksort.h kvec.h stdaln.h +bwape.o: utils.h +bwase.o: bntseq.h bwase.h bwt.h bwtaln.h kstring.h stdaln.h utils.h +bwaseqio.o: bamlite.h bwt.h bwtaln.h kseq.h stdaln.h utils.h +bwt.o: bwt.h kvec.h utils.h +bwt_gen.o: QSufSort.h utils.h +bwt_lite.o: bwt_lite.h utils.h +bwtaln.o: bwt.h bwtaln.h bwtgap.h stdaln.h utils.h +bwtgap.o: bwt.h bwtaln.h bwtgap.h stdaln.h utils.h +bwtindex.o: bntseq.h bwt.h main.h utils.h +bwtio.o: bwt.h utils.h +bwtmisc.o: bntseq.h bwt.h main.h utils.h +bwtsw2_aux.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h kseq.h ksort.h kstring.h +bwtsw2_aux.o: stdaln.h utils.h +bwtsw2_chain.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h ksort.h utils.h +bwtsw2_core.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h khash.h ksort.h kvec.h +bwtsw2_core.o: utils.h +bwtsw2_main.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h utils.h +bwtsw2_pair.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h kstring.h ksw.h utils.h +cs2nt.o: bwt.h bwtaln.h stdaln.h utils.h +fastmap.o: bntseq.h bwt.h kseq.h kvec.h utils.h +is.o: utils.h +kstring.o: kstring.h utils.h +ksw.o: ksw.h utils.h +main.o: main.h utils.h +simple_dp.o: kseq.h stdaln.h utils.h +stdaln.o: stdaln.h utils.h +utils.o: utils.h diff --git a/bamlite.c b/bamlite.c index 5aad392..ec365d1 100644 --- a/bamlite.c +++ b/bamlite.c @@ -2,6 +2,7 @@ #include #include #include +#include "utils.h" #include "bamlite.h" /********************* @@ -53,7 +54,7 @@ int bam_is_be; bam_header_t *bam_header_init() { bam_is_be = bam_is_big_endian(); - return (bam_header_t*)calloc(1, sizeof(bam_header_t)); + return (bam_header_t*)xcalloc(1, sizeof(bam_header_t)); } void bam_header_destroy(bam_header_t *header) @@ -62,11 +63,11 @@ void bam_header_destroy(bam_header_t *header) if (header == 0) return; if (header->target_name) { for (i = 0; i < header->n_targets; ++i) - free(header->target_name[i]); + if (header->target_name[i]) free(header->target_name[i]); + if (header->target_len) free(header->target_len); free(header->target_name); - free(header->target_len); } - free(header->text); + if (header->text) free(header->text); free(header); } @@ -80,28 +81,33 @@ bam_header_t *bam_header_read(bamFile fp) magic_len = bam_read(fp, buf, 4); if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) { fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n"); - return 0; + return NULL; } header = bam_header_init(); // read plain text and the number of reference sequences - bam_read(fp, &header->l_text, 4); + if (bam_read(fp, &header->l_text, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&header->l_text); - header->text = (char*)calloc(header->l_text + 1, 1); - bam_read(fp, header->text, header->l_text); - bam_read(fp, &header->n_targets, 4); + header->text = (char*)xcalloc(header->l_text + 1, 1); + if (bam_read(fp, header->text, header->l_text) != header->l_text) goto fail; + if (bam_read(fp, &header->n_targets, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&header->n_targets); // read reference sequence names and lengths - header->target_name = (char**)calloc(header->n_targets, sizeof(char*)); - header->target_len = (uint32_t*)calloc(header->n_targets, 4); + header->target_name = (char**)xcalloc(header->n_targets, sizeof(char*)); + header->target_len = (uint32_t*)xcalloc(header->n_targets, 4); for (i = 0; i != header->n_targets; ++i) { - bam_read(fp, &name_len, 4); + if (bam_read(fp, &name_len, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&name_len); - header->target_name[i] = (char*)calloc(name_len, 1); - bam_read(fp, header->target_name[i], name_len); - bam_read(fp, &header->target_len[i], 4); + header->target_name[i] = (char*)xcalloc(name_len, 1); + if (bam_read(fp, header->target_name[i], name_len) != name_len) { + goto fail; + } + if (bam_read(fp, &header->target_len[i], 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]); } return header; + fail: + bam_header_destroy(header); + return NULL; } static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data) @@ -146,7 +152,7 @@ int bam_read1(bamFile fp, bam1_t *b) if (b->m_data < b->data_len) { b->m_data = b->data_len; kroundup32(b->m_data); - b->data = (uint8_t*)realloc(b->data, b->m_data); + b->data = (uint8_t*)xrealloc(b->data, b->m_data); } if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4; b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2; diff --git a/bamlite.h b/bamlite.h index 167fa44..2b65c57 100644 --- a/bamlite.h +++ b/bamlite.h @@ -3,12 +3,13 @@ #include #include +#include "utils.h" typedef gzFile bamFile; -#define bam_open(fn, mode) gzopen(fn, mode) +#define bam_open(fn, mode) xzopen(fn, mode) #define bam_dopen(fd, mode) gzdopen(fd, mode) #define bam_close(fp) gzclose(fp) -#define bam_read(fp, buf, size) gzread(fp, buf, size) +#define bam_read(fp, buf, size) err_gzread(fp, buf, size) typedef struct { int32_t n_targets; @@ -71,7 +72,7 @@ typedef struct { #define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf) #define bam1_aux(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (b)->core.l_qseq + ((b)->core.l_qseq + 1)/2) -#define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t))) +#define bam_init1() ((bam1_t*)xcalloc(1, sizeof(bam1_t))) #define bam_destroy1(b) do { \ if (b) { free((b)->data); free(b); } \ } while (0) diff --git a/bntseq.c b/bntseq.c index adcd2d7..b795af8 100644 --- a/bntseq.c +++ b/bntseq.c @@ -30,12 +30,13 @@ #include #include #include +#include #include "bntseq.h" #include "main.h" #include "utils.h" #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +KSEQ_INIT(gzFile, err_gzread) unsigned char nst_nt4_table[256] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, @@ -64,25 +65,25 @@ void bns_dump(const bntseq_t *bns, const char *prefix) { // dump .ann strcpy(str, prefix); strcat(str, ".ann"); fp = xopen(str, "w"); - fprintf(fp, "%lld %d %u\n", (long long)bns->l_pac, bns->n_seqs, bns->seed); + err_fprintf(fp, "%lld %d %u\n", (long long)bns->l_pac, bns->n_seqs, bns->seed); for (i = 0; i != bns->n_seqs; ++i) { bntann1_t *p = bns->anns + i; - fprintf(fp, "%d %s", p->gi, p->name); - if (p->anno[0]) fprintf(fp, " %s\n", p->anno); - else fprintf(fp, "\n"); - fprintf(fp, "%lld %d %d\n", (long long)p->offset, p->len, p->n_ambs); + err_fprintf(fp, "%d %s", p->gi, p->name); + if (p->anno[0]) err_fprintf(fp, " %s\n", p->anno); + else err_fprintf(fp, "\n"); + err_fprintf(fp, "%lld %d %d\n", (long long)p->offset, p->len, p->n_ambs); } - fclose(fp); + err_fclose(fp); } { // dump .amb strcpy(str, prefix); strcat(str, ".amb"); fp = xopen(str, "w"); - fprintf(fp, "%lld %d %u\n", (long long)bns->l_pac, bns->n_seqs, bns->n_holes); + err_fprintf(fp, "%lld %d %u\n", (long long)bns->l_pac, bns->n_seqs, bns->n_holes); for (i = 0; i != bns->n_holes; ++i) { bntamb1_t *p = bns->ambs + i; - fprintf(fp, "%lld %d %c\n", (long long)p->offset, p->len, p->amb); + err_fprintf(fp, "%lld %d %c\n", (long long)p->offset, p->len, p->amb); } - fclose(fp); + err_fclose(fp); } } @@ -90,53 +91,71 @@ bntseq_t *bns_restore_core(const char *ann_filename, const char* amb_filename, c { char str[1024]; FILE *fp; + const char *fname; bntseq_t *bns; long long xx; int i; - bns = (bntseq_t*)calloc(1, sizeof(bntseq_t)); + int scanres; + bns = (bntseq_t*)xcalloc(1, sizeof(bntseq_t)); { // read .ann - fp = xopen(ann_filename, "r"); - fscanf(fp, "%lld%d%u", &xx, &bns->n_seqs, &bns->seed); + fp = xopen(fname = ann_filename, "r"); + scanres = fscanf(fp, "%lld%d%u", &xx, &bns->n_seqs, &bns->seed); + if (scanres != 3) goto badread; bns->l_pac = xx; - bns->anns = (bntann1_t*)calloc(bns->n_seqs, sizeof(bntann1_t)); + bns->anns = (bntann1_t*)xcalloc(bns->n_seqs, sizeof(bntann1_t)); for (i = 0; i < bns->n_seqs; ++i) { bntann1_t *p = bns->anns + i; char *q = str; int c; // read gi and sequence name - fscanf(fp, "%u%s", &p->gi, str); - p->name = strdup(str); + scanres = fscanf(fp, "%u%s", &p->gi, str); + if (scanres != 2) goto badread; + p->name = xstrdup(str); // read fasta comments - while ((c = fgetc(fp)) != '\n' && c != EOF) *q++ = c; + while (str - q < sizeof(str) - 1 && (c = fgetc(fp)) != '\n' && c != EOF) *q++ = c; + while (c != '\n' && c != EOF) c = fgetc(fp); + if (c == EOF) { + scanres = EOF; + goto badread; + } *q = 0; - if (q - str > 1) p->anno = strdup(str + 1); // skip leading space - else p->anno = strdup(""); + if (q - str > 1) p->anno = xstrdup(str + 1); // skip leading space + else p->anno = xstrdup(""); // read the rest - fscanf(fp, "%lld%d%d", &xx, &p->len, &p->n_ambs); + scanres = fscanf(fp, "%lld%d%d", &xx, &p->len, &p->n_ambs); + if (scanres != 3) goto badread; p->offset = xx; } - fclose(fp); + err_fclose(fp); } { // read .amb int64_t l_pac; int32_t n_seqs; - fp = xopen(amb_filename, "r"); - fscanf(fp, "%lld%d%d", &xx, &n_seqs, &bns->n_holes); + fp = xopen(fname = amb_filename, "r"); + scanres = fscanf(fp, "%lld%d%d", &xx, &n_seqs, &bns->n_holes); + if (scanres != 3) goto badread; l_pac = xx; xassert(l_pac == bns->l_pac && n_seqs == bns->n_seqs, "inconsistent .ann and .amb files."); - bns->ambs = (bntamb1_t*)calloc(bns->n_holes, sizeof(bntamb1_t)); + bns->ambs = (bntamb1_t*)xcalloc(bns->n_holes, sizeof(bntamb1_t)); for (i = 0; i < bns->n_holes; ++i) { bntamb1_t *p = bns->ambs + i; - fscanf(fp, "%lld%d%s", &xx, &p->len, str); + scanres = fscanf(fp, "%lld%d%s", &xx, &p->len, str); + if (scanres != 3) goto badread; p->offset = xx; p->amb = str[0]; } - fclose(fp); + err_fclose(fp); } { // open .pac bns->fp_pac = xopen(pac_filename, "rb"); } return bns; + + badread: + if (EOF == scanres) { + err_fatal(__func__, "Error reading %s : %s\n", fname, ferror(fp) ? strerror(errno) : "Unexpected end of file"); + } + err_fatal(__func__, "Parse error reading %s\n", fname); } bntseq_t *bns_restore(const char *prefix) @@ -153,7 +172,7 @@ void bns_destroy(bntseq_t *bns) if (bns == 0) return; else { int i; - if (bns->fp_pac) fclose(bns->fp_pac); + if (bns->fp_pac) err_fclose(bns->fp_pac); free(bns->ambs); for (i = 0; i < bns->n_seqs; ++i) { free(bns->anns[i].name); @@ -173,11 +192,11 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ int i, lasts; if (bns->n_seqs == *m_seqs) { *m_seqs <<= 1; - bns->anns = (bntann1_t*)realloc(bns->anns, *m_seqs * sizeof(bntann1_t)); + bns->anns = (bntann1_t*)xrealloc(bns->anns, *m_seqs * sizeof(bntann1_t)); } p = bns->anns + bns->n_seqs; - p->name = strdup((char*)seq->name.s); - p->anno = seq->comment.s? strdup((char*)seq->comment.s) : strdup("(null)"); + p->name = xstrdup((char*)seq->name.s); + p->anno = seq->comment.s? xstrdup((char*)seq->comment.s) : xstrdup("(null)"); p->gi = 0; p->len = seq->seq.l; p->offset = (bns->n_seqs == 0)? 0 : (p-1)->offset + (p-1)->len; p->n_ambs = 0; @@ -189,7 +208,7 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ } else { if (bns->n_holes == *m_holes) { (*m_holes) <<= 1; - bns->ambs = (bntamb1_t*)realloc(bns->ambs, (*m_holes) * sizeof(bntamb1_t)); + bns->ambs = (bntamb1_t*)xrealloc(bns->ambs, (*m_holes) * sizeof(bntamb1_t)); } *q = bns->ambs + bns->n_holes; (*q)->len = 1; @@ -204,7 +223,7 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ if (c >= 4) c = lrand48()&3; if (bns->l_pac == *m_pac) { // double the pac size *m_pac <<= 1; - pac = realloc(pac, *m_pac/4); + pac = xrealloc(pac, *m_pac/4); memset(pac + bns->l_pac/4, 0, (*m_pac - bns->l_pac)/4); } _set_pac(pac, bns->l_pac, c); @@ -229,13 +248,13 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) // initialization seq = kseq_init(fp_fa); - bns = (bntseq_t*)calloc(1, sizeof(bntseq_t)); + bns = (bntseq_t*)xcalloc(1, sizeof(bntseq_t)); bns->seed = 11; // fixed seed for random generator srand48(bns->seed); m_seqs = m_holes = 8; m_pac = 0x10000; - bns->anns = (bntann1_t*)calloc(m_seqs, sizeof(bntann1_t)); - bns->ambs = (bntamb1_t*)calloc(m_holes, sizeof(bntamb1_t)); - pac = calloc(m_pac/4, 1); + bns->anns = (bntann1_t*)xcalloc(m_seqs, sizeof(bntann1_t)); + bns->ambs = (bntamb1_t*)xcalloc(m_holes, sizeof(bntamb1_t)); + pac = xcalloc(m_pac/4, 1); q = bns->ambs; strcpy(name, prefix); strcat(name, ".pac"); fp = xopen(name, "wb"); @@ -243,7 +262,7 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) while (kseq_read(seq) >= 0) pac = add1(seq, bns, pac, &m_pac, &m_seqs, &m_holes, &q); if (!for_only) { // add the reverse complemented sequence m_pac = (bns->l_pac * 2 + 3) / 4 * 4; - pac = realloc(pac, m_pac/4); + pac = xrealloc(pac, m_pac/4); memset(pac + (bns->l_pac+3)/4, 0, (m_pac - (bns->l_pac+3)/4*4) / 4); for (l = bns->l_pac - 1; l >= 0; --l, ++bns->l_pac) _set_pac(pac, bns->l_pac, 3-_get_pac(pac, l)); @@ -251,16 +270,16 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) ret = bns->l_pac; { // finalize .pac file ubyte_t ct; - fwrite(pac, 1, (bns->l_pac>>2) + ((bns->l_pac&3) == 0? 0 : 1), fp); + err_fwrite(pac, 1, (bns->l_pac>>2) + ((bns->l_pac&3) == 0? 0 : 1), fp); // the following codes make the pac file size always (l_pac/4+1+1) if (bns->l_pac % 4 == 0) { ct = 0; - fwrite(&ct, 1, 1, fp); + err_fwrite(&ct, 1, 1, fp); } ct = bns->l_pac % 4; - fwrite(&ct, 1, 1, fp); + err_fwrite(&ct, 1, 1, fp); // close .pac file - fclose(fp); + err_fclose(fp); } bns_dump(bns, prefix); bns_destroy(bns); diff --git a/bwa.c b/bwa.c index 8e99f18..0b6a420 100644 --- a/bwa.c +++ b/bwa.c @@ -1,7 +1,9 @@ + #include #include #include #include +#include "utils.h" #include "bwa.h" #include "bwt.h" #include "bwtgap.h" @@ -38,8 +40,8 @@ bwa_idx_t *bwa_idx_load(const char *prefix) int l; char *str; l = strlen(prefix); - p = calloc(1, sizeof(bwa_idx_t)); - str = malloc(l + 10); + p = xcalloc(1, sizeof(bwa_idx_t)); + str = xmalloc(l + 10); strcpy(str, prefix); p->bns = bns_restore(str); strcpy(str + l, ".bwt"); @@ -48,9 +50,9 @@ bwa_idx_t *bwa_idx_load(const char *prefix) strcpy(str + l, ".sa"); bwt_restore_sa(str, p->bwt); free(str); - p->pac = calloc(p->bns->l_pac/4+1, 1); - fread(p->pac, 1, p->bns->l_pac/4+1, p->bns->fp_pac); - fclose(p->bns->fp_pac); + p->pac = xcalloc(p->bns->l_pac/4+1, 1); + err_fread_noeof(p->pac, 1, p->bns->l_pac/4+1, p->bns->fp_pac); + err_fclose(p->bns->fp_pac); p->bns->fp_pac = 0; return p; } @@ -69,7 +71,7 @@ bwa_buf_t *bwa_buf_init(const bwa_opt_t *opt, int max_score) extern int bwa_cal_maxdiff(int l, double err, double thres); int i; bwa_buf_t *p; - p = malloc(sizeof(bwa_buf_t)); + p = xmalloc(sizeof(bwa_buf_t)); p->stack = gap_init_stack2(max_score); p->opt = gap_init_opt(); p->opt->s_gapo = opt->s_gapo; @@ -80,10 +82,10 @@ bwa_buf_t *bwa_buf_init(const bwa_opt_t *opt, int max_score) p->opt->seed_len = opt->seed_len; p->opt->max_seed_diff = opt->max_seed_diff; p->opt->fnr = opt->fnr; - p->diff_tab = calloc(BWA_MAX_QUERY_LEN, sizeof(int)); + p->diff_tab = xcalloc(BWA_MAX_QUERY_LEN, sizeof(int)); for (i = 1; i < BWA_MAX_QUERY_LEN; ++i) p->diff_tab[i] = bwa_cal_maxdiff(i, BWA_AVG_ERR, opt->fnr); - p->logn = calloc(256, sizeof(int)); + p->logn = xcalloc(256, sizeof(int)); for (i = 1; i != 256; ++i) p->logn[i] = (int)(4.343 * log(i) + 0.499); return p; @@ -111,7 +113,7 @@ bwa_sai_t bwa_sai(const bwa_idx_t *idx, bwa_buf_t *buf, const char *seq) if (buf_len > buf->max_buf) { buf->max_buf = buf_len; kroundup32(buf->max_buf); - buf->buf = realloc(buf->buf, buf->max_buf); + buf->buf = xrealloc(buf->buf, buf->max_buf); } memset(buf->buf, 0, buf_len); seed_w = (bwt_width_t*)buf->buf; @@ -166,7 +168,7 @@ void bwa_sa2aln(const bwa_idx_t *idx, bwa_buf_t *buf, const char *seq, uint64_t if (seq_len<<1 > buf->max_buf) { buf->max_buf = seq_len<<1; kroundup32(buf->max_buf); - buf->buf = realloc(buf->buf, buf->max_buf); + buf->buf = xrealloc(buf->buf, buf->max_buf); } s[0] = buf->buf; s[1] = s[0] + seq_len; @@ -180,7 +182,7 @@ void bwa_sa2aln(const bwa_idx_t *idx, bwa_buf_t *buf, const char *seq, uint64_t bwa_cigar_t *cigar16; cigar16 = bwa_refine_gapped_core(idx->bns->l_pac, idx->pac, seq_len, s[strand], &pac_pos, strand? n_gaps : -n_gaps, &n_cigar, 1); aln->n_cigar = n_cigar; - aln->cigar = malloc(n_cigar * 4); + aln->cigar = xmalloc(n_cigar * 4); for (i = 0, pos3 = pac_pos; i < n_cigar; ++i) { int op = cigar16[i]>>14; int len = cigar16[i]&0x3fff; @@ -191,7 +193,7 @@ void bwa_sa2aln(const bwa_idx_t *idx, bwa_buf_t *buf, const char *seq, uint64_t free(cigar16); } else { // ungapped aln->n_cigar = 1; - aln->cigar = malloc(4); + aln->cigar = xmalloc(4); aln->cigar[0] = seq_len<<4 | 0; pos3 = pac_pos + seq_len; } @@ -214,7 +216,7 @@ bwa_one_t *bwa_se(const bwa_idx_t *idx, bwa_buf_t *buf, const char *seq, int gen int best, cnt, i, seq_len; seq_len = strlen(seq); - one = calloc(1, sizeof(bwa_one_t)); + one = xcalloc(1, sizeof(bwa_one_t)); one->sai = bwa_sai(idx, buf, seq); if (one->sai.n == 0) return one; // count number of hits; randomly select one alignment diff --git a/bwape.c b/bwape.c index 779670f..f16d684 100644 --- a/bwape.c +++ b/bwape.c @@ -58,7 +58,7 @@ void bwa_print_sam_PG(); pe_opt_t *bwa_init_pe_opt() { pe_opt_t *po; - po = (pe_opt_t*)calloc(1, sizeof(pe_opt_t)); + po = (pe_opt_t*)xcalloc(1, sizeof(pe_opt_t)); po->max_isize = 500; po->force_isize = 0; po->max_occ = 100000; @@ -104,7 +104,7 @@ static int infer_isize(int n_seqs, bwa_seq_t *seqs[2], isize_info_t *ii, double ii->avg = ii->std = -1.0; ii->low = ii->high = ii->high_bayesian = 0; - isizes = (uint64_t*)calloc(n_seqs, 8); + isizes = (uint64_t*)xcalloc(n_seqs, 8); for (i = 0, tot = 0; i != n_seqs; ++i) { bwa_seq_t *p[2]; p[0] = seqs[0] + i; p[1] = seqs[1] + i; @@ -292,9 +292,9 @@ int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bw pe_data_t *d; aln_buf_t *buf[2]; - d = (pe_data_t*)calloc(1, sizeof(pe_data_t)); - buf[0] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t)); - buf[1] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t)); + d = (pe_data_t*)xcalloc(1, sizeof(pe_data_t)); + buf[0] = (aln_buf_t*)xcalloc(n_seqs, sizeof(aln_buf_t)); + buf[1] = (aln_buf_t*)xcalloc(n_seqs, sizeof(aln_buf_t)); if (_bwt == 0) { // load forward SA strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); @@ -309,11 +309,11 @@ int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bw p[j] = seqs[j] + i; p[j]->n_multi = 0; p[j]->extra_flag |= SAM_FPD | (j == 0? SAM_FR1 : SAM_FR2); - fread(&n_aln, 4, 1, fp_sa[j]); + err_fread_noeof(&n_aln, 4, 1, fp_sa[j]); if (n_aln > kv_max(d->aln[j])) kv_resize(bwt_aln1_t, d->aln[j], n_aln); d->aln[j].n = n_aln; - fread(d->aln[j].a, sizeof(bwt_aln1_t), n_aln, fp_sa[j]); + err_fread_noeof(d->aln[j].a, sizeof(bwt_aln1_t), n_aln, fp_sa[j]); kv_copy(bwt_aln1_t, buf[j][i].aln, d->aln[j]); // backup d->aln[j] // generate SE alignment and mapping quality bwa_aln2seq(n_aln, d->aln[j].a, p[j]); @@ -367,7 +367,7 @@ int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bw if (ret) { // not in the hash table; ret must equal 1 as we never remove elements poslist_t *z = &kh_val(g_hash, iter); z->n = r->l - r->k + 1; - z->a = (bwtint_t*)malloc(sizeof(bwtint_t) * z->n); + z->a = (bwtint_t*)xmalloc(sizeof(bwtint_t) * z->n); for (l = r->k; l <= r->l; ++l) { int strand; z->a[l - r->k] = bwa_sa2pos(bns, bwt, l, p[j]->len, &strand)<<1; @@ -448,10 +448,10 @@ bwa_cigar_t *bwa_sw_core(bwtint_t l_pac, const ubyte_t *pacseq, int len, const u if ((float)x/len >= 0.25 || len - x < SW_MIN_MATCH_LEN) return 0; // get reference subsequence - ref_seq = (ubyte_t*)calloc(reglen, 1); + ref_seq = (ubyte_t*)xcalloc(reglen, 1); for (k = *beg, l = 0; l < reglen && k < l_pac; ++k) ref_seq[l++] = pacseq[k>>2] >> ((~k&3)<<1) & 3; - path = (path_t*)calloc(l+len, sizeof(path_t)); + path = (path_t*)xcalloc(l+len, sizeof(path_t)); // do alignment ret = aln_local_core(ref_seq, l, (ubyte_t*)seq, len, &ap, path, &path_len, 1, &subo); @@ -480,7 +480,7 @@ bwa_cigar_t *bwa_sw_core(bwtint_t l_pac, const ubyte_t *pacseq, int len, const u *beg += (p->i? p->i : 1) - 1; start = (p->j? p->j : 1) - 1; end = path->j; - cigar = (bwa_cigar_t*)realloc(cigar, sizeof(bwa_cigar_t) * (*n_cigar + 2)); + cigar = (bwa_cigar_t*)xrealloc(cigar, sizeof(bwa_cigar_t) * (*n_cigar + 2)); if (start) { memmove(cigar + 1, cigar, sizeof(bwa_cigar_t) * (*n_cigar)); cigar[0] = __cigar_create(3, start); @@ -525,9 +525,9 @@ ubyte_t *bwa_paired_sw(const bntseq_t *bns, const ubyte_t *_pacseq, int n_seqs, // load reference sequence if (_pacseq == 0) { - pacseq = (ubyte_t*)calloc(bns->l_pac/4+1, 1); - rewind(bns->fp_pac); - fread(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); + pacseq = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + err_rewind(bns->fp_pac); + err_fread_noeof(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); } else pacseq = (ubyte_t*)_pacseq; if (!popt->is_sw || ii->avg < 0.0) return pacseq; @@ -683,10 +683,10 @@ void bwa_sai2sam_pe_core(const char *prefix, char *const fn_sa[2], char *const f g_hash = kh_init(b128); last_ii.avg = -1.0; - fread(&opt, sizeof(gap_opt_t), 1, fp_sa[0]); + err_fread_noeof(&opt, sizeof(gap_opt_t), 1, fp_sa[0]); ks[0] = bwa_open_reads(opt.mode, fn_fa[0]); opt0 = opt; - fread(&opt, sizeof(gap_opt_t), 1, fp_sa[1]); // overwritten! + err_fread_noeof(&opt, sizeof(gap_opt_t), 1, fp_sa[1]); // overwritten! ks[1] = bwa_open_reads(opt.mode, fn_fa[1]); if (!(opt.mode & BWA_MODE_COMPREAD)) { popt->type = BWA_PET_SOLID; @@ -695,9 +695,9 @@ void bwa_sai2sam_pe_core(const char *prefix, char *const fn_sa[2], char *const f if (popt->is_preload) { strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); strcpy(str, prefix); strcat(str, ".sa"); bwt_restore_sa(str, bwt); - pac = (ubyte_t*)calloc(bns->l_pac/4+1, 1); - rewind(bns->fp_pac); - fread(pac, 1, bns->l_pac/4+1, bns->fp_pac); + pac = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + err_rewind(bns->fp_pac); + err_fread_noeof(pac, 1, bns->l_pac/4+1, bns->fp_pac); } } @@ -752,7 +752,7 @@ void bwa_sai2sam_pe_core(const char *prefix, char *const fn_sa[2], char *const f if (ntbns) bns_destroy(ntbns); for (i = 0; i < 2; ++i) { bwa_seq_close(ks[i]); - fclose(fp_sa[i]); + err_fclose(fp_sa[i]); } for (iter = kh_begin(g_hash); iter != kh_end(g_hash); ++iter) if (kh_exist(g_hash, iter)) free(kh_val(g_hash, iter).a); diff --git a/bwase.c b/bwase.c index 35744e7..afe8154 100644 --- a/bwase.c +++ b/bwase.c @@ -59,7 +59,7 @@ void bwa_aln2seq_core(int n_aln, const bwt_aln1_t *aln, bwa_seq_t *s, int set_ma * simply output all hits, but the following samples "rest" * number of random hits. */ rest = n_occ > n_multi + 1? n_multi + 1 : n_occ; // find one additional for ->sa - s->multi = calloc(rest, sizeof(bwt_multi1_t)); + s->multi = xcalloc(rest, sizeof(bwt_multi1_t)); for (k = 0; k < n_aln; ++k) { const bwt_aln1_t *q = aln + k; if (q->l - q->k + 1 <= rest) { @@ -172,16 +172,16 @@ bwa_cigar_t *bwa_refine_gapped_core(bwtint_t l_pac, const ubyte_t *pacseq, int l ref_len = len + abs(ext); if (ext > 0) { - ref_seq = (ubyte_t*)calloc(ref_len, 1); + ref_seq = (ubyte_t*)xcalloc(ref_len, 1); for (k = __pos; k < __pos + ref_len && k < l_pac; ++k) ref_seq[l++] = pacseq[k>>2] >> ((~k&3)<<1) & 3; } else { int64_t x = __pos + (is_end_correct? len : ref_len); - ref_seq = (ubyte_t*)calloc(ref_len, 1); + ref_seq = (ubyte_t*)xcalloc(ref_len, 1); for (l = 0, k = x - ref_len > 0? x - ref_len : 0; k < x && k < l_pac; ++k) ref_seq[l++] = pacseq[k>>2] >> ((~k&3)<<1) & 3; } - path = (path_t*)calloc(l+len, sizeof(path_t)); + path = (path_t*)xcalloc(l+len, sizeof(path_t)); aln_global_core(ref_seq, l, (ubyte_t*)seq, len, &ap, path, &path_len); cigar = bwa_aln_path2cigar(path, path_len, n_cigar); @@ -257,7 +257,7 @@ char *bwa_cal_md1(int n_cigar, bwa_cigar_t *cigar, int len, bwtint_t pos, ubyte_ } ksprintf(str, "%d", u); *_nm = nm; - return strdup(str->s); + return xstrdup(str->s); } void bwa_correct_trimmed(bwa_seq_t *s) @@ -269,11 +269,11 @@ void bwa_correct_trimmed(bwa_seq_t *s) } else { if (s->cigar == 0) { s->n_cigar = 2; - s->cigar = calloc(s->n_cigar, sizeof(bwa_cigar_t)); + s->cigar = xcalloc(s->n_cigar, sizeof(bwa_cigar_t)); s->cigar[0] = __cigar_create(0, s->len); } else { ++s->n_cigar; - s->cigar = realloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); + s->cigar = xrealloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); } s->cigar[s->n_cigar-1] = __cigar_create(3, (s->full_len - s->len)); } @@ -283,11 +283,11 @@ void bwa_correct_trimmed(bwa_seq_t *s) } else { if (s->cigar == 0) { s->n_cigar = 2; - s->cigar = calloc(s->n_cigar, sizeof(bwa_cigar_t)); + s->cigar = xcalloc(s->n_cigar, sizeof(bwa_cigar_t)); s->cigar[1] = __cigar_create(0, s->len); } else { ++s->n_cigar; - s->cigar = realloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); + s->cigar = xrealloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); memmove(s->cigar + 1, s->cigar, (s->n_cigar-1) * sizeof(bwa_cigar_t)); } s->cigar[0] = __cigar_create(3, (s->full_len - s->len)); @@ -303,15 +303,15 @@ void bwa_refine_gapped(const bntseq_t *bns, int n_seqs, bwa_seq_t *seqs, ubyte_t kstring_t *str; if (ntbns) { // in color space - ntpac = (ubyte_t*)calloc(ntbns->l_pac/4+1, 1); - rewind(ntbns->fp_pac); - fread(ntpac, 1, ntbns->l_pac/4 + 1, ntbns->fp_pac); + ntpac = (ubyte_t*)xcalloc(ntbns->l_pac/4+1, 1); + err_rewind(ntbns->fp_pac); + err_fread_noeof(ntpac, 1, ntbns->l_pac/4 + 1, ntbns->fp_pac); } if (!_pacseq) { - pacseq = (ubyte_t*)calloc(bns->l_pac/4+1, 1); - rewind(bns->fp_pac); - fread(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); + pacseq = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + err_rewind(bns->fp_pac); + err_fread_noeof(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); } else pacseq = _pacseq; for (i = 0; i != n_seqs; ++i) { bwa_seq_t *s = seqs + i; @@ -351,7 +351,7 @@ void bwa_refine_gapped(const bntseq_t *bns, int n_seqs, bwa_seq_t *seqs, ubyte_t } #endif // generate MD tag - str = (kstring_t*)calloc(1, sizeof(kstring_t)); + str = (kstring_t*)xcalloc(1, sizeof(kstring_t)); for (i = 0; i != n_seqs; ++i) { bwa_seq_t *s = seqs + i; if (s->type != BWA_TYPE_NO_MATCH) { @@ -523,7 +523,7 @@ bntseq_t *bwa_open_nt(const char *prefix) { bntseq_t *ntbns; char *str; - str = (char*)calloc(strlen(prefix) + 10, 1); + str = (char*)xcalloc(strlen(prefix) + 10, 1); strcat(strcpy(str, prefix), ".nt"); ntbns = bns_restore(str); free(str); @@ -566,14 +566,14 @@ int bwa_set_rg(const char *s) if (strstr(s, "@RG") != s) return -1; if (bwa_rg_line) free(bwa_rg_line); if (bwa_rg_id) free(bwa_rg_id); - bwa_rg_line = strdup(s); + bwa_rg_line = xstrdup(s); bwa_rg_id = 0; bwa_escape(bwa_rg_line); p = strstr(bwa_rg_line, "\tID:"); if (p == 0) return -1; p += 4; for (q = p; *q && *q != '\t' && *q != '\n'; ++q); - bwa_rg_id = calloc(q - p + 1, 1); + bwa_rg_id = xcalloc(q - p + 1, 1); for (q = p, r = bwa_rg_id; *q && *q != '\t' && *q != '\n'; ++q) *r++ = *q; return 0; @@ -598,7 +598,7 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f fp_sa = xopen(fn_sa, "r"); m_aln = 0; - fread(&opt, sizeof(gap_opt_t), 1, fp_sa); + err_fread_noeof(&opt, sizeof(gap_opt_t), 1, fp_sa); if (!(opt.mode & BWA_MODE_COMPREAD)) // in color space; initialize ntpac ntbns = bwa_open_nt(prefix); bwa_print_sam_SQ(bns); @@ -614,12 +614,12 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f for (i = 0; i < n_seqs; ++i) { bwa_seq_t *p = seqs + i; int n_aln; - fread(&n_aln, 4, 1, fp_sa); + err_fread_noeof(&n_aln, 4, 1, fp_sa); if (n_aln > m_aln) { m_aln = n_aln; - aln = (bwt_aln1_t*)realloc(aln, sizeof(bwt_aln1_t) * m_aln); + aln = (bwt_aln1_t*)xrealloc(aln, sizeof(bwt_aln1_t) * m_aln); } - fread(aln, sizeof(bwt_aln1_t), n_aln, fp_sa); + err_fread_noeof(aln, sizeof(bwt_aln1_t), n_aln, fp_sa); bwa_aln2seq_core(n_aln, aln, p, 1, n_occ); } @@ -644,7 +644,7 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f bwa_seq_close(ks); if (ntbns) bns_destroy(ntbns); bns_destroy(bns); - fclose(fp_sa); + err_fclose(fp_sa); free(aln); } diff --git a/bwaseqio.c b/bwaseqio.c index e22d4cd..716f9b2 100644 --- a/bwaseqio.c +++ b/bwaseqio.c @@ -5,7 +5,7 @@ #include "bamlite.h" #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +KSEQ_INIT(gzFile, err_gzread) extern unsigned char nst_nt4_table[256]; static char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; @@ -22,7 +22,7 @@ bwa_seqio_t *bwa_bam_open(const char *fn, int which) { bwa_seqio_t *bs; bam_header_t *h; - bs = (bwa_seqio_t*)calloc(1, sizeof(bwa_seqio_t)); + bs = (bwa_seqio_t*)xcalloc(1, sizeof(bwa_seqio_t)); bs->is_bam = 1; bs->which = which; bs->fp = bam_open(fn, "r"); @@ -35,7 +35,7 @@ bwa_seqio_t *bwa_seq_open(const char *fn) { gzFile fp; bwa_seqio_t *bs; - bs = (bwa_seqio_t*)calloc(1, sizeof(bwa_seqio_t)); + bs = (bwa_seqio_t*)xcalloc(1, sizeof(bwa_seqio_t)); fp = xzopen(fn, "r"); bs->ks = kseq_init(fp); return bs; @@ -93,7 +93,7 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com b = bam_init1(); n_seqs = 0; - seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t)); + seqs = (bwa_seq_t*)xcalloc(n_needed, sizeof(bwa_seq_t)); while (bam_read1(bs->fp, b) >= 0) { uint8_t *s, *q; int go = 0; @@ -108,8 +108,8 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com p->full_len = p->clip_len = p->len = l; n_tot += p->full_len; s = bam1_seq(b); q = bam1_qual(b); - p->seq = (ubyte_t*)calloc(p->len + 1, 1); - p->qual = (ubyte_t*)calloc(p->len + 1, 1); + p->seq = (ubyte_t*)xcalloc(p->len + 1, 1); + p->qual = (ubyte_t*)xcalloc(p->len + 1, 1); for (i = 0; i != p->full_len; ++i) { p->seq[i] = bam_nt16_nt4_table[(int)bam1_seqi(s, i)]; p->qual[i] = q[i] + 33 < 126? q[i] + 33 : 126; @@ -119,11 +119,11 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com seq_reverse(p->len, p->qual, 0); } if (trim_qual >= 1) n_trimmed += bwa_trim_read(trim_qual, p); - p->rseq = (ubyte_t*)calloc(p->full_len, 1); + p->rseq = (ubyte_t*)xcalloc(p->full_len, 1); memcpy(p->rseq, p->seq, p->len); seq_reverse(p->len, p->seq, 0); // *IMPORTANT*: will be reversed back in bwa_refine_gapped() seq_reverse(p->len, p->rseq, is_comp); - p->name = strdup((const char*)bam1_qname(b)); + p->name = xstrdup((const char*)bam1_qname(b)); if (n_seqs == n_needed) break; } *n = n_seqs; @@ -153,7 +153,7 @@ bwa_seq_t *bwa_read_seq(bwa_seqio_t *bs, int n_needed, int *n, int mode, int tri } if (bs->is_bam) return bwa_read_bam(bs, n_needed, n, is_comp, trim_qual); // l_bc has no effect for BAM input n_seqs = 0; - seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t)); + seqs = (bwa_seq_t*)xcalloc(n_needed, sizeof(bwa_seq_t)); while ((l = kseq_read(seq)) >= 0) { if ((mode & BWA_MODE_CFY) && (seq->comment.l != 0)) { // skip reads that are marked to be filtered by Casava @@ -184,18 +184,18 @@ bwa_seq_t *bwa_read_seq(bwa_seqio_t *bs, int n_needed, int *n, int mode, int tri p->qual = 0; p->full_len = p->clip_len = p->len = l; n_tot += p->full_len; - p->seq = (ubyte_t*)calloc(p->len, 1); + p->seq = (ubyte_t*)xcalloc(p->full_len, 1); for (i = 0; i != p->full_len; ++i) p->seq[i] = nst_nt4_table[(int)seq->seq.s[i]]; if (seq->qual.l) { // copy quality - p->qual = (ubyte_t*)strdup((char*)seq->qual.s); + p->qual = (ubyte_t*)xstrdup((char*)seq->qual.s); if (trim_qual >= 1) n_trimmed += bwa_trim_read(trim_qual, p); } - p->rseq = (ubyte_t*)calloc(p->full_len, 1); + p->rseq = (ubyte_t*)xcalloc(p->full_len, 1); memcpy(p->rseq, p->seq, p->len); seq_reverse(p->len, p->seq, 0); // *IMPORTANT*: will be reversed back in bwa_refine_gapped() seq_reverse(p->len, p->rseq, is_comp); - p->name = strdup((const char*)seq->name.s); + p->name = xstrdup((const char*)seq->name.s); { // trim /[12]$ int t = strlen(p->name); if (t > 2 && p->name[t-2] == '/' && (p->name[t-1] == '1' || p->name[t-1] == '2')) p->name[t-2] = '\0'; diff --git a/bwt.c b/bwt.c index 966b718..eb85bb0 100644 --- a/bwt.c +++ b/bwt.c @@ -58,11 +58,7 @@ void bwt_cal_sa(bwt_t *bwt, int intv) if (bwt->sa) free(bwt->sa); bwt->sa_intv = intv; bwt->n_sa = (bwt->seq_len + intv) / intv; - bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t)); - if (bwt->sa == 0) { - fprintf(stderr, "[%s] Fail to allocate %.3fMB memory. Abort!\n", __func__, bwt->n_sa * sizeof(bwtint_t) / 1024.0/1024.0); - abort(); - } + bwt->sa = (bwtint_t*)xcalloc(bwt->n_sa, sizeof(bwtint_t)); // calculate SA value isa = 0; sa = bwt->seq_len; for (i = 0; i < bwt->seq_len; ++i) { diff --git a/bwt_gen.c b/bwt_gen.c index cac6a5f..48bd662 100644 --- a/bwt_gen.c +++ b/bwt_gen.c @@ -28,6 +28,7 @@ #include #include #include "QSufSort.h" +#include "utils.h" typedef uint64_t bgint_t; typedef int64_t sbgint_t; @@ -319,25 +320,25 @@ BWT *BWTCreate(const bgint_t textLength, unsigned int *decodeTable) { BWT *bwt; - bwt = (BWT*)calloc(1, sizeof(BWT)); + bwt = (BWT*)xcalloc(1, sizeof(BWT)); bwt->textLength = 0; - bwt->cumulativeFreq = (bgint_t*)calloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); + bwt->cumulativeFreq = (bgint_t*)xcalloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); initializeVAL_bg(bwt->cumulativeFreq, ALPHABET_SIZE + 1, 0); bwt->bwtSizeInWord = 0; // Generate decode tables if (decodeTable == NULL) { - bwt->decodeTable = (unsigned*)calloc(DNA_OCC_CNT_TABLE_SIZE_IN_WORD, sizeof(unsigned int)); + bwt->decodeTable = (unsigned*)xcalloc(DNA_OCC_CNT_TABLE_SIZE_IN_WORD, sizeof(unsigned int)); GenerateDNAOccCountTable(bwt->decodeTable); } else { bwt->decodeTable = decodeTable; } bwt->occMajorSizeInWord = BWTOccValueMajorSizeInWord(textLength); - bwt->occValueMajor = (bgint_t*)calloc(bwt->occMajorSizeInWord, sizeof(bgint_t)); + bwt->occValueMajor = (bgint_t*)xcalloc(bwt->occMajorSizeInWord, sizeof(bgint_t)); bwt->occSizeInWord = 0; bwt->occValue = NULL; @@ -353,16 +354,16 @@ BWTInc *BWTIncCreate(const bgint_t textLength, unsigned int initialMaxBuildSize, if (textLength < incMaxBuildSize) incMaxBuildSize = textLength; if (textLength < initialMaxBuildSize) initialMaxBuildSize = textLength; - bwtInc = (BWTInc*)calloc(1, sizeof(BWTInc)); + bwtInc = (BWTInc*)xcalloc(1, sizeof(BWTInc)); bwtInc->numberOfIterationDone = 0; bwtInc->bwt = BWTCreate(textLength, NULL); bwtInc->initialMaxBuildSize = initialMaxBuildSize; bwtInc->incMaxBuildSize = incMaxBuildSize; - bwtInc->cumulativeCountInCurrentBuild = (bgint_t*)calloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); + bwtInc->cumulativeCountInCurrentBuild = (bgint_t*)xcalloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); initializeVAL_bg(bwtInc->cumulativeCountInCurrentBuild, ALPHABET_SIZE + 1, 0); // Build frequently accessed data - bwtInc->packedShift = (unsigned*)calloc(CHAR_PER_WORD, sizeof(unsigned int)); + bwtInc->packedShift = (unsigned*)xcalloc(CHAR_PER_WORD, sizeof(unsigned int)); for (i=0; ipackedShift[i] = BITS_IN_WORD - (i+1) * BIT_PER_CHAR; @@ -372,7 +373,7 @@ BWTInc *BWTIncCreate(const bgint_t textLength, unsigned int initialMaxBuildSize, + incMaxBuildSize/5 * 3 * (sizeof(bgint_t) / 4); // space for the 3 temporary arrays in each iteration if (bwtInc->availableWord < MIN_AVAILABLE_WORD) bwtInc->availableWord = MIN_AVAILABLE_WORD; // lh3: otherwise segfaul when availableWord is too small fprintf(stderr, "[%s] textLength=%ld, availableWord=%ld\n", __func__, (long)textLength, (long)bwtInc->availableWord); - bwtInc->workingMemory = (unsigned*)calloc(bwtInc->availableWord, BYTES_IN_WORD); + bwtInc->workingMemory = (unsigned*)xcalloc(bwtInc->availableWord, BYTES_IN_WORD); return bwtInc; } @@ -1447,9 +1448,9 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB exit(1); } - fseek(packedFile, -1, SEEK_END); + err_fseek(packedFile, -1, SEEK_END); packedFileLen = ftell(packedFile); - fread(&lastByteLength, sizeof(unsigned char), 1, packedFile); + err_fread_noeof(&lastByteLength, sizeof(unsigned char), 1, packedFile); totalTextLength = TextLengthFromBytePacked(packedFileLen, BIT_PER_CHAR, lastByteLength); bwtInc = BWTIncCreate(totalTextLength, initialMaxBuildSize, incMaxBuildSize); @@ -1463,10 +1464,10 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB } textSizeInByte = textToLoad / CHAR_PER_BYTE; // excluded the odd byte - fseek(packedFile, -2, SEEK_CUR); - fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); - fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile); - fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR); + err_fseek(packedFile, -2, SEEK_CUR); + err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); + err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile); + err_fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR); ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad); BWTIncConstruct(bwtInc, textToLoad); @@ -1479,9 +1480,9 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB textToLoad = totalTextLength - processedTextLength; } textSizeInByte = textToLoad / CHAR_PER_BYTE; - fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); - fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile); - fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); + err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); + err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile); + err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad); BWTIncConstruct(bwtInc, textToLoad); processedTextLength += textToLoad; @@ -1530,11 +1531,11 @@ void BWTSaveBwtCodeAndOcc(const BWT *bwt, const char *bwtFileName, const char *o exit(1); } - fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile); - fwrite(bwt->cumulativeFreq + 1, sizeof(bgint_t), ALPHABET_SIZE, bwtFile); + err_fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile); + err_fwrite(bwt->cumulativeFreq + 1, sizeof(bgint_t), ALPHABET_SIZE, bwtFile); bwtLength = BWTFileSizeInWord(bwt->textLength); - fwrite(bwt->bwtCode, sizeof(unsigned int), bwtLength, bwtFile); - fclose(bwtFile); + err_fwrite(bwt->bwtCode, sizeof(unsigned int), bwtLength, bwtFile); + err_fclose(bwtFile); } void bwt_bwtgen(const char *fn_pac, const char *fn_bwt) diff --git a/bwt_lite.c b/bwt_lite.c index 902e0fc..83dafc4 100644 --- a/bwt_lite.c +++ b/bwt_lite.c @@ -2,6 +2,7 @@ #include #include #include "bwt_lite.h" +#include "utils.h" int is_sa(const uint8_t *T, uint32_t *SA, int n); int is_bwt(uint8_t *T, int n); @@ -10,21 +11,21 @@ bwtl_t *bwtl_seq2bwtl(int len, const uint8_t *seq) { bwtl_t *b; int i; - b = (bwtl_t*)calloc(1, sizeof(bwtl_t)); + b = (bwtl_t*)xcalloc(1, sizeof(bwtl_t)); b->seq_len = len; { // calculate b->bwt uint8_t *s; - b->sa = (uint32_t*)calloc(len + 1, 4); + b->sa = (uint32_t*)xcalloc(len + 1, 4); is_sa(seq, b->sa, len); - s = (uint8_t*)calloc(len + 1, 1); + s = (uint8_t*)xcalloc(len + 1, 1); for (i = 0; i <= len; ++i) { if (b->sa[i] == 0) b->primary = i; else s[i] = seq[b->sa[i] - 1]; } for (i = b->primary; i < len; ++i) s[i] = s[i + 1]; b->bwt_size = (len + 15) / 16; - b->bwt = (uint32_t*)calloc(b->bwt_size, 4); + b->bwt = (uint32_t*)xcalloc(b->bwt_size, 4); for (i = 0; i < len; ++i) b->bwt[i>>4] |= s[i] << ((15 - (i&15)) << 1); free(s); @@ -32,7 +33,7 @@ bwtl_t *bwtl_seq2bwtl(int len, const uint8_t *seq) { // calculate b->occ uint32_t c[4]; b->n_occ = (len + 15) / 16 * 4; - b->occ = (uint32_t*)calloc(b->n_occ, 4); + b->occ = (uint32_t*)xcalloc(b->n_occ, 4); memset(c, 0, 16); for (i = 0; i < len; ++i) { if (i % 16 == 0) diff --git a/bwtaln.c b/bwtaln.c index efc7f66..109f964 100644 --- a/bwtaln.c +++ b/bwtaln.c @@ -19,7 +19,7 @@ gap_opt_t *gap_init_opt() { gap_opt_t *o; - o = (gap_opt_t*)calloc(1, sizeof(gap_opt_t)); + o = (gap_opt_t*)xcalloc(1, sizeof(gap_opt_t)); /* IMPORTANT: s_mm*10 should be about the average base error rate. Voilating this requirement will break pairing! */ o->s_mm = 3; o->s_gapo = 11; o->s_gape = 4; @@ -89,7 +89,7 @@ void bwa_cal_sa_reg_gap(int tid, bwt_t *const bwt, int n_seqs, bwa_seq_t *seqs, if (local_opt.max_diff < local_opt.max_gapo) local_opt.max_gapo = local_opt.max_diff; stack = gap_init_stack(local_opt.max_diff, local_opt.max_gapo, local_opt.max_gape, &local_opt); - seed_w = (bwt_width_t*)calloc(opt->seed_len+1, sizeof(bwt_width_t)); + seed_w = (bwt_width_t*)xcalloc(opt->seed_len+1, sizeof(bwt_width_t)); w = 0; for (i = 0; i != n_seqs; ++i) { bwa_seq_t *p = seqs + i; @@ -99,7 +99,7 @@ void bwa_cal_sa_reg_gap(int tid, bwt_t *const bwt, int n_seqs, bwa_seq_t *seqs, p->sa = 0; p->type = BWA_TYPE_NO_MATCH; p->c1 = p->c2 = 0; p->n_aln = 0; p->aln = 0; if (max_l < p->len) { max_l = p->len; - w = (bwt_width_t*)realloc(w, (max_l + 1) * sizeof(bwt_width_t)); + w = (bwt_width_t*)xrealloc(w, (max_l + 1) * sizeof(bwt_width_t)); memset(w, 0, (max_l + 1) * sizeof(bwt_width_t)); } bwt_cal_width(bwt, p->len, p->seq, w); @@ -162,7 +162,7 @@ void bwa_aln_core(const char *prefix, const char *fn_fa, const gap_opt_t *opt) ks = bwa_open_reads(opt->mode, fn_fa); { // load BWT - char *str = (char*)calloc(strlen(prefix) + 10, 1); + char *str = (char*)xcalloc(strlen(prefix) + 10, 1); strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); free(str); } @@ -185,8 +185,8 @@ void bwa_aln_core(const char *prefix, const char *fn_fa, const gap_opt_t *opt) int j; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t)); - tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); + data = (thread_aux_t*)xcalloc(opt->n_threads, sizeof(thread_aux_t)); + tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); for (j = 0; j < opt->n_threads; ++j) { data[j].tid = j; data[j].bwt = bwt; data[j].n_seqs = n_seqs; data[j].seqs = seqs; data[j].opt = opt; @@ -225,7 +225,7 @@ char *bwa_infer_prefix(const char *hint) int l_hint; FILE *fp; l_hint = strlen(hint); - prefix = malloc(l_hint + 3 + 4 + 1); + prefix = xmalloc(l_hint + 3 + 4 + 1); strcpy(prefix, hint); strcpy(prefix + l_hint, ".64.bwt"); if ((fp = fopen(prefix, "rb")) != 0) { diff --git a/bwtgap.c b/bwtgap.c index 364717c..cef9561 100644 --- a/bwtgap.c +++ b/bwtgap.c @@ -3,6 +3,7 @@ #include #include "bwtgap.h" #include "bwtaln.h" +#include "utils.h" #define STATE_M 0 #define STATE_I 1 @@ -13,9 +14,9 @@ gap_stack_t *gap_init_stack2(int max_score) { gap_stack_t *stack; - stack = (gap_stack_t*)calloc(1, sizeof(gap_stack_t)); + stack = (gap_stack_t*)xcalloc(1, sizeof(gap_stack_t)); stack->n_stacks = max_score; - stack->stacks = (gap_stack1_t*)calloc(stack->n_stacks, sizeof(gap_stack1_t)); + stack->stacks = (gap_stack1_t*)xcalloc(stack->n_stacks, sizeof(gap_stack1_t)); return stack; } @@ -51,7 +52,7 @@ static inline void gap_push(gap_stack_t *stack, int i, bwtint_t k, bwtint_t l, i q = stack->stacks + score; if (q->n_entries == q->m_entries) { q->m_entries = q->m_entries? q->m_entries<<1 : 4; - q->stack = (gap_entry_t*)realloc(q->stack, sizeof(gap_entry_t) * q->m_entries); + q->stack = (gap_entry_t*)xrealloc(q->stack, sizeof(gap_entry_t) * q->m_entries); } p = q->stack + q->n_entries; p->info = (u_int32_t)score<<21 | i; p->k = k; p->l = l; @@ -110,7 +111,7 @@ bwt_aln1_t *bwt_match_gap(bwt_t *const bwt, int len, const ubyte_t *seq, bwt_wid bwt_aln1_t *aln; m_aln = 4; n_aln = 0; - aln = (bwt_aln1_t*)calloc(m_aln, sizeof(bwt_aln1_t)); + aln = (bwt_aln1_t*)xcalloc(m_aln, sizeof(bwt_aln1_t)); // check whether there are too many N for (j = _j = 0; j < len; ++j) @@ -177,7 +178,7 @@ bwt_aln1_t *bwt_match_gap(bwt_t *const bwt, int len, const ubyte_t *seq, bwt_wid gap_shadow(l - k + 1, len, bwt->seq_len, e.last_diff_pos, width); if (n_aln == m_aln) { m_aln <<= 1; - aln = (bwt_aln1_t*)realloc(aln, m_aln * sizeof(bwt_aln1_t)); + aln = (bwt_aln1_t*)xrealloc(aln, m_aln * sizeof(bwt_aln1_t)); memset(aln + m_aln/2, 0, m_aln/2*sizeof(bwt_aln1_t)); } p = aln + n_aln; diff --git a/bwtindex.c b/bwtindex.c index 938e982..f430e62 100644 --- a/bwtindex.c +++ b/bwtindex.c @@ -54,7 +54,7 @@ int bwa_index(int argc, char *argv[]) else if (strcmp(optarg, "is") == 0) algo_type = 3; else err_fatal(__func__, "unknown algorithm: '%s'.", optarg); break; - case 'p': prefix = strdup(optarg); break; + case 'p': prefix = xstrdup(optarg); break; case 'c': is_color = 1; break; case '6': is_64 = 1; break; default: return 1; @@ -75,13 +75,13 @@ int bwa_index(int argc, char *argv[]) return 1; } if (prefix == 0) { - prefix = malloc(strlen(argv[optind]) + 4); + prefix = xmalloc(strlen(argv[optind]) + 4); strcpy(prefix, argv[optind]); if (is_64) strcat(prefix, ".64"); } - str = (char*)calloc(strlen(prefix) + 10, 1); - str2 = (char*)calloc(strlen(prefix) + 10, 1); - str3 = (char*)calloc(strlen(prefix) + 10, 1); + str = (char*)xcalloc(strlen(prefix) + 10, 1); + str2 = (char*)xcalloc(strlen(prefix) + 10, 1); + str3 = (char*)xcalloc(strlen(prefix) + 10, 1); if (is_color == 0) { // nucleotide indexing gzFile fp = xzopen(argv[optind], "r"); diff --git a/bwtio.c b/bwtio.c index 7508609..0d4623e 100644 --- a/bwtio.c +++ b/bwtio.c @@ -6,24 +6,24 @@ void bwt_dump_bwt(const char *fn, const bwt_t *bwt) { - FILE *fp; + FILE *fp = NULL; fp = xopen(fn, "wb"); - fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp); - fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp); - fwrite(bwt->bwt, 4, bwt->bwt_size, fp); - fclose(fp); + err_fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp); + err_fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp); + err_fwrite(bwt->bwt, 4, bwt->bwt_size, fp); + err_fclose(fp); } void bwt_dump_sa(const char *fn, const bwt_t *bwt) { FILE *fp; fp = xopen(fn, "wb"); - fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp); - fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp); - fwrite(&bwt->sa_intv, sizeof(bwtint_t), 1, fp); - fwrite(&bwt->seq_len, sizeof(bwtint_t), 1, fp); - fwrite(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp); - fclose(fp); + err_fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp); + err_fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp); + err_fwrite(&bwt->sa_intv, sizeof(bwtint_t), 1, fp); + err_fwrite(&bwt->seq_len, sizeof(bwtint_t), 1, fp); + err_fwrite(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp); + err_fclose(fp); } void bwt_restore_sa(const char *fn, bwt_t *bwt) @@ -33,19 +33,19 @@ void bwt_restore_sa(const char *fn, bwt_t *bwt) bwtint_t primary; fp = xopen(fn, "rb"); - fread(&primary, sizeof(bwtint_t), 1, fp); + err_fread_noeof(&primary, sizeof(bwtint_t), 1, fp); xassert(primary == bwt->primary, "SA-BWT inconsistency: primary is not the same."); - fread(skipped, sizeof(bwtint_t), 4, fp); // skip - fread(&bwt->sa_intv, sizeof(bwtint_t), 1, fp); - fread(&primary, sizeof(bwtint_t), 1, fp); + err_fread_noeof(skipped, sizeof(bwtint_t), 4, fp); // skip + err_fread_noeof(&bwt->sa_intv, sizeof(bwtint_t), 1, fp); + err_fread_noeof(&primary, sizeof(bwtint_t), 1, fp); xassert(primary == bwt->seq_len, "SA-BWT inconsistency: seq_len is not the same."); bwt->n_sa = (bwt->seq_len + bwt->sa_intv) / bwt->sa_intv; - bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t)); + bwt->sa = (bwtint_t*)xcalloc(bwt->n_sa, sizeof(bwtint_t)); bwt->sa[0] = -1; - fread(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp); - fclose(fp); + err_fread_noeof(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp); + err_fclose(fp); } bwt_t *bwt_restore_bwt(const char *fn) @@ -53,17 +53,17 @@ bwt_t *bwt_restore_bwt(const char *fn) bwt_t *bwt; FILE *fp; - bwt = (bwt_t*)calloc(1, sizeof(bwt_t)); + bwt = (bwt_t*)xcalloc(1, sizeof(bwt_t)); fp = xopen(fn, "rb"); - fseek(fp, 0, SEEK_END); - bwt->bwt_size = (ftell(fp) - sizeof(bwtint_t) * 5) >> 2; - bwt->bwt = (uint32_t*)calloc(bwt->bwt_size, 4); - fseek(fp, 0, SEEK_SET); - fread(&bwt->primary, sizeof(bwtint_t), 1, fp); - fread(bwt->L2+1, sizeof(bwtint_t), 4, fp); - fread(bwt->bwt, 4, bwt->bwt_size, fp); + err_fseek(fp, 0, SEEK_END); + bwt->bwt_size = (err_ftell(fp) - sizeof(bwtint_t) * 5) >> 2; + bwt->bwt = (uint32_t*)xcalloc(bwt->bwt_size, 4); + err_fseek(fp, 0, SEEK_SET); + err_fread_noeof(&bwt->primary, sizeof(bwtint_t), 1, fp); + err_fread_noeof(bwt->L2+1, sizeof(bwtint_t), 4, fp); + err_fread_noeof(bwt->bwt, 4, bwt->bwt_size, fp); bwt->seq_len = bwt->L2[4]; - fclose(fp); + err_fclose(fp); bwt_gen_cnt_table(bwt); return bwt; diff --git a/bwtmisc.c b/bwtmisc.c index c35d684..49aa5aa 100644 --- a/bwtmisc.c +++ b/bwtmisc.c @@ -46,10 +46,10 @@ int64_t bwa_seq_len(const char *fn_pac) int64_t pac_len; ubyte_t c; fp = xopen(fn_pac, "rb"); - fseek(fp, -1, SEEK_END); - pac_len = ftell(fp); - fread(&c, 1, 1, fp); - fclose(fp); + err_fseek(fp, -1, SEEK_END); + pac_len = err_ftell(fp); + err_fread_noeof(&c, 1, 1, fp); + err_fclose(fp); return (pac_len - 1) * 4 + (int)c; } @@ -61,18 +61,18 @@ bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is) FILE *fp; // initialization - bwt = (bwt_t*)calloc(1, sizeof(bwt_t)); + bwt = (bwt_t*)xcalloc(1, sizeof(bwt_t)); bwt->seq_len = bwa_seq_len(fn_pac); bwt->bwt_size = (bwt->seq_len + 15) >> 4; fp = xopen(fn_pac, "rb"); // prepare sequence pac_size = (bwt->seq_len>>2) + ((bwt->seq_len&3) == 0? 0 : 1); - buf2 = (ubyte_t*)calloc(pac_size, 1); - fread(buf2, 1, pac_size, fp); - fclose(fp); + buf2 = (ubyte_t*)xcalloc(pac_size, 1); + err_fread_noeof(buf2, 1, pac_size, fp); + err_fclose(fp); memset(bwt->L2, 0, 5 * 4); - buf = (ubyte_t*)calloc(bwt->seq_len + 1, 1); + buf = (ubyte_t*)xcalloc(bwt->seq_len + 1, 1); for (i = 0; i < bwt->seq_len; ++i) { buf[i] = buf2[i>>2] >> ((3 - (i&3)) << 1) & 3; ++bwt->L2[1+buf[i]]; @@ -90,7 +90,7 @@ bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is) err_fatal_simple("libdivsufsort is not compiled in."); #endif } - bwt->bwt = (u_int32_t*)calloc(bwt->bwt_size, 4); + bwt->bwt = (u_int32_t*)xcalloc(bwt->bwt_size, 4); for (i = 0; i < bwt->seq_len; ++i) bwt->bwt[i>>4] |= buf[i] << ((15 - (i&15)) << 1); free(buf); @@ -126,7 +126,7 @@ void bwt_bwtupdate_core(bwt_t *bwt) n_occ = (bwt->seq_len + OCC_INTERVAL - 1) / OCC_INTERVAL + 1; bwt->bwt_size += n_occ * sizeof(bwtint_t); // the new size - buf = (uint32_t*)calloc(bwt->bwt_size, 4); // will be the new bwt + buf = (uint32_t*)xcalloc(bwt->bwt_size, 4); // will be the new bwt c[0] = c[1] = c[2] = c[3] = 0; for (i = k = 0; i < bwt->seq_len; ++i) { if (i % OCC_INTERVAL == 0) { @@ -167,10 +167,10 @@ uint8_t *bwa_pac2cspac_core(const bntseq_t *bns) uint8_t *pac, *cspac; bwtint_t i; int c1, c2; - pac = (uint8_t*)calloc(bns->l_pac/4 + 1, 1); - cspac = (uint8_t*)calloc(bns->l_pac/4 + 1, 1); - fread(pac, 1, bns->l_pac/4+1, bns->fp_pac); - rewind(bns->fp_pac); + pac = (uint8_t*)xcalloc(bns->l_pac/4 + 1, 1); + cspac = (uint8_t*)xcalloc(bns->l_pac/4 + 1, 1); + err_fread_noeof(pac, 1, bns->l_pac/4+1, bns->fp_pac); + err_rewind(bns->fp_pac); c1 = pac[0]>>6; cspac[0] = c1<<6; for (i = 1; i < bns->l_pac; ++i) { c2 = pac[i>>2] >> (~i&3)*2 & 3; @@ -196,13 +196,13 @@ int bwa_pac2cspac(int argc, char *argv[]) cspac = bwa_pac2cspac_core(bns); bns_dump(bns, argv[2]); // now write cspac - str = (char*)calloc(strlen(argv[2]) + 5, 1); + str = (char*)xcalloc(strlen(argv[2]) + 5, 1); strcat(strcpy(str, argv[2]), ".pac"); fp = xopen(str, "wb"); - fwrite(cspac, 1, bns->l_pac/4 + 1, fp); + err_fwrite(cspac, 1, bns->l_pac/4 + 1, fp); ct = bns->l_pac % 4; - fwrite(&ct, 1, 1, fp); - fclose(fp); + err_fwrite(&ct, 1, 1, fp); + err_fclose(fp); bns_destroy(bns); free(cspac); return 0; diff --git a/bwtsw2_aux.c b/bwtsw2_aux.c index 5e8161c..ca39919 100644 --- a/bwtsw2_aux.c +++ b/bwtsw2_aux.c @@ -15,7 +15,7 @@ #include "kstring.h" #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +KSEQ_INIT(gzFile, err_gzread) #include "ksort.h" #define __left_lt(a, b) ((a).end > (b).end) @@ -47,7 +47,7 @@ extern int bsw2_resolve_query_overlaps(bwtsw2_t *b, float mask_level); bsw2opt_t *bsw2_init_opt() { - bsw2opt_t *o = (bsw2opt_t*)calloc(1, sizeof(bsw2opt_t)); + bsw2opt_t *o = (bsw2opt_t*)xcalloc(1, sizeof(bsw2opt_t)); o->a = 1; o->b = 3; o->q = 5; o->r = 2; o->t = 30; o->bw = 50; o->max_ins = 20000; @@ -72,11 +72,11 @@ void bsw2_destroy(bwtsw2_t *b) bwtsw2_t *bsw2_dup_no_cigar(const bwtsw2_t *b) { bwtsw2_t *p; - p = calloc(1, sizeof(bwtsw2_t)); + p = xcalloc(1, sizeof(bwtsw2_t)); p->max = p->n = b->n; if (b->n) { kroundup32(p->max); - p->hits = calloc(p->max, sizeof(bsw2hit_t)); + p->hits = xcalloc(p->max, sizeof(bsw2hit_t)); memcpy(p->hits, b->hits, p->n * sizeof(bsw2hit_t)); } return p; @@ -100,10 +100,10 @@ void bsw2_extend_left(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *_query, int lq par.matrix = matrix; __gen_ap(par, opt); - query = calloc(lq, 1); + query = xcalloc(lq, 1); // sort according to the descending order of query end ks_introsort(hit, b->n, b->hits); - target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); + target = xcalloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); // reverse _query for (i = 0; i < lq; ++i) query[lq - i - 1] = _query[i]; // core loop @@ -146,7 +146,7 @@ void bsw2_extend_rght(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *query, int lq, par.matrix = matrix; __gen_ap(par, opt); - target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); + target = xcalloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); for (i = 0; i < b->n; ++i) { bsw2hit_t *p = b->hits + i; int lt = ((lq - p->beg + 1) / 2 * opt->a + opt->r) / opt->r + lq; @@ -178,8 +178,8 @@ static void gen_cigar(const bsw2opt_t *opt, int lq, uint8_t *seq[2], const uint8 par.matrix = matrix; __gen_ap(par, opt); i = ((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq; // maximum possible target length - target = calloc(i, 1); - path = calloc(i + lq, sizeof(path_t)); + target = xcalloc(i, 1); + path = xcalloc(i + lq, sizeof(path_t)); // generate CIGAR for (i = 0; i < b->n; ++i) { bsw2hit_t *p = b->hits + i; @@ -206,7 +206,7 @@ static void gen_cigar(const bsw2opt_t *opt, int lq, uint8_t *seq[2], const uint8 } #endif if (beg != 0 || end < lq) { // write soft clipping - q->cigar = realloc(q->cigar, 4 * (q->n_cigar + 2)); + q->cigar = xrealloc(q->cigar, 4 * (q->n_cigar + 2)); if (beg != 0) { memmove(q->cigar + 1, q->cigar, q->n_cigar * 4); q->cigar[0] = beg<<4 | 4; @@ -238,7 +238,7 @@ static void merge_hits(bwtsw2_t *b[2], int l, int is_reverse) int i; if (b[0]->n + b[1]->n > b[0]->max) { b[0]->max = b[0]->n + b[1]->n; - b[0]->hits = realloc(b[0]->hits, b[0]->max * sizeof(bsw2hit_t)); + b[0]->hits = xrealloc(b[0]->hits, b[0]->max * sizeof(bsw2hit_t)); } for (i = 0; i < b[1]->n; ++i) { bsw2hit_t *p = b[0]->hits + b[0]->n + i; @@ -266,9 +266,9 @@ static bwtsw2_t *bsw2_aln1_core(const bsw2opt_t *opt, const bntseq_t *bns, uint8 _b = bsw2_core(bns, opt, query, target, pool); bwtl_destroy(query); for (k = 0; k < 2; ++k) { - bb[k] = calloc(2, sizeof(void*)); - bb[k][0] = calloc(1, sizeof(bwtsw2_t)); - bb[k][1] = calloc(1, sizeof(bwtsw2_t)); + bb[k] = xcalloc(2, sizeof(void*)); + bb[k][0] = xcalloc(1, sizeof(bwtsw2_t)); + bb[k][1] = xcalloc(1, sizeof(bwtsw2_t)); } for (k = 0; k < 2; ++k) { // separate _b into bb[2] based on the strand for (j = 0; j < _b[k]->n; ++j) { @@ -276,7 +276,7 @@ static bwtsw2_t *bsw2_aln1_core(const bsw2opt_t *opt, const bntseq_t *bns, uint8 p = bb[_b[k]->hits[j].is_rev][k]; if (p->n == p->max) { p->max = p->max? p->max<<1 : 8; - p->hits = realloc(p->hits, p->max * sizeof(bsw2hit_t)); + p->hits = xrealloc(p->hits, p->max * sizeof(bsw2hit_t)); } q = &p->hits[p->n++]; *q = _b[k]->hits[j]; @@ -355,7 +355,7 @@ static int fix_cigar(const bntseq_t *bns, bsw2hit_t *p, int n_cigar, uint32_t *c uint32_t *cn; bwtint_t kk = 0; nc = mq[0] = mq[1] = nlen[0] = nlen[1] = 0; - cn = calloc(n_cigar + 3, 4); + cn = xcalloc(n_cigar + 3, 4); x = coor; y = 0; for (i = j = 0; i < n_cigar; ++i) { int op = cigar[i]&0xf, ln = cigar[i]>>4; @@ -434,9 +434,9 @@ static void write_aux(const bsw2opt_t *opt, const bntseq_t *bns, int qlen, uint8 if (b->n<<1 < b->max) { b->max = b->n; kroundup32(b->max); - b->hits = realloc(b->hits, b->max * sizeof(bsw2hit_t)); + b->hits = xrealloc(b->hits, b->max * sizeof(bsw2hit_t)); } - b->aux = calloc(b->n, sizeof(bsw2aux_t)); + b->aux = xcalloc(b->n, sizeof(bsw2aux_t)); // generate CIGAR gen_cigar(opt, qlen, seq, pac, b, name); // fix CIGAR, generate mapQ, and write chromosomal position @@ -596,7 +596,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t bsw2opt_t opt; bsw2global_t *pool = bsw2_global_init(); bwtsw2_t **buf; - buf = calloc(_seq->n, sizeof(void*)); + buf = xcalloc(_seq->n, sizeof(void*)); for (x = 0; x < _seq->n; ++x) { bsw2seq1_t *p = _seq->seq + x; uint8_t *seq[2], *rseq[2]; @@ -607,10 +607,10 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t if (pool->max_l < l) { // then enlarge working space for aln_extend_core() int tmp = ((l + 1) / 2 * opt.a + opt.r) / opt.r + l; pool->max_l = l; - pool->aln_mem = realloc(pool->aln_mem, (tmp + 2) * 24); + pool->aln_mem = xrealloc(pool->aln_mem, (tmp + 2) * 24); } // set seq[2] and rseq[2] - seq[0] = calloc(l * 4, 1); + seq[0] = xcalloc(l * 4, 1); seq[1] = seq[0] + l; rseq[0] = seq[1] + l; rseq[1] = rseq[0] + l; // convert sequences to 2-bit representation @@ -623,7 +623,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t rseq[1][i] = c; } if (l - k < opt.t) { // too few unambiguous bases - buf[x] = calloc(1, sizeof(bwtsw2_t)); + buf[x] = xcalloc(1, sizeof(bwtsw2_t)); free(seq[0]); continue; } // alignment @@ -655,7 +655,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t bsw2seq1_t *p = _seq->seq + x; uint8_t *seq[2]; int i; - seq[0] = malloc(p->l * 2); seq[1] = seq[0] + p->l; + seq[0] = xmalloc(p->l * 2); seq[1] = seq[0] + p->l; for (i = 0; i < p->l; ++i) { int c = nst_nt4_table[(int)p->seq[i]]; if (c >= 4) c = (int)(drand48() * 4); @@ -711,16 +711,16 @@ static void process_seqs(bsw2seq_t *_seq, const bsw2opt_t *opt, const bntseq_t * int j; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t)); - tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); + data = (thread_aux_t*)xcalloc(opt->n_threads, sizeof(thread_aux_t)); + tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); for (j = 0; j < opt->n_threads; ++j) { thread_aux_t *p = data + j; p->tid = j; p->_opt = opt; p->bns = bns; p->is_pe = is_pe; p->pac = pac; p->target = target; - p->_seq = calloc(1, sizeof(bsw2seq_t)); + p->_seq = xcalloc(1, sizeof(bsw2seq_t)); p->_seq->max = (_seq->n + opt->n_threads - 1) / opt->n_threads + 1; p->_seq->n = 0; - p->_seq->seq = calloc(p->_seq->max, sizeof(bsw2seq1_t)); + p->_seq->seq = xcalloc(p->_seq->max, sizeof(bsw2seq1_t)); } for (i = 0; i < _seq->n; ++i) { // assign sequences to each thread bsw2seq_t *p = data[(i>>is_pe)%opt->n_threads]._seq; @@ -760,10 +760,10 @@ static void kseq_to_bsw2seq(const kseq_t *ks, bsw2seq1_t *p) { p->tid = -1; p->l = ks->seq.l; - p->name = strdup(ks->name.s); - p->seq = strdup(ks->seq.s); - p->qual = ks->qual.l? strdup(ks->qual.s) : 0; - p->comment = ks->comment.l? strdup(ks->comment.s) : 0; + p->name = xstrdup(ks->name.s); + p->seq = xstrdup(ks->seq.s); + p->qual = ks->qual.l? xstrdup(ks->qual.s) : 0; + p->comment = ks->comment.l? xstrdup(ks->comment.s) : 0; p->sam = 0; } @@ -775,17 +775,13 @@ void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target, c uint8_t *pac; bsw2seq_t *_seq; - pac = calloc(bns->l_pac/4+1, 1); - if (pac == 0) { - fprintf(stderr, "[bsw2_aln] insufficient memory!\n"); - return; - } + pac = xcalloc(bns->l_pac/4+1, 1); for (l = 0; l < bns->n_seqs; ++l) printf("@SQ\tSN:%s\tLN:%d\n", bns->anns[l].name, bns->anns[l].len); - fread(pac, 1, bns->l_pac/4+1, bns->fp_pac); + err_fread_noeof(pac, 1, bns->l_pac/4+1, bns->fp_pac); fp = xzopen(fn, "r"); ks = kseq_init(fp); - _seq = calloc(1, sizeof(bsw2seq_t)); + _seq = xcalloc(1, sizeof(bsw2seq_t)); if (fn2) { fp2 = xzopen(fn2, "r"); ks2 = kseq_init(fp2); @@ -796,7 +792,7 @@ void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target, c ks->name.l -= 2, ks->name.s[ks->name.l] = 0; if (_seq->n == _seq->max) { _seq->max = _seq->max? _seq->max<<1 : 1024; - _seq->seq = realloc(_seq->seq, _seq->max * sizeof(bsw2seq1_t)); + _seq->seq = xrealloc(_seq->seq, _seq->max * sizeof(bsw2seq1_t)); } kseq_to_bsw2seq(ks, &_seq->seq[_seq->n++]); size += ks->seq.l; diff --git a/bwtsw2_chain.c b/bwtsw2_chain.c index 381d0b7..6bd320f 100644 --- a/bwtsw2_chain.c +++ b/bwtsw2_chain.c @@ -1,5 +1,6 @@ #include #include "bwtsw2.h" +#include "utils.h" typedef struct { uint32_t tbeg, tend; @@ -48,9 +49,9 @@ void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2]) char *flag; // initialization n[0] = b[0]->n; n[1] = b[1]->n; - z[0] = calloc(n[0] + n[1], sizeof(hsaip_t)); + z[0] = xcalloc(n[0] + n[1], sizeof(hsaip_t)); z[1] = z[0] + n[0]; - chain[0] = calloc(n[0] + n[1], sizeof(hsaip_t)); + chain[0] = xcalloc(n[0] + n[1], sizeof(hsaip_t)); for (k = j = 0; k < 2; ++k) { for (i = 0; i < b[k]->n; ++i) { bsw2hit_t *p = b[k]->hits + i; @@ -73,7 +74,7 @@ void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2]) } //for (k = 0; k < m[0]; ++k) printf("%d, [%d,%d), [%d,%d)\n", chain[0][k].chain, chain[0][k].tbeg, chain[0][k].tend, chain[0][k].qbeg, chain[0][k].qend); // filtering - flag = calloc(m[0] + m[1], 1); + flag = xcalloc(m[0] + m[1], 1); ks_introsort(hsaip, m[0] + m[1], chain[0]); for (k = 1; k < m[0] + m[1]; ++k) { hsaip_t *p = chain[0] + k; diff --git a/bwtsw2_core.c b/bwtsw2_core.c index 67f126c..d64f74b 100644 --- a/bwtsw2_core.c +++ b/bwtsw2_core.c @@ -7,6 +7,7 @@ #include "bwtsw2.h" #include "bwt.h" #include "kvec.h" +#include "utils.h" typedef struct { bwtint_t k, l; @@ -71,7 +72,7 @@ typedef struct __mempool_t { inline static bsw2entry_p mp_alloc(mempool_t *mp) { ++mp->cnt; - if (kv_size(mp->pool) == 0) return (bsw2entry_t*)calloc(1, sizeof(bsw2entry_t)); + if (kv_size(mp->pool) == 0) return (bsw2entry_t*)xcalloc(1, sizeof(bsw2entry_t)); else return kv_pop(mp->pool); } inline static void mp_free(mempool_t *mp, bsw2entry_p e) @@ -133,7 +134,7 @@ static void cut_tail(bsw2entry_t *u, int T, bsw2entry_t *aux) if (u->n <= T) return; if (aux->max < u->n) { aux->max = u->n; - aux->array = (bsw2cell_t*)realloc(aux->array, aux->max * sizeof(bsw2cell_t)); + aux->array = (bsw2cell_t*)xrealloc(aux->array, aux->max * sizeof(bsw2cell_t)); } a = (int*)aux->array; for (i = n = 0; i != u->n; ++i) @@ -184,7 +185,7 @@ static void merge_entry(const bsw2opt_t * __restrict opt, bsw2entry_t *u, bsw2en int i; if (u->n + v->n >= u->max) { u->max = u->n + v->n; - u->array = (bsw2cell_t*)realloc(u->array, u->max * sizeof(bsw2cell_t)); + u->array = (bsw2cell_t*)xrealloc(u->array, u->max * sizeof(bsw2cell_t)); } for (i = 0; i != v->n; ++i) { bsw2cell_t *p = v->array + i; @@ -202,7 +203,7 @@ static inline bsw2cell_t *push_array_p(bsw2entry_t *e) { if (e->n == e->max) { e->max = e->max? e->max<<1 : 256; - e->array = (bsw2cell_t*)realloc(e->array, sizeof(bsw2cell_t) * e->max); + e->array = (bsw2cell_t*)xrealloc(e->array, sizeof(bsw2cell_t) * e->max); } return e->array + e->n; } @@ -250,7 +251,7 @@ static void save_narrow_hits(const bwtl_t *bwtl, bsw2entry_t *u, bwtsw2_t *b1, i if (p->G >= t && p->ql - p->qk + 1 <= IS) { // good narrow hit if (b1->max == b1->n) { b1->max = b1->max? b1->max<<1 : 4; - b1->hits = realloc(b1->hits, b1->max * sizeof(bsw2hit_t)); + b1->hits = xrealloc(b1->hits, b1->max * sizeof(bsw2hit_t)); } q = &b1->hits[b1->n++]; q->k = p->qk; q->l = p->ql; @@ -279,7 +280,7 @@ int bsw2_resolve_duphits(const bntseq_t *bns, const bwt_t *bwt, bwtsw2_t *b, int else if (p->G > 0) ++n; } b->n = b->max = n; - b->hits = calloc(b->max, sizeof(bsw2hit_t)); + b->hits = xcalloc(b->max, sizeof(bsw2hit_t)); for (i = j = 0; i < old_n; ++i) { bsw2hit_t *p = old_hits + i; if (p->l - p->k + 1 <= IS) { // the hit is no so repetitive @@ -399,9 +400,9 @@ bsw2global_t *bsw2_global_init() { bsw2global_t *pool; bsw2stack_t *stack; - pool = calloc(1, sizeof(bsw2global_t)); - stack = calloc(1, sizeof(bsw2stack_t)); - stack->pool = (mempool_t*)calloc(1, sizeof(mempool_t)); + pool = xcalloc(1, sizeof(bsw2global_t)); + stack = xcalloc(1, sizeof(bsw2stack_t)); + stack->pool = (mempool_t*)xcalloc(1, sizeof(mempool_t)); pool->stack = (void*)stack; return pool; } @@ -461,13 +462,13 @@ bwtsw2_t **bsw2_core(const bntseq_t *bns, const bsw2opt_t *opt, const bwtl_t *ta rhash = kh_init(qintv); init_bwtsw2(target, query, stack); heap_size = opt->z; - heap = calloc(heap_size, sizeof(int)); + heap = xcalloc(heap_size, sizeof(int)); // initialize the return struct - b = (bwtsw2_t*)calloc(1, sizeof(bwtsw2_t)); + b = (bwtsw2_t*)xcalloc(1, sizeof(bwtsw2_t)); b->n = b->max = target->seq_len * 2; - b->hits = calloc(b->max, sizeof(bsw2hit_t)); - b1 = (bwtsw2_t*)calloc(1, sizeof(bwtsw2_t)); - b_ret = calloc(2, sizeof(void*)); + b->hits = xcalloc(b->max, sizeof(bsw2hit_t)); + b1 = (bwtsw2_t*)xcalloc(1, sizeof(bwtsw2_t)); + b_ret = xcalloc(2, sizeof(void*)); b_ret[0] = b; b_ret[1] = b1; // initialize timer getrusage(0, &last); diff --git a/bwtsw2_pair.c b/bwtsw2_pair.c index a6f4d80..d195a09 100644 --- a/bwtsw2_pair.c +++ b/bwtsw2_pair.c @@ -2,6 +2,7 @@ #include #include #include +#include "utils.h" #include "bwt.h" #include "bntseq.h" #include "bwtsw2.h" @@ -30,7 +31,7 @@ bsw2pestat_t bsw2_stat(int n, bwtsw2_t **buf, kstring_t *msg, int max_ins) bsw2pestat_t r; memset(&r, 0, sizeof(bsw2pestat_t)); - isize = calloc(n, 8); + isize = xcalloc(n, 8); for (i = k = 0; i < n; i += 2) { bsw2hit_t *t[2]; int l; @@ -113,7 +114,7 @@ void bsw2_pair1(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, const b if (end > l_pac) end = l_pac; if (end - beg < l_mseq) return; // generate the sequence - seq = malloc(l_mseq + (end - beg)); + seq = xmalloc(l_mseq + (end - beg)); ref = seq + l_mseq; for (k = beg; k < end; ++k) ref[k - beg] = pac[k>>2] >> ((~k&3)<<1) & 0x3; @@ -221,7 +222,7 @@ void bsw2_pair(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, int n, b a[which].flag |= BSW2_FLAG_RESCUED; if (p[1]->max == 0) { p[1]->max = 1; - p[1]->hits = malloc(sizeof(bsw2hit_t)); + p[1]->hits = xmalloc(sizeof(bsw2hit_t)); } p[1]->hits[0] = a[which]; p[1]->n = 1; diff --git a/cs2nt.c b/cs2nt.c index dfbce60..3084f11 100644 --- a/cs2nt.c +++ b/cs2nt.c @@ -3,6 +3,7 @@ #include #include "bwtaln.h" #include "stdaln.h" +#include "utils.h" /* Here is a delicate example. ref_nt=ATTAAC(RBRBG), read_cs=RBBOG. If we @@ -118,7 +119,7 @@ void bwa_cs2nt_core(bwa_seq_t *p, bwtint_t l_pac, ubyte_t *pac) // set temporary arrays if (p->type == BWA_TYPE_NO_MATCH) return; len = p->len + p->n_gapo + p->n_gape + 100; // leave enough space - ta = (uint8_t*)malloc(len * 7); + ta = (uint8_t*)xmalloc(len * 7); nt_ref = ta; cs_read = nt_ref + len; nt_read = cs_read + len; diff --git a/fastmap.c b/fastmap.c index 4d7a675..7ef74a9 100644 --- a/fastmap.c +++ b/fastmap.c @@ -6,7 +6,8 @@ #include "bwt.h" #include "kvec.h" #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +#include "utils.h" +KSEQ_INIT(gzFile, err_gzread) extern unsigned char nst_nt4_table[256]; @@ -20,11 +21,11 @@ typedef struct { smem_i *smem_iter_init(const bwt_t *bwt) { smem_i *iter; - iter = calloc(1, sizeof(smem_i)); + iter = xcalloc(1, sizeof(smem_i)); iter->bwt = bwt; - iter->tmpvec[0] = calloc(1, sizeof(bwtintv_v)); - iter->tmpvec[1] = calloc(1, sizeof(bwtintv_v)); - iter->matches = calloc(1, sizeof(bwtintv_v)); + iter->tmpvec[0] = xcalloc(1, sizeof(bwtintv_v)); + iter->tmpvec[1] = xcalloc(1, sizeof(bwtintv_v)); + iter->matches = xcalloc(1, sizeof(bwtintv_v)); return iter; } @@ -78,7 +79,7 @@ int main_fastmap(int argc, char *argv[]) fp = gzopen(argv[optind + 1], "r"); seq = kseq_init(fp); { // load the packed sequences, BWT and SA - char *tmp = calloc(strlen(argv[optind]) + 5, 1); + char *tmp = xcalloc(strlen(argv[optind]) + 5, 1); strcat(strcpy(tmp, argv[optind]), ".bwt"); bwt = bwt_restore_bwt(tmp); strcat(strcpy(tmp, argv[optind]), ".sa"); diff --git a/is.c b/is.c index 9e50faf..8e94abd 100644 --- a/is.c +++ b/is.c @@ -25,6 +25,7 @@ */ #include +#include "utils.h" typedef unsigned char ubyte_t; #define chr(i) (cs == sizeof(int) ? ((const int *)T)[i]:((const unsigned char *)T)[i]) @@ -204,8 +205,9 @@ int is_sa(const ubyte_t *T, int *SA, int n) int is_bwt(ubyte_t *T, int n) { int *SA, i, primary = 0; - SA = (int*)calloc(n+1, sizeof(int)); - is_sa(T, SA, n); + SA = (int*)xcalloc(n+1, sizeof(int)); + + if (is_sa(T, SA, n)) err_fatal_simple("is_sa failed"); for (i = 0; i <= n; ++i) { if (SA[i] == 0) primary = i; diff --git a/khash.h b/khash.h index de6be6d..fae5008 100644 --- a/khash.h +++ b/khash.h @@ -95,6 +95,7 @@ int main() { #include #include #include +#include "utils.h" /* compipler specific configuration */ @@ -147,7 +148,7 @@ static const double __ac_HASH_UPPER = 0.77; khval_t *vals; \ } kh_##name##_t; \ static inline kh_##name##_t *kh_init_##name() { \ - return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \ + return (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t)); \ } \ static inline void kh_destroy_##name(kh_##name##_t *h) \ { \ @@ -188,12 +189,12 @@ static const double __ac_HASH_UPPER = 0.77; new_n_buckets = __ac_prime_list[t+1]; \ if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; \ else { \ - new_flags = (khint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \ + new_flags = (khint32_t*)xmalloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \ memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \ if (h->n_buckets < new_n_buckets) { \ - h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ + h->keys = (khkey_t*)xrealloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ if (kh_is_map) \ - h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \ + h->vals = (khval_t*)xrealloc(h->vals, new_n_buckets * sizeof(khval_t)); \ } \ } \ } \ @@ -227,9 +228,9 @@ static const double __ac_HASH_UPPER = 0.77; } \ } \ if (h->n_buckets > new_n_buckets) { \ - h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ + h->keys = (khkey_t*)xrealloc(h->keys, new_n_buckets * sizeof(khkey_t)); \ if (kh_is_map) \ - h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \ + h->vals = (khval_t*)xrealloc(h->vals, new_n_buckets * sizeof(khval_t)); \ } \ free(h->flags); \ h->flags = new_flags; \ diff --git a/kseq.h b/kseq.h index ad8937c..0fb1847 100644 --- a/kseq.h +++ b/kseq.h @@ -29,6 +29,7 @@ #include #include #include +#include "utils.h" #define __KS_TYPE(type_t) \ typedef struct __kstream_t { \ @@ -43,9 +44,9 @@ #define __KS_BASIC(type_t, __bufsize) \ static inline kstream_t *ks_init(type_t f) \ { \ - kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \ + kstream_t *ks = (kstream_t*)xcalloc(1, sizeof(kstream_t)); \ ks->f = f; \ - ks->buf = (char*)malloc(__bufsize); \ + ks->buf = (char*)xmalloc(__bufsize); \ return ks; \ } \ static inline void ks_destroy(kstream_t *ks) \ @@ -107,7 +108,7 @@ typedef struct __kstring_t { if (str->m - str->l < i - ks->begin + 1) { \ str->m = str->l + (i - ks->begin) + 1; \ kroundup32(str->m); \ - str->s = (char*)realloc(str->s, str->m); \ + str->s = (char*)xrealloc(str->s, str->m); \ } \ memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \ str->l = str->l + (i - ks->begin); \ @@ -130,7 +131,7 @@ typedef struct __kstring_t { #define __KSEQ_BASIC(type_t) \ static inline kseq_t *kseq_init(type_t fd) \ { \ - kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \ + kseq_t *s = (kseq_t*)xcalloc(1, sizeof(kseq_t)); \ s->f = ks_init(fd); \ return s; \ } \ @@ -170,7 +171,7 @@ typedef struct __kstring_t { if (seq->seq.l + 1 >= seq->seq.m) { /* double the memory */ \ seq->seq.m = seq->seq.l + 2; \ kroundup32(seq->seq.m); /* rounded to next closest 2^k */ \ - seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \ + seq->seq.s = (char*)xrealloc(seq->seq.s, seq->seq.m); \ } \ seq->seq.s[seq->seq.l++] = (char)c; \ } \ @@ -180,7 +181,7 @@ typedef struct __kstring_t { if (c != '+') return seq->seq.l; /* FASTA */ \ if (seq->qual.m < seq->seq.m) { /* allocate enough memory */ \ seq->qual.m = seq->seq.m; \ - seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \ + seq->qual.s = (char*)xrealloc(seq->qual.s, seq->qual.m); \ } \ while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \ if (c == -1) return -2; /* we should not stop here */ \ diff --git a/ksort.h b/ksort.h index 52812e1..68f9407 100644 --- a/ksort.h +++ b/ksort.h @@ -57,6 +57,7 @@ #include #include +#include "utils.h" typedef struct { void *left, *right; @@ -72,7 +73,7 @@ typedef struct { int curr, shift; \ \ a2[0] = array; \ - a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n); \ + a2[1] = temp? temp : (type_t*)xmalloc(sizeof(type_t) * n); \ for (curr = 0, shift = 0; (1ul< #include #include "kstring.h" +#include "utils.h" int ksprintf(kstring_t *s, const char *fmt, ...) { @@ -12,7 +13,7 @@ int ksprintf(kstring_t *s, const char *fmt, ...) if (l + 1 > s->m - s->l) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)realloc(s->s, s->m); + s->s = (char*)xrealloc(s->s, s->m); va_start(ap, fmt); l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); } diff --git a/kstring.h b/kstring.h index 398901f..88cf93a 100644 --- a/kstring.h +++ b/kstring.h @@ -3,6 +3,7 @@ #include #include +#include "utils.h" #ifndef kroundup32 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) @@ -22,7 +23,7 @@ static inline int kputs(const char *p, kstring_t *s) if (s->l + l + 1 >= s->m) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)realloc(s->s, s->m); + s->s = (char*)xrealloc(s->s, s->m); } strcpy(s->s + s->l, p); s->l += l; @@ -34,7 +35,7 @@ static inline int kputc(int c, kstring_t *s) if (s->l + 1 >= s->m) { s->m = s->l + 2; kroundup32(s->m); - s->s = (char*)realloc(s->s, s->m); + s->s = (char*)xrealloc(s->s, s->m); } s->s[s->l++] = c; s->s[s->l] = 0; diff --git a/ksw.c b/ksw.c index bd29e96..5d17a8f 100644 --- a/ksw.c +++ b/ksw.c @@ -28,6 +28,7 @@ #include #include #include "ksw.h" +#include "utils.h" #ifdef __GNUC__ #define LIKELY(x) __builtin_expect((x),1) @@ -51,7 +52,7 @@ ksw_query_t *ksw_qinit(int size, int qlen, const uint8_t *query, int m, const in size = size > 1? 2 : 1; p = 8 * (3 - size); // # values per __m128i slen = (qlen + p - 1) / p; // segmented length - q = malloc(sizeof(ksw_query_t) + 256 + 16 * slen * (m + 4)); // a single block of memory + q = xmalloc(sizeof(ksw_query_t) + 256 + 16 * slen * (m + 4)); // a single block of memory q->qp = (__m128i*)(((size_t)q + sizeof(ksw_query_t) + 15) >> 4 << 4); // align memory q->H0 = q->qp + slen * m; q->H1 = q->H0 + slen; @@ -169,7 +170,7 @@ end_loop16: if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { // then append if (n_b == m_b) { m_b = m_b? m_b<<1 : 8; - b = realloc(b, 8 * m_b); + b = xrealloc(b, 8 * m_b); } b[n_b++] = (uint64_t)imax<<32 | i; } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last @@ -264,7 +265,7 @@ end_loop8: if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { if (n_b == m_b) { m_b = m_b? m_b<<1 : 8; - b = realloc(b, 8 * m_b); + b = xrealloc(b, 8 * m_b); } b[n_b++] = (uint64_t)imax<<32 | i; } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last @@ -310,7 +311,7 @@ int ksw_sse2(ksw_query_t *q, int tlen, const uint8_t *target, ksw_aux_t *a) #include #include #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +KSEQ_INIT(gzFile, err_gzread) unsigned char seq_nt4_table[256] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, diff --git a/kvec.h b/kvec.h index 57204d6..8d83c42 100644 --- a/kvec.h +++ b/kvec.h @@ -60,7 +60,7 @@ int main() { #define kv_size(v) ((v).n) #define kv_max(v) ((v).m) -#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m)) +#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m)) #define kv_copy(type, v1, v0) do { \ if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \ @@ -71,19 +71,19 @@ int main() { #define kv_push(type, v, x) do { \ if ((v).n == (v).m) { \ (v).m = (v).m? (v).m<<1 : 2; \ - (v).a = (type*)realloc((v).a, sizeof(type) * (v).m); \ + (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m); \ } \ (v).a[(v).n++] = (x); \ } while (0) #define kv_pushp(type, v) (((v).n == (v).m)? \ ((v).m = ((v).m? (v).m<<1 : 2), \ - (v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ + (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m), 0) \ : 0), ((v).a + ((v).n++)) #define kv_a(type, v, i) ((v).m <= (size_t)(i)? \ ((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \ - (v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ + (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m), 0) \ : (v).n <= (size_t)(i)? (v).n = (i) \ : 0), (v).a[(i)] diff --git a/simple_dp.c b/simple_dp.c index 7c078c2..267e77f 100644 --- a/simple_dp.c +++ b/simple_dp.c @@ -8,7 +8,7 @@ #include "utils.h" #include "kseq.h" -KSEQ_INIT(gzFile, gzread) +KSEQ_INIT(gzFile, err_gzread) typedef struct { int l; @@ -64,20 +64,20 @@ static seqs_t *load_seqs(const char *fn) fp = xzopen(fn, "r"); seq = kseq_init(fp); - s = (seqs_t*)calloc(1, sizeof(seqs_t)); + s = (seqs_t*)xcalloc(1, sizeof(seqs_t)); s->m_seqs = 256; - s->seqs = (seq1_t*)calloc(s->m_seqs, sizeof(seq1_t)); + s->seqs = (seq1_t*)xcalloc(s->m_seqs, sizeof(seq1_t)); while ((l = kseq_read(seq)) >= 0) { if (s->n_seqs == s->m_seqs) { s->m_seqs <<= 1; - s->seqs = (seq1_t*)realloc(s->seqs, s->m_seqs * sizeof(seq1_t)); + s->seqs = (seq1_t*)xrealloc(s->seqs, s->m_seqs * sizeof(seq1_t)); } p = s->seqs + (s->n_seqs++); p->l = seq->seq.l; - p->s = (unsigned char*)malloc(p->l + 1); + p->s = (unsigned char*)xmalloc(p->l + 1); memcpy(p->s, seq->seq.s, p->l); p->s[p->l] = 0; - p->n = strdup((const char*)seq->name.s); + p->n = xstrdup((const char*)seq->name.s); } kseq_destroy(seq); gzclose(fp); diff --git a/stdaln.c b/stdaln.c index eb41882..1a8a3d1 100644 --- a/stdaln.c +++ b/stdaln.c @@ -28,6 +28,7 @@ #include #include #include "stdaln.h" +#include "utils.h" /* char -> 17 (=16+1) nucleotides */ unsigned char aln_nt16_table[256] = { @@ -232,7 +233,7 @@ AlnParam aln_param_aa2aa = { 10, 2, 2, aln_sm_blosum62, 22, 50 }; AlnAln *aln_init_AlnAln() { AlnAln *aa; - aa = (AlnAln*)malloc(sizeof(AlnAln)); + aa = (AlnAln*)xmalloc(sizeof(AlnAln)); aa->path = 0; aa->out1 = aa->out2 = aa->outm = 0; aa->path_len = 0; @@ -382,13 +383,13 @@ int aln_global_core(unsigned char *seq1, int len1, unsigned char *seq2, int len2 /* allocate memory */ end = (b1 + b2 <= len1)? (b1 + b2 + 1) : (len1 + 1); - dpcell = (dpcell_t**)malloc(sizeof(dpcell_t*) * (len2 + 1)); + dpcell = (dpcell_t**)xmalloc(sizeof(dpcell_t*) * (len2 + 1)); for (j = 0; j <= len2; ++j) - dpcell[j] = (dpcell_t*)malloc(sizeof(dpcell_t) * end); + dpcell[j] = (dpcell_t*)xmalloc(sizeof(dpcell_t) * end); for (j = b2 + 1; j <= len2; ++j) dpcell[j] -= j - b2; - curr = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); - last = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); + curr = (dpscore_t*)xmalloc(sizeof(dpscore_t) * (len1 + 1)); + last = (dpscore_t*)xmalloc(sizeof(dpscore_t) * (len1 + 1)); /* set first row */ SET_INF(*curr); curr->M = 0; @@ -556,11 +557,11 @@ int aln_local_core(unsigned char *seq1, int len1, unsigned char *seq2, int len2, if (len1 == 0 || len2 == 0) return -1; /* allocate memory */ - suba = (int*)malloc(sizeof(int) * (len2 + 1)); - eh = (NT_LOCAL_SCORE*)malloc(sizeof(NT_LOCAL_SCORE) * (len1 + 1)); - s_array = (int**)malloc(sizeof(int*) * N_MATRIX_ROW); + suba = (int*)xmalloc(sizeof(int) * (len2 + 1)); + eh = (NT_LOCAL_SCORE*)xmalloc(sizeof(NT_LOCAL_SCORE) * (len1 + 1)); + s_array = (int**)xmalloc(sizeof(int*) * N_MATRIX_ROW); for (i = 0; i != N_MATRIX_ROW; ++i) - s_array[i] = (int*)malloc(sizeof(int) * len1); + s_array[i] = (int*)xmalloc(sizeof(int) * len1); /* initialization */ aln_init_score_array(seq1, len1, N_MATRIX_ROW, score_matrix, s_array); q = gap_open; @@ -773,9 +774,9 @@ AlnAln *aln_stdaln_aux(const char *seq1, const char *seq2, const AlnParam *ap, if (len2 < 0) len2 = strlen(seq2); aa = aln_init_AlnAln(); - seq11 = (unsigned char*)malloc(sizeof(unsigned char) * len1); - seq22 = (unsigned char*)malloc(sizeof(unsigned char) * len2); - aa->path = (path_t*)malloc(sizeof(path_t) * (len1 + len2 + 1)); + seq11 = (unsigned char*)xmalloc(sizeof(unsigned char) * len1); + seq22 = (unsigned char*)xmalloc(sizeof(unsigned char) * len2); + aa->path = (path_t*)xmalloc(sizeof(path_t) * (len1 + len2 + 1)); if (ap->row < 10) { /* 4-nucleotide alignment */ for (i = 0; i < len1; ++i) @@ -805,9 +806,9 @@ AlnAln *aln_stdaln_aux(const char *seq1, const char *seq2, const AlnParam *ap, aa->score = score; if (thres > 0) { - out1 = aa->out1 = (char*)malloc(sizeof(char) * (aa->path_len + 1)); - out2 = aa->out2 = (char*)malloc(sizeof(char) * (aa->path_len + 1)); - outm = aa->outm = (char*)malloc(sizeof(char) * (aa->path_len + 1)); + out1 = aa->out1 = (char*)xmalloc(sizeof(char) * (aa->path_len + 1)); + out2 = aa->out2 = (char*)xmalloc(sizeof(char) * (aa->path_len + 1)); + outm = aa->outm = (char*)xmalloc(sizeof(char) * (aa->path_len + 1)); --seq1; --seq2; --seq11; --seq22; @@ -881,10 +882,10 @@ int aln_extend_core(unsigned char *seq1, int len1, unsigned char *seq2, int len2 if (len1 == 0 || len2 == 0) return -1; /* allocate memory */ - mem = _mem? _mem : calloc((len1 + 2) * (N_MATRIX_ROW + 1), 4); + mem = _mem? _mem : xcalloc((len1 + 2) * (N_MATRIX_ROW + 1), 4); _p = mem; eh = (uint32_t*)_p, _p += 4 * (len1 + 2); - s_array = calloc(N_MATRIX_ROW, sizeof(void*)); + s_array = xcalloc(N_MATRIX_ROW, sizeof(void*)); for (i = 0; i != N_MATRIX_ROW; ++i) s_array[i] = (int32_t*)_p, _p += 4 * len1; /* initialization */ @@ -1024,7 +1025,7 @@ uint32_t *aln_path2cigar32(const path_t *path, int path_len, int *n_cigar) last_type = path[i].ctype; } *n_cigar = n; - cigar = (uint32_t*)malloc(*n_cigar * 4); + cigar = (uint32_t*)xmalloc(*n_cigar * 4); cigar[0] = 1u << 4 | path[path_len-1].ctype; last_type = path[path_len-1].ctype; From 55f1b3653492c53bcbc423cb574cfa00e153d903 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Thu, 3 Jan 2013 16:57:37 +0000 Subject: [PATCH 3/8] New wrapper for gzclose; added err_fflush calls and made it call fsync too. Added a new utils.c wrapper err_gzclose and changed gzclose calls to use it. Put in some more err_fflush calls before files being written are closed. Made err_fflush call fsync. This is useful for remote filesystems where errors may not be reported on fflush or fclose as problems at the server end may only be detected after they have returned. If bwa is being used only to write to local filesystems, calling fsync is not really necessary. To disable it, comment out #define FSYNC_ON_FLUSH in utils.c. --- bamlite.h | 2 +- bntseq.c | 5 ++++- bwaseqio.c | 2 +- bwtindex.c | 6 +++--- bwtio.c | 2 ++ bwtmisc.c | 3 ++- bwtsw2_aux.c | 6 +++--- fastmap.c | 4 ++-- ksw.c | 8 ++++---- simple_dp.c | 4 ++-- utils.c | 39 +++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 12 files changed, 64 insertions(+), 18 deletions(-) diff --git a/bamlite.h b/bamlite.h index 2b65c57..0c080fd 100644 --- a/bamlite.h +++ b/bamlite.h @@ -8,7 +8,7 @@ typedef gzFile bamFile; #define bam_open(fn, mode) xzopen(fn, mode) #define bam_dopen(fd, mode) gzdopen(fd, mode) -#define bam_close(fp) gzclose(fp) +#define bam_close(fp) err_gzclose(fp) #define bam_read(fp, buf, size) err_gzread(fp, buf, size) typedef struct { diff --git a/bntseq.c b/bntseq.c index b795af8..af83211 100644 --- a/bntseq.c +++ b/bntseq.c @@ -73,6 +73,7 @@ void bns_dump(const bntseq_t *bns, const char *prefix) else err_fprintf(fp, "\n"); err_fprintf(fp, "%lld %d %d\n", (long long)p->offset, p->len, p->n_ambs); } + err_fflush(fp); err_fclose(fp); } { // dump .amb @@ -83,6 +84,7 @@ void bns_dump(const bntseq_t *bns, const char *prefix) bntamb1_t *p = bns->ambs + i; err_fprintf(fp, "%lld %d %c\n", (long long)p->offset, p->len, p->amb); } + err_fflush(fp); err_fclose(fp); } } @@ -279,6 +281,7 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) ct = bns->l_pac % 4; err_fwrite(&ct, 1, 1, fp); // close .pac file + err_fflush(fp); err_fclose(fp); } bns_dump(bns, prefix); @@ -303,7 +306,7 @@ int bwa_fa2pac(int argc, char *argv[]) } fp = xzopen(argv[optind], "r"); bns_fasta2bntseq(fp, (optind+1 < argc)? argv[optind+1] : argv[optind], for_only); - gzclose(fp); + err_gzclose(fp); return 0; } diff --git a/bwaseqio.c b/bwaseqio.c index 716f9b2..8d69b37 100644 --- a/bwaseqio.c +++ b/bwaseqio.c @@ -46,7 +46,7 @@ void bwa_seq_close(bwa_seqio_t *bs) if (bs == 0) return; if (bs->is_bam) bam_close(bs->fp); else { - gzclose(bs->ks->f->f); + err_gzclose(bs->ks->f->f); kseq_destroy(bs->ks); } free(bs); diff --git a/bwtindex.c b/bwtindex.c index f430e62..6d0604e 100644 --- a/bwtindex.c +++ b/bwtindex.c @@ -89,7 +89,7 @@ int bwa_index(int argc, char *argv[]) fprintf(stderr, "[bwa_index] Pack FASTA... "); l_pac = bns_fasta2bntseq(fp, prefix, 0); fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); - gzclose(fp); + err_gzclose(fp); } else { // color indexing gzFile fp = xzopen(argv[optind], "r"); strcat(strcpy(str, prefix), ".nt"); @@ -97,7 +97,7 @@ int bwa_index(int argc, char *argv[]) fprintf(stderr, "[bwa_index] Pack nucleotide FASTA... "); l_pac = bns_fasta2bntseq(fp, str, 0); fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); - gzclose(fp); + err_gzclose(fp); { char *tmp_argv[3]; tmp_argv[0] = argv[0]; tmp_argv[1] = str; tmp_argv[2] = prefix; @@ -139,7 +139,7 @@ int bwa_index(int argc, char *argv[]) fprintf(stderr, "[bwa_index] Pack forward-only FASTA... "); l_pac = bns_fasta2bntseq(fp, prefix, 1); fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); - gzclose(fp); + err_gzclose(fp); } { bwt_t *bwt; diff --git a/bwtio.c b/bwtio.c index 0d4623e..ca5a6c0 100644 --- a/bwtio.c +++ b/bwtio.c @@ -11,6 +11,7 @@ void bwt_dump_bwt(const char *fn, const bwt_t *bwt) err_fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp); err_fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp); err_fwrite(bwt->bwt, 4, bwt->bwt_size, fp); + err_fflush(fp); err_fclose(fp); } @@ -23,6 +24,7 @@ void bwt_dump_sa(const char *fn, const bwt_t *bwt) err_fwrite(&bwt->sa_intv, sizeof(bwtint_t), 1, fp); err_fwrite(&bwt->seq_len, sizeof(bwtint_t), 1, fp); err_fwrite(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp); + err_fflush(fp); err_fclose(fp); } diff --git a/bwtmisc.c b/bwtmisc.c index 49aa5aa..ccc82eb 100644 --- a/bwtmisc.c +++ b/bwtmisc.c @@ -201,7 +201,8 @@ int bwa_pac2cspac(int argc, char *argv[]) fp = xopen(str, "wb"); err_fwrite(cspac, 1, bns->l_pac/4 + 1, fp); ct = bns->l_pac % 4; - err_fwrite(&ct, 1, 1, fp); + err_fwrite(&ct, 1, 1, fp); + err_fflush(fp); err_fclose(fp); bns_destroy(bns); free(cspac); diff --git a/bwtsw2_aux.c b/bwtsw2_aux.c index ca39919..2228054 100644 --- a/bwtsw2_aux.c +++ b/bwtsw2_aux.c @@ -752,7 +752,7 @@ static void process_seqs(bsw2seq_t *_seq, const bsw2opt_t *opt, const bntseq_t * p->tid = -1; p->l = 0; p->name = p->seq = p->qual = p->sam = 0; } - fflush(stdout); + err_fflush(stdout); _seq->n = 0; } @@ -819,9 +819,9 @@ void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target, c free(pac); free(_seq->seq); free(_seq); kseq_destroy(ks); - gzclose(fp); + err_gzclose(fp); if (fn2) { kseq_destroy(ks2); - gzclose(fp2); + err_gzclose(fp2); } } diff --git a/fastmap.c b/fastmap.c index 7ef74a9..504e22e 100644 --- a/fastmap.c +++ b/fastmap.c @@ -76,7 +76,7 @@ int main_fastmap(int argc, char *argv[]) return 1; } - fp = gzopen(argv[optind + 1], "r"); + fp = xzopen(argv[optind + 1], "r"); seq = kseq_init(fp); { // load the packed sequences, BWT and SA char *tmp = xcalloc(strlen(argv[optind]) + 5, 1); @@ -123,6 +123,6 @@ int main_fastmap(int argc, char *argv[]) bns_destroy(bns); bwt_destroy(bwt); kseq_destroy(seq); - gzclose(fp); + err_gzclose(fp); return 0; } diff --git a/ksw.c b/ksw.c index 5d17a8f..270ecfb 100644 --- a/ksw.c +++ b/ksw.c @@ -364,8 +364,8 @@ int main(int argc, char *argv[]) } for (j = 0; j < 5; ++j) mat[k++] = 0; // open file - fpt = gzopen(argv[optind], "r"); kst = kseq_init(fpt); - fpq = gzopen(argv[optind+1], "r"); ksq = kseq_init(fpq); + fpt = xzopen(argv[optind], "r"); kst = kseq_init(fpt); + fpq = xzopen(argv[optind+1], "r"); ksq = kseq_init(fpq); // all-pair alignment while (kseq_read(ksq) > 0) { ksw_query_t *q[2]; @@ -394,8 +394,8 @@ int main(int argc, char *argv[]) } free(q[0]); free(q[1]); } - kseq_destroy(kst); gzclose(fpt); - kseq_destroy(ksq); gzclose(fpq); + kseq_destroy(kst); err_gzclose(fpt); + kseq_destroy(ksq); err_gzclose(fpq); return 0; } #endif // _KSW_MAIN diff --git a/simple_dp.c b/simple_dp.c index 267e77f..4c6a156 100644 --- a/simple_dp.c +++ b/simple_dp.c @@ -80,7 +80,7 @@ static seqs_t *load_seqs(const char *fn) p->n = xstrdup((const char*)seq->name.s); } kseq_destroy(seq); - gzclose(fp); + err_gzclose(fp); fprintf(stderr, "[load_seqs] %d sequences are loaded.\n", s->n_seqs); return s; } @@ -123,7 +123,7 @@ static void aln_seqs(const seqs_t *ss, const char *fn) } } kseq_destroy(seq); - gzclose(fp); + err_gzclose(fp); } int bwa_stdsw(int argc, char *argv[]) diff --git a/utils.c b/utils.c index bc39bf5..dc16308 100644 --- a/utils.c +++ b/utils.c @@ -24,6 +24,7 @@ */ /* Contact: Heng Li */ +#define FSYNC_ON_FLUSH #include #include @@ -31,6 +32,11 @@ #include #include #include +#ifdef FSYNC_ON_FLUSH +#include +#include +#include +#endif #include #include #include "utils.h" @@ -196,6 +202,28 @@ int err_fflush(FILE *stream) { _err_fatal_simple("fflush", strerror(errno)); } +#ifdef FSYNC_ON_FLUSH + /* Calling fflush() ensures that all the data has made it to the + kernel buffers, but this may not be sufficient for remote filesystems + (e.g. NFS, lustre) as an error may still occur while the kernel + is copying the buffered data to the file server. To be sure of + catching these errors, we need to call fsync() on the file + descriptor, but only if it is a regular file. */ + { + struct stat sbuf; + if (0 != fstat(fileno(stream), &sbuf)) + { + _err_fatal_simple("fstat", strerror(errno)); + } + if (S_ISREG(sbuf.st_mode)) + { + if (0 != fsync(fileno(stream))) + { + _err_fatal_simple("fsync", strerror(errno)); + } + } + } +#endif return ret; } @@ -209,6 +237,17 @@ int err_fclose(FILE *stream) return ret; } +int err_gzclose(gzFile file) +{ + int ret = gzclose(file); + if (Z_OK != ret) + { + _err_fatal_simple("gzclose", Z_ERRNO == ret ? strerror(errno) : zError(ret)); + } + + return ret; +} + void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func) { void *p = calloc(nmemb, size); diff --git a/utils.h b/utils.h index c6cfc81..f824245 100644 --- a/utils.h +++ b/utils.h @@ -78,6 +78,7 @@ extern "C" { ATTRIBUTE((format(printf, 1, 2))); int err_fflush(FILE *stream); int err_fclose(FILE *stream); + int err_gzclose(gzFile file); void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func); void *err_malloc(size_t size, const char *file, unsigned int line, const char *func); From 4f4e998d7f1e28595ef5fc36dd96508ae9b34752 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Wed, 9 Jan 2013 14:43:36 +0000 Subject: [PATCH 4/8] Added wrappers for fputc and fputs; more efficient sequence printing Added wrappers err_fputc and err_fputs to catch failures in fput and fputs. Macros err_putchar and err_puts call the new wrappers and can be used in place of putchar and puts. To avoid having to make millions of function calls when printing out sequences, the code to print them in bwa_print_sam1 using putchar has been replaced by a new version in bwa_print_seq that puts the sequence into a buffer and then outputs the lot with err_fwrite. In testing, the new code was slightly faster than the old version, with the added benefit that it will stop promptly if IO problems are detected. --- bwase.c | 38 +++++++++++++++++++++++++++++--------- fastmap.c | 12 ++++++------ utils.c | 22 ++++++++++++++++++++++ utils.h | 4 ++++ 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/bwase.c b/bwase.c index afe8154..84430fe 100644 --- a/bwase.c +++ b/bwase.c @@ -404,6 +404,26 @@ static int64_t pos_5(const bwa_seq_t *p) return -1; } +void bwa_print_seq(FILE *stream, bwa_seq_t *seq) { + char buffer[4096]; + const int bsz = sizeof(buffer); + int i, j, l; + + if (seq->strand == 0) { + for (i = 0; i < seq->full_len; i += bsz) { + l = seq->full_len - i > bsz ? bsz : seq->full_len - i; + for (j = 0; j < l; j++) buffer[j] = "ACGTN"[seq->seq[i + j]]; + err_fwrite(buffer, 1, l, stream); + } + } else { + for (i = seq->full_len - 1; i >= 0; i -= bsz) { + l = i + 1 > bsz ? bsz : i + 1; + for (j = 0; j < l; j++) buffer[j] = "TGCAN"[seq->seq[i - j]]; + err_fwrite(buffer, 1, l, stream); + } + } +} + void bwa_print_sam1(const bntseq_t *bns, bwa_seq_t *p, const bwa_seq_t *mate, int mode, int max_top2) { int j; @@ -455,10 +475,8 @@ void bwa_print_sam1(const bntseq_t *bns, bwa_seq_t *p, const bwa_seq_t *mate, in else err_printf("\t*\t0\t0\t"); // print sequence and quality - if (p->strand == 0) - for (j = 0; j != p->full_len; ++j) putchar("ACGTN"[(int)p->seq[j]]); - else for (j = 0; j != p->full_len; ++j) putchar("TGCAN"[p->seq[p->full_len - 1 - j]]); - putchar('\t'); + bwa_print_seq(stdout, p); + err_putchar('\t'); if (p->qual) { if (p->strand) seq_reverse(p->len, p->qual, 0); // reverse quality err_printf("%s", p->qual); @@ -500,14 +518,16 @@ void bwa_print_sam1(const bntseq_t *bns, bwa_seq_t *p, const bwa_seq_t *mate, in } } } - putchar('\n'); + err_putchar('\n'); } else { // this read has no match - ubyte_t *s = p->strand? p->rseq : p->seq; + //ubyte_t *s = p->strand? p->rseq : p->seq; int flag = p->extra_flag | SAM_FSU; if (mate && mate->type == BWA_TYPE_NO_MATCH) flag |= SAM_FMU; err_printf("%s\t%d\t*\t0\t0\t*\t*\t0\t0\t", p->name, flag); - for (j = 0; j != p->len; ++j) putchar("ACGTN"[(int)s[j]]); - putchar('\t'); + //Why did this work differently to the version above?? + //for (j = 0; j != p->len; ++j) putchar("ACGTN"[(int)s[j]]); + bwa_print_seq(stdout, p); + err_putchar('\t'); if (p->qual) { if (p->strand) seq_reverse(p->len, p->qual, 0); // reverse quality err_printf("%s", p->qual); @@ -515,7 +535,7 @@ void bwa_print_sam1(const bntseq_t *bns, bwa_seq_t *p, const bwa_seq_t *mate, in if (bwa_rg_id) err_printf("\tRG:Z:%s", bwa_rg_id); if (p->bc[0]) err_printf("\tBC:Z:%s", p->bc); if (p->clip_len < p->full_len) err_printf("\tXC:i:%d", p->clip_len); - putchar('\n'); + err_putchar('\n'); } } diff --git a/fastmap.c b/fastmap.c index 504e22e..5cb83fc 100644 --- a/fastmap.c +++ b/fastmap.c @@ -91,9 +91,9 @@ int main_fastmap(int argc, char *argv[]) while (kseq_read(seq) >= 0) { printf("SQ\t%s\t%ld", seq->name.s, seq->seq.l); if (print_seq) { - putchar('\t'); - puts(seq->seq.s); - } else putchar('\n'); + err_putchar('\t'); + err_puts(seq->seq.s); + } else err_putchar('\n'); for (i = 0; i < seq->seq.l; ++i) seq->seq.s[i] = nst_nt4_table[(int)seq->seq.s[i]]; smem_set_query(iter, seq->seq.l, (uint8_t*)seq->seq.s); @@ -112,11 +112,11 @@ int main_fastmap(int argc, char *argv[]) bns_cnt_ambi(bns, pos, len, &ref_id); printf("\t%s:%c%ld", bns->anns[ref_id].name, "+-"[is_rev], (long)(pos - bns->anns[ref_id].offset) + 1); } - } else fputs("\t*", stdout); - putchar('\n'); + } else err_puts("\t*"); + err_putchar('\n'); } } - puts("//"); + err_puts("//"); } smem_iter_destroy(iter); diff --git a/utils.c b/utils.c index dc16308..93c17c9 100644 --- a/utils.c +++ b/utils.c @@ -195,6 +195,28 @@ int err_fprintf(FILE *stream, const char *format, ...) return done; } +int err_fputc(int c, FILE *stream) +{ + int ret = putc(c, stream); + if (EOF == ret) + { + _err_fatal_simple("fputc", strerror(errno)); + } + + return ret; +} + +int err_fputs(const char *s, FILE *stream) +{ + int ret = fputs(s, stream); + if (EOF == ret) + { + _err_fatal_simple("fputs", strerror(errno)); + } + + return ret; +} + int err_fflush(FILE *stream) { int ret = fflush(stream); diff --git a/utils.h b/utils.h index f824245..b904701 100644 --- a/utils.h +++ b/utils.h @@ -76,6 +76,10 @@ extern "C" { ATTRIBUTE((format(printf, 2, 3))); int err_printf(const char *format, ...) ATTRIBUTE((format(printf, 1, 2))); + int err_fputc(int c, FILE *stream); +#define err_putchar(C) err_fputc((C), stdout) + int err_fputs(const char *s, FILE *stream); +#define err_puts(S) err_fputs((S), stdout) int err_fflush(FILE *stream); int err_fclose(FILE *stream); int err_gzclose(gzFile file); From 925aa6f60db008f43bc575e22dcd0491999e7cab Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Wed, 13 Mar 2013 09:18:18 +0000 Subject: [PATCH 5/8] Added Makefile.bak and bwamem-lite to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 57cb318..fba548e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ bwa test test64 .*.swp +Makefile.bak +bwamem-lite From 0aa7e0a4022fea82a2f038a6d44aa3721e3c60f3 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Mon, 29 Apr 2013 13:58:28 +0100 Subject: [PATCH 6/8] Ensure exit status of 1 if given invalid options or index files are not found. Added missing default cases in option scanning. Ensure exit value is 1 if bwa_idx_load or bwa_idx_infer_prefix fail. These changes extend the previous one, which only fixed the mem aligner. --- bwape.c | 2 +- bwase.c | 2 +- bwtaln.c | 2 +- bwtsw2_main.c | 3 ++- fastmap.c | 4 +++- pemerge.c | 1 + 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bwape.c b/bwape.c index b219527..030af0d 100644 --- a/bwape.c +++ b/bwape.c @@ -765,7 +765,7 @@ int bwa_sai2sam_pe(int argc, char *argv[]) } if ((prefix = bwa_idx_infer_prefix(argv[optind])) == 0) { fprintf(stderr, "[%s] fail to locate the index\n", __func__); - return 0; + return 1; } bwa_sai2sam_pe_core(prefix, argv + optind + 1, argv + optind+3, popt, rg_line); free(prefix); free(popt); diff --git a/bwase.c b/bwase.c index 41ab175..bf7e9cb 100644 --- a/bwase.c +++ b/bwase.c @@ -611,7 +611,7 @@ int bwa_sai2sam_se(int argc, char *argv[]) } if ((prefix = bwa_idx_infer_prefix(argv[optind])) == 0) { fprintf(stderr, "[%s] fail to locate the index\n", __func__); - return 0; + return 1; } bwa_sai2sam_se_core(prefix, argv[optind+1], argv[optind+2], n_occ, rg_line); free(prefix); diff --git a/bwtaln.c b/bwtaln.c index b906e1e..f6b26a8 100644 --- a/bwtaln.c +++ b/bwtaln.c @@ -306,7 +306,7 @@ int bwa_aln(int argc, char *argv[]) if ((prefix = bwa_idx_infer_prefix(argv[optind])) == 0) { fprintf(stderr, "[%s] fail to locate the index\n", __func__); free(opt); - return 0; + return 1; } bwa_aln_core(prefix, argv[optind+1], opt); free(opt); free(prefix); diff --git a/bwtsw2_main.c b/bwtsw2_main.c index ab126f2..40a9e0a 100644 --- a/bwtsw2_main.c +++ b/bwtsw2_main.c @@ -37,6 +37,7 @@ int bwa_bwtsw2(int argc, char *argv[]) case 'S': opt->skip_sw = 1; break; case 'C': opt->cpy_cmt = 1; break; case 'G': opt->max_chain_gap = atoi(optarg); break; + default: return 1; } } opt->qr = opt->q + opt->r; @@ -79,7 +80,7 @@ int bwa_bwtsw2(int argc, char *argv[]) opt->t *= opt->a; opt->coef *= opt->a; - if ((idx = bwa_idx_load(argv[optind], BWA_IDX_BWT|BWA_IDX_BNS)) == 0) return 0; + if ((idx = bwa_idx_load(argv[optind], BWA_IDX_BWT|BWA_IDX_BNS)) == 0) return 1; bsw2_aln(opt, idx->bns, idx->bwt, argv[optind+1], optind+2 < argc? argv[optind+2] : 0); bwa_idx_destroy(idx); free(opt); diff --git a/fastmap.c b/fastmap.c index d0651f9..592df02 100644 --- a/fastmap.c +++ b/fastmap.c @@ -52,6 +52,7 @@ int main_mem(int argc, char *argv[]) else if (c == 'R') { if ((rg_line = bwa_set_rg(optarg)) == 0) return 1; // FIXME: memory leak } else if (c == 's') opt->split_width = atoi(optarg); + else return 1; } if (opt->n_threads < 1) opt->n_threads = 1; if (optind + 1 >= argc) { @@ -164,6 +165,7 @@ int main_fastmap(int argc, char *argv[]) case 'p': print_seq = 1; break; case 'w': min_iwidth = atoi(optarg); break; case 'l': min_len = atoi(optarg); break; + default: return 1; } } if (optind + 1 >= argc) { @@ -173,7 +175,7 @@ int main_fastmap(int argc, char *argv[]) fp = xzopen(argv[optind + 1], "r"); seq = kseq_init(fp); - idx = bwa_idx_load(argv[optind], BWA_IDX_BWT|BWA_IDX_BNS); + if ((idx = bwa_idx_load(argv[optind], BWA_IDX_BWT|BWA_IDX_BNS)) == 0) return 1; itr = smem_itr_init(idx->bwt); while (kseq_read(seq) >= 0) { err_printf("SQ\t%s\t%ld", seq->name.s, seq->seq.l); diff --git a/pemerge.c b/pemerge.c index dbcfaab..b944819 100644 --- a/pemerge.c +++ b/pemerge.c @@ -226,6 +226,7 @@ int main_pemerge(int argc, char *argv[]) else if (c == 'Q') opt->q_thres = atoi(optarg); else if (c == 't') opt->n_threads = atoi(optarg); else if (c == 'T') min_ovlp = atoi(optarg); + else return 1; } if (flag == 0) flag = 3; opt->flag = flag; From 96e445d9e43e7d4a4cb32a1681098a6e7a170561 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Thu, 2 May 2013 15:12:01 +0100 Subject: [PATCH 7/8] Reduce dependency on utils.h - new malloc wrapping scheme. Remove xmalloc, xcalloc, xrealloc and xstrdup from utils.h and revert calls to the normal malloc, calloc, realloc, strdup. Add new files malloc_wrap.[ch] with the wrapper functions. malloc_wrap.h #defines malloc etc. to the wrapper, but only if USE_MALLOC_WRAPPERS has been defined. Put #include "malloc_wrap.h" in any file that uses *alloc or strdup. This is also in a #ifdef USE_MALLOC_WRAPPERS ... #endif block to make using the wrappers optional. Add -DUSE_MALLOC_WRAPPERS into the makefile so they should normally get added. This is an improvement on the previous method as we now don't need to worry about stray function calls that were not changed to the wrapped version and the code will still work even if the wrapping is disabled. Other possible methods of doing this are using malloc_hook (glibc-specific), adding -include malloc_wrap.h to the gcc command-line (somewhat gcc-specific) or making our own malloc function and using dlopen (scary). This way is probably the most portable. --- Makefile | 70 +++++++++++++++++++++++++++----------------------- bamlite.c | 17 +++++++----- bamlite.h | 6 ++++- bntseq.c | 38 +++++++++++++++------------ bwa.c | 26 +++++++++++-------- bwamem.c | 42 ++++++++++++++++-------------- bwamem_pair.c | 9 +++++-- bwape.c | 24 +++++++++-------- bwase.c | 26 +++++++++++-------- bwaseqio.c | 28 +++++++++++--------- bwt.c | 12 ++++++--- bwt_gen.c | 20 +++++++++------ bwt_lite.c | 15 ++++++----- bwtaln.c | 16 +++++++----- bwtgap.c | 15 ++++++----- bwtindex.c | 25 ++++++++++-------- bwtsw2_aux.c | 59 +++++++++++++++++++++++------------------- bwtsw2_chain.c | 11 +++++--- bwtsw2_core.c | 33 +++++++++++++----------- bwtsw2_pair.c | 11 +++++--- example.c | 4 +++ is.c | 6 ++++- kbtree.h | 21 ++++++++------- khash.h | 11 +++++--- kopen.c | 42 +++++++++++++++--------------- kseq.h | 21 ++++++++------- ksort.h | 9 ++++--- kstring.c | 9 ++++--- kstring.h | 17 +++++++----- ksw.c | 25 ++++++++++-------- ksw.h | 4 +++ kvec.h | 12 ++++++--- malloc_wrap.c | 57 ++++++++++++++++++++++++++++++++++++++++ malloc_wrap.h | 47 +++++++++++++++++++++++++++++++++ pemerge.c | 18 ++++++++----- utils.c | 41 ----------------------------- utils.h | 11 -------- 37 files changed, 516 insertions(+), 342 deletions(-) create mode 100644 malloc_wrap.c create mode 100644 malloc_wrap.h diff --git a/Makefile b/Makefile index 5bbbc89..7d78889 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ CC= gcc CFLAGS= -g -Wall -O2 +WRAP_MALLOC= -DUSE_MALLOC_WRAPPERS AR= ar -DFLAGS= -DHAVE_PTHREAD +DFLAGS= -DHAVE_PTHREAD $(WRAP_MALLOC) LOBJS= utils.o kstring.o ksw.o bwt.o bntseq.o bwa.o bwamem.o bwamem_pair.o AOBJS= QSufSort.o bwt_gen.o bwase.o bwaseqio.o bwtgap.o bwtaln.o bamlite.o \ is.o bwtindex.o bwape.o kopen.o pemerge.o \ bwtsw2_core.o bwtsw2_main.o bwtsw2_aux.o bwt_lite.o \ - bwtsw2_chain.o fastmap.o bwtsw2_pair.o + bwtsw2_chain.o fastmap.o bwtsw2_pair.o malloc_wrap.o PROG= bwa INCLUDES= LIBS= -lm -lz -lpthread @@ -32,39 +33,44 @@ clean: rm -f gmon.out *.o a.out $(PROG) *~ *.a depend: - ( LC_ALL=C ; export LC_ALL; makedepend -Y -- $(CFLAGS) -- *.c ) + ( LC_ALL=C ; export LC_ALL; makedepend -Y -- $(CFLAGS) $(DFLAGS) -- *.c ) # DO NOT DELETE THIS LINE -- make depend depends on it. QSufSort.o: QSufSort.h -bamlite.o: utils.h bamlite.h -bntseq.o: bntseq.h utils.h kseq.h -bwa.o: bntseq.h bwa.h bwt.h ksw.h utils.h kseq.h -bwamem.o: kstring.h utils.h bwamem.h bwt.h bntseq.h bwa.h ksw.h kvec.h -bwamem.o: ksort.h kbtree.h -bwamem_pair.o: kstring.h utils.h bwamem.h bwt.h bntseq.h bwa.h kvec.h ksw.h -bwape.o: bwtaln.h bwt.h kvec.h bntseq.h utils.h bwase.h bwa.h ksw.h khash.h -bwase.o: bwase.h bntseq.h bwt.h bwtaln.h utils.h kstring.h bwa.h ksw.h -bwaseqio.o: bwtaln.h bwt.h utils.h bamlite.h kseq.h -bwt.o: utils.h bwt.h kvec.h -bwt_gen.o: QSufSort.h utils.h -bwt_lite.o: bwt_lite.h utils.h -bwtaln.o: bwtaln.h bwt.h bwtgap.h utils.h bwa.h bntseq.h -bwtgap.o: bwtgap.h bwt.h bwtaln.h utils.h -bwtindex.o: bntseq.h bwt.h utils.h -bwtsw2_aux.o: bntseq.h bwt_lite.h utils.h bwtsw2.h bwt.h kstring.h bwa.h -bwtsw2_aux.o: ksw.h kseq.h ksort.h -bwtsw2_chain.o: bwtsw2.h bntseq.h bwt_lite.h bwt.h utils.h ksort.h -bwtsw2_core.o: bwt_lite.h bwtsw2.h bntseq.h bwt.h kvec.h utils.h khash.h -bwtsw2_core.o: ksort.h +bamlite.o: bamlite.h utils.h malloc_wrap.h +bntseq.o: bntseq.h utils.h kseq.h malloc_wrap.h +bwa.o: bntseq.h bwa.h bwt.h ksw.h malloc_wrap.h utils.h kseq.h +bwamem.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h ksw.h kvec.h +bwamem.o: ksort.h utils.h kbtree.h +bwamem_pair.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h kvec.h +bwamem_pair.o: utils.h ksw.h +bwape.o: bwtaln.h bwt.h kvec.h malloc_wrap.h bntseq.h utils.h bwase.h bwa.h +bwape.o: ksw.h khash.h +bwase.o: bwase.h bntseq.h bwt.h bwtaln.h utils.h kstring.h malloc_wrap.h +bwase.o: bwa.h ksw.h +bwaseqio.o: bwtaln.h bwt.h utils.h bamlite.h malloc_wrap.h kseq.h +bwt.o: utils.h bwt.h kvec.h malloc_wrap.h +bwt_gen.o: QSufSort.h utils.h malloc_wrap.h +bwt_lite.o: bwt_lite.h malloc_wrap.h +bwtaln.o: bwtaln.h bwt.h bwtgap.h utils.h bwa.h bntseq.h malloc_wrap.h +bwtgap.o: bwtgap.h bwt.h bwtaln.h malloc_wrap.h +bwtindex.o: bntseq.h bwt.h utils.h malloc_wrap.h +bwtsw2_aux.o: bntseq.h bwt_lite.h utils.h bwtsw2.h bwt.h kstring.h +bwtsw2_aux.o: malloc_wrap.h bwa.h ksw.h kseq.h ksort.h +bwtsw2_chain.o: bwtsw2.h bntseq.h bwt_lite.h bwt.h malloc_wrap.h ksort.h +bwtsw2_core.o: bwt_lite.h bwtsw2.h bntseq.h bwt.h kvec.h malloc_wrap.h +bwtsw2_core.o: khash.h ksort.h bwtsw2_main.o: bwt.h bwtsw2.h bntseq.h bwt_lite.h utils.h bwa.h -bwtsw2_pair.o: utils.h bwt.h bntseq.h bwtsw2.h bwt_lite.h kstring.h ksw.h -example.o: bwamem.h bwt.h bntseq.h bwa.h kseq.h utils.h -fastmap.o: bwa.h bntseq.h bwt.h bwamem.h kvec.h utils.h kseq.h -is.o: utils.h -kopen.o: utils.h -kstring.o: kstring.h utils.h -ksw.o: ksw.h utils.h +bwtsw2_pair.o: utils.h bwt.h bntseq.h bwtsw2.h bwt_lite.h kstring.h +bwtsw2_pair.o: malloc_wrap.h ksw.h +example.o: bwamem.h bwt.h bntseq.h bwa.h kseq.h malloc_wrap.h +fastmap.o: bwa.h bntseq.h bwt.h bwamem.h kvec.h malloc_wrap.h utils.h kseq.h +is.o: utils.h malloc_wrap.h +kopen.o: malloc_wrap.h +kstring.o: kstring.h malloc_wrap.h +ksw.o: ksw.h malloc_wrap.h main.o: utils.h -pemerge.o: ksw.h kseq.h utils.h kstring.h bwa.h bntseq.h bwt.h -utils.o: utils.h ksort.h kseq.h +malloc_wrap.o: malloc_wrap.h +pemerge.o: ksw.h malloc_wrap.h kseq.h kstring.h bwa.h bntseq.h bwt.h utils.h +utils.o: utils.h ksort.h malloc_wrap.h kseq.h diff --git a/bamlite.c b/bamlite.c index ec365d1..851cb6f 100644 --- a/bamlite.c +++ b/bamlite.c @@ -2,9 +2,12 @@ #include #include #include -#include "utils.h" #include "bamlite.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + /********************* * from bam_endian.c * *********************/ @@ -54,7 +57,7 @@ int bam_is_be; bam_header_t *bam_header_init() { bam_is_be = bam_is_big_endian(); - return (bam_header_t*)xcalloc(1, sizeof(bam_header_t)); + return (bam_header_t*)calloc(1, sizeof(bam_header_t)); } void bam_header_destroy(bam_header_t *header) @@ -87,17 +90,17 @@ bam_header_t *bam_header_read(bamFile fp) // read plain text and the number of reference sequences if (bam_read(fp, &header->l_text, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&header->l_text); - header->text = (char*)xcalloc(header->l_text + 1, 1); + header->text = (char*)calloc(header->l_text + 1, 1); if (bam_read(fp, header->text, header->l_text) != header->l_text) goto fail; if (bam_read(fp, &header->n_targets, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&header->n_targets); // read reference sequence names and lengths - header->target_name = (char**)xcalloc(header->n_targets, sizeof(char*)); - header->target_len = (uint32_t*)xcalloc(header->n_targets, 4); + header->target_name = (char**)calloc(header->n_targets, sizeof(char*)); + header->target_len = (uint32_t*)calloc(header->n_targets, 4); for (i = 0; i != header->n_targets; ++i) { if (bam_read(fp, &name_len, 4) != 4) goto fail; if (bam_is_be) bam_swap_endian_4p(&name_len); - header->target_name[i] = (char*)xcalloc(name_len, 1); + header->target_name[i] = (char*)calloc(name_len, 1); if (bam_read(fp, header->target_name[i], name_len) != name_len) { goto fail; } @@ -152,7 +155,7 @@ int bam_read1(bamFile fp, bam1_t *b) if (b->m_data < b->data_len) { b->m_data = b->data_len; kroundup32(b->m_data); - b->data = (uint8_t*)xrealloc(b->data, b->m_data); + b->data = (uint8_t*)realloc(b->data, b->m_data); } if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4; b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2; diff --git a/bamlite.h b/bamlite.h index 0c080fd..640e863 100644 --- a/bamlite.h +++ b/bamlite.h @@ -5,6 +5,10 @@ #include #include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + typedef gzFile bamFile; #define bam_open(fn, mode) xzopen(fn, mode) #define bam_dopen(fd, mode) gzdopen(fd, mode) @@ -72,7 +76,7 @@ typedef struct { #define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf) #define bam1_aux(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (b)->core.l_qseq + ((b)->core.l_qseq + 1)/2) -#define bam_init1() ((bam1_t*)xcalloc(1, sizeof(bam1_t))) +#define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t))) #define bam_destroy1(b) do { \ if (b) { free((b)->data); free(b); } \ } while (0) diff --git a/bntseq.c b/bntseq.c index 01a5e3c..e1cd323 100644 --- a/bntseq.c +++ b/bntseq.c @@ -37,6 +37,10 @@ #include "kseq.h" KSEQ_DECLARE(gzFile) +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + unsigned char nst_nt4_table[256] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, @@ -97,13 +101,13 @@ bntseq_t *bns_restore_core(const char *ann_filename, const char* amb_filename, c long long xx; int i; int scanres; - bns = (bntseq_t*)xcalloc(1, sizeof(bntseq_t)); + bns = (bntseq_t*)calloc(1, sizeof(bntseq_t)); { // read .ann fp = xopen(fname = ann_filename, "r"); scanres = fscanf(fp, "%lld%d%u", &xx, &bns->n_seqs, &bns->seed); if (scanres != 3) goto badread; bns->l_pac = xx; - bns->anns = (bntann1_t*)xcalloc(bns->n_seqs, sizeof(bntann1_t)); + bns->anns = (bntann1_t*)calloc(bns->n_seqs, sizeof(bntann1_t)); for (i = 0; i < bns->n_seqs; ++i) { bntann1_t *p = bns->anns + i; char *q = str; @@ -111,7 +115,7 @@ bntseq_t *bns_restore_core(const char *ann_filename, const char* amb_filename, c // read gi and sequence name scanres = fscanf(fp, "%u%s", &p->gi, str); if (scanres != 2) goto badread; - p->name = xstrdup(str); + p->name = strdup(str); // read fasta comments while (str - q < sizeof(str) - 1 && (c = fgetc(fp)) != '\n' && c != EOF) *q++ = c; while (c != '\n' && c != EOF) c = fgetc(fp); @@ -120,8 +124,8 @@ bntseq_t *bns_restore_core(const char *ann_filename, const char* amb_filename, c goto badread; } *q = 0; - if (q - str > 1) p->anno = xstrdup(str + 1); // skip leading space - else p->anno = xstrdup(""); + if (q - str > 1) p->anno = strdup(str + 1); // skip leading space + else p->anno = strdup(""); // read the rest scanres = fscanf(fp, "%lld%d%d", &xx, &p->len, &p->n_ambs); if (scanres != 3) goto badread; @@ -137,7 +141,7 @@ bntseq_t *bns_restore_core(const char *ann_filename, const char* amb_filename, c if (scanres != 3) goto badread; l_pac = xx; xassert(l_pac == bns->l_pac && n_seqs == bns->n_seqs, "inconsistent .ann and .amb files."); - bns->ambs = bns->n_holes? (bntamb1_t*)xcalloc(bns->n_holes, sizeof(bntamb1_t)) : 0; + bns->ambs = bns->n_holes? (bntamb1_t*)calloc(bns->n_holes, sizeof(bntamb1_t)) : 0; for (i = 0; i < bns->n_holes; ++i) { bntamb1_t *p = bns->ambs + i; scanres = fscanf(fp, "%lld%d%s", &xx, &p->len, str); @@ -193,11 +197,11 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ int i, lasts; if (bns->n_seqs == *m_seqs) { *m_seqs <<= 1; - bns->anns = (bntann1_t*)xrealloc(bns->anns, *m_seqs * sizeof(bntann1_t)); + bns->anns = (bntann1_t*)realloc(bns->anns, *m_seqs * sizeof(bntann1_t)); } p = bns->anns + bns->n_seqs; - p->name = xstrdup((char*)seq->name.s); - p->anno = seq->comment.s? xstrdup((char*)seq->comment.s) : xstrdup("(null)"); + p->name = strdup((char*)seq->name.s); + p->anno = seq->comment.s? strdup((char*)seq->comment.s) : strdup("(null)"); p->gi = 0; p->len = seq->seq.l; p->offset = (bns->n_seqs == 0)? 0 : (p-1)->offset + (p-1)->len; p->n_ambs = 0; @@ -209,7 +213,7 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ } else { if (bns->n_holes == *m_holes) { (*m_holes) <<= 1; - bns->ambs = (bntamb1_t*)xrealloc(bns->ambs, (*m_holes) * sizeof(bntamb1_t)); + bns->ambs = (bntamb1_t*)realloc(bns->ambs, (*m_holes) * sizeof(bntamb1_t)); } *q = bns->ambs + bns->n_holes; (*q)->len = 1; @@ -224,7 +228,7 @@ static uint8_t *add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_ if (c >= 4) c = lrand48()&3; if (bns->l_pac == *m_pac) { // double the pac size *m_pac <<= 1; - pac = xrealloc(pac, *m_pac/4); + pac = realloc(pac, *m_pac/4); memset(pac + bns->l_pac/4, 0, (*m_pac - bns->l_pac)/4); } _set_pac(pac, bns->l_pac, c); @@ -249,13 +253,13 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) // initialization seq = kseq_init(fp_fa); - bns = (bntseq_t*)xcalloc(1, sizeof(bntseq_t)); + bns = (bntseq_t*)calloc(1, sizeof(bntseq_t)); bns->seed = 11; // fixed seed for random generator srand48(bns->seed); m_seqs = m_holes = 8; m_pac = 0x10000; - bns->anns = (bntann1_t*)xcalloc(m_seqs, sizeof(bntann1_t)); - bns->ambs = (bntamb1_t*)xcalloc(m_holes, sizeof(bntamb1_t)); - pac = xcalloc(m_pac/4, 1); + bns->anns = (bntann1_t*)calloc(m_seqs, sizeof(bntann1_t)); + bns->ambs = (bntamb1_t*)calloc(m_holes, sizeof(bntamb1_t)); + pac = calloc(m_pac/4, 1); q = bns->ambs; strcpy(name, prefix); strcat(name, ".pac"); fp = xopen(name, "wb"); @@ -263,7 +267,7 @@ int64_t bns_fasta2bntseq(gzFile fp_fa, const char *prefix, int for_only) while (kseq_read(seq) >= 0) pac = add1(seq, bns, pac, &m_pac, &m_seqs, &m_holes, &q); if (!for_only) { // add the reverse complemented sequence m_pac = (bns->l_pac * 2 + 3) / 4 * 4; - pac = xrealloc(pac, m_pac/4); + pac = realloc(pac, m_pac/4); memset(pac + (bns->l_pac+3)/4, 0, (m_pac - (bns->l_pac+3)/4*4) / 4); for (l = bns->l_pac - 1; l >= 0; --l, ++bns->l_pac) _set_pac(pac, bns->l_pac, 3-_get_pac(pac, l)); @@ -357,7 +361,7 @@ uint8_t *bns_get_seq(int64_t l_pac, const uint8_t *pac, int64_t beg, int64_t end if (beg >= l_pac || end <= l_pac) { int64_t k, l = 0; *len = end - beg; - seq = xmalloc(end - beg); + seq = malloc(end - beg); if (beg >= l_pac) { // reverse strand int64_t beg_f = (l_pac<<1) - 1 - end; int64_t end_f = (l_pac<<1) - 1 - beg; diff --git a/bwa.c b/bwa.c index 181124c..a20c027 100644 --- a/bwa.c +++ b/bwa.c @@ -7,6 +7,10 @@ #include "ksw.h" #include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + int bwa_verbose = 3; char bwa_rg_id[256]; @@ -25,10 +29,10 @@ static inline void trim_readno(kstring_t *s) static inline void kseq2bseq1(const kseq_t *ks, bseq1_t *s) { // TODO: it would be better to allocate one chunk of memory, but probably it does not matter in practice - s->name = xstrdup(ks->name.s); - s->comment = ks->comment.l? xstrdup(ks->comment.s) : 0; - s->seq = xstrdup(ks->seq.s); - s->qual = ks->qual.l? xstrdup(ks->qual.s) : 0; + s->name = strdup(ks->name.s); + s->comment = ks->comment.l? strdup(ks->comment.s) : 0; + s->seq = strdup(ks->seq.s); + s->qual = ks->qual.l? strdup(ks->qual.s) : 0; s->l_seq = strlen(s->seq); } @@ -45,7 +49,7 @@ bseq1_t *bseq_read(int chunk_size, int *n_, void *ks1_, void *ks2_) } if (n >= m) { m = m? m<<1 : 256; - seqs = xrealloc(seqs, m * sizeof(bseq1_t)); + seqs = realloc(seqs, m * sizeof(bseq1_t)); } trim_readno(&ks->name); kseq2bseq1(ks, &seqs[n]); @@ -98,7 +102,7 @@ uint32_t *bwa_gen_cigar(const int8_t mat[25], int q, int r, int w_, int64_t l_pa tmp = rseq[i], rseq[i] = rseq[rlen - 1 - i], rseq[rlen - 1 - i] = tmp; } if (l_query == re - rb && w_ == 0) { // no gap; no need to do DP - cigar = xmalloc(4); + cigar = malloc(4); cigar[0] = l_query<<4 | 0; *n_cigar = 1; for (i = 0, *score = 0; i < l_query; ++i) @@ -205,7 +209,7 @@ char *bwa_idx_infer_prefix(const char *hint) int l_hint; FILE *fp; l_hint = strlen(hint); - prefix = xmalloc(l_hint + 3 + 4 + 1); + prefix = malloc(l_hint + 3 + 4 + 1); strcpy(prefix, hint); strcpy(prefix + l_hint, ".64.bwt"); if ((fp = fopen(prefix, "rb")) != 0) { @@ -234,7 +238,7 @@ bwt_t *bwa_idx_load_bwt(const char *hint) if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] fail to locate the index files\n", __func__); return 0; } - tmp = xcalloc(strlen(prefix) + 5, 1); + tmp = calloc(strlen(prefix) + 5, 1); strcat(strcpy(tmp, prefix), ".bwt"); // FM-index bwt = bwt_restore_bwt(tmp); strcat(strcpy(tmp, prefix), ".sa"); // partial suffix array (SA) @@ -252,12 +256,12 @@ bwaidx_t *bwa_idx_load(const char *hint, int which) if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] fail to locate the index files\n", __func__); return 0; } - idx = xcalloc(1, sizeof(bwaidx_t)); + idx = calloc(1, sizeof(bwaidx_t)); if (which & BWA_IDX_BWT) idx->bwt = bwa_idx_load_bwt(hint); if (which & BWA_IDX_BNS) { idx->bns = bns_restore(prefix); if (which & BWA_IDX_PAC) { - idx->pac = xcalloc(idx->bns->l_pac/4+1, 1); + idx->pac = calloc(idx->bns->l_pac/4+1, 1); err_fread_noeof(idx->pac, 1, idx->bns->l_pac/4+1, idx->bns->fp_pac); // concatenated 2-bit encoded sequence err_fclose(idx->bns->fp_pac); idx->bns->fp_pac = 0; @@ -312,7 +316,7 @@ char *bwa_set_rg(const char *s) if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] the read group line is not started with @RG\n", __func__); goto err_set_rg; } - rg_line = xstrdup(s); + rg_line = strdup(s); bwa_escape(rg_line); if ((p = strstr(rg_line, "\tID:")) == 0) { if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] no ID at the read group line\n", __func__); diff --git a/bwamem.c b/bwamem.c index 9f3aa9b..779a221 100644 --- a/bwamem.c +++ b/bwamem.c @@ -15,6 +15,10 @@ #include "ksort.h" #include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + /* Theory on probability and scoring *ungapped* alignment * * s'(a,b) = log[P(b|a)/P(b)] = log[4P(b|a)], assuming uniform base distribution @@ -41,7 +45,7 @@ mem_opt_t *mem_opt_init() { mem_opt_t *o; - o = xcalloc(1, sizeof(mem_opt_t)); + o = calloc(1, sizeof(mem_opt_t)); o->flag = 0; o->a = 1; o->b = 4; o->q = 6; o->r = 1; o->w = 100; o->T = 30; @@ -79,12 +83,12 @@ struct __smem_i { smem_i *smem_itr_init(const bwt_t *bwt) { smem_i *itr; - itr = xcalloc(1, sizeof(smem_i)); + itr = calloc(1, sizeof(smem_i)); itr->bwt = bwt; - itr->tmpvec[0] = xcalloc(1, sizeof(bwtintv_v)); - itr->tmpvec[1] = xcalloc(1, sizeof(bwtintv_v)); - itr->matches = xcalloc(1, sizeof(bwtintv_v)); - itr->sub = xcalloc(1, sizeof(bwtintv_v)); + itr->tmpvec[0] = calloc(1, sizeof(bwtintv_v)); + itr->tmpvec[1] = calloc(1, sizeof(bwtintv_v)); + itr->matches = calloc(1, sizeof(bwtintv_v)); + itr->sub = calloc(1, sizeof(bwtintv_v)); return itr; } @@ -181,7 +185,7 @@ static int test_and_merge(const mem_opt_t *opt, int64_t l_pac, mem_chain_t *c, c if (y >= 0 && x - y <= opt->w && y - x <= opt->w && x - last->len < opt->max_chain_gap && y - last->len < opt->max_chain_gap) { // grow the chain if (c->n == c->m) { c->m <<= 1; - c->seeds = xrealloc(c->seeds, c->m * sizeof(mem_seed_t)); + c->seeds = realloc(c->seeds, c->m * sizeof(mem_seed_t)); } c->seeds[c->n++] = *p; return 1; @@ -215,7 +219,7 @@ static void mem_insert_seed(const mem_opt_t *opt, int64_t l_pac, kbtree_t(chn) * } else to_add = 1; if (to_add) { // add the seed as a new chain tmp.n = 1; tmp.m = 4; - tmp.seeds = xcalloc(tmp.m, sizeof(mem_seed_t)); + tmp.seeds = calloc(tmp.m, sizeof(mem_seed_t)); tmp.seeds[0] = s; kb_putp(chn, tree, &tmp); } @@ -283,7 +287,7 @@ int mem_chain_flt(const mem_opt_t *opt, int n_chn, mem_chain_t *chains) flt_aux_t *a; int i, j, n; if (n_chn <= 1) return n_chn; // no need to filter - a = xmalloc(sizeof(flt_aux_t) * n_chn); + a = malloc(sizeof(flt_aux_t) * n_chn); for (i = 0; i < n_chn; ++i) { mem_chain_t *c = &chains[i]; int64_t end; @@ -309,7 +313,7 @@ int mem_chain_flt(const mem_opt_t *opt, int n_chn, mem_chain_t *chains) ks_introsort(mem_flt, n_chn, a); { // reorder chains such that the best chain appears first mem_chain_t *swap; - swap = xmalloc(sizeof(mem_chain_t) * n_chn); + swap = malloc(sizeof(mem_chain_t) * n_chn); for (i = 0; i < n_chn; ++i) { swap[i] = *((mem_chain_t*)a[i].p); a[i].p = &chains[i]; // as we will memcpy() below, a[i].p is changed @@ -512,7 +516,7 @@ void mem_chain2aln(const mem_opt_t *opt, int64_t l_pac, const uint8_t *pac, int rseq = bns_get_seq(l_pac, pac, rmax[0], rmax[1], &rlen); assert(rlen == rmax[1] - rmax[0]); - srt = xmalloc(c->n * 8); + srt = malloc(c->n * 8); for (i = 0; i < c->n; ++i) srt[i] = (uint64_t)c->seeds[i].len<<32 | i; ks_introsort_64(c->n, srt); @@ -560,10 +564,10 @@ void mem_chain2aln(const mem_opt_t *opt, int64_t l_pac, const uint8_t *pac, int if (s->qbeg) { // left extension uint8_t *rs, *qs; int qle, tle, gtle, gscore; - qs = xmalloc(s->qbeg); + qs = malloc(s->qbeg); for (i = 0; i < s->qbeg; ++i) qs[i] = query[s->qbeg - 1 - i]; tmp = s->rbeg - rmax[0]; - rs = xmalloc(tmp); + rs = malloc(tmp); for (i = 0; i < tmp; ++i) rs[i] = rseq[tmp - 1 - i]; for (i = 0; i < MAX_BAND_TRY; ++i) { int prev = a->score; @@ -838,7 +842,7 @@ mem_alnreg_v mem_align1(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t * { // the difference from mem_align1_core() is that this routine: 1) calls mem_mark_primary_se(); 2) does not modify the input sequence mem_alnreg_v ar; char *seq; - seq = xmalloc(l_seq); + seq = malloc(l_seq); memcpy(seq, seq_, l_seq); // makes a copy of seq_ ar = mem_align1_core(opt, bwt, bns, pac, l_seq, seq); mem_mark_primary_se(opt, ar.n, ar.a); @@ -861,7 +865,7 @@ mem_aln_t mem_reg2aln(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t * } qb = ar->qb, qe = ar->qe; rb = ar->rb, re = ar->re; - query = xmalloc(l_query); + query = malloc(l_query); for (i = 0; i < l_query; ++i) // convert to the nt4 encoding query[i] = query_[i] < 5? query_[i] : nst_nt4_table[(int)query_[i]]; a.mapq = ar->secondary < 0? mem_approx_mapq_se(opt, ar) : 0; @@ -887,7 +891,7 @@ mem_aln_t mem_reg2aln(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t * int clip5, clip3; clip5 = is_rev? l_query - qe : qb; clip3 = is_rev? qb : l_query - qe; - a.cigar = xrealloc(a.cigar, 4 * (a.n_cigar + 2)); + a.cigar = realloc(a.cigar, 4 * (a.n_cigar + 2)); if (clip5) { memmove(a.cigar+1, a.cigar, a.n_cigar * 4); a.cigar[0] = clip5<<4 | (opt->flag&MEM_F_HARDCLIP? 4 : 3); @@ -958,8 +962,8 @@ void mem_process_seqs(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t *bn mem_alnreg_v *regs; mem_pestat_t pes[4]; - w = xcalloc(opt->n_threads, sizeof(worker_t)); - regs = xmalloc(n * sizeof(mem_alnreg_v)); + w = calloc(opt->n_threads, sizeof(worker_t)); + regs = malloc(n * sizeof(mem_alnreg_v)); for (i = 0; i < opt->n_threads; ++i) { worker_t *p = &w[i]; p->start = i; p->step = opt->n_threads; p->n = n; @@ -980,7 +984,7 @@ void mem_process_seqs(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t *bn #ifdef HAVE_PTHREAD } else { pthread_t *tid; - tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); + tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); for (i = 0; i < opt->n_threads; ++i) pthread_create(&tid[i], 0, worker1, &w[i]); for (i = 0; i < opt->n_threads; ++i) pthread_join(tid[i], 0); if (opt->flag&MEM_F_PE) { diff --git a/bwamem_pair.c b/bwamem_pair.c index 19fc83b..06aacff 100644 --- a/bwamem_pair.c +++ b/bwamem_pair.c @@ -8,6 +8,11 @@ #include "utils.h" #include "ksw.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + + #define MIN_RATIO 0.8 #define MIN_DIR_CNT 10 #define MIN_DIR_RATIO 0.05 @@ -121,7 +126,7 @@ int mem_matesw(const mem_opt_t *opt, int64_t l_pac, const uint8_t *pac, const me is_rev = (r>>1 != (r&1)); // whether to reverse complement the mate is_larger = !(r>>1); // whether the mate has larger coordinate if (is_rev) { - rev = xmalloc(l_ms); // this is the reverse complement of $ms + rev = malloc(l_ms); // this is the reverse complement of $ms for (i = 0; i < l_ms; ++i) rev[l_ms - 1 - i] = ms[i] < 4? 3 - ms[i] : 4; seq = rev; } else seq = (uint8_t*)ms; @@ -294,7 +299,7 @@ int mem_sam_pe(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, co // write SAM h[0] = mem_reg2aln(opt, bns, pac, s[0].l_seq, s[0].seq, &a[0].a[z[0]]); h[0].mapq = q_se[0]; h[0].flag |= 0x40 | extra_flag; h[1] = mem_reg2aln(opt, bns, pac, s[1].l_seq, s[1].seq, &a[1].a[z[1]]); h[1].mapq = q_se[1]; h[1].flag |= 0x80 | extra_flag; - mem_aln2sam(bns, &str, &s[0], 1, &h[0], 0, &h[1]); s[0].sam = xstrdup(str.s); str.l = 0; + mem_aln2sam(bns, &str, &s[0], 1, &h[0], 0, &h[1]); s[0].sam = strdup(str.s); str.l = 0; mem_aln2sam(bns, &str, &s[1], 1, &h[1], 0, &h[0]); s[1].sam = str.s; free(h[0].cigar); free(h[1].cigar); } else goto no_pairing; diff --git a/bwape.c b/bwape.c index 030af0d..2a7f46e 100644 --- a/bwape.c +++ b/bwape.c @@ -12,6 +12,10 @@ #include "bwa.h" #include "ksw.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + typedef struct { int n; bwtint_t *a; @@ -50,7 +54,7 @@ void bwa_print_sam_PG(); pe_opt_t *bwa_init_pe_opt() { pe_opt_t *po; - po = (pe_opt_t*)xcalloc(1, sizeof(pe_opt_t)); + po = (pe_opt_t*)calloc(1, sizeof(pe_opt_t)); po->max_isize = 500; po->force_isize = 0; po->max_occ = 100000; @@ -83,7 +87,7 @@ static int infer_isize(int n_seqs, bwa_seq_t *seqs[2], isize_info_t *ii, double ii->avg = ii->std = -1.0; ii->low = ii->high = ii->high_bayesian = 0; - isizes = (uint64_t*)xcalloc(n_seqs, 8); + isizes = (uint64_t*)calloc(n_seqs, 8); for (i = 0, tot = 0; i != n_seqs; ++i) { bwa_seq_t *p[2]; p[0] = seqs[0] + i; p[1] = seqs[1] + i; @@ -263,9 +267,9 @@ int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bw pe_data_t *d; aln_buf_t *buf[2]; - d = (pe_data_t*)xcalloc(1, sizeof(pe_data_t)); - buf[0] = (aln_buf_t*)xcalloc(n_seqs, sizeof(aln_buf_t)); - buf[1] = (aln_buf_t*)xcalloc(n_seqs, sizeof(aln_buf_t)); + d = (pe_data_t*)calloc(1, sizeof(pe_data_t)); + buf[0] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t)); + buf[1] = (aln_buf_t*)calloc(n_seqs, sizeof(aln_buf_t)); if (_bwt == 0) { // load forward SA strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); @@ -338,7 +342,7 @@ int bwa_cal_pac_pos_pe(const bntseq_t *bns, const char *prefix, bwt_t *const _bw if (ret) { // not in the hash table; ret must equal 1 as we never remove elements poslist_t *z = &kh_val(g_hash, iter); z->n = r->l - r->k + 1; - z->a = (bwtint_t*)xmalloc(sizeof(bwtint_t) * z->n); + z->a = (bwtint_t*)malloc(sizeof(bwtint_t) * z->n); for (l = r->k; l <= r->l; ++l) { int strand; z->a[l - r->k] = bwa_sa2pos(bns, bwt, l, p[j]->len, &strand)<<1; @@ -420,7 +424,7 @@ bwa_cigar_t *bwa_sw_core(bwtint_t l_pac, const ubyte_t *pacseq, int len, const u if ((float)x/len >= 0.25 || len - x < SW_MIN_MATCH_LEN) return 0; // get reference subsequence - ref_seq = (ubyte_t*)xcalloc(reglen, 1); + ref_seq = (ubyte_t*)calloc(reglen, 1); for (k = *beg, l = 0; l < reglen && k < l_pac; ++k) ref_seq[l++] = pacseq[k>>2] >> ((~k&3)<<1) & 3; @@ -453,7 +457,7 @@ bwa_cigar_t *bwa_sw_core(bwtint_t l_pac, const ubyte_t *pacseq, int len, const u { // update cigar and coordinate; int start = r.qb, end = r.qe + 1; *beg += r.tb; - cigar = (bwa_cigar_t*)xrealloc(cigar, sizeof(bwa_cigar_t) * (*n_cigar + 2)); + cigar = (bwa_cigar_t*)realloc(cigar, sizeof(bwa_cigar_t) * (*n_cigar + 2)); if (start) { memmove(cigar + 1, cigar, sizeof(bwa_cigar_t) * (*n_cigar)); cigar[0] = __cigar_create(3, start); @@ -497,7 +501,7 @@ ubyte_t *bwa_paired_sw(const bntseq_t *bns, const ubyte_t *_pacseq, int n_seqs, // load reference sequence if (_pacseq == 0) { - pacseq = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + pacseq = (ubyte_t*)calloc(bns->l_pac/4+1, 1); err_rewind(bns->fp_pac); err_fread_noeof(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); } else pacseq = (ubyte_t*)_pacseq; @@ -653,7 +657,7 @@ void bwa_sai2sam_pe_core(const char *prefix, char *const fn_sa[2], char *const f if (popt->is_preload) { strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); strcpy(str, prefix); strcat(str, ".sa"); bwt_restore_sa(str, bwt); - pac = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + pac = (ubyte_t*)calloc(bns->l_pac/4+1, 1); err_rewind(bns->fp_pac); err_fread_noeof(pac, 1, bns->l_pac/4+1, bns->fp_pac); } diff --git a/bwase.c b/bwase.c index bf7e9cb..85165d6 100644 --- a/bwase.c +++ b/bwase.c @@ -12,6 +12,10 @@ #include "bwa.h" #include "ksw.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + int g_log_n[256]; void bwa_print_sam_PG(); @@ -59,7 +63,7 @@ void bwa_aln2seq_core(int n_aln, const bwt_aln1_t *aln, bwa_seq_t *s, int set_ma * simply output all hits, but the following samples "rest" * number of random hits. */ rest = n_occ > n_multi + 1? n_multi + 1 : n_occ; // find one additional for ->sa - s->multi = xcalloc(rest, sizeof(bwt_multi1_t)); + s->multi = calloc(rest, sizeof(bwt_multi1_t)); for (k = 0; k < n_aln; ++k) { const bwt_aln1_t *q = aln + k; if (q->l - q->k + 1 <= rest) { @@ -184,7 +188,7 @@ bwa_cigar_t *bwa_refine_gapped_core(bwtint_t l_pac, const ubyte_t *pacseq, int l if (rlen == 0) goto refine_gapped_err; ksw_global(qle, &seq[len-qle], rlen, rseq, 5, mat, 5, 1, SW_BW, n_cigar, &cigar32); if (qle < len) { // write soft clip - cigar = xrealloc(cigar, (*n_cigar + 1) * 4); + cigar = realloc(cigar, (*n_cigar + 1) * 4); memmove(cigar + 1, cigar, *n_cigar * 4); cigar[0] = (len - qle)<<4 | FROM_S; ++(*n_cigar); @@ -199,7 +203,7 @@ bwa_cigar_t *bwa_refine_gapped_core(bwtint_t l_pac, const ubyte_t *pacseq, int l if (rlen == 0) goto refine_gapped_err; ksw_global(qle, seq, rlen, rseq, 5, mat, 5, 1, SW_BW, n_cigar, &cigar32); // right extension if (qle < len) { - cigar = xrealloc(cigar, (*n_cigar + 1) * 4); + cigar = realloc(cigar, (*n_cigar + 1) * 4); cigar[*n_cigar - 1] = (len - qle)<<4 | FROM_S; ++(*n_cigar); } @@ -265,7 +269,7 @@ char *bwa_cal_md1(int n_cigar, bwa_cigar_t *cigar, int len, bwtint_t pos, ubyte_ } ksprintf(str, "%d", u); *_nm = nm; - return xstrdup(str->s); + return strdup(str->s); } void bwa_correct_trimmed(bwa_seq_t *s) @@ -277,11 +281,11 @@ void bwa_correct_trimmed(bwa_seq_t *s) } else { if (s->cigar == 0) { s->n_cigar = 2; - s->cigar = xcalloc(s->n_cigar, sizeof(bwa_cigar_t)); + s->cigar = calloc(s->n_cigar, sizeof(bwa_cigar_t)); s->cigar[0] = __cigar_create(0, s->len); } else { ++s->n_cigar; - s->cigar = xrealloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); + s->cigar = realloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); } s->cigar[s->n_cigar-1] = __cigar_create(3, (s->full_len - s->len)); } @@ -291,11 +295,11 @@ void bwa_correct_trimmed(bwa_seq_t *s) } else { if (s->cigar == 0) { s->n_cigar = 2; - s->cigar = xcalloc(s->n_cigar, sizeof(bwa_cigar_t)); + s->cigar = calloc(s->n_cigar, sizeof(bwa_cigar_t)); s->cigar[1] = __cigar_create(0, s->len); } else { ++s->n_cigar; - s->cigar = xrealloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); + s->cigar = realloc(s->cigar, s->n_cigar * sizeof(bwa_cigar_t)); memmove(s->cigar + 1, s->cigar, (s->n_cigar-1) * sizeof(bwa_cigar_t)); } s->cigar[0] = __cigar_create(3, (s->full_len - s->len)); @@ -311,7 +315,7 @@ void bwa_refine_gapped(const bntseq_t *bns, int n_seqs, bwa_seq_t *seqs, ubyte_t kstring_t *str; if (!_pacseq) { - pacseq = (ubyte_t*)xcalloc(bns->l_pac/4+1, 1); + pacseq = (ubyte_t*)calloc(bns->l_pac/4+1, 1); err_rewind(bns->fp_pac); err_fread_noeof(pacseq, 1, bns->l_pac/4+1, bns->fp_pac); } else pacseq = _pacseq; @@ -330,7 +334,7 @@ void bwa_refine_gapped(const bntseq_t *bns, int n_seqs, bwa_seq_t *seqs, ubyte_t if (s->cigar == 0) s->type = BWA_TYPE_NO_MATCH; } // generate MD tag - str = (kstring_t*)xcalloc(1, sizeof(kstring_t)); + str = (kstring_t*)calloc(1, sizeof(kstring_t)); for (i = 0; i != n_seqs; ++i) { bwa_seq_t *s = seqs + i; if (s->type != BWA_TYPE_NO_MATCH) { @@ -559,7 +563,7 @@ void bwa_sai2sam_se_core(const char *prefix, const char *fn_sa, const char *fn_f err_fread_noeof(&n_aln, 4, 1, fp_sa); if (n_aln > m_aln) { m_aln = n_aln; - aln = (bwt_aln1_t*)xrealloc(aln, sizeof(bwt_aln1_t) * m_aln); + aln = (bwt_aln1_t*)realloc(aln, sizeof(bwt_aln1_t) * m_aln); } err_fread_noeof(aln, sizeof(bwt_aln1_t), n_aln, fp_sa); bwa_aln2seq_core(n_aln, aln, p, 1, n_occ); diff --git a/bwaseqio.c b/bwaseqio.c index 57ed654..d157945 100644 --- a/bwaseqio.c +++ b/bwaseqio.c @@ -7,6 +7,10 @@ #include "kseq.h" KSEQ_DECLARE(gzFile) +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + extern unsigned char nst_nt4_table[256]; static char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; @@ -22,7 +26,7 @@ bwa_seqio_t *bwa_bam_open(const char *fn, int which) { bwa_seqio_t *bs; bam_header_t *h; - bs = (bwa_seqio_t*)xcalloc(1, sizeof(bwa_seqio_t)); + bs = (bwa_seqio_t*)calloc(1, sizeof(bwa_seqio_t)); bs->is_bam = 1; bs->which = which; bs->fp = bam_open(fn, "r"); @@ -35,7 +39,7 @@ bwa_seqio_t *bwa_seq_open(const char *fn) { gzFile fp; bwa_seqio_t *bs; - bs = (bwa_seqio_t*)xcalloc(1, sizeof(bwa_seqio_t)); + bs = (bwa_seqio_t*)calloc(1, sizeof(bwa_seqio_t)); fp = xzopen(fn, "r"); bs->ks = kseq_init(fp); return bs; @@ -93,7 +97,7 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com b = bam_init1(); n_seqs = 0; - seqs = (bwa_seq_t*)xcalloc(n_needed, sizeof(bwa_seq_t)); + seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t)); while (bam_read1(bs->fp, b) >= 0) { uint8_t *s, *q; int go = 0; @@ -108,8 +112,8 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com p->full_len = p->clip_len = p->len = l; n_tot += p->full_len; s = bam1_seq(b); q = bam1_qual(b); - p->seq = (ubyte_t*)xcalloc(p->len + 1, 1); - p->qual = (ubyte_t*)xcalloc(p->len + 1, 1); + p->seq = (ubyte_t*)calloc(p->len + 1, 1); + p->qual = (ubyte_t*)calloc(p->len + 1, 1); for (i = 0; i != p->full_len; ++i) { p->seq[i] = bam_nt16_nt4_table[(int)bam1_seqi(s, i)]; p->qual[i] = q[i] + 33 < 126? q[i] + 33 : 126; @@ -119,11 +123,11 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com seq_reverse(p->len, p->qual, 0); } if (trim_qual >= 1) n_trimmed += bwa_trim_read(trim_qual, p); - p->rseq = (ubyte_t*)xcalloc(p->full_len, 1); + p->rseq = (ubyte_t*)calloc(p->full_len, 1); memcpy(p->rseq, p->seq, p->len); seq_reverse(p->len, p->seq, 0); // *IMPORTANT*: will be reversed back in bwa_refine_gapped() seq_reverse(p->len, p->rseq, is_comp); - p->name = xstrdup((const char*)bam1_qname(b)); + p->name = strdup((const char*)bam1_qname(b)); if (n_seqs == n_needed) break; } *n = n_seqs; @@ -153,7 +157,7 @@ bwa_seq_t *bwa_read_seq(bwa_seqio_t *bs, int n_needed, int *n, int mode, int tri } if (bs->is_bam) return bwa_read_bam(bs, n_needed, n, is_comp, trim_qual); // l_bc has no effect for BAM input n_seqs = 0; - seqs = (bwa_seq_t*)xcalloc(n_needed, sizeof(bwa_seq_t)); + seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t)); while ((l = kseq_read(seq)) >= 0) { if ((mode & BWA_MODE_CFY) && (seq->comment.l != 0)) { // skip reads that are marked to be filtered by Casava @@ -184,18 +188,18 @@ bwa_seq_t *bwa_read_seq(bwa_seqio_t *bs, int n_needed, int *n, int mode, int tri p->qual = 0; p->full_len = p->clip_len = p->len = l; n_tot += p->full_len; - p->seq = (ubyte_t*)xcalloc(p->full_len, 1); + p->seq = (ubyte_t*)calloc(p->full_len, 1); for (i = 0; i != p->full_len; ++i) p->seq[i] = nst_nt4_table[(int)seq->seq.s[i]]; if (seq->qual.l) { // copy quality - p->qual = (ubyte_t*)xstrdup((char*)seq->qual.s); + p->qual = (ubyte_t*)strdup((char*)seq->qual.s); if (trim_qual >= 1) n_trimmed += bwa_trim_read(trim_qual, p); } - p->rseq = (ubyte_t*)xcalloc(p->full_len, 1); + p->rseq = (ubyte_t*)calloc(p->full_len, 1); memcpy(p->rseq, p->seq, p->len); seq_reverse(p->len, p->seq, 0); // *IMPORTANT*: will be reversed back in bwa_refine_gapped() seq_reverse(p->len, p->rseq, is_comp); - p->name = xstrdup((const char*)seq->name.s); + p->name = strdup((const char*)seq->name.s); { // trim /[12]$ int t = strlen(p->name); if (t > 2 && p->name[t-2] == '/' && (p->name[t-1] == '1' || p->name[t-1] == '2')) p->name[t-2] = '\0'; diff --git a/bwt.c b/bwt.c index edd0afc..c9bf6a3 100644 --- a/bwt.c +++ b/bwt.c @@ -34,6 +34,10 @@ #include "bwt.h" #include "kvec.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + void bwt_gen_cnt_table(bwt_t *bwt) { int i, j; @@ -66,7 +70,7 @@ void bwt_cal_sa(bwt_t *bwt, int intv) if (bwt->sa) free(bwt->sa); bwt->sa_intv = intv; bwt->n_sa = (bwt->seq_len + intv) / intv; - bwt->sa = (bwtint_t*)xcalloc(bwt->n_sa, sizeof(bwtint_t)); + bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t)); // calculate SA value isa = 0; sa = bwt->seq_len; for (i = 0; i < bwt->seq_len; ++i) { @@ -397,7 +401,7 @@ void bwt_restore_sa(const char *fn, bwt_t *bwt) xassert(primary == bwt->seq_len, "SA-BWT inconsistency: seq_len is not the same."); bwt->n_sa = (bwt->seq_len + bwt->sa_intv) / bwt->sa_intv; - bwt->sa = (bwtint_t*)xcalloc(bwt->n_sa, sizeof(bwtint_t)); + bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t)); bwt->sa[0] = -1; fread_fix(fp, sizeof(bwtint_t) * (bwt->n_sa - 1), bwt->sa + 1); @@ -409,11 +413,11 @@ bwt_t *bwt_restore_bwt(const char *fn) bwt_t *bwt; FILE *fp; - bwt = (bwt_t*)xcalloc(1, sizeof(bwt_t)); + bwt = (bwt_t*)calloc(1, sizeof(bwt_t)); fp = xopen(fn, "rb"); err_fseek(fp, 0, SEEK_END); bwt->bwt_size = (err_ftell(fp) - sizeof(bwtint_t) * 5) >> 2; - bwt->bwt = (uint32_t*)xcalloc(bwt->bwt_size, 4); + bwt->bwt = (uint32_t*)calloc(bwt->bwt_size, 4); err_fseek(fp, 0, SEEK_SET); err_fread_noeof(&bwt->primary, sizeof(bwtint_t), 1, fp); err_fread_noeof(bwt->L2+1, sizeof(bwtint_t), 4, fp); diff --git a/bwt_gen.c b/bwt_gen.c index e9a5c93..d68b30f 100644 --- a/bwt_gen.c +++ b/bwt_gen.c @@ -30,6 +30,10 @@ #include "QSufSort.h" #include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + typedef uint64_t bgint_t; typedef int64_t sbgint_t; @@ -320,25 +324,25 @@ BWT *BWTCreate(const bgint_t textLength, unsigned int *decodeTable) { BWT *bwt; - bwt = (BWT*)xcalloc(1, sizeof(BWT)); + bwt = (BWT*)calloc(1, sizeof(BWT)); bwt->textLength = 0; - bwt->cumulativeFreq = (bgint_t*)xcalloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); + bwt->cumulativeFreq = (bgint_t*)calloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); initializeVAL_bg(bwt->cumulativeFreq, ALPHABET_SIZE + 1, 0); bwt->bwtSizeInWord = 0; // Generate decode tables if (decodeTable == NULL) { - bwt->decodeTable = (unsigned*)xcalloc(DNA_OCC_CNT_TABLE_SIZE_IN_WORD, sizeof(unsigned int)); + bwt->decodeTable = (unsigned*)calloc(DNA_OCC_CNT_TABLE_SIZE_IN_WORD, sizeof(unsigned int)); GenerateDNAOccCountTable(bwt->decodeTable); } else { bwt->decodeTable = decodeTable; } bwt->occMajorSizeInWord = BWTOccValueMajorSizeInWord(textLength); - bwt->occValueMajor = (bgint_t*)xcalloc(bwt->occMajorSizeInWord, sizeof(bgint_t)); + bwt->occValueMajor = (bgint_t*)calloc(bwt->occMajorSizeInWord, sizeof(bgint_t)); bwt->occSizeInWord = 0; bwt->occValue = NULL; @@ -354,16 +358,16 @@ BWTInc *BWTIncCreate(const bgint_t textLength, unsigned int initialMaxBuildSize, if (textLength < incMaxBuildSize) incMaxBuildSize = textLength; if (textLength < initialMaxBuildSize) initialMaxBuildSize = textLength; - bwtInc = (BWTInc*)xcalloc(1, sizeof(BWTInc)); + bwtInc = (BWTInc*)calloc(1, sizeof(BWTInc)); bwtInc->numberOfIterationDone = 0; bwtInc->bwt = BWTCreate(textLength, NULL); bwtInc->initialMaxBuildSize = initialMaxBuildSize; bwtInc->incMaxBuildSize = incMaxBuildSize; - bwtInc->cumulativeCountInCurrentBuild = (bgint_t*)xcalloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); + bwtInc->cumulativeCountInCurrentBuild = (bgint_t*)calloc((ALPHABET_SIZE + 1), sizeof(bgint_t)); initializeVAL_bg(bwtInc->cumulativeCountInCurrentBuild, ALPHABET_SIZE + 1, 0); // Build frequently accessed data - bwtInc->packedShift = (unsigned*)xcalloc(CHAR_PER_WORD, sizeof(unsigned int)); + bwtInc->packedShift = (unsigned*)calloc(CHAR_PER_WORD, sizeof(unsigned int)); for (i=0; ipackedShift[i] = BITS_IN_WORD - (i+1) * BIT_PER_CHAR; @@ -373,7 +377,7 @@ BWTInc *BWTIncCreate(const bgint_t textLength, unsigned int initialMaxBuildSize, + incMaxBuildSize/5 * 3 * (sizeof(bgint_t) / 4); // space for the 3 temporary arrays in each iteration if (bwtInc->availableWord < MIN_AVAILABLE_WORD) bwtInc->availableWord = MIN_AVAILABLE_WORD; // lh3: otherwise segfaul when availableWord is too small fprintf(stderr, "[%s] textLength=%ld, availableWord=%ld\n", __func__, (long)textLength, (long)bwtInc->availableWord); - bwtInc->workingMemory = (unsigned*)xcalloc(bwtInc->availableWord, BYTES_IN_WORD); + bwtInc->workingMemory = (unsigned*)calloc(bwtInc->availableWord, BYTES_IN_WORD); return bwtInc; } diff --git a/bwt_lite.c b/bwt_lite.c index 83dafc4..6cd3b1d 100644 --- a/bwt_lite.c +++ b/bwt_lite.c @@ -2,7 +2,10 @@ #include #include #include "bwt_lite.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif int is_sa(const uint8_t *T, uint32_t *SA, int n); int is_bwt(uint8_t *T, int n); @@ -11,21 +14,21 @@ bwtl_t *bwtl_seq2bwtl(int len, const uint8_t *seq) { bwtl_t *b; int i; - b = (bwtl_t*)xcalloc(1, sizeof(bwtl_t)); + b = (bwtl_t*)calloc(1, sizeof(bwtl_t)); b->seq_len = len; { // calculate b->bwt uint8_t *s; - b->sa = (uint32_t*)xcalloc(len + 1, 4); + b->sa = (uint32_t*)calloc(len + 1, 4); is_sa(seq, b->sa, len); - s = (uint8_t*)xcalloc(len + 1, 1); + s = (uint8_t*)calloc(len + 1, 1); for (i = 0; i <= len; ++i) { if (b->sa[i] == 0) b->primary = i; else s[i] = seq[b->sa[i] - 1]; } for (i = b->primary; i < len; ++i) s[i] = s[i + 1]; b->bwt_size = (len + 15) / 16; - b->bwt = (uint32_t*)xcalloc(b->bwt_size, 4); + b->bwt = (uint32_t*)calloc(b->bwt_size, 4); for (i = 0; i < len; ++i) b->bwt[i>>4] |= s[i] << ((15 - (i&15)) << 1); free(s); @@ -33,7 +36,7 @@ bwtl_t *bwtl_seq2bwtl(int len, const uint8_t *seq) { // calculate b->occ uint32_t c[4]; b->n_occ = (len + 15) / 16 * 4; - b->occ = (uint32_t*)xcalloc(b->n_occ, 4); + b->occ = (uint32_t*)calloc(b->n_occ, 4); memset(c, 0, 16); for (i = 0; i < len; ++i) { if (i % 16 == 0) diff --git a/bwtaln.c b/bwtaln.c index f6b26a8..e772792 100644 --- a/bwtaln.c +++ b/bwtaln.c @@ -17,10 +17,14 @@ #include #endif +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + gap_opt_t *gap_init_opt() { gap_opt_t *o; - o = (gap_opt_t*)xcalloc(1, sizeof(gap_opt_t)); + o = (gap_opt_t*)calloc(1, sizeof(gap_opt_t)); /* IMPORTANT: s_mm*10 should be about the average base error rate. Voilating this requirement will break pairing! */ o->s_mm = 3; o->s_gapo = 11; o->s_gape = 4; @@ -90,7 +94,7 @@ void bwa_cal_sa_reg_gap(int tid, bwt_t *const bwt, int n_seqs, bwa_seq_t *seqs, if (local_opt.max_diff < local_opt.max_gapo) local_opt.max_gapo = local_opt.max_diff; stack = gap_init_stack(local_opt.max_diff, local_opt.max_gapo, local_opt.max_gape, &local_opt); - seed_w = (bwt_width_t*)xcalloc(opt->seed_len+1, sizeof(bwt_width_t)); + seed_w = (bwt_width_t*)calloc(opt->seed_len+1, sizeof(bwt_width_t)); w = 0; for (i = 0; i != n_seqs; ++i) { bwa_seq_t *p = seqs + i; @@ -100,7 +104,7 @@ void bwa_cal_sa_reg_gap(int tid, bwt_t *const bwt, int n_seqs, bwa_seq_t *seqs, p->sa = 0; p->type = BWA_TYPE_NO_MATCH; p->c1 = p->c2 = 0; p->n_aln = 0; p->aln = 0; if (max_l < p->len) { max_l = p->len; - w = (bwt_width_t*)xrealloc(w, (max_l + 1) * sizeof(bwt_width_t)); + w = (bwt_width_t*)realloc(w, (max_l + 1) * sizeof(bwt_width_t)); memset(w, 0, (max_l + 1) * sizeof(bwt_width_t)); } bwt_cal_width(bwt, p->len, p->seq, w); @@ -163,7 +167,7 @@ void bwa_aln_core(const char *prefix, const char *fn_fa, const gap_opt_t *opt) ks = bwa_open_reads(opt->mode, fn_fa); { // load BWT - char *str = (char*)xcalloc(strlen(prefix) + 10, 1); + char *str = (char*)calloc(strlen(prefix) + 10, 1); strcpy(str, prefix); strcat(str, ".bwt"); bwt = bwt_restore_bwt(str); free(str); } @@ -186,8 +190,8 @@ void bwa_aln_core(const char *prefix, const char *fn_fa, const gap_opt_t *opt) int j; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - data = (thread_aux_t*)xcalloc(opt->n_threads, sizeof(thread_aux_t)); - tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); + data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t)); + tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); for (j = 0; j < opt->n_threads; ++j) { data[j].tid = j; data[j].bwt = bwt; data[j].n_seqs = n_seqs; data[j].seqs = seqs; data[j].opt = opt; diff --git a/bwtgap.c b/bwtgap.c index cef9561..16d9025 100644 --- a/bwtgap.c +++ b/bwtgap.c @@ -3,7 +3,10 @@ #include #include "bwtgap.h" #include "bwtaln.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif #define STATE_M 0 #define STATE_I 1 @@ -14,9 +17,9 @@ gap_stack_t *gap_init_stack2(int max_score) { gap_stack_t *stack; - stack = (gap_stack_t*)xcalloc(1, sizeof(gap_stack_t)); + stack = (gap_stack_t*)calloc(1, sizeof(gap_stack_t)); stack->n_stacks = max_score; - stack->stacks = (gap_stack1_t*)xcalloc(stack->n_stacks, sizeof(gap_stack1_t)); + stack->stacks = (gap_stack1_t*)calloc(stack->n_stacks, sizeof(gap_stack1_t)); return stack; } @@ -52,7 +55,7 @@ static inline void gap_push(gap_stack_t *stack, int i, bwtint_t k, bwtint_t l, i q = stack->stacks + score; if (q->n_entries == q->m_entries) { q->m_entries = q->m_entries? q->m_entries<<1 : 4; - q->stack = (gap_entry_t*)xrealloc(q->stack, sizeof(gap_entry_t) * q->m_entries); + q->stack = (gap_entry_t*)realloc(q->stack, sizeof(gap_entry_t) * q->m_entries); } p = q->stack + q->n_entries; p->info = (u_int32_t)score<<21 | i; p->k = k; p->l = l; @@ -111,7 +114,7 @@ bwt_aln1_t *bwt_match_gap(bwt_t *const bwt, int len, const ubyte_t *seq, bwt_wid bwt_aln1_t *aln; m_aln = 4; n_aln = 0; - aln = (bwt_aln1_t*)xcalloc(m_aln, sizeof(bwt_aln1_t)); + aln = (bwt_aln1_t*)calloc(m_aln, sizeof(bwt_aln1_t)); // check whether there are too many N for (j = _j = 0; j < len; ++j) @@ -178,7 +181,7 @@ bwt_aln1_t *bwt_match_gap(bwt_t *const bwt, int len, const ubyte_t *seq, bwt_wid gap_shadow(l - k + 1, len, bwt->seq_len, e.last_diff_pos, width); if (n_aln == m_aln) { m_aln <<= 1; - aln = (bwt_aln1_t*)xrealloc(aln, m_aln * sizeof(bwt_aln1_t)); + aln = (bwt_aln1_t*)realloc(aln, m_aln * sizeof(bwt_aln1_t)); memset(aln + m_aln/2, 0, m_aln/2*sizeof(bwt_aln1_t)); } p = aln + n_aln; diff --git a/bwtindex.c b/bwtindex.c index 6c1731c..9e3ec15 100644 --- a/bwtindex.c +++ b/bwtindex.c @@ -39,6 +39,11 @@ #include "divsufsort.h" #endif +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + + int is_bwt(ubyte_t *T, int n); int64_t bwa_seq_len(const char *fn_pac) @@ -62,18 +67,18 @@ bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is) FILE *fp; // initialization - bwt = (bwt_t*)xcalloc(1, sizeof(bwt_t)); + bwt = (bwt_t*)calloc(1, sizeof(bwt_t)); bwt->seq_len = bwa_seq_len(fn_pac); bwt->bwt_size = (bwt->seq_len + 15) >> 4; fp = xopen(fn_pac, "rb"); // prepare sequence pac_size = (bwt->seq_len>>2) + ((bwt->seq_len&3) == 0? 0 : 1); - buf2 = (ubyte_t*)xcalloc(pac_size, 1); + buf2 = (ubyte_t*)calloc(pac_size, 1); err_fread_noeof(buf2, 1, pac_size, fp); err_fclose(fp); memset(bwt->L2, 0, 5 * 4); - buf = (ubyte_t*)xcalloc(bwt->seq_len + 1, 1); + buf = (ubyte_t*)calloc(bwt->seq_len + 1, 1); for (i = 0; i < bwt->seq_len; ++i) { buf[i] = buf2[i>>2] >> ((3 - (i&3)) << 1) & 3; ++bwt->L2[1+buf[i]]; @@ -91,7 +96,7 @@ bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is) err_fatal_simple("libdivsufsort is not compiled in."); #endif } - bwt->bwt = (u_int32_t*)xcalloc(bwt->bwt_size, 4); + bwt->bwt = (u_int32_t*)calloc(bwt->bwt_size, 4); for (i = 0; i < bwt->seq_len; ++i) bwt->bwt[i>>4] |= buf[i] << ((15 - (i&15)) << 1); free(buf); @@ -127,7 +132,7 @@ void bwt_bwtupdate_core(bwt_t *bwt) n_occ = (bwt->seq_len + OCC_INTERVAL - 1) / OCC_INTERVAL + 1; bwt->bwt_size += n_occ * sizeof(bwtint_t); // the new size - buf = (uint32_t*)xcalloc(bwt->bwt_size, 4); // will be the new bwt + buf = (uint32_t*)calloc(bwt->bwt_size, 4); // will be the new bwt c[0] = c[1] = c[2] = c[3] = 0; for (i = k = 0; i < bwt->seq_len; ++i) { if (i % OCC_INTERVAL == 0) { @@ -196,7 +201,7 @@ int bwa_index(int argc, char *argv[]) // the "index" command else if (strcmp(optarg, "is") == 0) algo_type = 3; else err_fatal(__func__, "unknown algorithm: '%s'.", optarg); break; - case 'p': prefix = xstrdup(optarg); break; + case 'p': prefix = strdup(optarg); break; case '6': is_64 = 1; break; default: return 1; } @@ -215,13 +220,13 @@ int bwa_index(int argc, char *argv[]) // the "index" command return 1; } if (prefix == 0) { - prefix = xmalloc(strlen(argv[optind]) + 4); + prefix = malloc(strlen(argv[optind]) + 4); strcpy(prefix, argv[optind]); if (is_64) strcat(prefix, ".64"); } - str = (char*)xcalloc(strlen(prefix) + 10, 1); - str2 = (char*)xcalloc(strlen(prefix) + 10, 1); - str3 = (char*)xcalloc(strlen(prefix) + 10, 1); + str = (char*)calloc(strlen(prefix) + 10, 1); + str2 = (char*)calloc(strlen(prefix) + 10, 1); + str3 = (char*)calloc(strlen(prefix) + 10, 1); { // nucleotide indexing gzFile fp = xzopen(argv[optind], "r"); diff --git a/bwtsw2_aux.c b/bwtsw2_aux.c index 074af10..d225187 100644 --- a/bwtsw2_aux.c +++ b/bwtsw2_aux.c @@ -22,6 +22,11 @@ KSEQ_DECLARE(gzFile) #define __left_lt(a, b) ((a).end > (b).end) KSORT_INIT(hit, bsw2hit_t, __left_lt) +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + + extern unsigned char nst_nt4_table[256]; unsigned char nt_comp_table[256] = { @@ -48,7 +53,7 @@ extern int bsw2_resolve_query_overlaps(bwtsw2_t *b, float mask_level); bsw2opt_t *bsw2_init_opt() { - bsw2opt_t *o = (bsw2opt_t*)xcalloc(1, sizeof(bsw2opt_t)); + bsw2opt_t *o = (bsw2opt_t*)calloc(1, sizeof(bsw2opt_t)); o->a = 1; o->b = 3; o->q = 5; o->r = 2; o->t = 30; o->bw = 50; o->max_ins = 20000; @@ -73,11 +78,11 @@ void bsw2_destroy(bwtsw2_t *b) bwtsw2_t *bsw2_dup_no_cigar(const bwtsw2_t *b) { bwtsw2_t *p; - p = xcalloc(1, sizeof(bwtsw2_t)); + p = calloc(1, sizeof(bwtsw2_t)); p->max = p->n = b->n; if (b->n) { kroundup32(p->max); - p->hits = xcalloc(p->max, sizeof(bsw2hit_t)); + p->hits = calloc(p->max, sizeof(bsw2hit_t)); memcpy(p->hits, b->hits, p->n * sizeof(bsw2hit_t)); } return p; @@ -100,10 +105,10 @@ void bsw2_extend_left(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *_query, int lq int8_t mat[25]; bwa_fill_scmat(opt->a, opt->b, mat); - query = xcalloc(lq, 1); + query = calloc(lq, 1); // sort according to the descending order of query end ks_introsort(hit, b->n, b->hits); - target = xcalloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); + target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); // reverse _query for (i = 0; i < lq; ++i) query[lq - i - 1] = _query[i]; // core loop @@ -144,7 +149,7 @@ void bsw2_extend_rght(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *query, int lq, int8_t mat[25]; bwa_fill_scmat(opt->a, opt->b, mat); - target = xcalloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); + target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1); for (i = 0; i < b->n; ++i) { bsw2hit_t *p = b->hits + i; int lt = ((lq - p->beg + 1) / 2 * opt->a + opt->r) / opt->r + lq; @@ -192,7 +197,7 @@ static void gen_cigar(const bsw2opt_t *opt, int lq, uint8_t *seq[2], int64_t l_p } #endif if (q->cigar && (beg != 0 || end < lq)) { // write soft clipping - q->cigar = xrealloc(q->cigar, 4 * (q->n_cigar + 2)); + q->cigar = realloc(q->cigar, 4 * (q->n_cigar + 2)); if (beg != 0) { memmove(q->cigar + 1, q->cigar, q->n_cigar * 4); q->cigar[0] = beg<<4 | 4; @@ -223,7 +228,7 @@ static void merge_hits(bwtsw2_t *b[2], int l, int is_reverse) int i; if (b[0]->n + b[1]->n > b[0]->max) { b[0]->max = b[0]->n + b[1]->n; - b[0]->hits = xrealloc(b[0]->hits, b[0]->max * sizeof(bsw2hit_t)); + b[0]->hits = realloc(b[0]->hits, b[0]->max * sizeof(bsw2hit_t)); } for (i = 0; i < b[1]->n; ++i) { bsw2hit_t *p = b[0]->hits + b[0]->n + i; @@ -251,9 +256,9 @@ static bwtsw2_t *bsw2_aln1_core(const bsw2opt_t *opt, const bntseq_t *bns, uint8 _b = bsw2_core(bns, opt, query, target, pool); bwtl_destroy(query); for (k = 0; k < 2; ++k) { - bb[k] = xcalloc(2, sizeof(void*)); - bb[k][0] = xcalloc(1, sizeof(bwtsw2_t)); - bb[k][1] = xcalloc(1, sizeof(bwtsw2_t)); + bb[k] = calloc(2, sizeof(void*)); + bb[k][0] = calloc(1, sizeof(bwtsw2_t)); + bb[k][1] = calloc(1, sizeof(bwtsw2_t)); } for (k = 0; k < 2; ++k) { // separate _b into bb[2] based on the strand for (j = 0; j < _b[k]->n; ++j) { @@ -261,7 +266,7 @@ static bwtsw2_t *bsw2_aln1_core(const bsw2opt_t *opt, const bntseq_t *bns, uint8 p = bb[_b[k]->hits[j].is_rev][k]; if (p->n == p->max) { p->max = p->max? p->max<<1 : 8; - p->hits = xrealloc(p->hits, p->max * sizeof(bsw2hit_t)); + p->hits = realloc(p->hits, p->max * sizeof(bsw2hit_t)); } q = &p->hits[p->n++]; *q = _b[k]->hits[j]; @@ -340,7 +345,7 @@ static int fix_cigar(const bntseq_t *bns, bsw2hit_t *p, int n_cigar, uint32_t *c uint32_t *cn; bwtint_t kk = 0; nc = mq[0] = mq[1] = nlen[0] = nlen[1] = 0; - cn = xcalloc(n_cigar + 3, 4); + cn = calloc(n_cigar + 3, 4); x = coor; y = 0; for (i = j = 0; i < n_cigar; ++i) { int op = cigar[i]&0xf, ln = cigar[i]>>4; @@ -398,9 +403,9 @@ static void write_aux(const bsw2opt_t *opt, const bntseq_t *bns, int qlen, uint8 if (b->n<<1 < b->max) { b->max = b->n; kroundup32(b->max); - b->hits = xrealloc(b->hits, b->max * sizeof(bsw2hit_t)); + b->hits = realloc(b->hits, b->max * sizeof(bsw2hit_t)); } - b->aux = xcalloc(b->n, sizeof(bsw2aux_t)); + b->aux = calloc(b->n, sizeof(bsw2aux_t)); // generate CIGAR gen_cigar(opt, qlen, seq, bns->l_pac, pac, b, name); // fix CIGAR, generate mapQ, and write chromosomal position @@ -559,7 +564,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t bsw2opt_t opt; bsw2global_t *pool = bsw2_global_init(); bwtsw2_t **buf; - buf = xcalloc(_seq->n, sizeof(void*)); + buf = calloc(_seq->n, sizeof(void*)); for (x = 0; x < _seq->n; ++x) { bsw2seq1_t *p = _seq->seq + x; uint8_t *seq[2], *rseq[2]; @@ -570,10 +575,10 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t if (pool->max_l < l) { // then enlarge working space for aln_extend_core() int tmp = ((l + 1) / 2 * opt.a + opt.r) / opt.r + l; pool->max_l = l; - pool->aln_mem = xrealloc(pool->aln_mem, (tmp + 2) * 24); + pool->aln_mem = realloc(pool->aln_mem, (tmp + 2) * 24); } // set seq[2] and rseq[2] - seq[0] = xcalloc(l * 4, 1); + seq[0] = calloc(l * 4, 1); seq[1] = seq[0] + l; rseq[0] = seq[1] + l; rseq[1] = rseq[0] + l; // convert sequences to 2-bit representation @@ -586,7 +591,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t rseq[1][i] = c; } if (l - k < opt.t) { // too few unambiguous bases - buf[x] = xcalloc(1, sizeof(bwtsw2_t)); + buf[x] = calloc(1, sizeof(bwtsw2_t)); free(seq[0]); continue; } // alignment @@ -618,7 +623,7 @@ static void bsw2_aln_core(bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t bsw2seq1_t *p = _seq->seq + x; uint8_t *seq[2]; int i; - seq[0] = xmalloc(p->l * 2); seq[1] = seq[0] + p->l; + seq[0] = malloc(p->l * 2); seq[1] = seq[0] + p->l; for (i = 0; i < p->l; ++i) { int c = nst_nt4_table[(int)p->seq[i]]; if (c >= 4) c = (int)(drand48() * 4); @@ -674,16 +679,16 @@ static void process_seqs(bsw2seq_t *_seq, const bsw2opt_t *opt, const bntseq_t * int j; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - data = (thread_aux_t*)xcalloc(opt->n_threads, sizeof(thread_aux_t)); - tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); + data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t)); + tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); for (j = 0; j < opt->n_threads; ++j) { thread_aux_t *p = data + j; p->tid = j; p->_opt = opt; p->bns = bns; p->is_pe = is_pe; p->pac = pac; p->target = target; - p->_seq = xcalloc(1, sizeof(bsw2seq_t)); + p->_seq = calloc(1, sizeof(bsw2seq_t)); p->_seq->max = (_seq->n + opt->n_threads - 1) / opt->n_threads + 1; p->_seq->n = 0; - p->_seq->seq = xcalloc(p->_seq->max, sizeof(bsw2seq1_t)); + p->_seq->seq = calloc(p->_seq->max, sizeof(bsw2seq1_t)); } for (i = 0; i < _seq->n; ++i) { // assign sequences to each thread bsw2seq_t *p = data[(i>>is_pe)%opt->n_threads]._seq; @@ -728,13 +733,13 @@ void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target, c bsw2seq_t *_seq; bseq1_t *bseq; - pac = xcalloc(bns->l_pac/4+1, 1); + pac = calloc(bns->l_pac/4+1, 1); for (l = 0; l < bns->n_seqs; ++l) err_printf("@SQ\tSN:%s\tLN:%d\n", bns->anns[l].name, bns->anns[l].len); err_fread_noeof(pac, 1, bns->l_pac/4+1, bns->fp_pac); fp = xzopen(fn, "r"); ks = kseq_init(fp); - _seq = xcalloc(1, sizeof(bsw2seq_t)); + _seq = calloc(1, sizeof(bsw2seq_t)); if (fn2) { fp2 = xzopen(fn2, "r"); ks2 = kseq_init(fp2); @@ -745,7 +750,7 @@ void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target, c if (n > _seq->max) { _seq->max = n; kroundup32(_seq->max); - _seq->seq = xrealloc(_seq->seq, _seq->max * sizeof(bsw2seq1_t)); + _seq->seq = realloc(_seq->seq, _seq->max * sizeof(bsw2seq1_t)); } _seq->n = n; for (i = 0; i < n; ++i) { diff --git a/bwtsw2_chain.c b/bwtsw2_chain.c index 6bd320f..ade77e7 100644 --- a/bwtsw2_chain.c +++ b/bwtsw2_chain.c @@ -1,6 +1,9 @@ #include #include "bwtsw2.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif typedef struct { uint32_t tbeg, tend; @@ -49,9 +52,9 @@ void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2]) char *flag; // initialization n[0] = b[0]->n; n[1] = b[1]->n; - z[0] = xcalloc(n[0] + n[1], sizeof(hsaip_t)); + z[0] = calloc(n[0] + n[1], sizeof(hsaip_t)); z[1] = z[0] + n[0]; - chain[0] = xcalloc(n[0] + n[1], sizeof(hsaip_t)); + chain[0] = calloc(n[0] + n[1], sizeof(hsaip_t)); for (k = j = 0; k < 2; ++k) { for (i = 0; i < b[k]->n; ++i) { bsw2hit_t *p = b[k]->hits + i; @@ -74,7 +77,7 @@ void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2]) } //for (k = 0; k < m[0]; ++k) printf("%d, [%d,%d), [%d,%d)\n", chain[0][k].chain, chain[0][k].tbeg, chain[0][k].tend, chain[0][k].qbeg, chain[0][k].qend); // filtering - flag = xcalloc(m[0] + m[1], 1); + flag = calloc(m[0] + m[1], 1); ks_introsort(hsaip, m[0] + m[1], chain[0]); for (k = 1; k < m[0] + m[1]; ++k) { hsaip_t *p = chain[0] + k; diff --git a/bwtsw2_core.c b/bwtsw2_core.c index d64f74b..1119601 100644 --- a/bwtsw2_core.c +++ b/bwtsw2_core.c @@ -7,7 +7,10 @@ #include "bwtsw2.h" #include "bwt.h" #include "kvec.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif typedef struct { bwtint_t k, l; @@ -72,7 +75,7 @@ typedef struct __mempool_t { inline static bsw2entry_p mp_alloc(mempool_t *mp) { ++mp->cnt; - if (kv_size(mp->pool) == 0) return (bsw2entry_t*)xcalloc(1, sizeof(bsw2entry_t)); + if (kv_size(mp->pool) == 0) return (bsw2entry_t*)calloc(1, sizeof(bsw2entry_t)); else return kv_pop(mp->pool); } inline static void mp_free(mempool_t *mp, bsw2entry_p e) @@ -134,7 +137,7 @@ static void cut_tail(bsw2entry_t *u, int T, bsw2entry_t *aux) if (u->n <= T) return; if (aux->max < u->n) { aux->max = u->n; - aux->array = (bsw2cell_t*)xrealloc(aux->array, aux->max * sizeof(bsw2cell_t)); + aux->array = (bsw2cell_t*)realloc(aux->array, aux->max * sizeof(bsw2cell_t)); } a = (int*)aux->array; for (i = n = 0; i != u->n; ++i) @@ -185,7 +188,7 @@ static void merge_entry(const bsw2opt_t * __restrict opt, bsw2entry_t *u, bsw2en int i; if (u->n + v->n >= u->max) { u->max = u->n + v->n; - u->array = (bsw2cell_t*)xrealloc(u->array, u->max * sizeof(bsw2cell_t)); + u->array = (bsw2cell_t*)realloc(u->array, u->max * sizeof(bsw2cell_t)); } for (i = 0; i != v->n; ++i) { bsw2cell_t *p = v->array + i; @@ -203,7 +206,7 @@ static inline bsw2cell_t *push_array_p(bsw2entry_t *e) { if (e->n == e->max) { e->max = e->max? e->max<<1 : 256; - e->array = (bsw2cell_t*)xrealloc(e->array, sizeof(bsw2cell_t) * e->max); + e->array = (bsw2cell_t*)realloc(e->array, sizeof(bsw2cell_t) * e->max); } return e->array + e->n; } @@ -251,7 +254,7 @@ static void save_narrow_hits(const bwtl_t *bwtl, bsw2entry_t *u, bwtsw2_t *b1, i if (p->G >= t && p->ql - p->qk + 1 <= IS) { // good narrow hit if (b1->max == b1->n) { b1->max = b1->max? b1->max<<1 : 4; - b1->hits = xrealloc(b1->hits, b1->max * sizeof(bsw2hit_t)); + b1->hits = realloc(b1->hits, b1->max * sizeof(bsw2hit_t)); } q = &b1->hits[b1->n++]; q->k = p->qk; q->l = p->ql; @@ -280,7 +283,7 @@ int bsw2_resolve_duphits(const bntseq_t *bns, const bwt_t *bwt, bwtsw2_t *b, int else if (p->G > 0) ++n; } b->n = b->max = n; - b->hits = xcalloc(b->max, sizeof(bsw2hit_t)); + b->hits = calloc(b->max, sizeof(bsw2hit_t)); for (i = j = 0; i < old_n; ++i) { bsw2hit_t *p = old_hits + i; if (p->l - p->k + 1 <= IS) { // the hit is no so repetitive @@ -400,9 +403,9 @@ bsw2global_t *bsw2_global_init() { bsw2global_t *pool; bsw2stack_t *stack; - pool = xcalloc(1, sizeof(bsw2global_t)); - stack = xcalloc(1, sizeof(bsw2stack_t)); - stack->pool = (mempool_t*)xcalloc(1, sizeof(mempool_t)); + pool = calloc(1, sizeof(bsw2global_t)); + stack = calloc(1, sizeof(bsw2stack_t)); + stack->pool = (mempool_t*)calloc(1, sizeof(mempool_t)); pool->stack = (void*)stack; return pool; } @@ -462,13 +465,13 @@ bwtsw2_t **bsw2_core(const bntseq_t *bns, const bsw2opt_t *opt, const bwtl_t *ta rhash = kh_init(qintv); init_bwtsw2(target, query, stack); heap_size = opt->z; - heap = xcalloc(heap_size, sizeof(int)); + heap = calloc(heap_size, sizeof(int)); // initialize the return struct - b = (bwtsw2_t*)xcalloc(1, sizeof(bwtsw2_t)); + b = (bwtsw2_t*)calloc(1, sizeof(bwtsw2_t)); b->n = b->max = target->seq_len * 2; - b->hits = xcalloc(b->max, sizeof(bsw2hit_t)); - b1 = (bwtsw2_t*)xcalloc(1, sizeof(bwtsw2_t)); - b_ret = xcalloc(2, sizeof(void*)); + b->hits = calloc(b->max, sizeof(bsw2hit_t)); + b1 = (bwtsw2_t*)calloc(1, sizeof(bwtsw2_t)); + b_ret = calloc(2, sizeof(void*)); b_ret[0] = b; b_ret[1] = b1; // initialize timer getrusage(0, &last); diff --git a/bwtsw2_pair.c b/bwtsw2_pair.c index df62e3f..24905df 100644 --- a/bwtsw2_pair.c +++ b/bwtsw2_pair.c @@ -7,9 +7,12 @@ #include "bntseq.h" #include "bwtsw2.h" #include "kstring.h" -#include "utils.h" #include "ksw.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + #define MIN_RATIO 0.8 #define OUTLIER_BOUND 2.0 #define MAX_STDDEV 4.0 @@ -27,7 +30,7 @@ bsw2pestat_t bsw2_stat(int n, bwtsw2_t **buf, kstring_t *msg, int max_ins) bsw2pestat_t r; memset(&r, 0, sizeof(bsw2pestat_t)); - isize = xcalloc(n, 8); + isize = calloc(n, 8); for (i = k = 0; i < n; i += 2) { bsw2hit_t *t[2]; int l; @@ -116,7 +119,7 @@ void bsw2_pair1(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, const b if (end > l_pac) end = l_pac; if (end - beg < l_mseq) return; // generate the sequence - seq = xmalloc(l_mseq + (end - beg)); + seq = malloc(l_mseq + (end - beg)); ref = seq + l_mseq; for (k = beg; k < end; ++k) ref[k - beg] = pac[k>>2] >> ((~k&3)<<1) & 0x3; @@ -195,7 +198,7 @@ void bsw2_pair(const bsw2opt_t *opt, int64_t l_pac, const uint8_t *pac, int n, b a[which].flag |= BSW2_FLAG_RESCUED; if (p[1]->max == 0) { p[1]->max = 1; - p[1]->hits = xmalloc(sizeof(bsw2hit_t)); + p[1]->hits = malloc(sizeof(bsw2hit_t)); } p[1]->hits[0] = a[which]; p[1]->n = 1; diff --git a/example.c b/example.c index aafe5d4..a6c9bdd 100644 --- a/example.c +++ b/example.c @@ -7,6 +7,10 @@ #include "kseq.h" // for the FASTA/Q parser KSEQ_DECLARE(gzFile) +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + int main(int argc, char *argv[]) { bwaidx_t *idx; diff --git a/is.c b/is.c index 8e94abd..1891668 100644 --- a/is.c +++ b/is.c @@ -27,6 +27,10 @@ #include #include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + typedef unsigned char ubyte_t; #define chr(i) (cs == sizeof(int) ? ((const int *)T)[i]:((const unsigned char *)T)[i]) @@ -205,7 +209,7 @@ int is_sa(const ubyte_t *T, int *SA, int n) int is_bwt(ubyte_t *T, int n) { int *SA, i, primary = 0; - SA = (int*)xcalloc(n+1, sizeof(int)); + SA = (int*)calloc(n+1, sizeof(int)); if (is_sa(T, SA, n)) err_fatal_simple("is_sa failed"); diff --git a/kbtree.h b/kbtree.h index bab4f0a..2b76953 100644 --- a/kbtree.h +++ b/kbtree.h @@ -31,7 +31,10 @@ #include #include #include -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif typedef struct { int32_t is_internal:1, n:31; @@ -52,7 +55,7 @@ typedef struct { kbtree_##name##_t *kb_init_##name(int size) \ { \ kbtree_##name##_t *b; \ - b = (kbtree_##name##_t*)xcalloc(1, sizeof(kbtree_##name##_t)); \ + b = (kbtree_##name##_t*)calloc(1, sizeof(kbtree_##name##_t)); \ b->t = ((size - 4 - sizeof(void*)) / (sizeof(void*) + sizeof(key_t)) + 1) >> 1; \ if (b->t < 2) { \ free(b); return 0; \ @@ -61,7 +64,7 @@ typedef struct { b->off_ptr = 4 + b->n * sizeof(key_t); \ b->ilen = (4 + sizeof(void*) + b->n * (sizeof(void*) + sizeof(key_t)) + 3) >> 2 << 2; \ b->elen = (b->off_ptr + 3) >> 2 << 2; \ - b->root = (kbnode_t*)xcalloc(1, b->ilen); \ + b->root = (kbnode_t*)calloc(1, b->ilen); \ ++b->n_nodes; \ return b; \ } @@ -70,7 +73,7 @@ typedef struct { int i, max = 8; \ kbnode_t *x, **top, **stack = 0; \ if (b) { \ - top = stack = (kbnode_t**)xcalloc(max, sizeof(kbnode_t*)); \ + top = stack = (kbnode_t**)calloc(max, sizeof(kbnode_t*)); \ *top++ = (b)->root; \ while (top != stack) { \ x = *--top; \ @@ -79,7 +82,7 @@ typedef struct { if (__KB_PTR(b, x)[i]) { \ if (top - stack == max) { \ max <<= 1; \ - stack = (kbnode_t**)xrealloc(stack, max * sizeof(kbnode_t*)); \ + stack = (kbnode_t**)realloc(stack, max * sizeof(kbnode_t*)); \ top = stack + (max>>1); \ } \ *top++ = __KB_PTR(b, x)[i]; \ @@ -173,7 +176,7 @@ typedef struct { static void __kb_split_##name(kbtree_##name##_t *b, kbnode_t *x, int i, kbnode_t *y) \ { \ kbnode_t *z; \ - z = (kbnode_t*)xcalloc(1, y->is_internal? b->ilen : b->elen); \ + z = (kbnode_t*)calloc(1, y->is_internal? b->ilen : b->elen); \ ++b->n_nodes; \ z->is_internal = y->is_internal; \ z->n = b->t - 1; \ @@ -211,7 +214,7 @@ typedef struct { r = b->root; \ if (r->n == 2 * b->t - 1) { \ ++b->n_nodes; \ - s = (kbnode_t*)xcalloc(1, b->ilen); \ + s = (kbnode_t*)calloc(1, b->ilen); \ b->root = s; s->is_internal = 1; s->n = 0; \ __KB_PTR(b, s)[0] = r; \ __kb_split_##name(b, s, 0, r); \ @@ -333,13 +336,13 @@ typedef struct { #define __kb_traverse(key_t, b, __func) do { \ int __kmax = 8; \ __kbstack_t *__kstack, *__kp; \ - __kp = __kstack = (__kbstack_t*)xcalloc(__kmax, sizeof(__kbstack_t)); \ + __kp = __kstack = (__kbstack_t*)calloc(__kmax, sizeof(__kbstack_t)); \ __kp->x = (b)->root; __kp->i = 0; \ for (;;) { \ while (__kp->x && __kp->i <= __kp->x->n) { \ if (__kp - __kstack == __kmax - 1) { \ __kmax <<= 1; \ - __kstack = (__kbstack_t*)xrealloc(__kstack, __kmax * sizeof(__kbstack_t)); \ + __kstack = (__kbstack_t*)realloc(__kstack, __kmax * sizeof(__kbstack_t)); \ __kp = __kstack + (__kmax>>1) - 1; \ } \ (__kp+1)->i = 0; (__kp+1)->x = __kp->x->is_internal? __KB_PTR(b, __kp->x)[__kp->i] : 0; \ diff --git a/khash.h b/khash.h index e206d35..12e5542 100644 --- a/khash.h +++ b/khash.h @@ -115,7 +115,10 @@ int main() { #include #include #include -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif /* compipler specific configuration */ @@ -161,13 +164,13 @@ typedef khint_t khiter_t; #endif #ifndef kcalloc -#define kcalloc(N,Z) xcalloc(N,Z) +#define kcalloc(N,Z) calloc(N,Z) #endif #ifndef kmalloc -#define kmalloc(Z) xmalloc(Z) +#define kmalloc(Z) malloc(Z) #endif #ifndef krealloc -#define krealloc(P,Z) xrealloc(P,Z) +#define krealloc(P,Z) realloc(P,Z) #endif #ifndef kfree #define kfree(P) free(P) diff --git a/kopen.c b/kopen.c index 82f2812..d238226 100644 --- a/kopen.c +++ b/kopen.c @@ -14,7 +14,9 @@ #include #endif -#include "utils.h" +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif #ifdef _WIN32 #define _KO_NO_NET @@ -82,7 +84,7 @@ static int http_open(const char *fn) // set ->http_host for (p = (char*)fn + 7; *p && *p != '/'; ++p); l = p - fn - 7; - http_host = xcalloc(l + 1, 1); + http_host = calloc(l + 1, 1); strncpy(http_host, fn + 7, l); http_host[l] = 0; for (q = http_host; *q && *q != ':'; ++q); @@ -91,21 +93,21 @@ static int http_open(const char *fn) proxy = getenv("http_proxy"); // set host, port and path if (proxy == 0) { - host = xstrdup(http_host); // when there is no proxy, server name is identical to http_host name. - port = xstrdup(*q? q : "80"); - path = xstrdup(*p? p : "/"); + host = strdup(http_host); // when there is no proxy, server name is identical to http_host name. + port = strdup(*q? q : "80"); + path = strdup(*p? p : "/"); } else { - host = (strstr(proxy, "http://") == proxy)? xstrdup(proxy + 7) : xstrdup(proxy); + host = (strstr(proxy, "http://") == proxy)? strdup(proxy + 7) : strdup(proxy); for (q = host; *q && *q != ':'; ++q); if (*q == ':') *q++ = 0; - port = xstrdup(*q? q : "80"); - path = xstrdup(fn); + port = strdup(*q? q : "80"); + path = strdup(fn); } /* connect; adapted from khttp_connect() in knetfile.c */ l = 0; fd = socket_connect(host, port); - buf = xcalloc(bufsz, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough. + buf = calloc(bufsz, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough. l += snprintf(buf + l, bufsz, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", path, http_host); if (write_bytes(fd, buf, l) != 0) { @@ -152,7 +154,7 @@ static int kftp_get_response(ftpaux_t *aux) while (read(aux->ctrl_fd, &c, 1)) { // FIXME: this is *VERY BAD* for unbuffered I/O if (n >= aux->max_response) { aux->max_response = aux->max_response? aux->max_response<<1 : 256; - aux->response = xrealloc(aux->response, aux->max_response); + aux->response = realloc(aux->response, aux->max_response); } aux->response[n++] = c; if (c == '\n') { @@ -186,10 +188,10 @@ static int ftp_open(const char *fn) for (p = (char*)fn + 6; *p && *p != '/'; ++p); if (*p != '/') return 0; l = p - fn - 6; - port = xstrdup("21"); - host = xcalloc(l + 1, 1); + port = strdup("21"); + host = calloc(l + 1, 1); strncpy(host, fn + 6, l); - retr = xcalloc(strlen(p) + 8, 1); + retr = calloc(strlen(p) + 8, 1); sprintf(retr, "RETR %s\r\n", p); /* connect to ctrl */ @@ -241,8 +243,8 @@ static char **cmd2argv(const char *cmd) for (i = beg + 1, argc = 0; i < end; ++i) if (isspace(cmd[i]) && !isspace(cmd[i-1])) ++argc; - argv = (char**)xcalloc(argc + 2, sizeof(void*)); - argv[0] = str = (char*)xcalloc(end - beg + 1, 1); + argv = (char**)calloc(argc + 2, sizeof(void*)); + argv[0] = str = (char*)calloc(end - beg + 1, 1); strncpy(argv[0], cmd + beg, end - beg); for (i = argc = 1; i < end - beg; ++i) if (isspace(str[i])) str[i] = 0; @@ -266,15 +268,15 @@ void *kopen(const char *fn, int *_fd) koaux_t *aux = 0; *_fd = -1; if (strstr(fn, "http://") == fn) { - aux = xcalloc(1, sizeof(koaux_t)); + aux = calloc(1, sizeof(koaux_t)); aux->type = KO_HTTP; aux->fd = http_open(fn); } else if (strstr(fn, "ftp://") == fn) { - aux = xcalloc(1, sizeof(koaux_t)); + aux = calloc(1, sizeof(koaux_t)); aux->type = KO_FTP; aux->fd = ftp_open(fn); } else if (strcmp(fn, "-") == 0) { - aux = xcalloc(1, sizeof(koaux_t)); + aux = calloc(1, sizeof(koaux_t)); aux->type = KO_STDIN; aux->fd = STDIN_FILENO; } else { @@ -308,7 +310,7 @@ void *kopen(const char *fn, int *_fd) exit(1); } else { /* parent process */ close(pfd[1]); - aux = xcalloc(1, sizeof(koaux_t)); + aux = calloc(1, sizeof(koaux_t)); aux->type = KO_PIPE; aux->fd = pfd[0]; aux->pid = pid; @@ -320,7 +322,7 @@ void *kopen(const char *fn, int *_fd) *_fd = open(fn, O_RDONLY); #endif if (*_fd >= 0) { - aux = xcalloc(1, sizeof(koaux_t)); + aux = calloc(1, sizeof(koaux_t)); aux->type = KO_FILE; aux->fd = *_fd; } diff --git a/kseq.h b/kseq.h index 55405a8..642cd33 100644 --- a/kseq.h +++ b/kseq.h @@ -31,7 +31,10 @@ #include #include #include -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif #define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r #define KS_SEP_TAB 1 // isspace() && !' ' @@ -51,9 +54,9 @@ #define __KS_BASIC(type_t, __bufsize) \ static inline kstream_t *ks_init(type_t f) \ { \ - kstream_t *ks = (kstream_t*)xcalloc(1, sizeof(kstream_t)); \ + kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \ ks->f = f; \ - ks->buf = (unsigned char*)xmalloc(__bufsize); \ + ks->buf = (unsigned char*)malloc(__bufsize); \ return ks; \ } \ static inline void ks_destroy(kstream_t *ks) \ @@ -121,7 +124,7 @@ typedef struct __kstring_t { if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \ str->m = str->l + (i - ks->begin) + 1; \ kroundup32(str->m); \ - str->s = (char*)xrealloc(str->s, str->m); \ + str->s = (char*)realloc(str->s, str->m); \ } \ memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \ str->l = str->l + (i - ks->begin); \ @@ -133,7 +136,7 @@ typedef struct __kstring_t { } \ if (str->s == 0) { \ str->m = 1; \ - str->s = (char*)xcalloc(1, 1); \ + str->s = (char*)calloc(1, 1); \ } else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \ str->s[str->l] = '\0'; \ return str->l; \ @@ -152,7 +155,7 @@ typedef struct __kstring_t { #define __KSEQ_BASIC(SCOPE, type_t) \ SCOPE kseq_t *kseq_init(type_t fd) \ { \ - kseq_t *s = (kseq_t*)xcalloc(1, sizeof(kseq_t)); \ + kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \ s->f = ks_init(fd); \ return s; \ } \ @@ -184,7 +187,7 @@ typedef struct __kstring_t { if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \ if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \ seq->seq.m = 256; \ - seq->seq.s = (char*)xmalloc(seq->seq.m); \ + seq->seq.s = (char*)malloc(seq->seq.m); \ } \ while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \ if (c == '\n') continue; /* skip empty lines */ \ @@ -195,13 +198,13 @@ typedef struct __kstring_t { if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \ seq->seq.m = seq->seq.l + 2; \ kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \ - seq->seq.s = (char*)xrealloc(seq->seq.s, seq->seq.m); \ + seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \ } \ seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \ if (c != '+') return seq->seq.l; /* FASTA */ \ if (seq->qual.m < seq->seq.m) { /* allocate memory for qual in case insufficient */ \ seq->qual.m = seq->seq.m; \ - seq->qual.s = (char*)xrealloc(seq->qual.s, seq->qual.m); \ + seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \ } \ while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \ if (c == -1) return -2; /* error: no quality string */ \ diff --git a/ksort.h b/ksort.h index 9f334e2..5851b0d 100644 --- a/ksort.h +++ b/ksort.h @@ -57,7 +57,10 @@ #include #include -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif typedef struct { void *left, *right; @@ -73,7 +76,7 @@ typedef struct { int curr, shift; \ \ a2[0] = array; \ - a2[1] = temp? temp : (type_t*)xmalloc(sizeof(type_t) * n); \ + a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n); \ for (curr = 0, shift = 0; (1ul< #include #include "kstring.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif int ksprintf(kstring_t *s, const char *fmt, ...) { @@ -13,7 +16,7 @@ int ksprintf(kstring_t *s, const char *fmt, ...) if (l + 1 > s->m - s->l) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); va_start(ap, fmt); l = vsnprintf(s->s + s->l, s->m - s->l, fmt, ap); } @@ -27,7 +30,7 @@ int ksprintf(kstring_t *s, const char *fmt, ...) int main() { kstring_t *s; - s = (kstring_t*)xcalloc(1, sizeof(kstring_t)); + s = (kstring_t*)calloc(1, sizeof(kstring_t)); ksprintf(s, "abcdefg: %d", 100); printf("%s\n", s->s); free(s); diff --git a/kstring.h b/kstring.h index 04194ec..fe7fa95 100644 --- a/kstring.h +++ b/kstring.h @@ -3,7 +3,10 @@ #include #include -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif #ifndef kroundup32 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) @@ -22,7 +25,7 @@ static inline void ks_resize(kstring_t *s, size_t size) if (s->m < size) { s->m = size; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } } @@ -31,7 +34,7 @@ static inline int kputsn(const char *p, int l, kstring_t *s) if (s->l + l + 1 >= s->m) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } memcpy(s->s + s->l, p, l); s->l += l; @@ -49,7 +52,7 @@ static inline int kputc(int c, kstring_t *s) if (s->l + 1 >= s->m) { s->m = s->l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } s->s[s->l++] = c; s->s[s->l] = 0; @@ -66,7 +69,7 @@ static inline int kputw(int c, kstring_t *s) if (s->l + l + 1 >= s->m) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; s->s[s->l] = 0; @@ -83,7 +86,7 @@ static inline int kputuw(unsigned c, kstring_t *s) if (s->l + l + 1 >= s->m) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; s->s[s->l] = 0; @@ -100,7 +103,7 @@ static inline int kputl(long c, kstring_t *s) if (s->l + l + 1 >= s->m) { s->m = s->l + l + 2; kroundup32(s->m); - s->s = (char*)xrealloc(s->s, s->m); + s->s = (char*)realloc(s->s, s->m); } for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; s->s[s->l] = 0; diff --git a/ksw.c b/ksw.c index c1ff5a6..454c49d 100644 --- a/ksw.c +++ b/ksw.c @@ -27,7 +27,10 @@ #include #include #include "ksw.h" -#include "utils.h" + +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif #ifdef __GNUC__ #define LIKELY(x) __builtin_expect((x),1) @@ -64,7 +67,7 @@ kswq_t *ksw_qinit(int size, int qlen, const uint8_t *query, int m, const int8_t size = size > 1? 2 : 1; p = 8 * (3 - size); // # values per __m128i slen = (qlen + p - 1) / p; // segmented length - q = (kswq_t*)xmalloc(sizeof(kswq_t) + 256 + 16 * slen * (m + 4)); // a single block of memory + q = (kswq_t*)malloc(sizeof(kswq_t) + 256 + 16 * slen * (m + 4)); // a single block of memory q->qp = (__m128i*)(((size_t)q + sizeof(kswq_t) + 15) >> 4 << 4); // align memory q->H0 = q->qp + slen * m; q->H1 = q->H0 + slen; @@ -186,7 +189,7 @@ end_loop16: if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { // then append if (n_b == m_b) { m_b = m_b? m_b<<1 : 8; - b = (uint64_t*)xrealloc(b, 8 * m_b); + b = (uint64_t*)realloc(b, 8 * m_b); } b[n_b++] = (uint64_t)imax<<32 | i; } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last @@ -289,7 +292,7 @@ end_loop8: if (n_b == 0 || (int32_t)b[n_b-1] + 1 != i) { if (n_b == m_b) { m_b = m_b? m_b<<1 : 8; - b = (uint64_t*)xrealloc(b, 8 * m_b); + b = (uint64_t*)realloc(b, 8 * m_b); } b[n_b++] = (uint64_t)imax<<32 | i; } else if ((int)(b[n_b-1]>>32) < imax) b[n_b-1] = (uint64_t)imax<<32 | i; // modify the last @@ -369,8 +372,8 @@ int ksw_extend(int qlen, const uint8_t *query, int tlen, const uint8_t *target, int i, j, k, gapoe = gapo + gape, beg, end, max, max_i, max_j, max_gap, max_ie, gscore, max_off; if (h0 < 0) h0 = 0; // allocate memory - qp = xmalloc(qlen * m); - eh = xcalloc(qlen + 1, 8); + qp = malloc(qlen * m); + eh = calloc(qlen + 1, 8); // generate the query profile for (k = i = 0; k < m; ++k) { const int8_t *p = &mat[k * m]; @@ -461,7 +464,7 @@ static inline uint32_t *push_cigar(int *n_cigar, int *m_cigar, uint32_t *cigar, if (*n_cigar == 0 || op != (cigar[(*n_cigar) - 1]&0xf)) { if (*n_cigar == *m_cigar) { *m_cigar = *m_cigar? (*m_cigar)<<1 : 4; - cigar = xrealloc(cigar, (*m_cigar) << 2); + cigar = realloc(cigar, (*m_cigar) << 2); } cigar[(*n_cigar)++] = len<<4 | op; } else cigar[(*n_cigar)-1] += len<<4; @@ -477,9 +480,9 @@ int ksw_global(int qlen, const uint8_t *query, int tlen, const uint8_t *target, if (n_cigar_) *n_cigar_ = 0; // allocate memory n_col = qlen < 2*w+1? qlen : 2*w+1; // maximum #columns of the backtrack matrix - z = xmalloc(n_col * tlen); - qp = xmalloc(qlen * m); - eh = xcalloc(qlen + 1, 8); + z = malloc(n_col * tlen); + qp = malloc(qlen * m); + eh = calloc(qlen + 1, 8); // generate the query profile for (k = i = 0; k < m; ++k) { const int8_t *p = &mat[k * m]; @@ -620,7 +623,7 @@ int main(int argc, char *argv[]) if (!forward_only) { // reverse if ((int)ksq->seq.m > max_rseq) { max_rseq = ksq->seq.m; - rseq = (uint8_t*)xrealloc(rseq, max_rseq); + rseq = (uint8_t*)realloc(rseq, max_rseq); } for (i = 0, j = ksq->seq.l - 1; i < (int)ksq->seq.l; ++i, --j) rseq[j] = ksq->seq.s[i] == 4? 4 : 3 - ksq->seq.s[i]; diff --git a/ksw.h b/ksw.h index 97559fd..f9d22c6 100644 --- a/ksw.h +++ b/ksw.h @@ -3,6 +3,10 @@ #include +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + #define KSW_XBYTE 0x10000 #define KSW_XSTOP 0x20000 #define KSW_XSUBO 0x40000 diff --git a/kvec.h b/kvec.h index 7aaf4a5..83ad483 100644 --- a/kvec.h +++ b/kvec.h @@ -50,6 +50,10 @@ int main() { #include +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) #define kvec_t(type) struct { size_t n, m; type *a; } @@ -60,7 +64,7 @@ int main() { #define kv_size(v) ((v).n) #define kv_max(v) ((v).m) -#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m)) +#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m)) #define kv_copy(type, v1, v0) do { \ if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \ @@ -71,19 +75,19 @@ int main() { #define kv_push(type, v, x) do { \ if ((v).n == (v).m) { \ (v).m = (v).m? (v).m<<1 : 2; \ - (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m); \ + (v).a = (type*)realloc((v).a, sizeof(type) * (v).m); \ } \ (v).a[(v).n++] = (x); \ } while (0) #define kv_pushp(type, v) ((((v).n == (v).m)? \ ((v).m = ((v).m? (v).m<<1 : 2), \ - (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m), 0) \ + (v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ : 0), &(v).a[(v).n++]) #define kv_a(type, v, i) (((v).m <= (size_t)(i)? \ ((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \ - (v).a = (type*)xrealloc((v).a, sizeof(type) * (v).m), 0) \ + (v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ : (v).n <= (size_t)(i)? (v).n = (i) + 1 \ : 0), (v).a[(i)]) diff --git a/malloc_wrap.c b/malloc_wrap.c new file mode 100644 index 0000000..100b8cb --- /dev/null +++ b/malloc_wrap.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#ifdef USE_MALLOC_WRAPPERS +/* Don't wrap ourselves */ +# undef USE_MALLOC_WRAPPERS +#endif +#include "malloc_wrap.h" + +void *wrap_calloc(size_t nmemb, size_t size, + const char *file, unsigned int line, const char *func) { + void *p = calloc(nmemb, size); + if (NULL == p) { + fprintf(stderr, + "[%s] Failed to allocate %zd bytes at %s line %u: %s\n", + func, nmemb * size, file, line, strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + +void *wrap_malloc(size_t size, + const char *file, unsigned int line, const char *func) { + void *p = malloc(size); + if (NULL == p) { + fprintf(stderr, + "[%s] Failed to allocate %zd bytes at %s line %u: %s\n", + func, size, file, line, strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + +void *wrap_realloc(void *ptr, size_t size, + const char *file, unsigned int line, const char *func) { + void *p = realloc(ptr, size); + if (NULL == p) { + fprintf(stderr, + "[%s] Failed to allocate %zd bytes at %s line %u: %s\n", + func, size, file, line, strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + +char *wrap_strdup(const char *s, + const char *file, unsigned int line, const char *func) { + char *p = strdup(s); + if (NULL == p) { + fprintf(stderr, + "[%s] Failed to allocate %zd bytes at %s line %u: %s\n", + func, strlen(s), file, line, strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} diff --git a/malloc_wrap.h b/malloc_wrap.h new file mode 100644 index 0000000..a55876a --- /dev/null +++ b/malloc_wrap.h @@ -0,0 +1,47 @@ +#ifndef MALLOC_WRAP_H +#define MALLOC_WRAP_H + +#include /* Avoid breaking the usual definitions */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + + void *wrap_calloc(size_t nmemb, size_t size, + const char *file, unsigned int line, const char *func); + void *wrap_malloc(size_t size, + const char *file, unsigned int line, const char *func); + void *wrap_realloc(void *ptr, size_t size, + const char *file, unsigned int line, const char *func); + char *wrap_strdup(const char *s, + const char *file, unsigned int line, const char *func); + +#ifdef __cplusplus +} +#endif + +#ifdef USE_MALLOC_WRAPPERS +# ifdef calloc +# undef calloc +# endif +# define calloc(n, s) wrap_calloc( (n), (s), __FILE__, __LINE__, __func__) + +# ifdef malloc +# undef malloc +# endif +# define malloc(s) wrap_malloc( (s), __FILE__, __LINE__, __func__) + +# ifdef realloc +# undef realloc +# endif +# define realloc(p, s) wrap_realloc((p), (s), __FILE__, __LINE__, __func__) + +# ifdef strdup +# undef strdup +# endif +# define strdup(s) wrap_strdup( (s), __FILE__, __LINE__, __func__) + +#endif /* USE_MALLOC_WRAPPERS */ + +#endif /* MALLOC_WRAP_H */ diff --git a/pemerge.c b/pemerge.c index b944819..725885f 100644 --- a/pemerge.c +++ b/pemerge.c @@ -12,6 +12,10 @@ #include "utils.h" KSEQ_DECLARE(gzFile) +#ifdef USE_MALLOC_WRAPPERS +# include "malloc_wrap.h" +#endif + #define MAX_SCORE_RATIO 0.9f #define MAX_ERR 8 @@ -40,7 +44,7 @@ typedef struct { pem_opt_t *pem_opt_init() { pem_opt_t *opt; - opt = xcalloc(1, sizeof(pem_opt_t)); + opt = calloc(1, sizeof(pem_opt_t)); opt->a = 5; opt->b = 4; opt->q = 2, opt->r = 17; opt->w = 20; opt->T = opt->a * 10; opt->q_def = 20; @@ -58,8 +62,8 @@ int bwa_pemerge(const pem_opt_t *opt, bseq1_t x[2]) int i, xtra, l, l_seq, sum_q, ret = 0; kswr_t r; - s[0] = xmalloc(x[0].l_seq); q[0] = xmalloc(x[0].l_seq); - s[1] = xmalloc(x[1].l_seq); q[1] = xmalloc(x[1].l_seq); + s[0] = malloc(x[0].l_seq); q[0] = malloc(x[0].l_seq); + s[1] = malloc(x[1].l_seq); q[1] = malloc(x[1].l_seq); for (i = 0; i < x[0].l_seq; ++i) { int c = x[0].seq[i]; s[0][i] = c < 0 || c > 127? 4 : c <= 4? c : nst_nt4_table[c]; @@ -103,8 +107,8 @@ int bwa_pemerge(const pem_opt_t *opt, bseq1_t x[2]) l = x[0].l_seq - (r.tb - r.qb); // length to merge l_seq = x[0].l_seq + x[1].l_seq - l; - seq = xmalloc(l_seq + 1); - qual = xmalloc(l_seq + 1); + seq = malloc(l_seq + 1); + qual = malloc(l_seq + 1); memcpy(seq, s[0], x[0].l_seq); memcpy(seq + x[0].l_seq, &s[1][l], x[1].l_seq - l); memcpy(qual, q[0], x[0].l_seq); memcpy(qual + x[0].l_seq, &q[1][l], x[1].l_seq - l); for (i = 0, sum_q = 0; i < l; ++i) { @@ -174,7 +178,7 @@ static void process_seqs(const pem_opt_t *opt, int n_, bseq1_t *seqs, int64_t cn int i, j, n = n_>>1<<1; worker_t *w; - w = xcalloc(opt->n_threads, sizeof(worker_t)); + w = calloc(opt->n_threads, sizeof(worker_t)); for (i = 0; i < opt->n_threads; ++i) { worker_t *p = &w[i]; p->start = i; p->n = n; @@ -185,7 +189,7 @@ static void process_seqs(const pem_opt_t *opt, int n_, bseq1_t *seqs, int64_t cn worker(w); } else { pthread_t *tid; - tid = (pthread_t*)xcalloc(opt->n_threads, sizeof(pthread_t)); + tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t)); for (i = 0; i < opt->n_threads; ++i) pthread_create(&tid[i], 0, worker, &w[i]); for (i = 0; i < opt->n_threads; ++i) pthread_join(tid[i], 0); free(tid); diff --git a/utils.c b/utils.c index ad2f734..00be7f0 100644 --- a/utils.c +++ b/utils.c @@ -264,47 +264,6 @@ int err_gzclose(gzFile file) return ret; } -void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func) -{ - void *p = calloc(nmemb, size); - if (NULL == p) - { - err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", nmemb * size, file, line, strerror(errno)); - } - return p; -} - -void *err_malloc(size_t size, const char *file, unsigned int line, const char *func) -{ - void *p = malloc(size); - if (NULL == p) - { - err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", size, file, line, strerror(errno)); - } - return p; -} - -void *err_realloc(void *ptr, size_t size, const char *file, unsigned int line, const char *func) -{ - void *p = realloc(ptr, size); - if (NULL == p) - { - err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", size, file, line, strerror(errno)); - } - return p; -} - -char *err_strdup(const char *s, const char *file, unsigned int line, const char *func) -{ - char *p = strdup(s); - - if (NULL == p) - { - err_fatal(func, "Failed to allocate %zd bytes at %s line %u: %s\n", strlen(s), file, line, strerror(errno)); - } - return p; -} - /********* * Timer * *********/ diff --git a/utils.h b/utils.h index 8567d3f..5ef6ac4 100644 --- a/utils.h +++ b/utils.h @@ -48,12 +48,6 @@ #define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg) -#define xcalloc(n, s) err_calloc( (n), (s), __FILE__, __LINE__, __func__) -#define xmalloc(s) err_malloc( (s), __FILE__, __LINE__, __func__) -#define xrealloc(p, s) err_realloc((p), (s), __FILE__, __LINE__, __func__) -#define xstrdup(s) err_strdup( (s), __FILE__, __LINE__, __func__) - - typedef struct { uint64_t x, y; } pair64_t; @@ -91,11 +85,6 @@ extern "C" { int err_fclose(FILE *stream); int err_gzclose(gzFile file); - void *err_calloc(size_t nmemb, size_t size, const char *file, unsigned int line, const char *func); - void *err_malloc(size_t size, const char *file, unsigned int line, const char *func); - void *err_realloc(void *ptr, size_t size, const char *file, unsigned int line, const char *func); - char *err_strdup(const char *s, const char *file, unsigned int line, const char *func); - double cputime(); double realtime(); From 5c43a1fdc9dbdf141200c1b2353d5c3502426433 Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Fri, 3 May 2013 11:38:48 +0100 Subject: [PATCH 8/8] Removed more dependencies on utils.h bamlite.c now includes some wrappers for gzopen/gzread/gzclose that print messages when errors occur. They do not attempt to quit the program but pass on the return code. bwaseqio.c now checks the return codes from bam_open, bam_close and bam_read1. Code in bwt_gen.c now checks for IO errors itself instead of using the wrappers. A benefit of this is it can now say which file had a problem. Removed call to err_fatal_simple in is_bwt and unnecessary inclusion of malloc_wrap.h in ksw.h. --- Makefile | 10 +++--- bamlite.c | 46 ++++++++++++++++++++++++++++ bamlite.h | 25 ++++++++++++--- bwaseqio.c | 10 ++++-- bwt_gen.c | 90 +++++++++++++++++++++++++++++++++++++++++++----------- is.c | 3 +- ksw.h | 4 --- 7 files changed, 152 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 7d78889..d39a787 100644 --- a/Makefile +++ b/Makefile @@ -38,9 +38,9 @@ depend: # DO NOT DELETE THIS LINE -- make depend depends on it. QSufSort.o: QSufSort.h -bamlite.o: bamlite.h utils.h malloc_wrap.h +bamlite.o: bamlite.h malloc_wrap.h bntseq.o: bntseq.h utils.h kseq.h malloc_wrap.h -bwa.o: bntseq.h bwa.h bwt.h ksw.h malloc_wrap.h utils.h kseq.h +bwa.o: bntseq.h bwa.h bwt.h ksw.h utils.h malloc_wrap.h kseq.h bwamem.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h ksw.h kvec.h bwamem.o: ksort.h utils.h kbtree.h bwamem_pair.o: kstring.h malloc_wrap.h bwamem.h bwt.h bntseq.h bwa.h kvec.h @@ -51,7 +51,7 @@ bwase.o: bwase.h bntseq.h bwt.h bwtaln.h utils.h kstring.h malloc_wrap.h bwase.o: bwa.h ksw.h bwaseqio.o: bwtaln.h bwt.h utils.h bamlite.h malloc_wrap.h kseq.h bwt.o: utils.h bwt.h kvec.h malloc_wrap.h -bwt_gen.o: QSufSort.h utils.h malloc_wrap.h +bwt_gen.o: QSufSort.h malloc_wrap.h bwt_lite.o: bwt_lite.h malloc_wrap.h bwtaln.o: bwtaln.h bwt.h bwtgap.h utils.h bwa.h bntseq.h malloc_wrap.h bwtgap.o: bwtgap.h bwt.h bwtaln.h malloc_wrap.h @@ -66,11 +66,11 @@ bwtsw2_pair.o: utils.h bwt.h bntseq.h bwtsw2.h bwt_lite.h kstring.h bwtsw2_pair.o: malloc_wrap.h ksw.h example.o: bwamem.h bwt.h bntseq.h bwa.h kseq.h malloc_wrap.h fastmap.o: bwa.h bntseq.h bwt.h bwamem.h kvec.h malloc_wrap.h utils.h kseq.h -is.o: utils.h malloc_wrap.h +is.o: malloc_wrap.h kopen.o: malloc_wrap.h kstring.o: kstring.h malloc_wrap.h ksw.o: ksw.h malloc_wrap.h main.o: utils.h malloc_wrap.o: malloc_wrap.h -pemerge.o: ksw.h malloc_wrap.h kseq.h kstring.h bwa.h bntseq.h bwt.h utils.h +pemerge.o: ksw.h kseq.h malloc_wrap.h kstring.h bwa.h bntseq.h bwt.h utils.h utils.o: utils.h ksort.h malloc_wrap.h kseq.h diff --git a/bamlite.c b/bamlite.c index 851cb6f..3704beb 100644 --- a/bamlite.c +++ b/bamlite.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "bamlite.h" #ifdef USE_MALLOC_WRAPPERS @@ -162,3 +163,48 @@ int bam_read1(bamFile fp, bam1_t *b) if (bam_is_be) swap_endian_data(c, b->data_len, b->data); return 4 + block_len; } + + +#ifdef USE_VERBOSE_ZLIB_WRAPPERS +// Versions of gzopen, gzread and gzclose that print up error messages + +gzFile bamlite_gzopen(const char *fn, const char *mode) { + gzFile fp; + if (strcmp(fn, "-") == 0) { + fp = gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode); + if (!fp) { + fprintf(stderr, "Couldn't open %s : %s", + (strstr(mode, "r"))? "stdin" : "stdout", + strerror(errno)); + } + return fp; + } + if ((fp = gzopen(fn, mode)) == 0) { + fprintf(stderr, "Couldn't open %s : %s\n", fn, + errno ? strerror(errno) : "Out of memory"); + } + return fp; +} + +int bamlite_gzread(gzFile file, void *ptr, unsigned int len) { + int ret = gzread(file, ptr, len); + + if (ret < 0) { + int errnum = 0; + const char *msg = gzerror(file, &errnum); + fprintf(stderr, "gzread error: %s\n", + Z_ERRNO == errnum ? strerror(errno) : msg); + } + return ret; +} + +int bamlite_gzclose(gzFile file) { + int ret = gzclose(file); + if (Z_OK != ret) { + fprintf(stderr, "gzclose error: %s\n", + Z_ERRNO == ret ? strerror(errno) : zError(ret)); + } + + return ret; +} +#endif /* USE_VERBOSE_ZLIB_WRAPPERS */ diff --git a/bamlite.h b/bamlite.h index 640e863..efab7ac 100644 --- a/bamlite.h +++ b/bamlite.h @@ -3,17 +3,26 @@ #include #include -#include "utils.h" #ifdef USE_MALLOC_WRAPPERS # include "malloc_wrap.h" #endif +#define USE_VERBOSE_ZLIB_WRAPPERS + typedef gzFile bamFile; -#define bam_open(fn, mode) xzopen(fn, mode) -#define bam_dopen(fd, mode) gzdopen(fd, mode) -#define bam_close(fp) err_gzclose(fp) -#define bam_read(fp, buf, size) err_gzread(fp, buf, size) +#ifdef USE_VERBOSE_ZLIB_WRAPPERS +/* These print error messages on failure */ +# define bam_open(fn, mode) bamlite_gzopen(fn, mode) +# define bam_dopen(fd, mode) gzdopen(fd, mode) +# define bam_close(fp) bamlite_gzclose(fp) +# define bam_read(fp, buf, size) bamlite_gzread(fp, buf, size) +#else +# define bam_open(fn, mode) gzopen(fn, mode) +# define bam_dopen(fd, mode) gzdopen(fd, mode) +# define bam_close(fp) gzclose(fp) +# define bam_read(fp, buf, size) gzread(fp, buf, size) +#endif /* USE_VERBOSE_ZLIB_WRAPPERS */ typedef struct { int32_t n_targets; @@ -92,6 +101,12 @@ extern "C" { bam_header_t *bam_header_read(bamFile fp); int bam_read1(bamFile fp, bam1_t *b); +#ifdef USE_VERBOSE_ZLIB_WRAPPERS + gzFile bamlite_gzopen(const char *fn, const char *mode); + int bamlite_gzread(gzFile file, void *ptr, unsigned int len); + int bamlite_gzclose(gzFile file); +#endif /* USE_VERBOSE_ZLIB_WRAPPERS */ + #ifdef __cplusplus } #endif diff --git a/bwaseqio.c b/bwaseqio.c index d157945..d850307 100644 --- a/bwaseqio.c +++ b/bwaseqio.c @@ -30,6 +30,7 @@ bwa_seqio_t *bwa_bam_open(const char *fn, int which) bs->is_bam = 1; bs->which = which; bs->fp = bam_open(fn, "r"); + if (0 == bs->fp) err_fatal_simple("Couldn't open bam file"); h = bam_header_read(bs->fp); bam_header_destroy(h); return bs; @@ -48,8 +49,9 @@ bwa_seqio_t *bwa_seq_open(const char *fn) void bwa_seq_close(bwa_seqio_t *bs) { if (bs == 0) return; - if (bs->is_bam) bam_close(bs->fp); - else { + if (bs->is_bam) { + if (0 != bam_close(bs->fp)) err_fatal_simple("Error closing bam file"); + } else { err_gzclose(bs->ks->f->f); kseq_destroy(bs->ks); } @@ -94,11 +96,12 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com int n_seqs, l, i; long n_trimmed = 0, n_tot = 0; bam1_t *b; + int res; b = bam_init1(); n_seqs = 0; seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t)); - while (bam_read1(bs->fp, b) >= 0) { + while ((res = bam_read1(bs->fp, b)) >= 0) { uint8_t *s, *q; int go = 0; if ((bs->which & 1) && (b->core.flag & BAM_FREAD1)) go = 1; @@ -130,6 +133,7 @@ static bwa_seq_t *bwa_read_bam(bwa_seqio_t *bs, int n_needed, int *n, int is_com p->name = strdup((const char*)bam1_qname(b)); if (n_seqs == n_needed) break; } + if (res < 0 && res != -1) err_fatal_simple("Error reading bam file"); *n = n_seqs; if (n_seqs && trim_qual >= 1) fprintf(stderr, "[bwa_read_seq] %.1f%% bases are trimmed.\n", 100.0f * n_trimmed/n_tot); diff --git a/bwt_gen.c b/bwt_gen.c index d68b30f..6139d80 100644 --- a/bwt_gen.c +++ b/bwt_gen.c @@ -27,8 +27,8 @@ #include #include #include +#include #include "QSufSort.h" -#include "utils.h" #ifdef USE_MALLOC_WRAPPERS # include "malloc_wrap.h" @@ -1448,13 +1448,29 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB packedFile = (FILE*)fopen(inputFileName, "rb"); if (packedFile == NULL) { - fprintf(stderr, "BWTIncConstructFromPacked() : Cannot open inputFileName!\n"); + fprintf(stderr, "BWTIncConstructFromPacked() : Cannot open %s : %s\n", + inputFileName, strerror(errno)); exit(1); } - err_fseek(packedFile, -1, SEEK_END); - packedFileLen = err_ftell(packedFile); - err_fread_noeof(&lastByteLength, sizeof(unsigned char), 1, packedFile); + if (fseek(packedFile, -1, SEEK_END) != 0) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } + packedFileLen = ftell(packedFile); + if (packedFileLen == -1) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't ftell on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } + if (fread(&lastByteLength, sizeof(unsigned char), 1, packedFile) != 1) { + fprintf(stderr, + "BWTIncConstructFromPacked() : Can't read from %s : %s\n", + inputFileName, + ferror(packedFile)? strerror(errno) : "Unexpected end of file"); + exit(1); + } totalTextLength = TextLengthFromBytePacked(packedFileLen, BIT_PER_CHAR, lastByteLength); bwtInc = BWTIncCreate(totalTextLength, initialMaxBuildSize, incMaxBuildSize); @@ -1468,10 +1484,23 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB } textSizeInByte = textToLoad / CHAR_PER_BYTE; // excluded the odd byte - err_fseek(packedFile, -2, SEEK_CUR); - err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); - err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile); - err_fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR); + if (fseek(packedFile, -((long)textSizeInByte + 2), SEEK_CUR) != 0) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } + if (fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte + 1, packedFile) != textSizeInByte + 1) { + fprintf(stderr, + "BWTIncConstructFromPacked() : Can't read from %s : %s\n", + inputFileName, + ferror(packedFile)? strerror(errno) : "Unexpected end of file"); + exit(1); + } + if (fseek(packedFile, -((long)textSizeInByte + 1), SEEK_CUR) != 0) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad); BWTIncConstruct(bwtInc, textToLoad); @@ -1484,9 +1513,23 @@ BWTInc *BWTIncConstructFromPacked(const char *inputFileName, bgint_t initialMaxB textToLoad = totalTextLength - processedTextLength; } textSizeInByte = textToLoad / CHAR_PER_BYTE; - err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); - err_fread_noeof(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile); - err_fseek(packedFile, -((long)textSizeInByte), SEEK_CUR); + if (fseek(packedFile, -((long)textSizeInByte), SEEK_CUR) != 0) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } + if (fread(bwtInc->textBuffer, sizeof(unsigned char), textSizeInByte, packedFile) != textSizeInByte) { + fprintf(stderr, + "BWTIncConstructFromPacked() : Can't read from %s : %s\n", + inputFileName, + ferror(packedFile)? strerror(errno) : "Unexpected end of file"); + exit(1); + } + if (fseek(packedFile, -((long)textSizeInByte), SEEK_CUR) != 0) { + fprintf(stderr, "BWTIncConstructFromPacked() : Can't seek on %s : %s\n", + inputFileName, strerror(errno)); + exit(1); + } ConvertBytePackedToWordPacked(bwtInc->textBuffer, bwtInc->packedText, ALPHABET_SIZE, textToLoad); BWTIncConstruct(bwtInc, textToLoad); processedTextLength += textToLoad; @@ -1531,15 +1574,28 @@ void BWTSaveBwtCodeAndOcc(const BWT *bwt, const char *bwtFileName, const char *o bwtFile = (FILE*)fopen(bwtFileName, "wb"); if (bwtFile == NULL) { - fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Cannot open BWT code file!\n"); + fprintf(stderr, + "BWTSaveBwtCodeAndOcc(): Cannot open %s for writing: %s\n", + bwtFileName, strerror(errno)); exit(1); } - err_fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile); - err_fwrite(bwt->cumulativeFreq + 1, sizeof(bgint_t), ALPHABET_SIZE, bwtFile); bwtLength = BWTFileSizeInWord(bwt->textLength); - err_fwrite(bwt->bwtCode, sizeof(unsigned int), bwtLength, bwtFile); - err_fclose(bwtFile); + + if (fwrite(&bwt->inverseSa0, sizeof(bgint_t), 1, bwtFile) != 1 + || fwrite(bwt->cumulativeFreq + 1, + sizeof(bgint_t), ALPHABET_SIZE, bwtFile) != ALPHABET_SIZE + || fwrite(bwt->bwtCode, + sizeof(unsigned int), bwtLength, bwtFile) != bwtLength) { + fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Error writing to %s : %s\n", + bwtFileName, strerror(errno)); + exit(1); + } + if (fclose(bwtFile) != 0) { + fprintf(stderr, "BWTSaveBwtCodeAndOcc(): Error on closing %s : %s\n", + bwtFileName, strerror(errno)); + exit(1); + } } void bwt_bwtgen(const char *fn_pac, const char *fn_bwt) diff --git a/is.c b/is.c index 1891668..46f1772 100644 --- a/is.c +++ b/is.c @@ -25,7 +25,6 @@ */ #include -#include "utils.h" #ifdef USE_MALLOC_WRAPPERS # include "malloc_wrap.h" @@ -211,7 +210,7 @@ int is_bwt(ubyte_t *T, int n) int *SA, i, primary = 0; SA = (int*)calloc(n+1, sizeof(int)); - if (is_sa(T, SA, n)) err_fatal_simple("is_sa failed"); + if (is_sa(T, SA, n)) return -1; for (i = 0; i <= n; ++i) { if (SA[i] == 0) primary = i; diff --git a/ksw.h b/ksw.h index f9d22c6..97559fd 100644 --- a/ksw.h +++ b/ksw.h @@ -3,10 +3,6 @@ #include -#ifdef USE_MALLOC_WRAPPERS -# include "malloc_wrap.h" -#endif - #define KSW_XBYTE 0x10000 #define KSW_XSTOP 0x20000 #define KSW_XSUBO 0x40000