1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/builtin/linux_bare/memory_managment.v

93 lines
2.3 KiB
V
Raw Normal View History

module builtin
import dlmalloc
fn mm_alloc(size u64) (&byte, Errno) {
// BEGIN CONSTS
// the constants need to be here, since the initialization of other constants,
// which happen before these ones would, require malloc
mem_prot := unsafe { MemProt(int(MemProt.prot_read) | int(MemProt.prot_write)) }
map_flags := unsafe { MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous)) }
// END CONSTS
2022-04-15 14:58:56 +03:00
a, e := sys_mmap(&u8(0), size + sizeof(u64), mem_prot, map_flags, -1, 0)
if e == .enoerror {
unsafe {
mut ap := &u64(a)
*ap = size
2022-04-15 14:58:56 +03:00
x2 := &u8(a + sizeof(u64))
return x2, e
}
}
2022-04-15 14:58:56 +03:00
return &u8(0), e
}
2022-04-15 21:02:03 +03:00
fn mm_free(addr &u8) Errno {
unsafe {
ap := &u64(addr - sizeof(u64))
size := *ap
return sys_munmap(addr - sizeof(u64), size + sizeof(u64))
}
}
fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) {
// BEGIN CONSTS
// the constants need to be here, since the initialization of other constants,
// which happen before these ones would, require malloc
mem_prot := unsafe { MemProt(int(MemProt.prot_read) | int(MemProt.prot_write)) }
map_flags := unsafe { MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous)) }
// END CONSTS
2022-04-15 14:58:56 +03:00
a, e := sys_mmap(&u8(0), u64(size), mem_prot, map_flags, -1, 0)
if e == .enoerror {
return a, size, 0
}
2022-07-21 21:01:30 +03:00
return unsafe { nil }, 0, 0
}
fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr {
2022-07-21 21:01:30 +03:00
return unsafe { nil }
}
fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool {
_, e := sys_mremap(ptr, u64(oldsize), u64(newsize), 0)
if e == .enoerror {
return true
}
e2 := sys_munmap(voidptr(usize(ptr) + newsize), u64(oldsize - newsize))
return e2 == .enoerror
}
fn system_free(_ voidptr, ptr voidptr, size usize) bool {
unsafe {
return sys_munmap(ptr, u64(size)) == .enoerror
}
}
fn system_can_release_part(_ voidptr, _ u32) bool {
return true
}
fn system_allocates_zeros(_ voidptr) bool {
return true
}
fn system_page_size(_ voidptr) usize {
return 4096
}
fn get_linux_allocator() dlmalloc.Allocator {
return dlmalloc.Allocator{
alloc: system_alloc
remap: system_remap
free_part: system_free_part
free_: system_free
can_release_part: system_can_release_part
allocates_zeros: system_allocates_zeros
page_size: system_page_size
2022-07-21 21:01:30 +03:00
data: unsafe { nil }
}
}