Remove stack buffer usage

This commit is contained in:
世界 2023-07-03 21:22:53 +08:00
parent 513f49a03f
commit 2cedde0fbc
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
7 changed files with 20 additions and 63 deletions

View file

@ -7,7 +7,6 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -50,9 +49,7 @@ func (c *clientConn) Write(b []byte) (n int, err error) {
Network: N.NetworkTCP,
Destination: c.destination,
}
_buffer := buf.StackNewSize(streamRequestLen(request) + len(b))
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
buffer := buf.NewSize(streamRequestLen(request) + len(b))
defer buffer.Release()
EncodeStreamRequest(request, buffer)
buffer.Write(b)
@ -64,20 +61,6 @@ func (c *clientConn) Write(b []byte) (n int, err error) {
return len(b), nil
}
func (c *clientConn) ReadFrom(r io.Reader) (n int64, err error) {
if !c.requestWritten {
return bufio.ReadFrom0(c, r)
}
return bufio.Copy(c.Conn, r)
}
func (c *clientConn) WriteTo(w io.Writer) (n int64, err error) {
if !c.responseRead {
return bufio.WriteTo0(c, w)
}
return bufio.Copy(w, c.Conn)
}
func (c *clientConn) LocalAddr() net.Addr {
return c.Conn.LocalAddr()
}
@ -148,9 +131,7 @@ func (c *clientPacketConn) writeRequest(payload []byte) (n int, err error) {
if len(payload) > 0 {
rLen += 2 + len(payload)
}
_buffer := buf.StackNewSize(rLen)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
buffer := buf.NewSize(rLen)
defer buffer.Release()
EncodeStreamRequest(request, buffer)
if len(payload) > 0 {
@ -324,9 +305,7 @@ func (c *clientPacketAddrConn) writeRequest(payload []byte, destination M.Socksa
if len(payload) > 0 {
rLen += M.SocksaddrSerializer.AddrPortLen(destination) + 2 + len(payload)
}
_buffer := buf.StackNewSize(rLen)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
buffer := buf.NewSize(rLen)
defer buffer.Release()
EncodeStreamRequest(request, buffer)
if len(payload) > 0 {