Skip to content

Commit 5babe71

Browse files
authored
fix: correct the logic of discarding the rest in MallocAck (#393)
1 parent 453f027 commit 5babe71

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

nocopy_linkbuffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (b *UnsafeLinkBuffer) MallocAck(n int) (err error) {
403403
}
404404
// discard the rest
405405
for node := b.write.next; node != nil; node = node.next {
406-
node.off, node.malloc, node.refer, node.buf = 0, 0, 1, node.buf[:0]
406+
node.malloc, node.refer, node.buf = node.off, 1, node.buf[:node.off]
407407
}
408408
return nil
409409
}

nocopy_linkbuffer_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"bytes"
2222
"encoding/binary"
2323
"fmt"
24+
"reflect"
2425
"runtime"
2526
"sync/atomic"
2627
"testing"
@@ -778,6 +779,34 @@ func TestLinkBufferPeekOutOfMemory(t *testing.T) {
778779
}
779780
}
780781

782+
func TestMallocAck(t *testing.T) {
783+
sLen := 1024 * 7
784+
buf1 := []byte{1, 2, 3, 4}
785+
buf2 := []byte{5, 6, 7, 8}
786+
lb := NewLinkBuffer(0)
787+
788+
buf, err := lb.Malloc(4 + sLen)
789+
MustNil(t, err)
790+
copy(buf[:4], buf1)
791+
s := make([]byte, sLen)
792+
err = lb.WriteDirect(s, sLen)
793+
MustNil(t, err)
794+
795+
err = lb.MallocAck(4 + sLen)
796+
MustNil(t, err)
797+
lb.Flush()
798+
799+
buf, err = lb.Malloc(4)
800+
MustNil(t, err)
801+
copy(buf[:4], buf2)
802+
lb.Flush()
803+
804+
buf, err = lb.Next(8 + sLen)
805+
MustNil(t, err)
806+
807+
MustTrue(t, reflect.DeepEqual(buf, append(append(buf1, s...), buf2...)))
808+
}
809+
781810
func BenchmarkStringToSliceByte(b *testing.B) {
782811
b.StopTimer()
783812
s := "hello world"

0 commit comments

Comments
 (0)