procs.odin 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package runtime
  2. when ODIN_NO_CRT && ODIN_OS == .Windows {
  3. foreign import lib "system:NtDll.lib"
  4. @(private="file")
  5. @(default_calling_convention="system")
  6. foreign lib {
  7. RtlMoveMemory :: proc(dst, s: rawptr, length: int) ---
  8. RtlFillMemory :: proc(dst: rawptr, length: int, fill: i32) ---
  9. }
  10. @(link_name="memset", linkage="strong", require)
  11. memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
  12. RtlFillMemory(ptr, len, val)
  13. return ptr
  14. }
  15. @(link_name="memmove", linkage="strong", require)
  16. memmove :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
  17. RtlMoveMemory(dst, src, len)
  18. return dst
  19. }
  20. @(link_name="memcpy", linkage="strong", require)
  21. memcpy :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
  22. RtlMoveMemory(dst, src, len)
  23. return dst
  24. }
  25. } else when ODIN_NO_CRT || (ODIN_OS != .Orca && (ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32)) {
  26. // NOTE: on wasm, calls to these procs are generated (by LLVM) with type `i32` instead of `int`.
  27. //
  28. // NOTE: `#any_int` is also needed, because calls that we generate (and package code)
  29. // will be using `int` and need to be converted.
  30. int_t :: i32 when ODIN_ARCH == .wasm64p32 else int
  31. @(link_name="memset", linkage="strong", require)
  32. memset :: proc "c" (ptr: rawptr, val: i32, #any_int len: int_t) -> rawptr {
  33. if ptr != nil && len != 0 {
  34. b := byte(val)
  35. p := ([^]byte)(ptr)
  36. for i := int_t(0); i < len; i += 1 {
  37. p[i] = b
  38. }
  39. }
  40. return ptr
  41. }
  42. @(link_name="bzero", linkage="strong", require)
  43. bzero :: proc "c" (ptr: rawptr, #any_int len: int_t) -> rawptr {
  44. if ptr != nil && len != 0 {
  45. p := ([^]byte)(ptr)
  46. for i := int_t(0); i < len; i += 1 {
  47. p[i] = 0
  48. }
  49. }
  50. return ptr
  51. }
  52. @(link_name="memmove", linkage="strong", require)
  53. memmove :: proc "c" (dst, src: rawptr, #any_int len: int_t) -> rawptr {
  54. d, s := ([^]byte)(dst), ([^]byte)(src)
  55. if d == s || len == 0 {
  56. return dst
  57. }
  58. if d > s && uintptr(d)-uintptr(s) < uintptr(len) {
  59. for i := len-1; i >= 0; i -= 1 {
  60. d[i] = s[i]
  61. }
  62. return dst
  63. }
  64. if s > d && uintptr(s)-uintptr(d) < uintptr(len) {
  65. for i := int_t(0); i < len; i += 1 {
  66. d[i] = s[i]
  67. }
  68. return dst
  69. }
  70. return memcpy(dst, src, len)
  71. }
  72. @(link_name="memcpy", linkage="strong", require)
  73. memcpy :: proc "c" (dst, src: rawptr, #any_int len: int_t) -> rawptr {
  74. d, s := ([^]byte)(dst), ([^]byte)(src)
  75. if d != s {
  76. for i := int_t(0); i < len; i += 1 {
  77. d[i] = s[i]
  78. }
  79. }
  80. return d
  81. }
  82. } else {
  83. memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
  84. if ptr != nil && len != 0 {
  85. b := byte(val)
  86. p := ([^]byte)(ptr)
  87. for i := 0; i < len; i += 1 {
  88. p[i] = b
  89. }
  90. }
  91. return ptr
  92. }
  93. }