From 624c1f3bcf0518a4ab23b8150da21b3e686cf45c Mon Sep 17 00:00:00 2001 From: spaceface Date: Thu, 18 Mar 2021 15:23:29 +0100 Subject: [PATCH] cgen: make `bool`s take up a single byte, not 4 (#9352) --- vlib/builtin/map.v | 16 ++++++++-------- vlib/net/tcp.v | 3 ++- vlib/net/udp.v | 3 ++- vlib/v/gen/c/cheaders.v | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 8b5f857433..6acbd49484 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -260,19 +260,19 @@ fn map_eq_string(a voidptr, b voidptr) bool { } fn map_eq_int_1(a voidptr, b voidptr) bool { - return unsafe { *&byte(a) == *&byte(b) } + return unsafe { C.memcmp(a, b, 1) == 0 } } fn map_eq_int_2(a voidptr, b voidptr) bool { - return unsafe { *&u16(a) == *&u16(b) } + return unsafe { C.memcmp(a, b, 2) == 0 } } fn map_eq_int_4(a voidptr, b voidptr) bool { - return unsafe { *&u32(a) == *&u32(b) } + return unsafe { C.memcmp(a, b, 4) == 0 } } fn map_eq_int_8(a voidptr, b voidptr) bool { - return unsafe { *&u64(a) == *&u64(b) } + return unsafe { C.memcmp(a, b, 8) == 0 } } fn map_clone_string(dest voidptr, pkey voidptr) { @@ -284,25 +284,25 @@ fn map_clone_string(dest voidptr, pkey voidptr) { fn map_clone_int_1(dest voidptr, pkey voidptr) { unsafe { - *&byte(dest) = *&byte(pkey) + C.memcpy(dest, pkey, 1) } } fn map_clone_int_2(dest voidptr, pkey voidptr) { unsafe { - *&u16(dest) = *&u16(pkey) + C.memcpy(dest, pkey, 2) } } fn map_clone_int_4(dest voidptr, pkey voidptr) { unsafe { - *&u32(dest) = *&u32(pkey) + C.memcpy(dest, pkey, 4) } } fn map_clone_int_8(dest voidptr, pkey voidptr) { unsafe { - *&u64(dest) = *&u64(pkey) + C.memcpy(dest, pkey, 8) } } diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index 0c24daa9b0..8f2b5e4fcf 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -295,7 +295,8 @@ pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ? { // if opt !in opts_bool { // return err_option_wrong_type // } - socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ? + x := int(value) + socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int))) ? return none } diff --git a/vlib/net/udp.v b/vlib/net/udp.v index 8f674445f7..be6ba90724 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -211,7 +211,8 @@ pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? { // if opt !in opts_bool { // return err_option_wrong_type // } - socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ? + x := int(value) + socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &x, sizeof(int))) ? return none } diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index c9fec54afe..12c26c4804 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -483,7 +483,7 @@ typedef struct sync__Channel* chan; #ifndef __cplusplus #ifndef bool - typedef int bool; + typedef byte bool; #define true 1 #define false 0 #endif