Skip to content

Commit

Permalink
Remove UnsafeSlice and use unsafe.Slice
Browse files Browse the repository at this point in the history
References:
- #713
- #566

Signed-off-by: Chun-Hung Tseng <henrybear327@gmail.com>
  • Loading branch information
henrybear327 committed Apr 6, 2024
1 parent 2c29534 commit 9e3d732
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
11 changes: 4 additions & 7 deletions freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ func (f *freelist) read(p *page) {
if count == 0 {
f.ids = nil
} else {
var ids []pgid
data := unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), unsafe.Sizeof(ids[0]), idx)
unsafeSlice(unsafe.Pointer(&ids), data, count)
data := unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), unsafe.Sizeof(pgid(0)), idx)
ids := unsafe.Slice((*pgid)(data), count)

// copy the ids, so we don't modify on the freelist page directly
idsCopy := make([]pgid, count)
Expand Down Expand Up @@ -322,15 +321,13 @@ func (f *freelist) write(p *page) error {
p.count = uint16(l)
} else if l < 0xFFFF {
p.count = uint16(l)
var ids []pgid
data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))
unsafeSlice(unsafe.Pointer(&ids), data, l)
ids := unsafe.Slice((*pgid)(data), l)
f.copyall(ids)
} else {
p.count = 0xFFFF
var ids []pgid
data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))
unsafeSlice(unsafe.Pointer(&ids), data, l+1)
ids := unsafe.Slice((*pgid)(data), l+1)
ids[0] = pgid(l)
f.copyall(ids[1:])
}
Expand Down
6 changes: 2 additions & 4 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ func (p *page) leafPageElements() []leafPageElement {
if p.count == 0 {
return nil
}
var elems []leafPageElement
data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))
unsafeSlice(unsafe.Pointer(&elems), data, int(p.count))
elems := unsafe.Slice((*leafPageElement)(data), int(p.count))
return elems
}

Expand All @@ -91,9 +90,8 @@ func (p *page) branchPageElements() []branchPageElement {
if p.count == 0 {
return nil
}
var elems []branchPageElement
data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))
unsafeSlice(unsafe.Pointer(&elems), data, int(p.count))
elems := unsafe.Slice((*branchPageElement)(data), int(p.count))
return elems
}

Expand Down
12 changes: 0 additions & 12 deletions unsafe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bbolt

import (
"reflect"
"unsafe"
)

Expand All @@ -26,14 +25,3 @@ func unsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte {
// all), so this is believed to be correct.
return (*[maxAllocSize]byte)(unsafeAdd(base, offset))[i:j:j]
}

// unsafeSlice modifies the data, len, and cap of a slice variable pointed to by
// the slice parameter. This helper should be used over other direct
// manipulation of reflect.SliceHeader to prevent misuse, namely, converting
// from reflect.SliceHeader to a Go slice type.
func unsafeSlice(slice, data unsafe.Pointer, len int) {
s := (*reflect.SliceHeader)(slice) //nolint:staticcheck
s.Data = uintptr(data)
s.Cap = len
s.Len = len
}

0 comments on commit 9e3d732

Please sign in to comment.