mirror of
https://github.com/fumiama/orbyte.git
synced 2026-07-22 23:15:05 +08:00
fix(all): impl. new apis to make sure safety
This commit is contained in:
@@ -2,7 +2,6 @@ package pbuf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/orbyte"
|
||||
)
|
||||
@@ -21,54 +20,3 @@ func (bufferPool BufferPool) InvolveBuffer(buf *bytes.Buffer) *orbyte.Item[bytes
|
||||
func (bufferPool BufferPool) ParseBuffer(buf *bytes.Buffer) *orbyte.Item[bytes.Buffer] {
|
||||
return bufferPool.p.Parse(buf.Len(), buf)
|
||||
}
|
||||
|
||||
// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
|
||||
// The zero value for Buffer is an empty buffer ready to use.
|
||||
type buffer struct {
|
||||
buf []byte // contents are the bytes buf[off : len(buf)]
|
||||
off int // read at &buf[off], write at &buf[len(buf)]
|
||||
lastRead readOp // last read operation, so that Unread* can work correctly.
|
||||
}
|
||||
|
||||
func skip(w *bytes.Buffer, n int) {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
b := (*buffer)(unsafe.Pointer(w))
|
||||
b.lastRead = opInvalid
|
||||
if len(b.buf) <= b.off {
|
||||
// Buffer is empty, reset to recover space.
|
||||
w.Reset()
|
||||
return
|
||||
}
|
||||
n = minnum(n, len(b.buf[b.off:]))
|
||||
b.off += n
|
||||
if n > 0 {
|
||||
b.lastRead = opRead
|
||||
}
|
||||
}
|
||||
|
||||
// The readOp constants describe the last action performed on
|
||||
// the buffer, so that UnreadRune and UnreadByte can check for
|
||||
// invalid usage. opReadRuneX constants are chosen such that
|
||||
// converted to int they correspond to the rune size that was read.
|
||||
type readOp int8
|
||||
|
||||
// Don't use iota for these, as the values need to correspond with the
|
||||
// names and comments, which is easier to see when being explicit.
|
||||
const (
|
||||
opRead readOp = -1 // Any other read operation.
|
||||
opInvalid readOp = 0 // Non-read operation.
|
||||
opReadRune1 readOp = 1 // Read rune of size 1.
|
||||
opReadRune2 readOp = 2 // Read rune of size 2.
|
||||
opReadRune3 readOp = 3 // Read rune of size 3.
|
||||
opReadRune4 readOp = 4 // Read rune of size 4.
|
||||
)
|
||||
|
||||
// minnum 返回两数最小值,该函数将被内联
|
||||
func minnum[T int | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64](a, b T) T {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -20,38 +20,24 @@ func TestBuffer(t *testing.T) {
|
||||
}
|
||||
|
||||
func testBuffer(buf *orbyte.Item[bytes.Buffer], t *testing.T) {
|
||||
if buf.Pointer().Len() != 4096 {
|
||||
io.CopyN(buf.Pointer(), rand.Reader, 4096)
|
||||
if buf.Pointer().Len() != 4096 {
|
||||
t.Fatal("got", buf.Pointer().Len())
|
||||
buf.P(func(buf *bytes.Buffer) {
|
||||
if buf.Len() != 4096 {
|
||||
io.CopyN(buf, rand.Reader, 4096)
|
||||
if buf.Len() != 4096 {
|
||||
t.Fatal("got", buf.Len())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
bufcp := buf.Copy()
|
||||
if bufcp.Pointer().Len() != 4096 {
|
||||
t.Fatal("got", bufcp.Pointer().Len())
|
||||
}
|
||||
if !bytes.Equal(bufcp.Pointer().Bytes(), buf.Pointer().Bytes()) {
|
||||
t.Fatal("unexpected")
|
||||
}
|
||||
|
||||
bufr := buf.Ref()
|
||||
if bufr.Pointer().Len() != 4096 {
|
||||
t.Fatal("got", bufr.Pointer().Len())
|
||||
}
|
||||
if !bytes.Equal(bufr.Pointer().Bytes(), buf.Pointer().Bytes()) {
|
||||
t.Fatal("unexpected")
|
||||
}
|
||||
bufr.ManualDestroy()
|
||||
|
||||
bufcp = bufcp.Trans()
|
||||
if bufcp.Pointer().Len() != 4096 {
|
||||
t.Fatal("got", bufcp.Pointer().Len())
|
||||
}
|
||||
if !bytes.Equal(bufcp.Pointer().Bytes(), buf.Pointer().Bytes()) {
|
||||
t.Fatal("unexpected")
|
||||
}
|
||||
bufcp.ManualDestroy()
|
||||
dat := buf.Trans()
|
||||
bufcp.P(func(bufcp *bytes.Buffer) {
|
||||
if bufcp.Len() != 4096 {
|
||||
t.Fatal("got", bufcp.Len())
|
||||
}
|
||||
if !bytes.Equal(bufcp.Bytes(), dat.Bytes()) {
|
||||
t.Fatal("unexpected")
|
||||
}
|
||||
})
|
||||
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
|
||||
117
pbuf/bytes.go
117
pbuf/bytes.go
@@ -2,7 +2,6 @@ package pbuf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"runtime"
|
||||
|
||||
"github.com/fumiama/orbyte"
|
||||
)
|
||||
@@ -10,8 +9,8 @@ import (
|
||||
// Bytes wrap pooled buffer into []byte
|
||||
// while sharing the same pool.
|
||||
type Bytes struct {
|
||||
buf *orbyte.Item[bytes.Buffer]
|
||||
dat []byte
|
||||
buf *orbyte.Item[bytes.Buffer]
|
||||
a, b int
|
||||
}
|
||||
|
||||
// BufferItemToBytes convert between *orbyte.Item[bytes.Buffer]
|
||||
@@ -19,27 +18,43 @@ type Bytes struct {
|
||||
//
|
||||
// Please notice that Bytes cannnot convert back to
|
||||
// *orbyte.Item[bytes.Buffer] again.
|
||||
func BufferItemToBytes(buf *orbyte.Item[bytes.Buffer]) Bytes {
|
||||
return Bytes{buf: buf, dat: buf.Pointer().Bytes()}
|
||||
func BufferItemToBytes(buf *orbyte.Item[bytes.Buffer]) (b Bytes) {
|
||||
b.buf = buf
|
||||
buf.P(func(buf *bytes.Buffer) {
|
||||
b.b = buf.Len()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// NewBytes alloc sz bytes.
|
||||
func (bufferPool BufferPool) NewBytes(sz int) Bytes {
|
||||
func (bufferPool BufferPool) NewBytes(sz int) (b Bytes) {
|
||||
buf := bufferPool.p.New(sz)
|
||||
return Bytes{buf: buf, dat: buf.Pointer().Bytes()[:sz]}
|
||||
b.buf = buf
|
||||
buf.P(func(buf *bytes.Buffer) {
|
||||
b.b = buf.Len()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// InvolveBytes involve outside buf into pool.
|
||||
func (bufferPool BufferPool) InvolveBytes(b ...byte) Bytes {
|
||||
buf := bufferPool.p.Involve(len(b), bytes.NewBuffer(b))
|
||||
return Bytes{buf: buf, dat: buf.Pointer().Bytes()[:len(b)]}
|
||||
func (bufferPool BufferPool) InvolveBytes(p ...byte) (b Bytes) {
|
||||
buf := bufferPool.p.Involve(len(p), bytes.NewBuffer(p))
|
||||
b.buf = buf
|
||||
buf.P(func(buf *bytes.Buffer) {
|
||||
b.b = buf.Len()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ParseBytes convert outside bytes to Bytes safely
|
||||
// without adding it into pool.
|
||||
func (bufferPool BufferPool) ParseBytes(b ...byte) Bytes {
|
||||
buf := bufferPool.p.Parse(len(b), bytes.NewBuffer(b))
|
||||
return Bytes{buf: buf, dat: buf.Pointer().Bytes()}
|
||||
func (bufferPool BufferPool) ParseBytes(p ...byte) (b Bytes) {
|
||||
buf := bufferPool.p.Parse(len(p), bytes.NewBuffer(p))
|
||||
b.buf = buf
|
||||
buf.P(func(buf *bytes.Buffer) {
|
||||
b.b = buf.Len()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// HasInit whether this Bytes is made by pool or
|
||||
@@ -49,89 +64,49 @@ func (b Bytes) HasInit() bool {
|
||||
}
|
||||
|
||||
// Trans please refer to Item.Trans().
|
||||
func (b Bytes) Trans() (tb Bytes) {
|
||||
tb.buf = b.buf.Trans()
|
||||
tb.dat = b.dat
|
||||
return
|
||||
func (b Bytes) Trans() []byte {
|
||||
buf := b.buf.Trans()
|
||||
return buf.Bytes()[b.a:b.b]
|
||||
}
|
||||
|
||||
// Len of slice.
|
||||
func (b Bytes) Len() int {
|
||||
return len(b.dat)
|
||||
return b.b - b.a
|
||||
}
|
||||
|
||||
// Cap of slice.
|
||||
func (b Bytes) Cap() int {
|
||||
return cap(b.dat)
|
||||
func (b Bytes) Cap() (c int) {
|
||||
b.buf.P(func(b *bytes.Buffer) {
|
||||
c = b.Cap()
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
// Bytes is the inner value.
|
||||
func (b Bytes) Bytes() []byte {
|
||||
return b.dat
|
||||
}
|
||||
|
||||
// Ref please refer to Item.Ref().
|
||||
func (b Bytes) Ref() (rb Bytes) {
|
||||
rb.buf = b.buf.Ref()
|
||||
rb.dat = b.dat
|
||||
return
|
||||
// V use the inner value safely
|
||||
func (b Bytes) V(f func([]byte)) {
|
||||
b.buf.P(func(buf *bytes.Buffer) {
|
||||
f(buf.Bytes()[b.a:b.b])
|
||||
})
|
||||
}
|
||||
|
||||
// Copy please refer to Item.Copy().
|
||||
func (b Bytes) Copy() (cb Bytes) {
|
||||
cb.buf = b.buf.Copy()
|
||||
cb.dat = cb.buf.Pointer().Bytes()
|
||||
cb.a, cb.b = b.a, b.b
|
||||
return
|
||||
}
|
||||
|
||||
// SliceFrom dat[from:] with Ref.
|
||||
func (b Bytes) SliceFrom(from int) Bytes {
|
||||
if b.buf.IsTrans() {
|
||||
if b.buf.HasInvolved() {
|
||||
return InvolveBytes(b.dat[from:]...)
|
||||
}
|
||||
return ParseBytes(b.dat[from:]...)
|
||||
}
|
||||
nb := b.Ref()
|
||||
skip(nb.buf.Pointer(), from)
|
||||
nb.dat = b.dat[from:]
|
||||
return nb
|
||||
return Bytes{buf: b.buf, a: b.a + from, b: b.b}
|
||||
}
|
||||
|
||||
// SliceTo dat[:to] with Ref.
|
||||
func (b Bytes) SliceTo(to int) Bytes {
|
||||
if b.buf.IsTrans() {
|
||||
if b.buf.HasInvolved() {
|
||||
return InvolveBytes(b.dat[:to]...)
|
||||
}
|
||||
return ParseBytes(b.dat[:to]...)
|
||||
}
|
||||
nb := b.Ref()
|
||||
nb.buf.Pointer().Truncate(to)
|
||||
nb.dat = b.dat[:to]
|
||||
return nb
|
||||
return Bytes{buf: b.buf, a: b.a, b: b.a + to}
|
||||
}
|
||||
|
||||
// Slice dat[from:to] with Ref.
|
||||
func (b Bytes) Slice(from, to int) Bytes {
|
||||
if b.buf.IsTrans() {
|
||||
if b.buf.HasInvolved() {
|
||||
return InvolveBytes(b.dat[from:to]...)
|
||||
}
|
||||
return ParseBytes(b.dat[from:to]...)
|
||||
}
|
||||
nb := b.Ref()
|
||||
buf := nb.buf.Pointer()
|
||||
skip(buf, from)
|
||||
buf.Truncate(to - from)
|
||||
nb.dat = b.dat[from:to]
|
||||
return nb
|
||||
}
|
||||
|
||||
// KeepAlive marks Bytes value as reachable.
|
||||
func (b Bytes) KeepAlive() {
|
||||
_ = b.buf
|
||||
_ = b.dat
|
||||
runtime.KeepAlive(b.buf)
|
||||
runtime.KeepAlive(b.dat)
|
||||
return Bytes{buf: b.buf, a: b.a + from, b: b.a + to}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// manualDestroy please refer to Item.manualDestroy().
|
||||
//
|
||||
// Only for test purposes.
|
||||
func (b Bytes) manualDestroy() {
|
||||
b.buf.ManualDestroy()
|
||||
}
|
||||
|
||||
// TestBytesSlice sometimes fails at first run because
|
||||
// GC not collecting all unused items.
|
||||
func TestBytesSlice(t *testing.T) {
|
||||
@@ -26,27 +19,20 @@ func TestBytesSlice(t *testing.T) {
|
||||
if b.Len() != i {
|
||||
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
||||
}
|
||||
rand.Read(b.Bytes())
|
||||
b.V(func(b []byte) {
|
||||
rand.Read(b)
|
||||
})
|
||||
buf := make([]byte, b.Len())
|
||||
copy(buf, b.Bytes())
|
||||
// test normal slice
|
||||
y := b.SliceFrom(5)
|
||||
x := y.SliceTo(i - 5 - 5)
|
||||
if !bytes.Equal(buf[5:i-5], x.Bytes()) {
|
||||
b.V(func(b []byte) {
|
||||
copy(buf, b)
|
||||
})
|
||||
x := b.SliceFrom(5).SliceTo(i - 5 - 5)
|
||||
dat := x.Trans()
|
||||
if !bytes.Equal(buf[5:i-5], dat) {
|
||||
t.Log("exp:", hex.EncodeToString(buf[5:i-5]))
|
||||
t.Log("got:", hex.EncodeToString(x.Bytes()))
|
||||
t.Log("got:", hex.EncodeToString(dat))
|
||||
t.Fatal("index", i, "unexpected")
|
||||
}
|
||||
x.manualDestroy()
|
||||
y.manualDestroy()
|
||||
// test trans slice
|
||||
b = b.Trans().SliceFrom(5).SliceTo(i - 5 - 5)
|
||||
if !bytes.Equal(buf[5:i-5], b.Bytes()) {
|
||||
t.Log("exp:", hex.EncodeToString(buf[5:i-5]))
|
||||
t.Log("got:", hex.EncodeToString(b.Bytes()))
|
||||
t.Fatal("index", i, "unexpected")
|
||||
}
|
||||
b.manualDestroy()
|
||||
}
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
@@ -66,11 +52,12 @@ func TestBytesInvolve(t *testing.T) {
|
||||
if b.Len() != i {
|
||||
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
||||
}
|
||||
rand.Read(b.Bytes())
|
||||
if !bytes.Equal(b.Bytes(), buf[:i]) {
|
||||
b.V(func(b []byte) {
|
||||
rand.Read(b)
|
||||
})
|
||||
if !bytes.Equal(b.Trans(), buf[:i]) {
|
||||
t.Fatal("index", i, "unexpected")
|
||||
}
|
||||
b.manualDestroy()
|
||||
}
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
@@ -88,10 +75,9 @@ func TestBytesParse(t *testing.T) {
|
||||
if b.Len() != i {
|
||||
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
||||
}
|
||||
if !bytes.Equal(b.Bytes(), buf[:i]) {
|
||||
if !bytes.Equal(b.Trans(), buf[:i]) {
|
||||
t.Fatal("index", i, "unexpected")
|
||||
}
|
||||
b.manualDestroy()
|
||||
}
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
@@ -111,15 +97,12 @@ func TestBytesCopy(t *testing.T) {
|
||||
if b.Len() != i-10 {
|
||||
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
||||
}
|
||||
rand.Read(b.Bytes())
|
||||
// t.Log("org:", hex.EncodeToString(buf[:i]))
|
||||
// t.Log("new:", hex.EncodeToString(b.Bytes()))
|
||||
if bytes.Equal(b.Bytes(), buf[:i]) {
|
||||
b.V(func(b []byte) {
|
||||
rand.Read(b)
|
||||
})
|
||||
if bytes.Equal(b.Trans(), buf[:i]) {
|
||||
t.Fatal("index", i, "unexpected")
|
||||
}
|
||||
b.manualDestroy()
|
||||
x.manualDestroy()
|
||||
a.manualDestroy()
|
||||
}
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
@@ -141,15 +124,16 @@ func TestBytesTransMultithread(t *testing.T) {
|
||||
buf := NewBytes(65536)
|
||||
refer := make([]byte, 65536)
|
||||
rand.Read(refer)
|
||||
copy(buf.Bytes(), refer)
|
||||
buf.V(func(b []byte) {
|
||||
copy(b, refer)
|
||||
})
|
||||
wg.Add(1)
|
||||
go func(buf Bytes) {
|
||||
go func(buf []byte) {
|
||||
defer wg.Done()
|
||||
time.Sleep(time.Millisecond * time.Duration(mrand.Intn(10)))
|
||||
if !bytes.Equal(refer, buf.Bytes()) {
|
||||
if !bytes.Equal(refer, buf) {
|
||||
panic("unexpected")
|
||||
}
|
||||
buf.manualDestroy()
|
||||
}(buf.Trans())
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user