Skip to content

Commit

Permalink
feat: add bufiox interfaces and spilit ttheader codec codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Aug 15, 2024
1 parent 2dfa481 commit 0aa1d06
Show file tree
Hide file tree
Showing 11 changed files with 1,590 additions and 0 deletions.
44 changes: 44 additions & 0 deletions bufiox/bufreader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bufiox

// Reader is a buffer IO interface, which provides a user-space zero-copy method to reduce memory allocation and copy overhead.
type Reader interface {
// Next reads the next n bytes sequentially and returns a slice `p` of length `n`,
// otherwise returns an error if it is unable to read a buffer of n bytes.
// The returned `p` can be a shallow copy of the original buffer.
// Must ensure that the data in `p` is not modified before calling Release.
//
// Callers cannot use the returned data after calling Release.
Next(n int) (p []byte, err error)

// Peek behaves the same as Next, except that it doesn't advance the reader.
//
// Callers cannot use the returned data after calling Release.
Peek(n int) (buf []byte, err error)

// Skip skips the next n bytes sequentially, otherwise returns an error if it's unable to skip a buffer of n bytes.
Skip(n int) (err error)

// ReadLen returns the size that has already been read.
// Read/Next/Skip will increase the size. When the release function is called, ReadLen is set to 0.
ReadLen() (n int)

// Release will free the buffer. After release, buffer read by Next/Skip/Peek is invalid.
// Param e is used when the buffer release depend on error.
// For example, usually the write buffer will be released inside flush,
// but if flush error happen, write buffer may need to be released explicitly.
Release(e error) (err error)
}
31 changes: 31 additions & 0 deletions bufiox/bufwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bufiox

// Writer is a buffer IO interface, which provides a user-space zero-copy method to reduce memory allocation and copy overhead.
type Writer interface {
// Malloc returns a shallow copy of the write buffer with length n,
// otherwise returns an error if it's unable to get n bytes from the write buffer.
// Must ensure that the data written by the user to buf can be flushed to the underlying io.Writer.
//
// Caller cannot write data to the returned buf after calling Flush.
Malloc(n int) (buf []byte, err error)

// WrittenLen returns the total length of the buffer written.
WrittenLen() (length int)

// Flush writes any malloc data to the underlying io.Writer, and reset WrittenLen to zero.
Flush() (err error)
}
Loading

0 comments on commit 0aa1d06

Please sign in to comment.