Browse Source

improve code with go1.17 new unsafe API

development
Andrea Barisani 1 month ago
parent
commit
8a0eea3e95
  1. 16
      dma/alloc.go
  2. 2
      go.mod

16
dma/alloc.go

@ -11,27 +11,23 @@ package dma
import (
"container/list"
"reflect"
"unsafe"
)
func (b *block) read(off int, buf []byte) {
var mem []byte
var ptr unsafe.Pointer
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
hdr.Data = uintptr(unsafe.Pointer(uintptr(b.addr + uint32(off))))
hdr.Len = len(buf)
hdr.Cap = hdr.Len
ptr = unsafe.Add(ptr, int(b.addr) + off)
mem := unsafe.Slice((*byte)(ptr), len(buf))
copy(buf, mem)
}
func (b *block) write(off int, buf []byte) {
var mem []byte
var ptr unsafe.Pointer
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
hdr.Data = uintptr(unsafe.Pointer(uintptr(b.addr + uint32(off))))
hdr.Len = len(buf)
ptr = unsafe.Add(ptr, int(b.addr) + off)
mem := unsafe.Slice((*byte)(ptr), len(buf))
copy(mem, buf)
}

2
go.mod

@ -1,3 +1,3 @@
module github.com/f-secure-foundry/tamago
go 1.16
go 1.17

Loading…
Cancel
Save