@@ -30,7 +30,7 @@ namespace tiledb {
3030namespace vcf {
3131
3232VCFV4::VCFV4 (SharingMode mode)
33- : mode_ (mode)
33+ : SharedPtrPool< bcf1_t > (mode)
3434 , open_(false )
3535 , inited_(false )
3636 , max_record_buffer_size_(10000 )
@@ -102,7 +102,7 @@ void VCFV4::open(const std::string& file, const std::string& index_file) {
102102void VCFV4::close () {
103103 // Clear the record queue and associated allocation pool.
104104 std::queue<SafeSharedBCFRec>().swap (record_queue_);
105- std::queue<SafeSharedBCFRec>(). swap (record_queue_pool_ );
105+ clear_ptr_pool ( );
106106
107107 if (index_hts_ != nullptr ) {
108108 hts_idx_destroy (index_hts_);
@@ -156,13 +156,7 @@ void VCFV4::pop_record() {
156156}
157157
158158void VCFV4::return_record (SafeSharedBCFRec& record) {
159- if (mode_ == SharingMode::MANUAL) {
160- record_queue_pool_.emplace (std::move (record));
161- } else {
162- LOG_ERROR (
163- " VCFV4::return_record records cannot be manually returned for mode {}" ,
164- modeToString (mode_));
165- }
159+ return_ptr_to_pool (record);
166160}
167161
168162std::string VCFV4::contig_name (bcf1_t * const r) const {
@@ -291,21 +285,6 @@ bool VCFV4::seek(const std::string& contig_name, uint32_t pos) {
291285 return !record_queue_.empty ();
292286}
293287
294- void VCFV4::create_record (SafeBCFRec& tmp_r) {
295- SafeSharedBCFRec r (bcf_dup (tmp_r.get ()), bcf_destroy);
296- bcf_unpack (r.get (), BCF_UN_ALL);
297- record_queue_.emplace (std::move (r));
298- }
299-
300- void VCFV4::reuse_record (SafeBCFRec& tmp_r) {
301- SafeSharedBCFRec r = record_queue_pool_.front ();
302- record_queue_pool_.pop ();
303- // Use `bcf_copy` to destroy (free) the stale data to prevent memory leaks
304- bcf_copy (r.get (), tmp_r.get ());
305- bcf_unpack (r.get (), BCF_UN_ALL);
306- record_queue_.emplace (std::move (r));
307- }
308-
309288void VCFV4::read_records () {
310289 if (!record_queue_.empty ())
311290 std::queue<SafeSharedBCFRec>().swap (record_queue_);
@@ -322,21 +301,17 @@ void VCFV4::read_records() {
322301 break ;
323302 }
324303
325- if (!record_queue_pool_.empty () && mode_ == SharingMode::MANUAL) {
326- reuse_record (tmp_r);
327- } else if (!record_queue_pool_.empty () && mode_ == SharingMode::AUTOMATIC) {
328- // Check if the record at the front of the pool is stale, i.e. the pool
329- // has the only copy
330- SafeSharedBCFRec& r = record_queue_pool_.front ();
331- if (r.use_count () == 1 ) {
332- reuse_record (tmp_r);
333- } else {
334- // Resuse a stale record
335- create_record (tmp_r);
336- }
337- record_queue_pool_.push (record_queue_.front ());
304+ // Add a new record to the pool or reuse a record from the pool
305+ if (ptr_pool_empty ()) {
306+ SafeSharedBCFRec r = create_pool_ptr (bcf_dup (tmp_r.get ()), bcf_destroy);
307+ bcf_unpack (r.get (), BCF_UN_ALL);
308+ record_queue_.emplace (std::move (r));
338309 } else {
339- create_record (tmp_r);
310+ SafeSharedBCFRec r = reuse_pool_ptr ();
311+ // Use `bcf_copy` to destroy (free) the stale data to prevent memory leaks
312+ bcf_copy (r.get (), tmp_r.get ());
313+ bcf_unpack (r.get (), BCF_UN_ALL);
314+ record_queue_.emplace (std::move (r));
340315 }
341316 record_buffer_size += sizeof (bcf1_t ) + record_queue_.front ()->shared .m +
342317 record_queue_.front ()->indiv .m ;
@@ -351,12 +326,11 @@ void VCFV4::read_records() {
351326}
352327
353328void VCFV4::swap (VCFV4& other) {
354- std ::swap (mode_, other. mode_ );
329+ SharedPtrPool< bcf1_t > ::swap (other);
355330 std::swap (open_, other.open_ );
356331 std::swap (path_, other.path_ );
357332 std::swap (index_path_, other.index_path_ );
358333 std::swap (record_queue_, other.record_queue_ );
359- std::swap (record_queue_pool_, other.record_queue_pool_ );
360334 record_iter_.swap (other.record_iter_ );
361335 std::swap (hdr_, other.hdr_ );
362336 std::swap (index_tbx_, other.index_tbx_ );
0 commit comments