Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Reuse and Initialize methods to LinkBuffer #310

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion nocopy_linkbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ func NewLinkBuffer(size ...int) *LinkBuffer {
}
var node = newLinkBufferNode(l)
buf.head, buf.read, buf.flush, buf.write = node, node, node, node
buf.enable = true
return buf
}

// LinkBuffer implements ReadWriter.
type LinkBuffer struct {
length int64
mallocSize int
enable bool

head *linkBufferNode // release head
read *linkBufferNode // read head
Expand All @@ -75,6 +77,25 @@ func (b *LinkBuffer) IsEmpty() (ok bool) {
return b.Len() == 0
}

// Reuse reactivates a LinkBuffer that has been closed or appended to another LinkBuffer.
func (b *LinkBuffer) Reuse(size ...int) {
if b.enable {
return
}
b.Initialize(size...)
b.enable = true
}

// Initialize initializes a LinkBuffer.
func (b *LinkBuffer) Initialize(size ...int) {
var l int
if len(size) > 0 {
l = size[0]
}
var node = newLinkBufferNode(l)
b.head, b.read, b.flush, b.write = node, node, node, node
}

// ------------------------------------------ implement zero-copy reader ------------------------------------------

// Next implements Reader.
Expand Down Expand Up @@ -419,7 +440,7 @@ func (b *LinkBuffer) WriteBuffer(buf *LinkBuffer) (err error) {
nd.Release()
}
buf.length, buf.mallocSize, buf.head, buf.read, buf.flush, buf.write = 0, 0, nil, nil, nil, nil

buf.enable = false
// DON'T MODIFY THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING !
//
// You may encounter a chain of bugs and not be able to
Expand Down Expand Up @@ -534,6 +555,7 @@ func (b *LinkBuffer) Close() (err error) {
nd.Release()
}
b.head, b.read, b.flush, b.write = nil, nil, nil, nil
b.enable = false
return nil
}

Expand Down
Loading