Skip to content

Commit

Permalink
macos/x64: use valloc with mmap instead of malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed Feb 26, 2022
1 parent c1eca98 commit decc52a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 49 deletions.
21 changes: 18 additions & 3 deletions core/source/macos/x64/fasmg.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ include '../../version.inc'
interpreter '/usr/lib/dyld'
uses '/usr/lib/libSystem.B.dylib'

import libc.malloc,'_malloc',\
libc.realloc,'_realloc',\
libc.free,'_free',\
import libc.free,'_free',\
libc.fopen,'_fopen',\
libc.fclose,'_fclose',\
libc.fread,'_fread',\
Expand All @@ -32,6 +30,8 @@ import libc.malloc,'_malloc',\
libc.ftell,'_ftell',\
libc.time,'_time',\
libc.write,'_write',\
mmap,'_mmap',\
munmap,'_munmap',\
getenv,'_getenv',\
gettimeofday,'_gettimeofday',\
exit,'_exit'
Expand Down Expand Up @@ -459,6 +459,7 @@ section '__text' align 16

use32on64

include '../../malloc.inc'
include '../../assembler.inc'
include '../../symbols.inc'
include '../../expressions.inc'
Expand Down Expand Up @@ -509,6 +510,9 @@ section '__bss' align 4

include '../../variables.inc'

mmap_hint dd ?
malloc_freelist dd ?

source_path dd ?
output_path dd ?
maximum_number_of_passes dd ?
Expand All @@ -532,4 +536,15 @@ section '__bss' align 4
verbosity_level dd ?
no_logo db ?

local_heap_available db ?

path_buffer rb 1000h

segment '__DATA' readable writable

align 1000h

LOCAL_HEAP_SIZE = 1000000h

local_heap rb LOCAL_HEAP_SIZE

116 changes: 70 additions & 46 deletions core/source/macos/x64/system.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,14 @@
LINE_FEED = 0Ah

system_init:
or [local_heap_available],1
ccall libc.time,timestamp
retn

system_shutdown:
retn

malloc:
malloc_fixed:
malloc_growable:
; in: ecx = requested size
; out: eax - allocated block, ecx = allocated size, on error jumps to out_of_memory (does not return)
; preserves: ebx, esi, edi
; note:
; use of malloc_fixed hints that block will be kept as is until the end of assembly
; use of malloc_growable hints that block is likely to be resized
push rbx rcx rsi rdi
ccall libc.malloc,rcx
pop rdi rsi rcx rbx
test eax,eax
jz out_of_memory
lea rdx,[rax+rcx-1]
shr rdx,32
jnz out_of_memory
retn
realloc:
; in: eax - memory block, ecx = requested size
; out: eax - resized block, ecx = allocated size, on error jumps to out_of_memory (does not return)
; preserves: ebx, esi, edi
push rbx rcx rsi rdi
ccall libc.realloc,rax,rcx
pop rdi rsi rcx rbx
test eax,eax
jz out_of_memory
lea rdx,[rax+rcx-1]
shr rdx,32
jnz out_of_memory
retn
mfree:
; in: eax - memory block
; out: cf set on error
; preserves: ebx, esi, edi
; note: eax may have value 0 or -1, it should be treated as invalid input then
test eax,eax
jz interface_error
cmp eax,-1
je interface_error
push rbx rsi rdi
ccall libc.free,rax
pop rdi rsi rbx
clc
retn
interface_error:
interface_error:
stc
retn

Expand Down Expand Up @@ -263,3 +219,71 @@ get_environment_variable:
jecxz environment_variable_ok
and byte [edi],0
ret

VALLOC_MINIMUM_SIZE = 100000h

valloc:
; in: ecx = requested minimum size
; out: eax - allocated block, ecx = allocated size, zero if failed
; preserves: ebx, esi, edi
cmp ecx,VALLOC_MINIMUM_SIZE
jbe valloc_size_minimum
dec ecx
and ecx,(-1) shl 12
add ecx,1 shl 12
jmp valloc_size_ready
valloc_size_minimum:
mov ecx,VALLOC_MINIMUM_SIZE
valloc_size_ready:
push rbx rsi rdi
cmp [local_heap_available],0
je valloc_mmap
cmp ecx,LOCAL_HEAP_SIZE
ja valloc_mmap
and [local_heap_available],0
mov eax,local_heap
mov ecx,LOCAL_HEAP_SIZE
jmp valloc_ok
valloc_mmap:
push rcx
ccall mmap,0,rcx, \
3, \ ; PROT_READ + PROT_WRITE
62h, \ ; MAP_PRIVATE + MAP_ANONYMOUS + MAP_32BIT
-1,0
pop rsi
cmp eax,-1
je valloc_mmap_with_hint
mov ecx,eax
cmp rcx,rax
jne valloc_mmap_unusable
add ecx,esi
jnc mmap_ok
valloc_mmap_unusable:
ccall munmap,rax,rsi
valloc_mmap_with_hint:
push rcx
mov edi,[mmap_hint]
ccall mmap,rdi,rcx, \
3, \ ; PROT_READ + PROT_WRITE
22h, \ ; MAP_PRIVATE + MAP_ANONYMOUS
-1,0
pop rsi
cmp eax,-1
je valloc_failed
mov ecx,eax
cmp rcx,rax
jne valloc_failed
add ecx,esi
jc valloc_failed
mmap_ok:
sub ecx,eax
valloc_ok:
lea edx,[eax+ecx]
mov [mmap_hint],edx
pop rdi rsi rbx
retn
valloc_failed:
xor ecx,ecx
pop rdi rsi rbx
retn

0 comments on commit decc52a

Please sign in to comment.