Procházet zdrojové kódy

rename alloc to buf

Darien Raymond před 9 roky
rodič
revize
7a80409e30
60 změnil soubory, kde provedl 199 přidání a 196 odebrání
  1. 2 2
      app/dispatcher/impl/default.go
  2. 2 2
      app/dispatcher/testing/dispatcher.go
  3. 5 5
      app/dns/nameserver.go
  4. 1 1
      common/buf/buffer.go
  5. 1 1
      common/buf/buffer_pool.go
  6. 2 2
      common/buf/buffer_test.go
  7. 3 3
      common/crypto/auth.go
  8. 3 3
      common/io/buffered_reader.go
  9. 2 2
      common/io/buffered_reader_test.go
  10. 3 3
      common/io/buffered_writer.go
  11. 3 3
      common/io/buffered_writer_test.go
  12. 2 2
      common/io/chain_writer.go
  13. 2 2
      common/io/chan_reader.go
  14. 7 7
      common/io/reader.go
  15. 3 3
      common/io/reader_test.go
  16. 3 3
      common/io/writer.go
  17. 2 2
      common/io/writer_test.go
  18. 3 2
      common/serial/hash.go
  19. 4 3
      common/serial/numbers.go
  20. 3 2
      common/serial/string.go
  21. 2 2
      proxy/blackhole/blackhole.go
  22. 2 2
      proxy/blackhole/config.go
  23. 2 2
      proxy/blackhole/config_test.go
  24. 3 3
      proxy/dokodemo/dokodemo.go
  25. 2 2
      proxy/freedom/freedom.go
  26. 2 2
      proxy/freedom/freedom_test.go
  27. 2 2
      proxy/proxy.go
  28. 2 2
      proxy/shadowsocks/client.go
  29. 7 7
      proxy/shadowsocks/ota.go
  30. 4 4
      proxy/shadowsocks/ota_test.go
  31. 9 9
      proxy/shadowsocks/protocol.go
  32. 7 7
      proxy/shadowsocks/protocol_test.go
  33. 3 3
      proxy/shadowsocks/server.go
  34. 3 3
      proxy/socks/protocol/socks.go
  35. 2 2
      proxy/socks/protocol/socks4_test.go
  36. 10 10
      proxy/socks/protocol/socks_test.go
  37. 4 4
      proxy/socks/protocol/udp.go
  38. 4 4
      proxy/socks/server_udp.go
  39. 2 2
      proxy/testing/mocks/outboundhandler.go
  40. 2 2
      proxy/vmess/encoding/commands.go
  41. 2 2
      proxy/vmess/encoding/commands_test.go
  42. 2 2
      proxy/vmess/encoding/encoding_test.go
  43. 2 2
      proxy/vmess/inbound/inbound.go
  44. 4 4
      proxy/vmess/outbound/outbound.go
  45. 2 2
      testing/scenarios/shadowsocks_test.go
  46. 5 5
      transport/internet/authenticator.go
  47. 10 10
      transport/internet/headers/http/http.go
  48. 4 4
      transport/internet/headers/http/http_test.go
  49. 2 2
      transport/internet/headers/srtp/srtp_test.go
  50. 2 2
      transport/internet/headers/utp/utp_test.go
  51. 2 2
      transport/internet/kcp/dialer.go
  52. 2 2
      transport/internet/kcp/listener.go
  53. 3 3
      transport/internet/kcp/output.go
  54. 2 2
      transport/internet/kcp/receiving.go
  55. 7 7
      transport/internet/kcp/segment.go
  56. 2 2
      transport/internet/kcp/segment_test.go
  57. 4 4
      transport/internet/udp/hub.go
  58. 2 2
      transport/internet/udp/hub_linux_test.go
  59. 4 4
      transport/internet/udp/udp_server.go
  60. 6 6
      transport/ray/direct.go

+ 2 - 2
app/dispatcher/impl/default.go

@@ -4,7 +4,7 @@ import (
 	"v2ray.com/core/app"
 	"v2ray.com/core/app/proxyman"
 	"v2ray.com/core/app/router"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -62,7 +62,7 @@ func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.I
 	}
 
 	if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
-		go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32), direct)
+		go dispatcher.Dispatch(destination, buf.NewLocalBuffer(32), direct)
 	} else {
 		go v.FilterPacketAndDispatch(destination, direct, dispatcher)
 	}

+ 2 - 2
app/dispatcher/testing/dispatcher.go

