unsafe: tag with unsafe{} some more pointer manipulations

pull/5941/head
Delyan Angelov 2020-07-22 21:42:51 +03:00
parent 949ed90b51
commit 276c1de190
9 changed files with 44 additions and 25 deletions

View File

@ -478,11 +478,13 @@ fn test_reverse() {
fn test_bytes_to_string() {
mut buf := vcalloc(10)
buf[0] = `h`
buf[1] = `e`
buf[2] = `l`
buf[3] = `l`
buf[4] = `o`
unsafe {
buf[0] = `h`
buf[1] = `e`
buf[2] = `l`
buf[3] = `l`
buf[4] = `o`
}
assert string(buf) == 'hello'
assert string(buf, 2) == 'he'
bytes := [`h`, `e`, `l`, `l`, `o`]

View File

@ -381,12 +381,12 @@ fn (cb &Clipboard) pick_target(prop Property) C.Atom {
//See if this data type is allowed and of higher priority (closer to zero)
//than the present one.
if cb.is_supported_target(atom_list[i]) {
index := cb.get_target_index(atom_list[i])
if priority > index && index >= 0
{
target := unsafe{ atom_list[i] }
if cb.is_supported_target(target) {
index := cb.get_target_index(target)
if priority > index && index >= 0 {
priority = index
to_be_requested = atom_list[i]
to_be_requested = target
}
}
}

View File

@ -36,5 +36,5 @@ fn getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > read_batch_size {
panic('getrandom() dont request more than $read_batch_size bytes at once.')
}
return C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0)
return unsafe { C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) }
}

View File

@ -48,7 +48,7 @@ fn (dtp DTP) read() []byte {
break
}
for i in 0 .. len {
data << buf[i]
data << unsafe { buf[i] }
}
unsafe {
free(buf)

View File

@ -2,17 +2,17 @@ module picohttpparser
[inline] [unsafe_fn]
fn cpy(dst, src byteptr, len int) int {
C.memcpy(dst, src, len)
unsafe { C.memcpy(dst, src, len) }
return len
}
[inline]
pub fn cmp(dst, src string) bool {
if dst.len != src.len { return false }
return C.memcmp(dst.str, src.str, src.len) == 0
return unsafe { C.memcmp(dst.str, src.str, src.len) == 0 }
}
[inline]
pub fn cmpn(dst, src string, n int) bool {
return C.memcmp(dst.str, src.str, n) == 0
return unsafe { C.memcmp(dst.str, src.str, n) == 0 }
}

View File

@ -24,7 +24,9 @@ fn test_autolocked_array() {
for {
mut finished_threads := 0
rlock abc {
finished_threads = abc[0]
finished_threads = unsafe {
abc[0]
}
}
if finished_threads == 2 {
break

View File

@ -17,14 +17,16 @@ fn test_autolocked_array_2() {
go inc_elements(shared abc, 1, sem)
go inc_elements(shared abc, 2, sem)
for _ in 0 .. iterations_per_thread2 {
abc[2]++
unsafe {
abc[2]++
}
}
// wait for the 2 coroutines to finish using the semaphore
for _ in 0 .. 2 {
sem.wait()
}
rlock abc {
assert abc[1] == iterations_per_thread2
assert abc[2] == 2 * iterations_per_thread2
assert unsafe { abc[1] } == iterations_per_thread2
assert unsafe { abc[2] } == 2 * iterations_per_thread2
}
}

View File

@ -13,7 +13,7 @@ fn test_semaphore() {
sem := sync.new_semaphore()
go run_forever(shared abc, sem)
for _ in 0 .. 1000 {
abc[0]--
unsafe { abc[0]-- }
}
// wait for the 2 coroutines to finish using the semaphore
stopwatch := time.new_stopwatch({})

View File

@ -20,14 +20,19 @@ fn test_shared_array() {
go incr(shared foo, 1)
for _ in 0 .. 50000 {
lock foo {
foo[0] -= 2
foo[1] += 3
unsafe {
foo[0] -= 2
foo[1] += 3
}
}
}
mut finished_threads := 0
for {
rlock foo {
finished_threads = foo[2]
finished_threads = unsafe {
foo[2]
}
}
if finished_threads == 4 {
break
@ -35,7 +40,15 @@ fn test_shared_array() {
time.sleep_ms(100)
}
rlock foo {
assert foo[0] == 100010
assert foo[1] == 350020
f0 := unsafe {
foo[0]
}
f1 := unsafe {
foo[1]
}
assert f0 == 100010
assert f1 == 350020
}
}