1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package runtime
@require foreign import "system:int64.lib"
foreign import kernel32 "system:Kernel32.lib"
windows_trap_array_bounds :: proc "contextless" () -> ! {
DWORD :: u32;
ULONG_PTR :: uint;
EXCEPTION_ARRAY_BOUNDS_EXCEEDED :: 0xC000008C;
foreign kernel32 {
RaiseException :: proc "stdcall" (dwExceptionCode, dwExceptionFlags, nNumberOfArguments: DWORD, lpArguments: ^ULONG_PTR) -> ! ---
}
RaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED, 0, 0, nil);
}
windows_trap_type_assertion :: proc "contextless" () -> ! {
windows_trap_array_bounds();
}
@(private, require, link_name="_fltused") _fltused: i32 = 0x9875;
@(private, require, link_name="_tls_index") _tls_index: u32;
@(private, require, link_name="_tls_array") _tls_array: u32;
@(link_name="memcpy")
memcpy :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
if dst == nil || src == nil || len == 0 || dst == src {
return dst;
}
d := uintptr(dst);
s := uintptr(src);
n := uintptr(len);
for i in 0..<n {
(^byte)(d+i)^ = (^byte)(s+i)^;
}
return dst;
}
@(link_name="memmove")
memmove :: proc "c" (dst, src: rawptr, len: int) -> rawptr {
if dst == nil || src == nil || len == 0 || dst == src {
return dst;
}
d := uintptr(dst);
s := uintptr(src);
n := uintptr(len);
if s < d && d < s+n {
// Overlap
for i := n-1; n >= 0; i -= 1 {
(^byte)(d+i)^ = (^byte)(s+i)^;
}
} else {
for i in 0..<n {
(^byte)(d+i)^ = (^byte)(s+i)^;
}
}
return dst;
}
@(link_name="memset")
memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
if ptr == nil || len == 0 {
return ptr;
}
d := uintptr(ptr);
b := byte(val);
for i in 0..<uintptr(len) {
(^byte)(d+i)^ = b;
}
return ptr;
}
// @(link_name="memcmp")
// memcmp :: proc "c" (dst, src: rawptr, len: int) -> i32 {
// if dst == nil || src == nil {
// return 0;
// }
// if dst == src {
// return 0;
// }
// d, s := uintptr(dst), uintptr(src);
// n := uintptr(len);
// for i := uintptr(0); i < n; i += 1 {
// x, y := (^byte)(d+i)^, (^byte)(s+i)^;
// if x != y {
// return x < y ? -1 : +1;
// }
// }
// return 0;
// }
|