@@ -1,7 +1,7 @@
 package testing
 
 import (
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/transport/ray"
@@ -20,7 +20,7 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic
 				if err != nil {
 					break
 				}
-				output := alloc.NewBuffer()
+				output := buf.NewBuffer()
 				output.Append([]byte("Processed: "))
 				output.Append(payload.Bytes())
 				payload.Release()

+ 5 - 5
app/dns/nameserver.go

@@ -6,7 +6,7 @@ import (
 	"time"
 
 	"v2ray.com/core/app/dispatcher"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/dice"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -101,7 +101,7 @@ func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
 }
 
 // Private: Visible for testing.
-func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) {
+func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *buf.Buffer) {
 	msg := new(dns.Msg)
 	err := msg.Unpack(payload.Bytes())
 	if err != nil {
@@ -144,8 +144,8 @@ func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Bu
 	close(request.response)
 }
 
-func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer {
-	buffer := alloc.NewBuffer()
+func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *buf.Buffer {
+	buffer := buf.NewBuffer()
 	msg := new(dns.Msg)
 	msg.Id = id
 	msg.RecursionDesired = true
@@ -162,7 +162,7 @@ func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer {
 	return buffer
 }
 
-func (v *UDPNameServer) DispatchQuery(payload *alloc.Buffer) {
+func (v *UDPNameServer) DispatchQuery(payload *buf.Buffer) {
 	v.udpServer.Dispatch(&proxy.SessionInfo{Source: pseudoDestination, Destination: v.address}, payload, v.HandleResponse)
 }
 

+ 1 - 1
common/alloc/buffer.go → common/buf/buffer.go

@@ -1,5 +1,5 @@
 // Package alloc provides a light-weight memory allocation mechanism.
-package alloc
+package buf
 
 import (
 	"io"

+ 1 - 1
common/alloc/buffer_pool.go → common/buf/buffer_pool.go

@@ -1,4 +1,4 @@
-package alloc
+package buf
 
 import (
 	"os"

+ 2 - 2
common/alloc/buffer_test.go → common/buf/buffer_test.go

@@ -1,9 +1,9 @@
-package alloc_test
+package buf_test
 
 import (
 	"testing"
 
-	. "v2ray.com/core/common/alloc"
+	. "v2ray.com/core/common/buf"
 	"v2ray.com/core/common/serial"
 	"v2ray.com/core/testing/assert"
 )

+ 3 - 3
common/crypto/auth.go

@@ -5,7 +5,7 @@ import (
 	"errors"
 	"io"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/serial"
 )
 
@@ -71,7 +71,7 @@ func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
 
 type AuthenticationReader struct {
 	auth   Authenticator
-	buffer *alloc.Buffer
+	buffer *buf.Buffer
 	reader io.Reader
 
 	chunk      []byte
@@ -81,7 +81,7 @@ type AuthenticationReader struct {
 func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader {
 	return &AuthenticationReader{
 		auth:       auth,
-		buffer:     alloc.NewLocalBuffer(32 * 1024),
+		buffer:     buf.NewLocalBuffer(32 * 1024),
 		reader:     reader,
 		aggressive: aggressive,
 	}

+ 3 - 3
common/io/buffered_reader.go

@@ -4,20 +4,20 @@ import (
 	"io"
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type BufferedReader struct {
 	sync.Mutex
 	reader io.Reader
-	buffer *alloc.Buffer
+	buffer *buf.Buffer
 	cached bool
 }
 
 func NewBufferedReader(rawReader io.Reader) *BufferedReader {
 	return &BufferedReader{
 		reader: rawReader,
-		buffer: alloc.NewBuffer(),
+		buffer: buf.NewBuffer(),
 		cached: true,
 	}
 }

+ 2 - 2
common/io/buffered_reader_test.go

@@ -4,7 +4,7 @@ import (
 	"testing"
 
 	"crypto/rand"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/common/io"
 	"v2ray.com/core/testing/assert"
 )
@@ -12,7 +12,7 @@ import (
 func TestBufferedReader(t *testing.T) {
 	assert := assert.On(t)
 
-	content := alloc.NewBuffer()
+	content := buf.NewBuffer()
 	content.FillFrom(rand.Reader)
 
 	len := content.Len()

+ 3 - 3
common/io/buffered_writer.go

@@ -3,21 +3,21 @@ package io
 import (
 	"io"
 	"sync"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 )
 
 type BufferedWriter struct {
 	sync.Mutex
 	writer io.Writer
-	buffer *alloc.Buffer
+	buffer *buf.Buffer
 	cached bool
 }
 
 func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
 	return &BufferedWriter{
 		writer: rawWriter,
-		buffer: alloc.NewSmallBuffer(),
+		buffer: buf.NewSmallBuffer(),
 		cached: true,
 	}
 }

+ 3 - 3
common/io/buffered_writer_test.go

@@ -4,7 +4,7 @@ import (
 	"crypto/rand"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/common/io"
 	"v2ray.com/core/testing/assert"
 )
@@ -12,7 +12,7 @@ import (
 func TestBufferedWriter(t *testing.T) {
 	assert := assert.On(t)
 
-	content := alloc.NewBuffer()
+	content := buf.NewBuffer()
 
 	writer := NewBufferedWriter(content)
 	assert.Bool(writer.Cached()).IsTrue()
@@ -32,7 +32,7 @@ func TestBufferedWriter(t *testing.T) {
 func TestBufferedWriterLargePayload(t *testing.T) {
 	assert := assert.On(t)
 
-	content := alloc.NewLocalBuffer(128 * 1024)
+	content := buf.NewLocalBuffer(128 * 1024)
 
 	writer := NewBufferedWriter(content)
 	assert.Bool(writer.Cached()).IsTrue()

+ 2 - 2
common/io/chain_writer.go

@@ -4,7 +4,7 @@ import (
 	"io"
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type ChainWriter struct {
@@ -28,7 +28,7 @@ func (v *ChainWriter) Write(payload []byte) (int, error) {
 	bytesWritten := 0
 	size := len(payload)
 	for size > 0 {
-		buffer := alloc.NewBuffer()
+		buffer := buf.NewBuffer()
 		nBytes, _ := buffer.Write(payload)
 		size -= nBytes
 		payload = payload[nBytes:]

+ 2 - 2
common/io/chan_reader.go

@@ -4,13 +4,13 @@ import (
 	"io"
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type ChanReader struct {
 	sync.Mutex
 	stream  Reader
-	current *alloc.Buffer
+	current *buf.Buffer
 	eof     bool
 }
 

+ 7 - 7
common/io/reader.go

@@ -4,20 +4,20 @@ import (
 	"io"
 
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 // Reader extends io.Reader with alloc.Buffer.
 type Reader interface {
 	common.Releasable
 	// Read reads content from underlying reader, and put it into an alloc.Buffer.
-	Read() (*alloc.Buffer, error)
+	Read() (*buf.Buffer, error)
 }
 
 // AdaptiveReader is a Reader that adjusts its reading speed automatically.
 type AdaptiveReader struct {
 	reader      io.Reader
-	largeBuffer *alloc.Buffer
+	largeBuffer *buf.Buffer
 	highVolumn  bool
 }
 
@@ -30,21 +30,21 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
 }
 
 // Read implements Reader.Read().
-func (v *AdaptiveReader) Read() (*alloc.Buffer, error) {
+func (v *AdaptiveReader) Read() (*buf.Buffer, error) {
 	if v.highVolumn && v.largeBuffer.IsEmpty() {
 		if v.largeBuffer == nil {
-			v.largeBuffer = alloc.NewLocalBuffer(32 * 1024)
+			v.largeBuffer = buf.NewLocalBuffer(32 * 1024)
 		}
 		nBytes, err := v.largeBuffer.FillFrom(v.reader)
 		if err != nil {
 			return nil, err
 		}
-		if nBytes < alloc.BufferSize {
+		if nBytes < buf.BufferSize {
 			v.highVolumn = false
 		}
 	}
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	if !v.largeBuffer.IsEmpty() {
 		buffer.FillFrom(v.largeBuffer)
 		return buffer, nil

+ 3 - 3
common/io/reader_test.go

@@ -4,7 +4,7 @@ import (
 	"bytes"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/common/io"
 	"v2ray.com/core/testing/assert"
 )
@@ -19,8 +19,8 @@ func TestAdaptiveReader(t *testing.T) {
 	b1, err := reader.Read()
 	assert.Error(err).IsNil()
 	assert.Bool(b1.IsFull()).IsTrue()
-	assert.Int(b1.Len()).Equals(alloc.BufferSize)
-	assert.Int(buffer.Len()).Equals(cap(rawContent) - alloc.BufferSize)
+	assert.Int(b1.Len()).Equals(buf.BufferSize)
+	assert.Int(buffer.Len()).Equals(cap(rawContent) - buf.BufferSize)
 
 	b2, err := reader.Read()
 	assert.Error(err).IsNil()

+ 3 - 3
common/io/writer.go

@@ -4,14 +4,14 @@ import (
 	"io"
 
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 // Writer extends io.Writer with alloc.Buffer.
 type Writer interface {
 	common.Releasable
 	// Write writes an alloc.Buffer into underlying writer.
-	Write(*alloc.Buffer) error
+	Write(*buf.Buffer) error
 }
 
 // AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
@@ -27,7 +27,7 @@ func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter {
 }
 
 // Write implements Writer.Write(). Write() takes ownership of the given buffer.
-func (v *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
+func (v *AdaptiveWriter) Write(buffer *buf.Buffer) error {
 	defer buffer.Release()
 	for {
 		nBytes, err := v.writer.Write(buffer.Bytes())

+ 2 - 2
common/io/writer_test.go

@@ -5,7 +5,7 @@ import (
 	"crypto/rand"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/common/io"
 	"v2ray.com/core/testing/assert"
 )
@@ -13,7 +13,7 @@ import (
 func TestAdaptiveWriter(t *testing.T) {
 	assert := assert.On(t)
 
-	lb := alloc.NewBuffer()
+	lb := buf.NewBuffer()
 	lb.FillFrom(rand.Reader)
 
 	expectedBytes := append([]byte(nil), lb.Bytes()...)

+ 3 - 2
common/serial/hash.go

@@ -2,10 +2,11 @@ package serial
 
 import (
 	"hash"
-	"v2ray.com/core/common/alloc"
+
+	"v2ray.com/core/common/buf"
 )
 
-func WriteHash(h hash.Hash) alloc.BytesWriter {
+func WriteHash(h hash.Hash) buf.BytesWriter {
 	return func(b []byte) int {
 		h.Sum(b[:0])
 		return h.Size()

+ 4 - 3
common/serial/numbers.go

@@ -2,7 +2,8 @@ package serial
 
 import (
 	"strconv"
-	"v2ray.com/core/common/alloc"
+
+	"v2ray.com/core/common/buf"
 )
 
 func Uint16ToBytes(value uint16, b []byte) []byte {
@@ -13,7 +14,7 @@ func Uint16ToString(value uint16) string {
 	return strconv.Itoa(int(value))
 }
 
-func WriteUint16(value uint16) alloc.BytesWriter {
+func WriteUint16(value uint16) buf.BytesWriter {
 	return func(b []byte) int {
 		b = Uint16ToBytes(value, b[:0])
 		return 2
@@ -28,7 +29,7 @@ func Uint32ToString(value uint32) string {
 	return strconv.FormatUint(uint64(value), 10)
 }
 
-func WriteUint32(value uint32) alloc.BytesWriter {
+func WriteUint32(value uint32) buf.BytesWriter {
 	return func(b []byte) int {
 		b = Uint32ToBytes(value, b[:0])
 		return 4

+ 3 - 2
common/serial/string.go

@@ -3,7 +3,8 @@ package serial
 import (
 	"fmt"
 	"strings"
-	"v2ray.com/core/common/alloc"
+
+	"v2ray.com/core/common/buf"
 )
 
 func ToString(v interface{}) string {
@@ -35,7 +36,7 @@ func Concat(v ...interface{}) string {
 	return strings.Join(values, "")
 }
 
-func WriteString(s string) alloc.BytesWriter {
+func WriteString(s string) buf.BytesWriter {
 	return func(b []byte) int {
 		copy(b, []byte(s))
 		return len(s)

+ 2 - 2
proxy/blackhole/blackhole.go

@@ -2,7 +2,7 @@ package blackhole
 
 import (
 	"v2ray.com/core/app"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/transport/ray"
@@ -25,7 +25,7 @@ func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMe
 	}, nil
 }
 
-func (v *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
+func (v *BlackHole) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
 	payload.Release()
 
 	v.response.WriteTo(ray.OutboundOutput())

+ 2 - 2
proxy/blackhole/config.go

@@ -1,7 +1,7 @@
 package blackhole
 
 import (
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2io "v2ray.com/core/common/io"
 
 	"github.com/golang/protobuf/ptypes"
@@ -32,7 +32,7 @@ func (v *NoneResponse) AsAny() *any.Any {
 }
 
 func (v *HTTPResponse) WriteTo(writer v2io.Writer) {
-	b := alloc.NewLocalBuffer(512)
+	b := buf.NewLocalBuffer(512)
 	b.AppendFunc(serial.WriteString(http403response))
 	writer.Write(b)
 }

+ 2 - 2
proxy/blackhole/config_test.go

@@ -5,7 +5,7 @@ import (
 	"net/http"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2io "v2ray.com/core/common/io"
 	. "v2ray.com/core/proxy/blackhole"
 	"v2ray.com/core/testing/assert"
@@ -14,7 +14,7 @@ import (
 func TestHTTPResponse(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 
 	httpResponse := new(HTTPResponse)
 	httpResponse.WriteTo(v2io.NewAdaptiveWriter(buffer))

+ 3 - 3
proxy/dokodemo/dokodemo.go

@@ -5,7 +5,7 @@ import (
 
 	"v2ray.com/core/app"
 	"v2ray.com/core/app/dispatcher"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	v2io "v2ray.com/core/common/io"
 	"v2ray.com/core/common/loader"
@@ -107,7 +107,7 @@ func (v *DokodemoDoor) ListenUDP() error {
 	return nil
 }
 
-func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) {
+func (v *DokodemoDoor) handleUDPPackets(payload *buf.Buffer, session *proxy.SessionInfo) {
 	if session.Destination.Network == v2net.Network_Unknown && v.address != nil && v.port > 0 {
 		session.Destination = v2net.UDPDestination(v.address, v.port)
 	}
@@ -119,7 +119,7 @@ func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.Se
 	v.udpServer.Dispatch(session, payload, v.handleUDPResponse)
 }
 
-func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
+func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *buf.Buffer) {
 	defer payload.Release()
 	v.udpMutex.RLock()
 	defer v.udpMutex.RUnlock()

+ 2 - 2
proxy/freedom/freedom.go

@@ -4,7 +4,7 @@ import (
 	"io"
 	"v2ray.com/core/app"
 	"v2ray.com/core/app/dns"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/dice"
 	"v2ray.com/core/common/errors"
 	v2io "v2ray.com/core/common/io"
@@ -67,7 +67,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti
 	return newDest
 }
 
-func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
+func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
 	log.Info("Freedom: Opening connection to ", destination)
 
 	defer payload.Release()

+ 2 - 2
proxy/freedom/freedom_test.go

@@ -9,7 +9,7 @@ import (
 	"v2ray.com/core/app/dns"
 	"v2ray.com/core/app/proxyman"
 	"v2ray.com/core/app/router"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	. "v2ray.com/core/proxy/freedom"
@@ -47,7 +47,7 @@ func TestSinglePacket(t *testing.T) {
 
 	traffic := ray.NewRay()
 	data2Send := "Data to be sent to remote"
-	payload := alloc.NewLocalBuffer(2048)
+	payload := buf.NewLocalBuffer(2048)
 	payload.Append([]byte(data2Send))
 
 	go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic)

+ 2 - 2
proxy/proxy.go

@@ -2,7 +2,7 @@
 package proxy
 
 import (
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
 	"v2ray.com/core/transport/internet"
@@ -58,5 +58,5 @@ type InboundHandler interface {
 // An OutboundHandler handles outbound network connection for V2Ray.
 type OutboundHandler interface {
 	// Dispatch sends one or more Packets to its destination.
-	Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay)
+	Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay)
 }

+ 2 - 2
proxy/shadowsocks/client.go

@@ -3,7 +3,7 @@ package shadowsocks
 import (
 	"sync"
 	"v2ray.com/core/app"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2io "v2ray.com/core/common/io"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -32,7 +32,7 @@ func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandle
 	return client, nil
 }
 
-func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
+func (v *Client) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
 	defer payload.Release()
 	defer ray.OutboundInput().Release()
 	defer ray.OutboundOutput().Close()

+ 7 - 7
proxy/shadowsocks/ota.go

@@ -6,7 +6,7 @@ import (
 	"crypto/sha1"
 	"io"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/serial"
 )
@@ -27,7 +27,7 @@ func NewAuthenticator(keygen KeyGenerator) *Authenticator {
 	}
 }
 
-func (v *Authenticator) Authenticate(data []byte) alloc.BytesWriter {
+func (v *Authenticator) Authenticate(data []byte) buf.BytesWriter {
 	hasher := hmac.New(sha1.New, v.key())
 	hasher.Write(data)
 	res := hasher.Sum(nil)
@@ -74,8 +74,8 @@ func (v *ChunkReader) Release() {
 	v.auth = nil
 }
 
-func (v *ChunkReader) Read() (*alloc.Buffer, error) {
-	buffer := alloc.NewBuffer()
+func (v *ChunkReader) Read() (*buf.Buffer, error) {
+	buffer := buf.NewBuffer()
 	if _, err := buffer.FillFullFrom(v.reader, 2); err != nil {
 		buffer.Release()
 		return nil, err
@@ -83,10 +83,10 @@ func (v *ChunkReader) Read() (*alloc.Buffer, error) {
 	// There is a potential buffer overflow here. Large buffer is 64K bytes,
 	// while uin16 + 10 will be more than that
 	length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
-	if length > alloc.BufferSize {
+	if length > buf.BufferSize {
 		// Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
 		buffer.Release()
-		buffer = alloc.NewLocalBuffer(int(length) + 128)
+		buffer = buf.NewLocalBuffer(int(length) + 128)
 	}
 
 	buffer.Clear()
@@ -128,7 +128,7 @@ func (v *ChunkWriter) Release() {
 	v.auth = nil
 }
 
-func (v *ChunkWriter) Write(payload *alloc.Buffer) error {
+func (v *ChunkWriter) Write(payload *buf.Buffer) error {
 	totalLength := payload.Len()
 	serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0])
 	v.auth.Authenticate(payload.Bytes())(v.buffer[2:])

+ 4 - 4
proxy/shadowsocks/ota_test.go

@@ -3,7 +3,7 @@ package shadowsocks_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	. "v2ray.com/core/proxy/shadowsocks"
 	"v2ray.com/core/testing/assert"
 )
@@ -11,7 +11,7 @@ import (
 func TestNormalChunkReading(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	buffer.AppendBytes(
 		0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18)
 	reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
@@ -24,11 +24,11 @@ func TestNormalChunkReading(t *testing.T) {
 func TestNormalChunkWriting(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewLocalBuffer(512)
+	buffer := buf.NewLocalBuffer(512)
 	writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator(
 		[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
 
-	b := alloc.NewLocalBuffer(256)
+	b := buf.NewLocalBuffer(256)
 	b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18})
 	err := writer.Write(b)
 	assert.Error(err).IsNil()

+ 9 - 9
proxy/shadowsocks/protocol.go

@@ -4,7 +4,7 @@ import (
 	"bytes"
 	"crypto/rand"
 	"io"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/crypto"
 	"v2ray.com/core/common/errors"
 	v2io "v2ray.com/core/common/io"
@@ -29,7 +29,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
 	}
 	account := rawAccount.(*ShadowsocksAccount)
 
-	buffer := alloc.NewLocalBuffer(512)
+	buffer := buf.NewLocalBuffer(512)
 	defer buffer.Release()
 
 	ivLen := account.Cipher.IVSize()
@@ -152,7 +152,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
 
 	writer = crypto.NewCryptionWriter(stream, writer)
 
-	header := alloc.NewLocalBuffer(512)
+	header := buf.NewLocalBuffer(512)
 
 	switch request.Address.Family() {
 	case v2net.AddressFamilyIPv4:
@@ -235,7 +235,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (v2io.W
 	return v2io.NewAdaptiveWriter(crypto.NewCryptionWriter(stream, writer)), nil
 }
 
-func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*alloc.Buffer, error) {
+func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf.Buffer, error) {
 	user := request.User
 	rawAccount, err := user.GetTypedAccount()
 	if err != nil {
@@ -243,7 +243,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a
 	}
 	account := rawAccount.(*ShadowsocksAccount)
 
-	buffer := alloc.NewSmallBuffer()
+	buffer := buf.NewSmallBuffer()
 	ivLen := account.Cipher.IVSize()
 	buffer.FillFullFrom(rand.Reader, ivLen)
 	iv := buffer.Bytes()
@@ -281,7 +281,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a
 	return buffer, nil
 }
 
-func DecodeUDPPacket(user *protocol.User, payload *alloc.Buffer) (*protocol.RequestHeader, *alloc.Buffer, error) {
+func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
 	rawAccount, err := user.GetTypedAccount()
 	if err != nil {
 		return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.")
@@ -359,8 +359,8 @@ type UDPReader struct {
 	User   *protocol.User
 }
 
-func (v *UDPReader) Read() (*alloc.Buffer, error) {
-	buffer := alloc.NewSmallBuffer()
+func (v *UDPReader) Read() (*buf.Buffer, error) {
+	buffer := buf.NewSmallBuffer()
 	_, err := buffer.FillFrom(v.Reader)
 	if err != nil {
 		buffer.Release()
@@ -382,7 +382,7 @@ type UDPWriter struct {
 	Request *protocol.RequestHeader
 }
 
-func (v *UDPWriter) Write(buffer *alloc.Buffer) error {
+func (v *UDPWriter) Write(buffer *buf.Buffer) error {
 	payload, err := EncodeUDPPacket(v.Request, buffer)
 	if err != nil {
 		return err

+ 7 - 7
proxy/shadowsocks/protocol_test.go

@@ -3,7 +3,7 @@ package shadowsocks_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/loader"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
@@ -30,7 +30,7 @@ func TestUDPEncoding(t *testing.T) {
 		},
 	}
 
-	data := alloc.NewLocalBuffer(256)
+	data := buf.NewLocalBuffer(256)
 	data.AppendFunc(serial.WriteString("test string"))
 	encodedData, err := EncodeUDPPacket(request, data)
 	assert.Error(err).IsNil()
@@ -60,9 +60,9 @@ func TestTCPRequest(t *testing.T) {
 		},
 	}
 
-	data := alloc.NewLocalBuffer(256)
+	data := buf.NewLocalBuffer(256)
 	data.AppendFunc(serial.WriteString("test string"))
-	cache := alloc.NewBuffer()
+	cache := buf.NewBuffer()
 
 	writer, err := WriteTCPRequest(request, cache)
 	assert.Error(err).IsNil()
@@ -88,7 +88,7 @@ func TestUDPReaderWriter(t *testing.T) {
 			CipherType: CipherType_CHACHA20_IEFT,
 		}),
 	}
-	cache := alloc.NewBuffer()
+	cache := buf.NewBuffer()
 	writer := &UDPWriter{
 		Writer: cache,
 		Request: &protocol.RequestHeader{
@@ -105,7 +105,7 @@ func TestUDPReaderWriter(t *testing.T) {
 		User:   user,
 	}
 
-	b := alloc.NewBuffer()
+	b := buf.NewBuffer()
 	b.AppendFunc(serial.WriteString("test payload"))
 	err := writer.Write(b)
 	assert.Error(err).IsNil()
@@ -114,7 +114,7 @@ func TestUDPReaderWriter(t *testing.T) {
 	assert.Error(err).IsNil()
 	assert.String(payload.String()).Equals("test payload")
 
-	b = alloc.NewBuffer()
+	b = buf.NewBuffer()
 	b.AppendFunc(serial.WriteString("test payload 2"))
 	err = writer.Write(b)
 	assert.Error(err).IsNil()

+ 3 - 3
proxy/shadowsocks/server.go

@@ -6,7 +6,7 @@ import (
 	"v2ray.com/core/app"
 	"v2ray.com/core/app/dispatcher"
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	v2io "v2ray.com/core/common/io"
 	"v2ray.com/core/common/log"
@@ -103,7 +103,7 @@ func (v *Server) Start() error {
 	return nil
 }
 
-func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) {
+func (v *Server) handlerUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
 	source := session.Source
 	request, data, err := DecodeUDPPacket(v.user, payload)
 	if err != nil {
@@ -129,7 +129,7 @@ func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.Session
 	log.Access(source, dest, log.AccessAccepted, "")
 	log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
 
-	v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *alloc.Buffer) {
+	v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *buf.Buffer) {
 		defer payload.Release()
 
 		data, err := EncodeUDPPacket(request, payload)

+ 3 - 3
proxy/socks/protocol/socks.go

@@ -3,7 +3,7 @@ package protocol
 import (
 	"fmt"
 	"io"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -120,7 +120,7 @@ func (request Socks5UserPassRequest) AuthDetail() string {
 }
 
 func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
-	buffer := alloc.NewLocalBuffer(512)
+	buffer := buf.NewLocalBuffer(512)
 	defer buffer.Release()
 
 	_, err = buffer.FillFullFrom(reader, 2)
@@ -188,7 +188,7 @@ type Socks5Request struct {
 }
 
 func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
-	buffer := alloc.NewLocalBuffer(512)
+	buffer := buf.NewLocalBuffer(512)
 	defer buffer.Release()
 
 	_, err = buffer.FillFullFrom(reader, 4)

+ 2 - 2
proxy/socks/protocol/socks4_test.go

@@ -4,7 +4,7 @@ import (
 	"bytes"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/testing/assert"
 )
@@ -31,7 +31,7 @@ func TestSocks4AuthenticationResponseToBytes(t *testing.T) {
 
 	response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4})
 
-	buffer := alloc.NewLocalBuffer(2048)
+	buffer := buf.NewLocalBuffer(2048)
 	defer buffer.Release()
 
 	response.Write(buffer)

+ 10 - 10
proxy/socks/protocol/socks_test.go

@@ -5,7 +5,7 @@ import (
 	"io"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/testing/assert"
@@ -30,7 +30,7 @@ func TestHasAuthenticationMethod(t *testing.T) {
 func TestAuthenticationRequestRead(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	buffer.AppendBytes(
 		0x05, // version
 		0x01, // nMethods
@@ -85,7 +85,7 @@ func TestResponseWrite(t *testing.T) {
 		[16]byte{},
 		v2net.Port(53),
 	}
-	buffer := alloc.NewLocalBuffer(2048)
+	buffer := buf.NewLocalBuffer(2048)
 	defer buffer.Release()
 
 	response.Write(buffer)
@@ -106,7 +106,7 @@ func TestSetIPv6(t *testing.T) {
 	response := NewSocks5Response()
 	response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
 
-	buffer := alloc.NewLocalBuffer(2048)
+	buffer := buf.NewLocalBuffer(2048)
 	defer buffer.Release()
 	response.Write(buffer)
 	assert.Bytes(buffer.Bytes()).Equals([]byte{
@@ -119,7 +119,7 @@ func TestSetDomain(t *testing.T) {
 	response := NewSocks5Response()
 	response.SetDomain("v2ray.com")
 
-	buffer := alloc.NewLocalBuffer(2048)
+	buffer := buf.NewLocalBuffer(2048)
 	defer buffer.Release()
 	response.Write(buffer)
 	assert.Bytes(buffer.Bytes()).Equals([]byte{
@@ -129,7 +129,7 @@ func TestSetDomain(t *testing.T) {
 func TestEmptyAuthRequest(t *testing.T) {
 	assert := assert.On(t)
 
-	_, _, err := ReadAuthentication(alloc.NewBuffer())
+	_, _, err := ReadAuthentication(buf.NewBuffer())
 	assert.Error(err).Equals(io.EOF)
 }
 
@@ -143,7 +143,7 @@ func TestSingleByteAuthRequest(t *testing.T) {
 func TestZeroAuthenticationMethod(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	buffer.AppendBytes(5, 0)
 	_, _, err := ReadAuthentication(buffer)
 	assert.Error(err).Equals(crypto.ErrAuthenticationFailed)
@@ -151,7 +151,7 @@ func TestZeroAuthenticationMethod(t *testing.T) {
 func TestWrongProtocolVersion(t *testing.T) {
 	assert := assert.On(t)
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	buffer.AppendBytes(6, 1, 0)
 	_, _, err := ReadAuthentication(buffer)
 	assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion)
@@ -160,14 +160,14 @@ func TestWrongProtocolVersion(t *testing.T) {
 func TestEmptyRequest(t *testing.T) {
 	assert := assert.On(t)
 
-	_, err := ReadRequest(alloc.NewBuffer())
+	_, err := ReadRequest(buf.NewBuffer())
 	assert.Error(err).Equals(io.EOF)
 }
 
 func TestIPv6Request(t *testing.T) {
 	assert := assert.On(t)
 
-	b := alloc.NewBuffer()
+	b := buf.NewBuffer()
 	b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)
 	request, err := ReadRequest(b)
 	assert.Error(err).IsNil()

+ 4 - 4
proxy/socks/protocol/udp.go

@@ -2,7 +2,7 @@ package protocol
 
 import (
 	"fmt"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/common/serial"
@@ -16,14 +16,14 @@ type Socks5UDPRequest struct {
 	Fragment byte
 	Address  v2net.Address
 	Port     v2net.Port
-	Data     *alloc.Buffer
+	Data     *buf.Buffer
 }
 
 func (request *Socks5UDPRequest) Destination() v2net.Destination {
 	return v2net.UDPDestination(request.Address, request.Port)
 }
 
-func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
+func (request *Socks5UDPRequest) Write(buffer *buf.Buffer) {
 	buffer.AppendBytes(0, 0, request.Fragment)
 	switch request.Address.Family() {
 	case v2net.AddressFamilyIPv4:
@@ -83,7 +83,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
 	}
 
 	if len(packet) > dataBegin {
-		b := alloc.NewSmallBuffer()
+		b := buf.NewSmallBuffer()
 		b.Append(packet[dataBegin:])
 		request.Data = b
 	}

+ 4 - 4
proxy/socks/server_udp.go

@@ -1,7 +1,7 @@
 package socks
 
 import (
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
@@ -23,7 +23,7 @@ func (v *Server) listenUDP() error {
 	return nil
 }
 
-func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) {
+func (v *Server) handleUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
 	source := session.Source
 	log.Info("Socks: Client UDP connection from ", source)
 	request, err := protocol.ReadUDPRequest(payload.Bytes())
@@ -46,7 +46,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI
 
 	log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes")
 	log.Access(source, request.Destination, log.AccessAccepted, "")
-	v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) {
+	v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *buf.Buffer) {
 		response := &protocol.Socks5UDPRequest{
 			Fragment: 0,
 			Address:  request.Destination().Address,
@@ -55,7 +55,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI
 		}
 		log.Info("Socks: Writing back UDP response with ", payload.Len(), " bytes to ", destination)
 
-		udpMessage := alloc.NewLocalBuffer(2048)
+		udpMessage := buf.NewLocalBuffer(2048)
 		response.Write(udpMessage)
 
 		v.udpMutex.RLock()

+ 2 - 2
proxy/testing/mocks/outboundhandler.go

@@ -5,7 +5,7 @@ import (
 	"sync"
 
 	"v2ray.com/core/app"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2io "v2ray.com/core/common/io"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
@@ -18,7 +18,7 @@ type OutboundConnectionHandler struct {
 	ConnOutput  io.Writer
 }
 
-func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
+func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
 	input := ray.OutboundInput()
 	output := ray.OutboundOutput()
 

+ 2 - 2
proxy/vmess/encoding/commands.go

@@ -3,7 +3,7 @@ package encoding
 import (
 	"io"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
@@ -32,7 +32,7 @@ func MarshalCommand(command interface{}, writer io.Writer) error {
 		return ErrUnknownCommand
 	}
 
-	buffer := alloc.NewLocalBuffer(512)
+	buffer := buf.NewLocalBuffer(512)
 	defer buffer.Release()
 
 	err := factory.Marshal(command, buffer)

+ 2 - 2
proxy/vmess/encoding/commands_test.go

@@ -3,7 +3,7 @@ package encoding_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/protocol"
 	"v2ray.com/core/common/uuid"
 	. "v2ray.com/core/proxy/vmess/encoding"
@@ -21,7 +21,7 @@ func TestSwitchAccount(t *testing.T) {
 		ValidMin: 16,
 	}
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	err := MarshalCommand(sa, buffer)
 	assert.Error(err).IsNil()
 

+ 2 - 2
proxy/vmess/encoding/encoding_test.go

@@ -3,7 +3,7 @@ package encoding_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/loader"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
@@ -36,7 +36,7 @@ func TestRequestSerialization(t *testing.T) {
 		Security: protocol.Security(protocol.SecurityType_AES128_GCM),
 	}
 
-	buffer := alloc.NewBuffer()
+	buffer := buf.NewBuffer()
 	client := NewClientSession(protocol.DefaultIDHash)
 	client.EncodeRequestHeader(expectedRequest, buffer)
 

+ 2 - 2
proxy/vmess/inbound/inbound.go

@@ -8,7 +8,7 @@ import (
 	"v2ray.com/core/app/dispatcher"
 	"v2ray.com/core/app/proxyman"
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	v2io "v2ray.com/core/common/io"
 	"v2ray.com/core/common/loader"
@@ -225,7 +225,7 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
 	}
 	output.Release()
 	if request.Option.Has(protocol.RequestOptionChunkStream) {
-		if err := bodyWriter.Write(alloc.NewLocalBuffer(8)); err != nil {
+		if err := bodyWriter.Write(buf.NewLocalBuffer(8)); err != nil {
 			connection.SetReusable(false)
 		}
 	}

+ 4 - 4
proxy/vmess/outbound/outbound.go

@@ -4,7 +4,7 @@ import (
 	"sync"
 
 	"v2ray.com/core/app"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2io "v2ray.com/core/common/io"
 	"v2ray.com/core/common/loader"
 	"v2ray.com/core/common/log"
@@ -25,7 +25,7 @@ type VMessOutboundHandler struct {
 	meta         *proxy.OutboundHandlerMeta
 }
 
-func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
+func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
 	defer ray.OutboundInput().Release()
 	defer ray.OutboundOutput().Close()
 
@@ -92,7 +92,7 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc
 	return
 }
 
-func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
+func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *buf.Buffer, input v2io.Reader, finish *sync.Mutex) {
 	defer finish.Unlock()
 
 	writer := v2io.NewBufferedWriter(conn)
@@ -116,7 +116,7 @@ func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, co
 	}
 
 	if request.Option.Has(protocol.RequestOptionChunkStream) {
-		err := bodyWriter.Write(alloc.NewLocalBuffer(8))
+		err := bodyWriter.Write(buf.NewLocalBuffer(8))
 		if err != nil {
 			conn.SetReusable(false)
 		}

+ 2 - 2
testing/scenarios/shadowsocks_test.go

@@ -4,7 +4,7 @@ import (
 	"fmt"
 	"net"
 	"testing"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/testing/assert"
 	"v2ray.com/core/testing/servers/tcp"
@@ -42,7 +42,7 @@ func TestShadowsocksTCP(t *testing.T) {
 
 		//conn.CloseWrite()
 
-		response := alloc.NewBuffer()
+		response := buf.NewBuffer()
 		finished := false
 		expectedResponse := "Processed: " + payload
 		for {

+ 5 - 5
transport/internet/authenticator.go

@@ -2,12 +2,12 @@ package internet
 
 import (
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type Authenticator interface {
-	Seal(*alloc.Buffer)
-	Open(*alloc.Buffer) bool
+	Seal(*buf.Buffer)
+	Open(*buf.Buffer) bool
 	Overhead() int
 }
 
@@ -53,7 +53,7 @@ func (v *AuthenticatorChain) Overhead() int {
 	return total
 }
 
-func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool {
+func (v *AuthenticatorChain) Open(payload *buf.Buffer) bool {
 	for _, auth := range v.authenticators {
 		if !auth.Open(payload) {
 			return false
@@ -62,7 +62,7 @@ func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool {
 	return true
 }
 
-func (v *AuthenticatorChain) Seal(payload *alloc.Buffer) {
+func (v *AuthenticatorChain) Seal(payload *buf.Buffer) {
 	for i := len(v.authenticators) - 1; i >= 0; i-- {
 		auth := v.authenticators[i]
 		auth.Seal(payload)

+ 10 - 10
transport/internet/headers/http/http.go

@@ -7,7 +7,7 @@ import (
 	"net/http"
 	"strings"
 	"time"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/loader"
 	"v2ray.com/core/common/serial"
 	"v2ray.com/core/transport/internet"
@@ -23,7 +23,7 @@ var (
 )
 
 type Reader interface {
-	Read(io.Reader) (*alloc.Buffer, error)
+	Read(io.Reader) (*buf.Buffer, error)
 }
 
 type Writer interface {
@@ -32,7 +32,7 @@ type Writer interface {
 
 type NoOpReader struct{}
 
-func (v *NoOpReader) Read(io.Reader) (*alloc.Buffer, error) {
+func (v *NoOpReader) Read(io.Reader) (*buf.Buffer, error) {
 	return nil, nil
 }
 
@@ -45,8 +45,8 @@ func (v *NoOpWriter) Write(io.Writer) error {
 type HeaderReader struct {
 }
 
-func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) {
-	buffer := alloc.NewSmallBuffer()
+func (*HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) {
+	buffer := buf.NewSmallBuffer()
 	for {
 		_, err := buffer.FillFrom(reader)
 		if err != nil {
@@ -69,10 +69,10 @@ func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) {
 }
 
 type HeaderWriter struct {
-	header *alloc.Buffer
+	header *buf.Buffer
 }
 
-func NewHeaderWriter(header *alloc.Buffer) *HeaderWriter {
+func NewHeaderWriter(header *buf.Buffer) *HeaderWriter {
 	return &HeaderWriter{
 		header: header,
 	}
@@ -91,7 +91,7 @@ func (v *HeaderWriter) Write(writer io.Writer) error {
 type HttpConn struct {
 	net.Conn
 
-	readBuffer    *alloc.Buffer
+	readBuffer    *buf.Buffer
 	oneTimeReader Reader
 	oneTimeWriter Writer
 }
@@ -143,7 +143,7 @@ type HttpAuthenticator struct {
 }
 
 func (v HttpAuthenticator) GetClientWriter() *HeaderWriter {
-	header := alloc.NewSmallBuffer()
+	header := buf.NewSmallBuffer()
 	config := v.config.Request
 	header.AppendFunc(serial.WriteString(strings.Join([]string{config.Method.GetValue(), config.PickUri(), config.GetFullVersion()}, " ")))
 	header.AppendFunc(writeCRLF)
@@ -160,7 +160,7 @@ func (v HttpAuthenticator) GetClientWriter() *HeaderWriter {
 }
 
 func (v HttpAuthenticator) GetServerWriter() *HeaderWriter {
-	header := alloc.NewSmallBuffer()
+	header := buf.NewSmallBuffer()
 	config := v.config.Response
 	header.AppendFunc(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.Status.GetCode(), config.Status.GetReason()}, " ")))
 	header.AppendFunc(writeCRLF)

+ 4 - 4
transport/internet/headers/http/http_test.go

@@ -3,7 +3,7 @@ package http_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/serial"
 	"v2ray.com/core/testing/assert"
 	. "v2ray.com/core/transport/internet/headers/http"
@@ -12,8 +12,8 @@ import (
 func TestReaderWriter(t *testing.T) {
 	assert := assert.On(t)
 
-	cache := alloc.NewBuffer()
-	b := alloc.NewLocalBuffer(256)
+	cache := buf.NewBuffer()
+	b := buf.NewLocalBuffer(256)
 	b.AppendFunc(serial.WriteString("abcd" + ENDING))
 	writer := NewHeaderWriter(b)
 	writer.Write(cache)
@@ -41,7 +41,7 @@ func TestRequestHeader(t *testing.T) {
 		},
 	}).(HttpAuthenticator)
 
-	cache := alloc.NewBuffer()
+	cache := buf.NewBuffer()
 	err := auth.GetClientWriter().Write(cache)
 	assert.Error(err).IsNil()
 

+ 2 - 2
transport/internet/headers/srtp/srtp_test.go

@@ -3,7 +3,7 @@ package srtp_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/testing/assert"
 	. "v2ray.com/core/transport/internet/headers/srtp"
 )
@@ -14,7 +14,7 @@ func TestSRTPWrite(t *testing.T) {
 	content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
 	srtp := SRTP{}
 
-	payload := alloc.NewLocalBuffer(2048)
+	payload := buf.NewLocalBuffer(2048)
 	payload.AppendFunc(srtp.Write)
 	payload.Append(content)
 

+ 2 - 2
transport/internet/headers/utp/utp_test.go

@@ -3,7 +3,7 @@ package utp_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/testing/assert"
 	. "v2ray.com/core/transport/internet/headers/utp"
 )
@@ -14,7 +14,7 @@ func TestUTPWrite(t *testing.T) {
 	content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
 	utp := UTP{}
 
-	payload := alloc.NewLocalBuffer(2048)
+	payload := buf.NewLocalBuffer(2048)
 	payload.AppendFunc(utp.Write)
 	payload.Append(content)
 

+ 2 - 2
transport/internet/kcp/dialer.go

@@ -8,7 +8,7 @@ import (
 
 	"crypto/cipher"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/dice"
 	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/log"
@@ -88,7 +88,7 @@ func (o *ClientConnection) ResetSecurity(header internet.PacketHeader, security
 }
 
 func (o *ClientConnection) Run() {
-	payload := alloc.NewSmallBuffer()
+	payload := buf.NewSmallBuffer()
 	defer payload.Release()
 
 	for {

+ 2 - 2
transport/internet/kcp/listener.go

@@ -9,7 +9,7 @@ import (
 
 	"crypto/cipher"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/errors"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -140,7 +140,7 @@ func NewListener(address v2net.Address, port v2net.Port, options internet.Listen
 	return l, nil
 }
 
-func (v *Listener) OnReceive(payload *alloc.Buffer, session *proxy.SessionInfo) {
+func (v *Listener) OnReceive(payload *buf.Buffer, session *proxy.SessionInfo) {
 	defer payload.Release()
 
 	src := session.Source

+ 3 - 3
transport/internet/kcp/output.go

@@ -4,7 +4,7 @@ import (
 	"io"
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type SegmentWriter interface {
@@ -14,7 +14,7 @@ type SegmentWriter interface {
 type BufferedSegmentWriter struct {
 	sync.Mutex
 	mtu    uint32
-	buffer *alloc.Buffer
+	buffer *buf.Buffer
 	writer io.Writer
 }
 
@@ -22,7 +22,7 @@ func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter {
 	return &BufferedSegmentWriter{
 		mtu:    mtu,
 		writer: writer,
-		buffer: alloc.NewSmallBuffer(),
+		buffer: buf.NewSmallBuffer(),
 	}
 }
 

+ 2 - 2
transport/internet/kcp/receiving.go

@@ -3,7 +3,7 @@ package kcp
 import (
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 type ReceivingWindow struct {
@@ -145,7 +145,7 @@ func (v *AckList) Flush(current uint32, rto uint32) {
 type ReceivingWorker struct {
 	sync.RWMutex
 	conn       *Connection
-	leftOver   *alloc.Buffer
+	leftOver   *buf.Buffer
 	window     *ReceivingWindow
 	acklist    *AckList
 	nextNumber uint32

+ 7 - 7
transport/internet/kcp/segment.go

@@ -2,7 +2,7 @@ package kcp
 
 import (
 	"v2ray.com/core/common"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/serial"
 )
 
@@ -31,7 +31,7 @@ type Segment interface {
 	Conversation() uint16
 	Command() Command
 	ByteSize() int
-	Bytes() alloc.BytesWriter
+	Bytes() buf.BytesWriter
 }
 
 const (
@@ -44,7 +44,7 @@ type DataSegment struct {
 	Timestamp   uint32
 	Number      uint32
 	SendingNext uint32
-	Data        *alloc.Buffer
+	Data        *buf.Buffer
 
 	timeout  uint32
 	transmit uint32
@@ -64,13 +64,13 @@ func (v *DataSegment) Command() Command {
 
 func (v *DataSegment) SetData(b []byte) {
 	if v.Data == nil {
-		v.Data = alloc.NewSmallBuffer()
+		v.Data = buf.NewSmallBuffer()
 	}
 	v.Data.Clear()
 	v.Data.Append(b)
 }
 
-func (v *DataSegment) Bytes() alloc.BytesWriter {
+func (v *DataSegment) Bytes() buf.BytesWriter {
 	return func(b []byte) int {
 		b = serial.Uint16ToBytes(v.Conv, b[:0])
 		b = append(b, byte(CommandData), byte(v.Option))
@@ -137,7 +137,7 @@ func (v *AckSegment) ByteSize() int {
 	return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4
 }
 
-func (v *AckSegment) Bytes() alloc.BytesWriter {
+func (v *AckSegment) Bytes() buf.BytesWriter {
 	return func(b []byte) int {
 		b = serial.Uint16ToBytes(v.Conv, b[:0])
 		b = append(b, byte(CommandACK), byte(v.Option))
@@ -181,7 +181,7 @@ func (v *CmdOnlySegment) ByteSize() int {
 	return 2 + 1 + 1 + 4 + 4 + 4
 }
 
-func (v *CmdOnlySegment) Bytes() alloc.BytesWriter {
+func (v *CmdOnlySegment) Bytes() buf.BytesWriter {
 	return func(b []byte) int {
 		b = serial.Uint16ToBytes(v.Conv, b[:0])
 		b = append(b, byte(v.Cmd), byte(v.Option))

+ 2 - 2
transport/internet/kcp/segment_test.go

@@ -3,7 +3,7 @@ package kcp_test
 import (
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/testing/assert"
 	. "v2ray.com/core/transport/internet/kcp"
 )
@@ -19,7 +19,7 @@ func TestBadSegment(t *testing.T) {
 func TestDataSegment(t *testing.T) {
 	assert := assert.On(t)
 
-	b := alloc.NewLocalBuffer(512)
+	b := buf.NewLocalBuffer(512)
 	b.Append([]byte{'a', 'b', 'c', 'd'})
 	seg := &DataSegment{
 		Conv:        1,

+ 4 - 4
transport/internet/udp/hub.go

@@ -4,7 +4,7 @@ import (
 	"net"
 	"sync"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/dice"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
@@ -14,11 +14,11 @@ import (
 )
 
 type UDPPayload struct {
-	payload *alloc.Buffer
+	payload *buf.Buffer
 	session *proxy.SessionInfo
 }
 
-type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo)
+type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo)
 
 type UDPPayloadQueue struct {
 	queue    []chan UDPPayload
@@ -135,7 +135,7 @@ func (v *UDPHub) start() {
 
 	oobBytes := make([]byte, 256)
 	for v.Running() {
-		buffer := alloc.NewSmallBuffer()
+		buffer := buf.NewSmallBuffer()
 		var noob int
 		var addr *net.UDPAddr
 		var err error

+ 2 - 2
transport/internet/udp/hub_linux_test.go

@@ -7,7 +7,7 @@ import (
 	"syscall"
 	"testing"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/testing/assert"
@@ -23,7 +23,7 @@ func TestHubSocksOption(t *testing.T) {
 	}
 
 	hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{
-		Callback:            func(*alloc.Buffer, *proxy.SessionInfo) {},
+		Callback:            func(*buf.Buffer, *proxy.SessionInfo) {},
 		ReceiveOriginalDest: true,
 	})
 	assert.Error(err).IsNil()

+ 4 - 4
transport/internet/udp/udp_server.go

@@ -5,14 +5,14 @@ import (
 	"time"
 
 	"v2ray.com/core/app/dispatcher"
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/log"
 	v2net "v2ray.com/core/common/net"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/transport/ray"
 )
 
-type UDPResponseCallback func(destination v2net.Destination, payload *alloc.Buffer)
+type UDPResponseCallback func(destination v2net.Destination, payload *buf.Buffer)
 
 type TimedInboundRay struct {
 	name       string
@@ -111,7 +111,7 @@ func (v *UDPServer) RemoveRay(name string) {
 	delete(v.conns, name)
 }
 
-func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer) bool {
+func (v *UDPServer) locateExistingAndDispatch(name string, payload *buf.Buffer) bool {
 	log.Debug("UDP Server: Locating existing connection for ", name)
 	v.RLock()
 	defer v.RUnlock()
@@ -130,7 +130,7 @@ func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer
 	return false
 }
 
-func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *alloc.Buffer, callback UDPResponseCallback) {
+func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *buf.Buffer, callback UDPResponseCallback) {
 	source := session.Source
 	destination := session.Destination
 

+ 6 - 6
transport/ray/direct.go

@@ -5,7 +5,7 @@ import (
 	"sync"
 	"time"
 
-	"v2ray.com/core/common/alloc"
+	"v2ray.com/core/common/buf"
 )
 
 const (
@@ -44,16 +44,16 @@ func (v *directRay) InboundOutput() InputStream {
 type Stream struct {
 	access sync.RWMutex
 	closed bool
-	buffer chan *alloc.Buffer
+	buffer chan *buf.Buffer
 }
 
 func NewStream() *Stream {
 	return &Stream{
-		buffer: make(chan *alloc.Buffer, bufferSize),
+		buffer: make(chan *buf.Buffer, bufferSize),
 	}
 }
 
-func (v *Stream) Read() (*alloc.Buffer, error) {
+func (v *Stream) Read() (*buf.Buffer, error) {
 	if v.buffer == nil {
 		return nil, io.EOF
 	}
@@ -71,7 +71,7 @@ func (v *Stream) Read() (*alloc.Buffer, error) {
 	return result, nil
 }
 
-func (v *Stream) Write(data *alloc.Buffer) error {
+func (v *Stream) Write(data *buf.Buffer) error {
 	for !v.closed {
 		err := v.TryWriteOnce(data)
 		if err != io.ErrNoProgress {
@@ -81,7 +81,7 @@ func (v *Stream) Write(data *alloc.Buffer) error {
 	return io.ErrClosedPipe
 }
 
-func (v *Stream) TryWriteOnce(data *alloc.Buffer) error {
+func (v *Stream) TryWriteOnce(data *buf.Buffer) error {
 	v.access.RLock()
 	defer v.access.RUnlock()
 	if v.closed {