Skip to content

Commit

Permalink
internal/xcnv: use Go-1.17 unsafe.Slice
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Apr 25, 2023
1 parent fdd927d commit e33b935
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 3 additions & 6 deletions internal/xcnv/eda2lcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"io"
"log"
"reflect"
"unsafe"

"github.com/go-lpc/mim/internal/eformat"
Expand Down Expand Up @@ -105,10 +104,8 @@ func i32sFrom(w *bytes.Buffer, d *eformat.DIF) []int32 {

// FIXME(sbinet): use a special io.Writer that writes to
// a []int32 instead of this unsafe business ?
hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&raw))
hdr.Len /= 4
hdr.Cap /= 4
ptr := (*int32)(unsafe.Pointer(&raw[0]))
sli := unsafe.Slice(ptr, len(raw)/i32sz)

data := *(*[]int32)(unsafe.Pointer(&hdr))
return data
return sli
}
13 changes: 7 additions & 6 deletions internal/xcnv/lcio2eda.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"log"
"reflect"
"unsafe"

"github.com/go-lpc/mim/internal/eformat"
Expand Down Expand Up @@ -53,10 +52,12 @@ func LCIO2EDA(w io.Writer, r *lcio.Reader, freq int, msg *log.Logger) error {
}

func bytesFromI32s(raw []int32) []byte {
n := len(raw)
if n == 0 {
return nil
}
const i32sz = 4
hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&raw))
hdr.Len *= i32sz
hdr.Cap *= i32sz

return *(*[]byte)(unsafe.Pointer(&hdr))
ptr := (*byte)(unsafe.Pointer(&raw[0]))
sli := unsafe.Slice(ptr, i32sz*n)
return sli
}

0 comments on commit e33b935

Please # to comment.