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 14, 2024
1 parent 969ae87 commit 56d53d2
Show file tree
Hide file tree
Showing 9 changed files with 1,051 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bufiox/bufreader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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)
}
17 changes: 17 additions & 0 deletions bufiox/bufwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 56d53d2

Please sign in to comment.