Skip to content

Commit

Permalink
remove MultiBuffer.Release
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Nov 17, 2018
1 parent b41513f commit 0f324a6
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 51 deletions.
5 changes: 2 additions & 3 deletions app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r *cachedReader) Cache(b *buf.Buffer) {
mb, _ := r.reader.ReadMultiBufferTimeout(time.Millisecond * 100)
r.Lock()
if !mb.IsEmpty() {
common.Must(r.cache.WriteMultiBuffer(mb))
r.cache, _ = buf.MergeMulti(r.cache, mb)
}
b.Clear()
rawBytes := b.Extend(buf.Size)
Expand Down Expand Up @@ -75,8 +75,7 @@ func (r *cachedReader) ReadMultiBufferTimeout(timeout time.Duration) (buf.MultiB
func (r *cachedReader) CloseError() {
r.Lock()
if r.cache != nil {
r.cache.Release()
r.cache = nil
r.cache = buf.ReleaseMulti(r.cache)
}
r.Unlock()
r.reader.CloseError()
Expand Down
2 changes: 1 addition & 1 deletion app/dispatcher/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestStatsWriter(t *testing.T) {
common.Must2(mb.Write([]byte("abcd")))
common.Must(writer.WriteMultiBuffer(mb))

mb.Release()
mb = buf.ReleaseMulti(mb)
common.Must2(mb.Write([]byte("efg")))
common.Must(writer.WriteMultiBuffer(mb))

Expand Down
32 changes: 16 additions & 16 deletions common/buf/multi_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func ReadAllToMultiBuffer(reader io.Reader) (MultiBuffer, error) {
mb := make(MultiBuffer, 0, 128)

if _, err := mb.ReadFrom(reader); err != nil {
mb.Release()
ReleaseMulti(mb)
return nil, err
}

Expand All @@ -31,7 +31,7 @@ func ReadAllToBytes(reader io.Reader) ([]byte, error) {
}
b := make([]byte, mb.Len())
common.Must2(mb.Read(b))
mb.Release()
ReleaseMulti(mb)
return b, nil
}

Expand All @@ -47,6 +47,15 @@ func MergeMulti(dest MultiBuffer, src MultiBuffer) (MultiBuffer, MultiBuffer) {
return dest, src[:0]
}

// ReleaseMulti release all content of the MultiBuffer, and returns an empty MultiBuffer.
func ReleaseMulti(mb MultiBuffer) MultiBuffer {
for i := range mb {
mb[i].Release()
mb[i] = nil
}
return mb[:0]
}

// Copy copied the beginning part of the MultiBuffer into the given byte array.
func (mb MultiBuffer) Copy(b []byte) int {
total := 0
Expand Down Expand Up @@ -107,7 +116,9 @@ func (mb *MultiBuffer) Read(b []byte) (int, error) {

// WriteTo implements io.WriterTo.
func (mb *MultiBuffer) WriteTo(writer io.Writer) (int64, error) {
defer mb.Release()
defer func() {
*mb = ReleaseMulti(*mb)
}()

totalBytes := int64(0)
for _, b := range *mb {
Expand Down Expand Up @@ -143,10 +154,7 @@ func (mb *MultiBuffer) Write(b []byte) (int, error) {

// WriteMultiBuffer implements Writer.
func (mb *MultiBuffer) WriteMultiBuffer(b MultiBuffer) error {
*mb = append(*mb, b...)
for i := range b {
b[i] = nil
}
*mb, _ = MergeMulti(*mb, b)
return nil
}

Expand All @@ -173,15 +181,7 @@ func (mb MultiBuffer) IsEmpty() bool {
return true
}

// Release releases all Buffers in the MultiBuffer.
func (mb *MultiBuffer) Release() {
for i, b := range *mb {
b.Release()
(*mb)[i] = nil
}
*mb = nil
}

// String returns the content of the MultiBuffer in string.
func (mb MultiBuffer) String() string {
v := make([]interface{}, len(mb))
for i, b := range mb {
Expand Down
4 changes: 2 additions & 2 deletions common/buf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (r *BufferedReader) Read(b []byte) (int, error) {
nBytes, err := r.Buffer.Read(b)
common.Must(err)
if r.Buffer.IsEmpty() {
r.Buffer.Release()
r.Buffer = nil
}
return nBytes, nil
Expand Down Expand Up @@ -123,7 +122,8 @@ func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error) {
// Close implements io.Closer.
func (r *BufferedReader) Close() error {
if !r.Buffer.IsEmpty() {
r.Buffer.Release()
ReleaseMulti(r.Buffer)
r.Buffer = nil
}
return common.Close(r.Reader)
}
Expand Down
6 changes: 2 additions & 4 deletions common/buf/readv_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ func (r *ReadVReader) readMulti() (MultiBuffer, error) {
r.mr.Clear()

if err != nil {
mb := MultiBuffer(bs)
mb.Release()
ReleaseMulti(MultiBuffer(bs))
return nil, err
}

if nBytes == 0 {
mb := MultiBuffer(bs)
mb.Release()
ReleaseMulti(MultiBuffer(bs))
return nil, io.EOF
}

Expand Down
8 changes: 4 additions & 4 deletions common/buf/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type BufferToBytesWriter struct {

// WriteMultiBuffer implements Writer. This method takes ownership of the given buffer.
func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
defer mb.Release()
defer ReleaseMulti(mb)

size := mb.Len()
if size == 0 {
Expand Down Expand Up @@ -134,7 +134,7 @@ func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
return w.writer.WriteMultiBuffer(b)
}

defer b.Release()
defer ReleaseMulti(b)

for !b.IsEmpty() {
if w.buffer == nil {
Expand Down Expand Up @@ -216,7 +216,7 @@ type SequentialWriter struct {

// WriteMultiBuffer implements Writer.
func (w *SequentialWriter) WriteMultiBuffer(mb MultiBuffer) error {
defer mb.Release()
defer ReleaseMulti(mb)

for _, b := range mb {
if b.IsEmpty() {
Expand All @@ -234,7 +234,7 @@ func (w *SequentialWriter) WriteMultiBuffer(mb MultiBuffer) error {
type noOpWriter byte

func (noOpWriter) WriteMultiBuffer(b MultiBuffer) error {
b.Release()
ReleaseMulti(b)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions common/crypto/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
const readSize = 16
mb := make(buf.MultiBuffer, 0, readSize)
if err := r.readInternal(false, &mb); err != nil {
mb.Release()
buf.ReleaseMulti(mb)
return nil, err
}

Expand All @@ -212,7 +212,7 @@ func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
break
}
if err != nil {
mb.Release()
buf.ReleaseMulti(mb)
return nil, err
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func (w *AuthenticationWriter) seal(b []byte) (*buf.Buffer, error) {
}

func (w *AuthenticationWriter) writeStream(mb buf.MultiBuffer) error {
defer mb.Release()
defer buf.ReleaseMulti(mb)

var maxPadding int32
if w.padding != nil {
Expand All @@ -286,7 +286,7 @@ func (w *AuthenticationWriter) writeStream(mb buf.MultiBuffer) error {
b.Release()

if err != nil {
mb2Write.Release()
buf.ReleaseMulti(mb2Write)
return err
}
mb2Write = append(mb2Write, eb)
Expand All @@ -299,7 +299,7 @@ func (w *AuthenticationWriter) writeStream(mb buf.MultiBuffer) error {
}

func (w *AuthenticationWriter) writePacket(mb buf.MultiBuffer) error {
defer mb.Release()
defer buf.ReleaseMulti(mb)

mb2Write := make(buf.MultiBuffer, 0, len(mb)+1)

Expand Down
2 changes: 1 addition & 1 deletion common/mux/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (w *Writer) writeData(mb buf.MultiBuffer) error {

// WriteMultiBuffer implements buf.Writer.
func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release()
defer buf.ReleaseMulti(mb)

if mb.IsEmpty() {
return w.writeMetaOnly()
Expand Down
4 changes: 2 additions & 2 deletions common/platform/ctlcmd/ctlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func Run(args []string, input io.Reader) (buf.MultiBuffer, error) {
if errBuffer.Len() > 0 {
msg += ": " + errBuffer.String()
}
errBuffer.Release()
outBuffer.Release()
buf.ReleaseMulti(errBuffer)
buf.ReleaseMulti(outBuffer)
return nil, newError(msg).Base(err)
}

Expand Down
8 changes: 2 additions & 6 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package core

import (
"bytes"
"context"

"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/features/routing"
)
Expand All @@ -21,11 +21,7 @@ func CreateObject(v *Instance, config interface{}) (interface{}, error) {
// StartInstance starts a new V2Ray instance with given serialized config.
// By default V2Ray only support config in protobuf format, i.e., configFormat = "protobuf". Caller need to load other packages to add JSON support.
func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
var mb buf.MultiBuffer
defer mb.Release()

common.Must2(mb.Write(configBytes))
config, err := LoadConfig(configFormat, "", &mb)
config, err := LoadConfig(configFormat, "", bytes.NewReader(configBytes))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion main/confloader/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type ClosableMultiBuffer struct {
}

func (c *ClosableMultiBuffer) Close() error {
c.MultiBuffer.Release()
buf.ReleaseMulti(c.MultiBuffer)
c.MultiBuffer = nil
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion proxy/shadowsocks/ota.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {

// WriteMultiBuffer implements buf.Writer.
func (w *ChunkWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release()
defer buf.ReleaseMulti(mb)

for {
payloadLen, _ := mb.Read(w.buffer[2+AuthSize:])
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/kcp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (c *Connection) Write(b []byte) (int, error) {

// WriteMultiBuffer implements buf.Writer.
func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release()
defer buf.ReleaseMulti(mb)

updatePending := false
defer func() {
Expand Down
3 changes: 2 additions & 1 deletion transport/internet/kcp/receiving.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ func NewReceivingWorker(kcp *Connection) *ReceivingWorker {

func (w *ReceivingWorker) Release() {
w.Lock()
w.leftOver.Release()
buf.ReleaseMulti(w.leftOver)
w.leftOver = nil
w.Unlock()
}

Expand Down
6 changes: 3 additions & 3 deletions transport/pipe/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ func (p *pipe) WriteMultiBuffer(mb buf.MultiBuffer) error {
runtime.Gosched()
return nil
case err == errBufferFull && p.option.discardOverflow:
mb.Release()
buf.ReleaseMulti(mb)
return nil
case err != errBufferFull:
mb.Release()
buf.ReleaseMulti(mb)
p.readSignal.Signal()
return err
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (p *pipe) CloseError() {
p.state = errord

if !p.data.IsEmpty() {
p.data.Release()
buf.ReleaseMulti(p.data)
p.data = nil
}

Expand Down

0 comments on commit 0f324a6

Please sign in to comment.