/red/runtime/imports.reds

http://github.com/dockimbel/Red · Redscript · 129 lines · 117 code · 12 blank · 0 comment · 4 complexity · d40871b5dc4e93fd06fd24eb97d3250a MD5 · raw file

  1. Red/System [
  2. Title: "Red runtime OS API imported functions definitions"
  3. Author: "Nenad Rakocevic"
  4. File: %imports.reds
  5. Rights: "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
  6. License: {
  7. Distributed under the Boost Software License, Version 1.0.
  8. See https://github.com/dockimbel/Red/blob/master/red-system/runtime/BSL-License.txt
  9. }
  10. ]
  11. #define OS-page-size 4096 ;@@ target/OS dependent
  12. #either OS = 'Windows [
  13. #import [
  14. "kernel32.dll" stdcall [
  15. OS-VirtualAlloc: "VirtualAlloc" [
  16. address [byte-ptr!]
  17. size [integer!]
  18. type [integer!]
  19. protection [integer!]
  20. return: [int-ptr!]
  21. ]
  22. OS-VirtualFree: "VirtualFree" [
  23. address [int-ptr!]
  24. size [integer!]
  25. return: [integer!]
  26. ]
  27. ]
  28. ]
  29. #define VA_COMMIT_RESERVE 3000h ;-- MEM_COMMIT | MEM_RESERVE
  30. #define VA_PAGE_RW 04h ;-- PAGE_READWRITE
  31. #define VA_PAGE_RWX 40h ;-- PAGE_EXECUTE_READWRITE
  32. ;-------------------------------------------
  33. ;-- Allocate paged virtual memory region from OS (Windows)
  34. ;-------------------------------------------
  35. OS-allocate-virtual: func [
  36. size [integer!] ;-- allocated size in bytes (page size multiple)
  37. exec? [logic!] ;-- TRUE => executable region
  38. return: [int-ptr!] ;-- allocated memory region pointer
  39. /local ptr prot
  40. ][
  41. prot: either exec? [VA_PAGE_RWX][VA_PAGE_RW]
  42. ptr: OS-VirtualAlloc
  43. null
  44. size
  45. VA_COMMIT_RESERVE
  46. prot
  47. if ptr = null [
  48. raise-error RED_ERR_VMEM_OUT_OF_MEMORY 0
  49. ]
  50. ptr
  51. ]
  52. ;-------------------------------------------
  53. ;-- Free paged virtual memory region from OS (Windows)
  54. ;-------------------------------------------
  55. OS-free-virtual: func [
  56. ptr [int-ptr!] ;-- address of memory region to release
  57. ][
  58. if negative? OS-VirtualFree ptr ptr/value [
  59. raise-error RED_ERR_VMEM_RELEASE_FAILED as-integer ptr
  60. ]
  61. ]
  62. ][
  63. #define MMAP_PROT_RW 03h ;-- PROT_READ | PROT_WRITE
  64. #define MMAP_PROT_RWX 07h ;-- PROT_READ | PROT_WRITE | PROT_EXEC
  65. #define MMAP_MAP_SHARED 01h
  66. #define MMAP_MAP_PRIVATE 02h
  67. #define MMAP_MAP_ANONYMOUS 20h
  68. #syscall [
  69. OS-mmap: SYSCALL_MMAP2 [
  70. address [byte-ptr!]
  71. size [integer!]
  72. protection [integer!]
  73. flags [integer!]
  74. fd [integer!]
  75. offset [integer!]
  76. return: [byte-ptr!]
  77. ]
  78. OS-munmap: SYSCALL_MUNMAP [
  79. address [byte-ptr!]
  80. size [integer!]
  81. return: [integer!]
  82. ]
  83. ]
  84. ;-------------------------------------------
  85. ;-- Allocate paged virtual memory region from OS (UNIX)
  86. ;-------------------------------------------
  87. OS-allocate-virtual: func [
  88. size [integer!] ;-- allocated size in bytes (page size multiple)
  89. exec? [logic!] ;-- TRUE => executable region
  90. return: [int-ptr!] ;-- allocated memory region pointer
  91. /local ptr prot
  92. ][
  93. assert zero? (size and 0Fh) ;-- size is a multiple of 16
  94. prot: either exec? [MMAP_PROT_RWX][MMAP_PROT_RW]
  95. ptr: OS-mmap
  96. null
  97. size
  98. prot
  99. MMAP_MAP_PRIVATE or MMAP_MAP_ANONYMOUS
  100. -1 ;-- portable value
  101. 0
  102. if negative? as-integer ptr [
  103. raise-error RED_ERR_VMEM_OUT_OF_MEMORY as-integer system/pc
  104. ]
  105. as int-ptr! ptr
  106. ]
  107. ;-------------------------------------------
  108. ;-- Free paged virtual memory region from OS (UNIX)
  109. ;-------------------------------------------
  110. OS-free-virtual: func [
  111. ptr [int-ptr!] ;-- address of memory region to release
  112. ][
  113. if negative? OS-munmap as byte-ptr! ptr ptr/value [
  114. raise-error RED_ERR_VMEM_RELEASE_FAILED as-integer system/pc
  115. ]
  116. ]
  117. ]