| 1 | module builtin |
| 2 | |
| 3 | import dlmalloc |
| 4 | |
| 5 | fn mm_alloc(size u64) (&u8, Errno) { |
| 6 | // BEGIN CONSTS |
| 7 | // the constants need to be here, since the initialization of other constants, |
| 8 | // which happen before these ones would, require malloc |
| 9 | mem_prot := unsafe { MemProt(int(MemProt.prot_read) | int(MemProt.prot_write)) } |
| 10 | map_flags := unsafe { MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous)) } |
| 11 | // END CONSTS |
| 12 | |
| 13 | a, e := sys_mmap(&u8(unsafe { nil }), size + sizeof(u64), mem_prot, map_flags, -1, 0) |
| 14 | if e == .enoerror { |
| 15 | unsafe { |
| 16 | mut ap := &u64(a) |
| 17 | *ap = size |
| 18 | x2 := &u8(a + sizeof(u64)) |
| 19 | return x2, e |
| 20 | } |
| 21 | } |
| 22 | return &u8(unsafe { nil }), e |
| 23 | } |
| 24 | |
| 25 | fn mm_free(addr &u8) Errno { |
| 26 | unsafe { |
| 27 | ap := &u64(addr - sizeof(u64)) |
| 28 | size := *ap |
| 29 | return sys_munmap(addr - sizeof(u64), size + sizeof(u64)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn system_alloc(_ voidptr, size usize) (voidptr, usize, u32) { |
| 34 | // BEGIN CONSTS |
| 35 | // the constants need to be here, since the initialization of other constants, |
| 36 | // which happen before these ones would, require malloc |
| 37 | mem_prot := unsafe { MemProt(int(MemProt.prot_read) | int(MemProt.prot_write)) } |
| 38 | map_flags := unsafe { MapFlags(int(MapFlags.map_private) | int(MapFlags.map_anonymous)) } |
| 39 | // END CONSTS |
| 40 | |
| 41 | a, e := sys_mmap(&u8(unsafe { nil }), u64(size), mem_prot, map_flags, -1, 0) |
| 42 | |
| 43 | if e == .enoerror { |
| 44 | return a, size, 0 |
| 45 | } |
| 46 | return unsafe { nil }, 0, 0 |
| 47 | } |
| 48 | |
| 49 | fn system_remap(_ voidptr, ptr voidptr, oldsize usize, newsize usize, can_move bool) voidptr { |
| 50 | return unsafe { nil } |
| 51 | } |
| 52 | |
| 53 | fn system_free_part(_ voidptr, ptr voidptr, oldsize usize, newsize usize) bool { |
| 54 | _, e := sys_mremap(ptr, u64(oldsize), u64(newsize), 0) |
| 55 | if e == .enoerror { |
| 56 | return true |
| 57 | } |
| 58 | e2 := sys_munmap(voidptr(usize(ptr) + newsize), u64(oldsize - newsize)) |
| 59 | |
| 60 | return e2 == .enoerror |
| 61 | } |
| 62 | |
| 63 | fn system_free(_ voidptr, ptr voidptr, size usize) bool { |
| 64 | unsafe { |
| 65 | return sys_munmap(ptr, u64(size)) == .enoerror |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn system_can_release_part(_ voidptr, _ u32) bool { |
| 70 | return true |
| 71 | } |
| 72 | |
| 73 | fn system_allocates_zeros(_ voidptr) bool { |
| 74 | return true |
| 75 | } |
| 76 | |
| 77 | fn system_page_size(_ voidptr) usize { |
| 78 | return 4096 |
| 79 | } |
| 80 | |
| 81 | fn get_linux_allocator() dlmalloc.Allocator { |
| 82 | return dlmalloc.Allocator{ |
| 83 | alloc: system_alloc |
| 84 | remap: system_remap |
| 85 | free_part: system_free_part |
| 86 | free_: system_free |
| 87 | can_release_part: system_can_release_part |
| 88 | allocates_zeros: system_allocates_zeros |
| 89 | page_size: system_page_size |
| 90 | data: unsafe { nil } |
| 91 | } |
| 92 | } |
| 93 | |