Patch memory error related to []byte pool

This commit is contained in:
2022-09-28 14:28:37 +02:00
parent 57ba460929
commit ebefa28cc0
5 changed files with 44 additions and 52 deletions

View File

@@ -129,9 +129,9 @@ func (s *BioSequence) Copy() *BioSequence {
newSeq.id = s.id
newSeq.definition = s.definition
newSeq.sequence = GetSlice(s.sequence...)
newSeq.qualities = GetSlice(s.qualities...)
newSeq.feature = GetSlice(s.feature...)
newSeq.sequence = CopySlice(s.sequence)
newSeq.qualities = CopySlice(s.qualities)
newSeq.feature = CopySlice(s.feature)
if len(s.annotations) > 0 {
newSeq.annotations = GetAnnotation(s.annotations)
@@ -340,7 +340,6 @@ func (s *BioSequence) ClearQualities() {
s.qualities = s.qualities[0:0]
}
// A method that appends a byte slice to the sequence.
func (s *BioSequence) Write(data []byte) (int, error) {
s.sequence = append(s.sequence, data...)

View File

@@ -1,6 +1,7 @@
package obiseq
import (
"log"
"sync"
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
@@ -14,22 +15,36 @@ var _BioSequenceByteSlicePool = sync.Pool{
}
func RecycleSlice(s *[]byte) {
if s != nil && *s != nil {
if s != nil && cap(*s) > 0 {
*s = (*s)[:0]
if cap(*s) == 0 {
log.Panicln("trying to store a NIL slice in the pool", s == nil, *s == nil, cap(*s))
}
_BioSequenceByteSlicePool.Put(s)
}
}
func GetSlice(values ...byte) []byte {
s := *(_BioSequenceByteSlicePool.Get().(*[]byte))
// It returns a slice of bytes from a pool of slices.
//
// the slice can be prefilled with the provided values
func GetSlice(capacity int) []byte {
p := _BioSequenceByteSlicePool.Get().(*[]byte)
if len(values) > 0 {
s = append(s, values...)
if p == nil || *p == nil || cap(*p) < capacity {
s := make([]byte, 0, capacity)
p = &s
}
s := *p
return s
}
func CopySlice(src []byte) []byte {
sl := GetSlice(len(src))
copy(sl,src)
return sl
}
var BioSequenceAnnotationPool = sync.Pool{
New: func() interface{} {
bs := make(Annotation, 5)