2019-11-29 08:14:17 +01:00
|
|
|
module builtin
|
|
|
|
|
2019-12-04 09:46:09 +01:00
|
|
|
const (
|
|
|
|
mem_prot = mm_prot(int(mm_prot.prot_read) | int(mm_prot.prot_write))
|
|
|
|
mem_flags = map_flags(int(map_flags.map_private) | int(map_flags.map_anonymous))
|
2019-12-07 20:25:19 +01:00
|
|
|
page_size = u64(linux_mem.page_size)
|
2019-12-04 09:46:09 +01:00
|
|
|
)
|
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
pub fn mm_pages(size u64) u32 {
|
2019-12-07 20:25:19 +01:00
|
|
|
pages := (size+u64(4)+page_size)/page_size
|
2019-11-29 08:14:17 +01:00
|
|
|
return u32(pages)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mm_alloc(size u64) (byteptr, errno) {
|
|
|
|
pages := mm_pages(size)
|
|
|
|
n_bytes := u64(pages*u32(linux_mem.page_size))
|
|
|
|
|
|
|
|
a, e := sys_mmap(0, n_bytes, mem_prot, mem_flags, -1, 0)
|
|
|
|
if e == .enoerror {
|
2020-04-22 20:12:30 +02:00
|
|
|
mut ap := &int(a)
|
2019-11-29 08:14:17 +01:00
|
|
|
*ap = pages
|
|
|
|
return byteptr(a+4), e
|
|
|
|
}
|
|
|
|
return byteptr(0), e
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mm_free(addr byteptr) errno {
|
2020-04-22 20:12:30 +02:00
|
|
|
ap := &int(addr-4)
|
2019-11-29 08:14:17 +01:00
|
|
|
size := u64(*ap) * u64(linux_mem.page_size)
|
|
|
|
|
2019-12-01 09:27:36 +01:00
|
|
|
return sys_munmap(ap, size)
|
|
|
|
}
|
2019-11-29 08:14:17 +01:00
|
|
|
|
2019-12-01 09:27:36 +01:00
|
|
|
pub fn mem_copy(dest0 voidptr, src0 voidptr, n int) voidptr {
|
|
|
|
mut dest := byteptr(dest0)
|
|
|
|
src := byteptr(src0)
|
|
|
|
for i in 0..n {
|
|
|
|
dest[i] = src[i]
|
|
|
|
}
|
|
|
|
return dest0
|
2019-11-29 08:14:17 +01:00
|
|
|
}
|
2019-12-08 11:44:52 +01:00
|
|
|
|
|
|
|
[unsafe_fn]
|
|
|
|
pub fn malloc(n int) byteptr {
|
|
|
|
if n < 0 {
|
|
|
|
panic('malloc(<0)')
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr, e := mm_alloc(u64(n))
|
|
|
|
assert e == .enoerror
|
|
|
|
assert !isnil(ptr)
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
[unsafe_fn]
|
|
|
|
pub fn free(ptr voidptr) {
|
|
|
|
assert mm_free(ptr) == .enoerror
|
|
|
|
}
|