Browse Source

remove MultiBuffer.Release

pull/1435/head
Darien Raymond 6 years ago
parent
commit
0f324a613e
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 5
      app/dispatcher/default.go
  2. 2
      app/dispatcher/stats_test.go
  3. 32
      common/buf/multi_buffer.go
  4. 4
      common/buf/reader.go
  5. 6
      common/buf/readv_reader.go
  6. 8
      common/buf/writer.go
  7. 10
      common/crypto/auth.go
  8. 2
      common/mux/writer.go
  9. 4
      common/platform/ctlcmd/ctlcmd.go
  10. 8
      functions.go
  11. 3
      main/confloader/external/external.go
  12. 2
      proxy/shadowsocks/ota.go
  13. 2
      transport/internet/kcp/connection.go
  14. 3
      transport/internet/kcp/receiving.go
  15. 6
      transport/pipe/impl.go

5
app/dispatcher/default.go

@ -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)
@ -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()

2
app/dispatcher/stats_test.go

@ -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))

32
common/buf/multi_buffer.go

@ -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
}
@ -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
}
@ -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
@ -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 {
@ -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
}
@ -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 {

4
common/buf/reader.go

@ -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
@ -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)
}

6
common/buf/readv_reader.go

@ -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
}

8
common/buf/writer.go

@ -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 {
@ -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 {
@ -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() {
@ -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
}

10
common/crypto/auth.go

@ -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
}
@ -212,7 +212,7 @@ func (r *AuthenticationReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
break
}
if err != nil {
mb.Release()
buf.ReleaseMulti(mb)
return nil, err
}
}
@ -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 {
@ -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)
@ -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)

2
common/mux/writer.go

@ -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()

4
common/platform/ctlcmd/ctlcmd.go

@ -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)
}

8
functions.go

@ -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"
)
@ -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
}

3
main/confloader/external/external.go vendored

@ -17,7 +17,8 @@ type ClosableMultiBuffer struct {
}
func (c *ClosableMultiBuffer) Close() error {
c.MultiBuffer.Release()
buf.ReleaseMulti(c.MultiBuffer)
c.MultiBuffer = nil
return nil
}

2
proxy/shadowsocks/ota.go

@ -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:])

2
transport/internet/kcp/connection.go

@ -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() {

3
transport/internet/kcp/receiving.go

@ -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()
}

6
transport/pipe/impl.go

@ -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
}
@ -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
}

Loading…
Cancel
Save