From c5d91a09518a29b8bd468afd3d85030aeef6df2c Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Fri, 13 Jul 2018 16:46:24 +0800 Subject: [PATCH] avoid allocate memory on Read --- tls.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tls.go b/tls.go index 859d015..d256af3 100644 --- a/tls.go +++ b/tls.go @@ -205,11 +205,12 @@ func (c *Conn) Read(buf []byte) (n int, err error) { return 0, nil } - bufLen := len(buf) - cbuf := C.malloc(C.size_t(bufLen)) - defer C.free(cbuf) + //bufLen := len(buf) + //cbuf := C.malloc(C.size_t(bufLen)) + //defer C.free(cbuf) - ret := C.gnutls_record_recv(c.sess.session, cbuf, C.size_t(bufLen)) + ret := C.gnutls_record_recv(c.sess.session, + unsafe.Pointer(&buf[0]), C.size_t(len(buf))) if int(ret) < 0 { return 0, fmt.Errorf("read error: %s", C.GoString(C.gnutls_strerror(C.int(ret)))) @@ -220,8 +221,8 @@ func (c *Conn) Read(buf []byte) (n int, err error) { } n = int(ret) - gobuf2 := C.GoBytes(cbuf, C.int(ret)) - copy(buf, gobuf2) + //gobuf2 := C.GoBytes(cbuf, C.int(ret)) + //copy(buf, gobuf2) return n, nil }