switch_memory.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. ## All of these library headers and source can be found in the github repo
  2. ## https://github.com/switchbrew/libnx.
  3. const virtMemHeader = "<switch/kernel/virtmem.h>"
  4. const svcHeader = "<switch/kernel/svc.h>"
  5. const mallocHeader = "<malloc.h>"
  6. ## Aligns a block of memory with request `size` to `bytes` size. For
  7. ## example, a request of memalign(0x1000, 0x1001) == 0x2000 bytes allocated
  8. proc memalign*(bytes: csize, size: csize): pointer {.importc: "memalign",
  9. header: mallocHeader.}
  10. # Should be required, but not needed now because of how
  11. # svcUnmapMemory frees all memory
  12. #proc free*(address: pointer) {.importc: "free",
  13. # header: mallocHeader.}
  14. ## Maps a memaligned block of memory from `src_addr` to `dst_addr`. The
  15. ## Nintendo Switch requires this call in order to make use of memory, otherwise
  16. ## an invalid memory access occurs.
  17. proc svcMapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): uint32 {.
  18. importc: "svcMapMemory", header: svcHeader.}
  19. ## Unmaps (frees) all memory from both `dst_addr` and `src_addr`. **Must** be called
  20. ## whenever svcMapMemory is used. The Switch will expect all memory to be allocated
  21. ## before gfxExit() calls (<switch/gfx/gfx.h>)
  22. proc svcUnmapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): uint32 {.
  23. importc: "svcUnmapMemory", header: svcHeader.}
  24. proc virtmemReserveMap*(size: csize): pointer {.importc: "virtmemReserveMap",
  25. header: virtMemHeader.}
  26. # Should be required, but not needed now because of how
  27. # svcUnmapMemory frees all memory
  28. #proc virtmemFreeMap*(address: pointer; size: csize) {.importc: "virtmemFreeMap",
  29. # header: virtMemHeader.}