avoid allocate memory on Read

master
fangdingjun 6 years ago
parent ea3bef53e0
commit c5d91a0951

@ -205,11 +205,12 @@ func (c *Conn) Read(buf []byte) (n int, err error) {
return 0, nil return 0, nil
} }
bufLen := len(buf) //bufLen := len(buf)
cbuf := C.malloc(C.size_t(bufLen)) //cbuf := C.malloc(C.size_t(bufLen))
defer C.free(cbuf) //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 { if int(ret) < 0 {
return 0, fmt.Errorf("read error: %s", return 0, fmt.Errorf("read error: %s",
C.GoString(C.gnutls_strerror(C.int(ret)))) 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) n = int(ret)
gobuf2 := C.GoBytes(cbuf, C.int(ret)) //gobuf2 := C.GoBytes(cbuf, C.int(ret))
copy(buf, gobuf2) //copy(buf, gobuf2)
return n, nil return n, nil
} }

Loading…
Cancel
Save