summary refs log tree commit diff stats
path: root/lib/nintendoswitch
diff options
context:
space:
mode:
authorJoey <jyapayne@gmail.com>2018-07-05 23:33:15 +0900
committerAndreas Rumpf <rumpf_a@web.de>2018-07-05 16:33:15 +0200
commita1457bfa9ffad3e59b49380fce70d29720485e7d (patch)
tree4cefb565430c226651681586e517c71dfbab158a /lib/nintendoswitch
parentf7b76c9ea71ca77d2ed32612b5770cd816672eeb (diff)
downloadNim-a1457bfa9ffad3e59b49380fce70d29720485e7d.tar.gz
Rewrite the memory management code for Nintendo Switch (#8169)
Rewrite the memory management code for Nintendo Switch

The first implementation was naive and did not account for multiple
memory allocations. However, this implementation may still be
incomplete. Currently, when running applications, the code runs fine.
When the application is exited via code (the end of the program is
reached or quit() is called), the Switch will crash. Not sure why this
happens, but I suspect it is from Nim memory allocations.

I suspect the memory allocations because when I compile the helloworld
application without any Nim allocations (just C function calls) and use
`--gc:none` as a compile option, the application exits fine.

Diffstat (limited to 'lib/nintendoswitch')
-rw-r--r--lib/nintendoswitch/switch_memory.nim25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/nintendoswitch/switch_memory.nim b/lib/nintendoswitch/switch_memory.nim
index 09b34c5d0..f34bd363a 100644
--- a/lib/nintendoswitch/switch_memory.nim
+++ b/lib/nintendoswitch/switch_memory.nim
@@ -1,21 +1,36 @@
+## All of these library headers and source can be found in the github repo
+## https://github.com/switchbrew/libnx.
+
 const virtMemHeader = "<switch/kernel/virtmem.h>"
-const svcHeader = "<switch/kernel/virtmem.h>"
+const svcHeader = "<switch/kernel/svc.h>"
 const mallocHeader = "<malloc.h>"
 
+## Aligns a block of memory with request `size` to `bytes` size. For
+## example, a request of memalign(0x1000, 0x1001) == 0x2000 bytes allocated
 proc memalign*(bytes: csize, size: csize): pointer {.importc: "memalign",
     header: mallocHeader.}
 
-proc free*(address: pointer) {.importc: "free",
-    header: mallocHeader.}
+# Should be required, but not needed now because of how
+# svcUnmapMemory frees all memory
+#proc free*(address: pointer) {.importc: "free",
+#    header: mallocHeader.}
 
+## Maps a memaligned block of memory from `src_addr` to `dst_addr`. The
+## Nintendo Switch requires this call in order to make use of memory, otherwise
+## an invalid memory access occurs.
 proc svcMapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): uint32 {.
     importc: "svcMapMemory", header: svcHeader.}
 
+## Unmaps (frees) all memory from both `dst_addr` and `src_addr`. **Must** be called
+## whenever svcMapMemory is used. The Switch will expect all memory to be allocated
+## before gfxExit() calls (<switch/gfx/gfx.h>)
 proc svcUnmapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): uint32 {.
     importc: "svcUnmapMemory", header: svcHeader.}
 
 proc virtmemReserveMap*(size: csize): pointer {.importc: "virtmemReserveMap",
     header: virtMemHeader.}
 
-proc virtmemFreeMap*(address: pointer; size: csize) {.importc: "virtmemFreeMap",
-    header: virtMemHeader.}
+# Should be required, but not needed now because of how
+# svcUnmapMemory frees all memory
+#proc virtmemFreeMap*(address: pointer; size: csize) {.importc: "virtmemFreeMap",
+#    header: virtMemHeader.}