mirror of
https://github.com/taigrr/gopher-os
synced 2025-01-18 04:43:13 -08:00
Implement memcopy
This commit is contained in:
parent
71dfc9ae70
commit
42ee2d1325
@ -27,3 +27,23 @@ func Memset(addr uintptr, value byte, size Size) {
|
||||
copy(target[index:], target[:index])
|
||||
}
|
||||
}
|
||||
|
||||
// Memcopy copies size bytes from src to dst.
|
||||
func Memcopy(src, dst uintptr, size Size) {
|
||||
if size == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
srcSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Len: int(size),
|
||||
Cap: int(size),
|
||||
Data: src,
|
||||
}))
|
||||
dstSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Len: int(size),
|
||||
Cap: int(size),
|
||||
Data: dst,
|
||||
}))
|
||||
|
||||
copy(dstSlice, srcSlice)
|
||||
}
|
@ -25,3 +25,28 @@ func TestMemset(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemcopy(t *testing.T) {
|
||||
// memcopy with a 0 size should be a no-op
|
||||
Memcopy(uintptr(0), uintptr(0), 0)
|
||||
|
||||
var (
|
||||
src = make([]byte, PageSize)
|
||||
dst = make([]byte, PageSize)
|
||||
)
|
||||
for i := 0; i < len(src); i++ {
|
||||
src[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
Memcopy(
|
||||
uintptr(unsafe.Pointer(&src[0])),
|
||||
uintptr(unsafe.Pointer(&dst[0])),
|
||||
PageSize,
|
||||
)
|
||||
|
||||
for i := 0; i < len(src); i++ {
|
||||
if got := dst[i]; got != src[i] {
|
||||
t.Errorf("value mismatch between src and dst at index %d", i)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user