@@ -2906,9 +2906,10 @@ ssize_t bam_parse_cigar(const char *in, char **end, bam1_t *b) {
29062906 * SAM threading
29072907 */
29082908// Size of SAM text block (reading)
2909- #define NM 240000
2910- // Number of BAM records (writing)
2911- #define NB 1000
2909+ #define SAM_NBYTES 240000
2910+
2911+ // Number of BAM records (writing, up to NB_mem in size)
2912+ #define SAM_NBAM 1000
29122913
29132914struct SAM_state ;
29142915
@@ -2918,7 +2919,8 @@ typedef struct sp_bams {
29182919 int serial ;
29192920
29202921 bam1_t * bams ;
2921- int nbams , abams ; // used and alloc
2922+ int nbams , abams ; // used and alloc for bams[] array
2923+ size_t bam_mem ; // very approximate total size
29222924
29232925 struct SAM_state * fd ;
29242926} sp_bams ;
@@ -3169,6 +3171,7 @@ static void *sam_parse_worker(void *arg) {
31693171 goto err ;
31703172 }
31713173 gb -> nbams = 0 ;
3174+ gb -> bam_mem = 0 ;
31723175 }
31733176 gb -> serial = gl -> serial ;
31743177 gb -> next = NULL ;
@@ -3221,6 +3224,7 @@ static void *sam_parse_worker(void *arg) {
32213224 cleanup_sp_lines (gl );
32223225 goto err ;
32233226 }
3227+
32243228 cp = nl ;
32253229 i ++ ;
32263230 }
@@ -3290,7 +3294,7 @@ static void *sam_dispatcher_read(void *vp) {
32903294 l = calloc (1 , sizeof (* l ));
32913295 if (!l )
32923296 goto err ;
3293- l -> alloc = NM ;
3297+ l -> alloc = SAM_NBYTES ;
32943298 l -> data = malloc (l -> alloc + 8 ); // +8 for optimisation in sam_parse1
32953299 if (!l -> data ) {
32963300 free (l );
@@ -3301,11 +3305,11 @@ static void *sam_dispatcher_read(void *vp) {
33013305 }
33023306 l -> next = NULL ;
33033307
3304- if (l -> alloc < line_frag + NM /2 ) {
3305- char * rp = realloc (l -> data , line_frag + NM /2 + 8 );
3308+ if (l -> alloc < line_frag + SAM_NBYTES /2 ) {
3309+ char * rp = realloc (l -> data , line_frag + SAM_NBYTES /2 + 8 );
33063310 if (!rp )
33073311 goto err ;
3308- l -> alloc = line_frag + NM /2 ;
3312+ l -> alloc = line_frag + SAM_NBYTES /2 ;
33093313 l -> data = rp ;
33103314 }
33113315 memcpy (l -> data , line .s , line_frag );
@@ -4302,16 +4306,18 @@ int sam_write1(htsFile *fp, const sam_hdr_t *h, const bam1_t *b)
43024306 fd -> bams = gb -> next ;
43034307 gb -> next = NULL ;
43044308 gb -> nbams = 0 ;
4309+ gb -> bam_mem = 0 ;
43054310 pthread_mutex_unlock (& fd -> lines_m );
43064311 } else {
43074312 pthread_mutex_unlock (& fd -> lines_m );
43084313 if (!(gb = calloc (1 , sizeof (* gb )))) return -1 ;
4309- if (!(gb -> bams = calloc (NB , sizeof (* gb -> bams )))) {
4314+ if (!(gb -> bams = calloc (SAM_NBAM , sizeof (* gb -> bams )))) {
43104315 free (gb );
43114316 return -1 ;
43124317 }
43134318 gb -> nbams = 0 ;
4314- gb -> abams = NB ;
4319+ gb -> abams = SAM_NBAM ;
4320+ gb -> bam_mem = 0 ;
43154321 gb -> fd = fd ;
43164322 fd -> curr_idx = 0 ;
43174323 fd -> curr_bam = gb ;
@@ -4320,11 +4326,11 @@ int sam_write1(htsFile *fp, const sam_hdr_t *h, const bam1_t *b)
43204326
43214327 if (!bam_copy1 (& gb -> bams [gb -> nbams ++ ], b ))
43224328 return -2 ;
4329+ gb -> bam_mem += b -> l_data + sizeof (* b );
43234330
43244331 // Dispatch if full
4325- if (gb -> nbams == NB ) {
4332+ if (gb -> nbams == SAM_NBAM || gb -> bam_mem > SAM_NBYTES * 0.8 ) {
43264333 gb -> serial = fd -> serial ++ ;
4327- //fprintf(stderr, "Dispatch another %d bams\n", NB);
43284334 pthread_mutex_lock (& fd -> command_m );
43294335 if (fd -> errcode != 0 ) {
43304336 pthread_mutex_unlock (& fd -> command_m );
0 commit comments