mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
vlib: replace macros that resolve to __builtin_bswapnn calls for tcc (#19305)
The tcc compiler does not have __builtin_bswap64, __builtin_bswap32, and __builtin_bswap16 functions. The various hton and ntoh macros resolve down to these functions. When compiling with tcc, we should be using the analogous functions from net.conv.
This commit is contained in:
parent
bea12e2623
commit
2d4ccf6829
4 changed files with 59 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
|||
module net
|
||||
|
||||
import io.util
|
||||
import net.conv
|
||||
import os
|
||||
|
||||
union AddrData {
|
||||
|
@ -16,11 +17,17 @@ const (
|
|||
|
||||
// new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr
|
||||
pub fn new_ip6(port u16, addr [16]u8) Addr {
|
||||
n_port := $if tinyc {
|
||||
conv.hton16(port)
|
||||
} $else {
|
||||
u16(C.htons(port))
|
||||
}
|
||||
|
||||
a := Addr{
|
||||
f: u8(AddrFamily.ip6)
|
||||
addr: AddrData{
|
||||
Ip6: Ip6{
|
||||
port: u16(C.htons(port))
|
||||
port: n_port
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,11 +39,17 @@ pub fn new_ip6(port u16, addr [16]u8) Addr {
|
|||
|
||||
// new_ip creates a new Addr from the IPv4 address family, based on the given port and addr
|
||||
pub fn new_ip(port u16, addr [4]u8) Addr {
|
||||
n_port := $if tinyc {
|
||||
conv.hton16(port)
|
||||
} $else {
|
||||
u16(C.htons(port))
|
||||
}
|
||||
|
||||
a := Addr{
|
||||
f: u8(AddrFamily.ip)
|
||||
addr: AddrData{
|
||||
Ip: Ip{
|
||||
port: u16(C.htons(port))
|
||||
port: n_port
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +92,11 @@ pub fn (a Ip) str() string {
|
|||
}
|
||||
|
||||
saddr := unsafe { cstring_to_vstring(&buf[0]) }
|
||||
port := C.ntohs(a.port)
|
||||
port := $if tinyc {
|
||||
conv.hton16(a.port)
|
||||
} $else {
|
||||
C.ntohs(a.port)
|
||||
}
|
||||
|
||||
return '${saddr}:${port}'
|
||||
}
|
||||
|
@ -95,7 +112,11 @@ pub fn (a Ip6) str() string {
|
|||
}
|
||||
|
||||
saddr := unsafe { cstring_to_vstring(&buf[0]) }
|
||||
port := C.ntohs(a.port)
|
||||
port := $if tinyc {
|
||||
conv.hton16(a.port)
|
||||
} $else {
|
||||
C.ntohs(a.port)
|
||||
}
|
||||
|
||||
return '[${saddr}]:${port}'
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
module websocket
|
||||
|
||||
import net
|
||||
import net.conv
|
||||
import net.http
|
||||
import net.ssl
|
||||
import net.urllib
|
||||
|
@ -261,7 +262,12 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
|
|||
if payload_len <= 125 {
|
||||
header[1] = u8(payload_len)
|
||||
} else if payload_len > 125 && payload_len <= 0xffff {
|
||||
len16 := C.htons(payload_len)
|
||||
len16 := $if tinyc {
|
||||
conv.hton16(u16(payload_len))
|
||||
} $else {
|
||||
C.htons(payload_len)
|
||||
}
|
||||
|
||||
header[1] = 126
|
||||
unsafe { C.memcpy(&header[2], &len16, 2) }
|
||||
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
|
||||
|
@ -277,7 +283,11 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
|
|||
header[4] = masking_key[2]
|
||||
header[5] = masking_key[3]
|
||||
} else if payload_len > 125 && payload_len <= 0xffff {
|
||||
len16 := C.htons(payload_len)
|
||||
len16 := $if tinyc {
|
||||
conv.hton16(u16(payload_len))
|
||||
} $else {
|
||||
C.htons(payload_len)
|
||||
}
|
||||
header[1] = (126 | 0x80)
|
||||
unsafe { C.memcpy(&header[2], &len16, 2) }
|
||||
header[4] = masking_key[0]
|
||||
|
@ -346,7 +356,11 @@ pub fn (mut ws Client) close(code int, message string) ! {
|
|||
ws.set_state(.closing)
|
||||
// mut code32 := 0
|
||||
if code > 0 {
|
||||
code_ := C.htons(code)
|
||||
code_ := $if tinyc {
|
||||
conv.hton16(u16(code))
|
||||
} $else {
|
||||
C.htons(code)
|
||||
}
|
||||
message_len := message.len + 2
|
||||
mut close_frame := []u8{len: message_len}
|
||||
close_frame[0] = u8(code_ & 0xFF)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module picoev
|
||||
|
||||
import net
|
||||
import net.conv
|
||||
import picohttpparser
|
||||
|
||||
#include <errno.h>
|
||||
|
@ -123,10 +124,23 @@ fn listen(config Config) int {
|
|||
}
|
||||
|
||||
// addr settings
|
||||
|
||||
sin_port := $if tinyc {
|
||||
conv.hton16(u16(config.port))
|
||||
} $else {
|
||||
C.htons(config.port)
|
||||
}
|
||||
|
||||
sin_addr := $if tinyc {
|
||||
conv.hton32(u32(C.INADDR_ANY))
|
||||
} $else {
|
||||
C.htonl(C.INADDR_ANY)
|
||||
}
|
||||
|
||||
mut addr := C.sockaddr_in{
|
||||
sin_family: u8(C.AF_INET)
|
||||
sin_port: C.htons(config.port)
|
||||
sin_addr: C.htonl(C.INADDR_ANY)
|
||||
sin_port: sin_port
|
||||
sin_addr: sin_addr
|
||||
}
|
||||
size := sizeof(C.sockaddr_in)
|
||||
bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)
|
||||
|
|
|
@ -803,7 +803,7 @@ static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B
|
|||
#if (WYHASH_LITTLE_ENDIAN)
|
||||
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;}
|
||||
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
#elif !defined(__TINYC__) && (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__))
|
||||
static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);}
|
||||
static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);}
|
||||
#elif defined(_MSC_VER)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue