Add GC support for Nintendo Switch
This commit is contained in:
parent
8a0bff973f
commit
c3490830e4
2 changed files with 53 additions and 0 deletions
24
lib/nintendoswitch/switch_memory.nim
Normal file
24
lib/nintendoswitch/switch_memory.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
const virtMemHeader = "<switch/kernel/virtmem.h>"
|
||||
const svcHeader = "<switch/kernel/virtmem.h>"
|
||||
const mallocHeader = "<malloc.h>"
|
||||
|
||||
type
|
||||
Result* = uint32
|
||||
|
||||
proc memalign*(bytes: csize, size: csize): pointer {.importc: "memalign",
|
||||
header: mallocHeader.}
|
||||
|
||||
proc free*(address: pointer) {.importc: "free",
|
||||
header: mallocHeader.}
|
||||
|
||||
proc svcMapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): Result {.
|
||||
importc: "svcMapMemory", header: svcHeader.}
|
||||
|
||||
proc svcUnmapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): Result {.
|
||||
importc: "svcUnmapMemory", header: svcHeader.}
|
||||
|
||||
proc virtmemReserveMap*(size: csize): pointer {.importc: "virtmemReserveMap",
|
||||
header: virtMemHeader.}
|
||||
|
||||
proc virtmemFreeMap*(address: pointer; size: csize) {.importc: "virtmemFreeMap",
|
||||
header: virtMemHeader.}
|
||||
|
|
@ -80,6 +80,35 @@ when defined(emscripten):
|
|||
elif defined(genode):
|
||||
include genode/alloc # osAllocPages, osTryAllocPages, osDeallocPages
|
||||
|
||||
elif defined(nintendoswitch):
|
||||
import nintendoswitch/switch_memory
|
||||
|
||||
var
|
||||
stack: pointer
|
||||
|
||||
proc osAllocPages(size: int): pointer {.inline.} =
|
||||
stack = memalign(0x1000, size)
|
||||
result = virtmemReserveMap(size.csize)
|
||||
let rc = svcMapMemory(result, stack, size.uint64)
|
||||
if rc.uint32 != 0:
|
||||
#discard svcUnmapMemory(result, stack, size.csize)
|
||||
raiseOutOfMem()
|
||||
|
||||
proc osTryAllocPages(size: int): pointer {.inline.} =
|
||||
stack = memalign(0x1000, size)
|
||||
result = virtmemReserveMap(size.csize)
|
||||
let rc = svcMapMemory(result, stack, size.uint64)
|
||||
if rc.uint32 != 0:
|
||||
#discard svcUnmapMemory(result, stack, size.csize)
|
||||
result = nil
|
||||
|
||||
proc osDeallocPages(p: pointer, size: int) {.inline.} =
|
||||
when reallyOsDealloc:
|
||||
discard svcUnmapMemory(p, stack, size.uint64)
|
||||
virtmemFreeMap(p, size.csize)
|
||||
free(stack)
|
||||
|
||||
|
||||
elif defined(posix):
|
||||
const
|
||||
PROT_READ = 1 # page can be read
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue