Skip to content

Commit

Permalink
vlib: handle some C. calls
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov committed Oct 31, 2023
1 parent a63f3e6 commit 6aebfcb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
16 changes: 0 additions & 16 deletions vlib/db/pg/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ pub fn (db DB) drop(table string) ! {

// utils

fn pg_stmt_worker(db DB, query string, data orm.QueryData, where orm.QueryData) ![]Row {
mut param_types := []u32{}
mut param_vals := []&char{}
mut param_lens := []int{}
mut param_formats := []int{}

pg_stmt_binder(mut param_types, mut param_vals, mut param_lens, mut param_formats,
data)
pg_stmt_binder(mut param_types, mut param_vals, mut param_lens, mut param_formats,
where)

res := C.PQexecParams(db.conn, &char(query.str), param_vals.len, param_types.data,
param_vals.data, param_lens.data, param_formats.data, 0) // here, the last 0 means require text results, 1 - binary results
return db.handle_error_or_result(res, 'orm_stmt_worker')
}

fn pg_stmt_binder(mut types []u32, mut vals []&char, mut lens []int, mut formats []int, d orm.QueryData) {
for data in d.data {
pg_stmt_match(mut types, mut vals, mut lens, mut formats, data)
Expand Down
16 changes: 16 additions & 0 deletions vlib/db/pg/pg.v → vlib/db/pg/pg.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,19 @@ pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) !int {

return 0
}

fn pg_stmt_worker(db DB, query string, data orm.QueryData, where orm.QueryData) ![]Row {
mut param_types := []u32{}
mut param_vals := []&char{}
mut param_lens := []int{}
mut param_formats := []int{}

pg_stmt_binder(mut param_types, mut param_vals, mut param_lens, mut param_formats,
data)
pg_stmt_binder(mut param_types, mut param_vals, mut param_lens, mut param_formats,
where)

res := C.PQexecParams(db.conn, &char(query.str), param_vals.len, param_types.data,
param_vals.data, param_lens.data, param_formats.data, 0) // here, the last 0 means require text results, 1 - binary results
return db.handle_error_or_result(res, 'orm_stmt_worker')
}
File renamed without changes.
18 changes: 9 additions & 9 deletions vlib/net/websocket/websocket_client.v
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
} else if payload_len > 125 && payload_len <= 0xffff {
len16 := conv.hton16(u16(payload_len))
header[1] = 126
unsafe { C.memcpy(&header[2], &len16, 2) }
unsafe { vmemcpy(&header[2], &len16, 2) }
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
len_bytes := htonl64(u64(payload_len))
header[1] = 127
unsafe { C.memcpy(&header[2], len_bytes.data, 8) }
unsafe { vmemcpy(&header[2], len_bytes.data, 8) }
}
} else {
if payload_len <= 125 {
Expand All @@ -280,15 +280,15 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
} else if payload_len > 125 && payload_len <= 0xffff {
len16 := conv.hton16(u16(payload_len))
header[1] = (126 | 0x80)
unsafe { C.memcpy(&header[2], &len16, 2) }
unsafe { vmemcpy(&header[2], &len16, 2) }
header[4] = masking_key[0]
header[5] = masking_key[1]
header[6] = masking_key[2]
header[7] = masking_key[3]
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
len64 := htonl64(u64(payload_len))
header[1] = (127 | 0x80)
unsafe { C.memcpy(&header[2], len64.data, 8) }
unsafe { vmemcpy(&header[2], len64.data, 8) }
header[10] = masking_key[0]
header[11] = masking_key[1]
header[12] = masking_key[2]
Expand All @@ -301,9 +301,9 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
len := header.len + payload_len
mut frame_buf := []u8{len: len}
unsafe {
C.memcpy(&frame_buf[0], &u8(header.data), header.len)
vmemcpy(&frame_buf[0], &u8(header.data), header.len)
if payload_len > 0 {
C.memcpy(&frame_buf[header.len], bytes, payload_len)
vmemcpy(&frame_buf[header.len], bytes, payload_len)
}
}
if !ws.is_server {
Expand Down Expand Up @@ -396,14 +396,14 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u
if payload.len >= 2 {
if !ws.is_server {
mut parsed_payload := []u8{len: payload.len + 1}
unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) }
unsafe { vmemcpy(parsed_payload.data, &payload[0], payload.len) }
parsed_payload[payload.len] = `\0`
for i in 0 .. payload.len {
control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff
}
unsafe { parsed_payload.free() }
} else {
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
unsafe { vmemcpy(&control_frame[2], &payload[0], payload.len) }
}
}
} else {
Expand All @@ -415,7 +415,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u
}
} else {
if payload.len > 0 {
unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) }
unsafe { vmemcpy(&control_frame[2], &payload[0], payload.len) }
}
}
}
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions vlib/v/slow_tests/valgrind/base64.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
//
encoded_size := base64.encode_in_buffer(s_original, ebuffer)
mut encoded_in_buf := []u8{len: encoded_size}
unsafe { C.memcpy(encoded_in_buf.data, ebuffer, encoded_size) }
unsafe { vmemcpy(encoded_in_buf.data, ebuffer, encoded_size) }
assert input_size * 4 / 3 == encoded_size
assert encoded_in_buf[0] == `Y`
assert encoded_in_buf[1] == `W`
Expand All @@ -38,7 +38,7 @@ fn main() {
decoded_size := base64.decode_in_buffer(s_encoded, dbuffer)
assert decoded_size == input_size
mut decoded_in_buf := []u8{len: decoded_size}
unsafe { C.memcpy(decoded_in_buf.data, dbuffer, decoded_size) }
unsafe { vmemcpy(decoded_in_buf.data, dbuffer, decoded_size) }
assert decoded_in_buf == s_original

mut s := 0
Expand Down
2 changes: 1 addition & 1 deletion vlib/x/ttf/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn (mut bmp BitMap) format_texture() {
pub fn (mut bmp BitMap) save_as_ppm(file_name string) {
tmp_buf := bmp.buf
mut buf := unsafe { malloc_noscan(bmp.buf_size) }
unsafe { C.memcpy(buf, tmp_buf, bmp.buf_size) }
unsafe { vmemcpy(buf, tmp_buf, bmp.buf_size) }
bmp.buf = buf

bmp.format_texture()
Expand Down

0 comments on commit 6aebfcb

Please sign in to comment.