use unsafe.Slice

This commit is contained in:
Yasuhiro Matsumoto
2026-03-04 09:15:06 +09:00
committed by mattn
parent 8366a001c1
commit 5a1f4d3045

View File

@@ -17,7 +17,6 @@ import "C"
import ( import (
"fmt" "fmt"
"math" "math"
"reflect"
"unsafe" "unsafe"
) )
@@ -43,14 +42,8 @@ func (c *SQLiteConn) Serialize(schema string) ([]byte, error) {
return nil, fmt.Errorf("serialized database is too large (%d bytes)", sz) return nil, fmt.Errorf("serialized database is too large (%d bytes)", sz)
} }
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(ptr)),
Len: int(sz),
Cap: int(sz),
}))
res := make([]byte, int(sz)) res := make([]byte, int(sz))
copy(res, cBuf) copy(res, unsafe.Slice((*byte)(unsafe.Pointer(ptr)), int(sz)))
return res, nil return res, nil
} }
@@ -67,12 +60,7 @@ func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
defer C.free(unsafe.Pointer(zSchema)) defer C.free(unsafe.Pointer(zSchema))
tmpBuf := (*C.uchar)(C.sqlite3_malloc64(C.sqlite3_uint64(len(b)))) tmpBuf := (*C.uchar)(C.sqlite3_malloc64(C.sqlite3_uint64(len(b))))
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ copy(unsafe.Slice((*byte)(unsafe.Pointer(tmpBuf)), len(b)), b)
Data: uintptr(unsafe.Pointer(tmpBuf)),
Len: len(b),
Cap: len(b),
}))
copy(cBuf, b)
rc := C.sqlite3_deserialize(c.db, zSchema, tmpBuf, C.sqlite3_int64(len(b)), rc := C.sqlite3_deserialize(c.db, zSchema, tmpBuf, C.sqlite3_int64(len(b)),
C.sqlite3_int64(len(b)), C.SQLITE_DESERIALIZE_FREEONCLOSE) C.sqlite3_int64(len(b)), C.SQLITE_DESERIALIZE_FREEONCLOSE)