From c4397a9400a2825ad10c875bb0d1795d46fb5fe2 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Sat, 11 Apr 2020 22:05:07 -0400 Subject: [PATCH] r226: more careful about ma_hit_t_alloc memory --- Assembly.cpp | 24 +++++++++++++++--------- CommandLines.h | 2 +- Overlaps.cpp | 39 +++++++++++++-------------------------- Overlaps.h | 3 +-- Process_Read.cpp | 4 ++-- Purge_Dups.cpp | 2 +- 6 files changed, 33 insertions(+), 41 deletions(-) diff --git a/Assembly.cpp b/Assembly.cpp index ec21c47..d41d10a 100644 --- a/Assembly.cpp +++ b/Assembly.cpp @@ -250,7 +250,12 @@ inline void push_cigar(Compressed_Cigar_record* records, long long ID, Cigar_rec void push_overlaps(ma_hit_t_alloc* paf, overlap_region_alloc* overlap_list, int flag, All_reads* R_INF, int if_reverse) { long long i = 0, xLen, yLen; + int32_t size = 0; ma_hit_t tmp; + for (i = 0; i < (long long)overlap_list->length; ++i) + if (overlap_list->list[i].is_match == flag) + ++size; + resize_ma_hit_t_alloc(paf, size); clear_ma_hit_t_alloc(paf); for (i = 0; i < (long long)overlap_list->length; i++) { @@ -278,7 +283,6 @@ void push_overlaps(ma_hit_t_alloc* paf, overlap_region_alloc* overlap_list, int tmp.te = overlap_list->list[i].y_pos_e; } - ///for overlap_list, the x_strand of all overlaps are 0, so the tmp.rev is the same as the y_strand tmp.rev = overlap_list->list[i].y_pos_strand; @@ -322,7 +326,7 @@ long long push_final_overlaps(ma_hit_t_alloc* paf, ma_hit_t_alloc* reverse_paf_l long long i = 0; long long available_overlaps = 0; ma_hit_t tmp; - clear_ma_hit_t_alloc(paf); + clear_ma_hit_t_alloc(paf); // paf has been preallocated, so we don't need preallocation for (i = 0; i < (long long)overlap_list->length; i++) { if (overlap_list->list[i].is_match == flag) @@ -374,7 +378,7 @@ long long push_final_overlaps(ma_hit_t_alloc* paf, ma_hit_t_alloc* reverse_paf_l } typedef struct { - int is_final; + int is_final, save_ov; // chaining and overlapping related buffers UC_Read self_read, ovlp_read; Candidates_list clist; @@ -390,11 +394,11 @@ typedef struct { Round2_alignment round2; } ha_ovec_buf_t; -ha_ovec_buf_t *ha_ovec_init(int is_final) +ha_ovec_buf_t *ha_ovec_init(int is_final, int save_ov) { ha_ovec_buf_t *b; CALLOC(b, 1); - b->is_final = !!is_final; + b->is_final = !!is_final, b->save_ov = !!save_ov; init_UC_Read(&b->self_read); init_UC_Read(&b->ovlp_read); init_Candidates_list(&b->clist); @@ -493,8 +497,10 @@ static void worker_ovec(void *data, long i, int tid) } R_INF.paf[i].is_abnormal = abnormal; - push_overlaps(&(R_INF.paf[i]), &b->olist, 1, &R_INF, asm_opt.roundID%2); - push_overlaps(&(R_INF.reverse_paf[i]), &b->olist, 2, &R_INF, asm_opt.roundID%2); + if (b->save_ov) { + push_overlaps(&(R_INF.paf[i]), &b->olist, 1, &R_INF, 1); + push_overlaps(&(R_INF.reverse_paf[i]), &b->olist, 2, &R_INF, 1); + } } static void worker_ovec_related_reads(void *data, long i, int tid) @@ -632,7 +638,7 @@ void ha_overlap_and_correct(int round) // overlap and correct reads CALLOC(b, asm_opt.thread_num); for (i = 0; i < asm_opt.thread_num; ++i) - b[i] = ha_ovec_init(0); + b[i] = ha_ovec_init(0, (round == asm_opt.number_of_round - 1)); ha_idx = ha_pt_gen(&asm_opt, ha_flt_tab, round == 0? 0 : 1, &R_INF, &hom_cov); // build the index if (round == 0 && ha_flt_tab == 0) // then asm_opt.hom_cov hasn't been updated ha_opt_update_cov(&asm_opt, hom_cov); @@ -1140,7 +1146,7 @@ void ha_overlap_final(void) ha_ovec_buf_t **b; CALLOC(b, asm_opt.thread_num); for (i = 0; i < asm_opt.thread_num; ++i) - b[i] = ha_ovec_init(1); + b[i] = ha_ovec_init(1, 1); ha_idx = ha_pt_gen(&asm_opt, ha_flt_tab, 1, &R_INF, &hom_cov); // build the index kt_for(asm_opt.thread_num, worker_ov_final, b, R_INF.total_reads); ha_pt_destroy(ha_idx); diff --git a/CommandLines.h b/CommandLines.h index 3d50b46..bf59be7 100644 --- a/CommandLines.h +++ b/CommandLines.h @@ -3,7 +3,7 @@ #include -#define HA_VERSION "0.3.0-dirty-r224" +#define HA_VERSION "0.3.0-dirty-r226" #define VERBOSE 0 diff --git a/Overlaps.cpp b/Overlaps.cpp index 4eae1f2..d829111 100644 --- a/Overlaps.cpp +++ b/Overlaps.cpp @@ -358,44 +358,31 @@ void clear_ma_hit_t_alloc(ma_hit_t_alloc* x) x->length = 0; } -void resize_ma_hit_t_alloc(ma_hit_t_alloc* x, uint64_t size) +void resize_ma_hit_t_alloc(ma_hit_t_alloc* x, uint32_t size) { - if(size > x->size) - { - x->size = size; - x->buffer = (ma_hit_t*)realloc(x->buffer, x->size*sizeof(ma_hit_t)); - } + if (size > x->size) { + x->size = size; + kroundup32(x->size); + REALLOC(x->buffer, x->size); + } } void destory_ma_hit_t_alloc(ma_hit_t_alloc* x) { - free(x->buffer); -} - -void destory_all_ma_hit_t_alloc(ma_hit_t_alloc* x, uint64_t n_read) -{ - uint64_t i = 0; - for (i = 0; i < n_read; i++) - { - free(x[i].buffer); - } - free(x); + free(x->buffer); } void add_ma_hit_t_alloc(ma_hit_t_alloc* x, ma_hit_t* element) { - if(x->length + 1 > x->size) - { - x->size = (x->length + 1) * 2; - x->buffer = (ma_hit_t*)realloc(x->buffer, x->size*sizeof(ma_hit_t)); - } - - x->buffer[x->length] = (*element); - x->length++; + if (x->length + 1 > x->size) { + x->size = x->length + 1; + kroundup32(x->size); + REALLOC(x->buffer, x->size); + } + x->buffer[x->length++] = *element; } - long long get_specific_overlap(ma_hit_t_alloc* x, uint32_t qn, uint32_t tn) { long long i; diff --git a/Overlaps.h b/Overlaps.h index 0bb802e..88d4c96 100644 --- a/Overlaps.h +++ b/Overlaps.h @@ -68,8 +68,7 @@ typedef struct { void init_ma_hit_t_alloc(ma_hit_t_alloc* x); void clear_ma_hit_t_alloc(ma_hit_t_alloc* x); -void resize_ma_hit_t_alloc(ma_hit_t_alloc* x, uint64_t size); -void destory_all_ma_hit_t_alloc(ma_hit_t_alloc* x, uint64_t n_read); +void resize_ma_hit_t_alloc(ma_hit_t_alloc* x, uint32_t size); void destory_ma_hit_t_alloc(ma_hit_t_alloc* x); void add_ma_hit_t_alloc(ma_hit_t_alloc* x, ma_hit_t* element); void ma_hit_sort_tn(ma_hit_t *a, long long n); diff --git a/Process_Read.cpp b/Process_Read.cpp index 7529c9b..79ed41b 100644 --- a/Process_Read.cpp +++ b/Process_Read.cpp @@ -44,8 +44,8 @@ void destory_All_reads(All_reads* r) for (i = 0; i < r->total_reads; i++) { if (r->N_site[i]) free(r->N_site[i]); if (r->read_sperate[i]) free(r->read_sperate[i]); - if (r->paf&&r->paf[i].buffer) free(r->paf[i].buffer); - if (r->reverse_paf&&r->reverse_paf[i].buffer) free(r->reverse_paf[i].buffer); + if (r->paf && r->paf[i].buffer) free(r->paf[i].buffer); + if (r->reverse_paf && r->reverse_paf[i].buffer) free(r->reverse_paf[i].buffer); } free(r->paf); free(r->reverse_paf); diff --git a/Purge_Dups.cpp b/Purge_Dups.cpp index 0958404..e993665 100644 --- a/Purge_Dups.cpp +++ b/Purge_Dups.cpp @@ -1098,7 +1098,7 @@ void print_asg_arc_t_offset(asg_arc_t_offset* x, long long n, const char* info) x_off = (long long)(x[i].Off>>32); y_off = (long long)((uint32_t)x[i].Off); fprintf(stderr, "i: %lld, x_off: %lld, y_off: %lld, weight: %lu, rev: %u, ol: %u\n", - i, x_off, y_off, x[i].weight, x[i].x.el, x[i].x.ol); + i, x_off, y_off, (unsigned long)x[i].weight, x[i].x.el, x[i].x.ol); } }