Skip to content

Commit

Permalink
Fix constants for Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
quelgar committed Jan 19, 2024
1 parent 17753fa commit 4bf5213
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 48 deletions.
71 changes: 58 additions & 13 deletions src/main/resources/scala-native/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,120 @@
#include <netinet/in.h>
#include <string.h>

void uv_scala_buf_init(char *base, unsigned int len, uv_buf_t *buffer)
void scala_uv_buf_init(char *base, unsigned int len, uv_buf_t *buffer)
{
uv_buf_t buf = uv_buf_init(base, len);
buffer->base = buf.base;
buffer->len = buf.len;
}

void *uv_scala_buf_base(const uv_buf_t *buffer)
void *scala_uv_buf_base(const uv_buf_t *buffer)
{
return buffer->base;
}

size_t uv_scala_buf_len(const uv_buf_t *buffer)
size_t scala_uv_buf_len(const uv_buf_t *buffer)
{
return buffer->len;
}

size_t uv_scala_buf_struct_size()
size_t scala_uv_buf_struct_size()
{
return sizeof(uv_buf_t);
}

size_t uv_scala_mutex_t_size()
size_t scala_uv_mutex_t_size()
{
return sizeof(uv_mutex_t);
}

uv_stream_t *uv_scala_connect_stream_handle(const uv_connect_t *req)
uv_stream_t *scala_uv_connect_stream_handle(const uv_connect_t *req)
{
return req->handle;
}

uv_stream_t *uv_scala_shutdown_stream_handle(const uv_shutdown_t *req)
uv_stream_t *scala_uv_shutdown_stream_handle(const uv_shutdown_t *req)
{
return req->handle;
}

uv_stream_t *uv_scala_write_stream_handle(const uv_write_t *req)
uv_stream_t *scala_uv_write_stream_handle(const uv_write_t *req)
{
return req->handle;
}

uv_stream_t *uv_scala_send_stream_handle(const uv_write_t *req)
uv_stream_t *scala_uv_send_stream_handle(const uv_write_t *req)
{
return req->send_handle;
}

size_t uv_scala_sizeof_sockaddr_in()
size_t scala_uv_sizeof_sockaddr_in()
{
return sizeof(struct sockaddr_in);
}

void uv_scala_init_sockaddr_in(int address, int port, struct sockaddr_in *addr)
void scala_uv_init_sockaddr_in(int address, int port, struct sockaddr_in *addr)
{
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl(address);
addr->sin_port = htons(port);
}

size_t uv_scala_sizeof_sockaddr_in6()
size_t scala_uv_sizeof_sockaddr_in6()
{
return sizeof(struct sockaddr_in6);
}

void uv_scala_init_sockaddr_in6(const char *address, int port, unsigned int flow_info, unsigned int scope_id, struct sockaddr_in6 *addr)
void scala_uv_init_sockaddr_in6(const char *address, int port, unsigned int flow_info, unsigned int scope_id, struct sockaddr_in6 *addr)
{
addr->sin6_family = AF_INET6;
memcpy(&(addr->sin6_addr), address, sizeof(struct in6_addr));
addr->sin6_port = htons(port);
addr->sin6_flowinfo = htonl(flow_info);
addr->sin6_scope_id = htonl(scope_id);
}

int scala_uv_value_o_rdonly()
{
return O_RDONLY;
}

int scala_uv_value_o_wronly()
{
return O_WRONLY;
}

int scala_uv_value_o_rdwr()
{
return O_RDWR;
}

int scala_uv_value_o_creat()
{
return O_CREAT;
}

int scala_uv_value_o_excl()
{
return O_EXCL;
}

int scala_uv_value_o_trunc()
{
return O_TRUNC;
}

int scala_uv_value_o_append()
{
return O_APPEND;
}

int scala_uv_value_o_dsync()
{
return O_DSYNC;
}

int scala_uv_value_o_sync()
{
return O_SYNC;
}
20 changes: 10 additions & 10 deletions src/main/scala/scalauv/Buffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object Buffer {

extension (buffer: Buffer) {

def base: Ptr[Byte] = helpers.uv_scala_buf_base(buffer)
def length: Int = helpers.uv_scala_buf_len(buffer).toInt
def base: Ptr[Byte] = helpers.scala_uv_buf_base(buffer)
def length: Int = helpers.scala_uv_buf_len(buffer).toInt

def apply(index: Int): Byte = base(index)

Expand Down Expand Up @@ -45,15 +45,15 @@ object Buffer {
inline def toPtr: Ptr[Byte] = buffer

inline def init(base: Ptr[Byte], size: CSize): Unit =
helpers.uv_scala_buf_init(base, size.toUInt, buffer)
helpers.scala_uv_buf_init(base, size.toUInt, buffer)

inline def mallocInit(size: CSize): Unit =
helpers.uv_scala_buf_init(stdlib.malloc(size), size.toUInt, buffer)
helpers.scala_uv_buf_init(stdlib.malloc(size), size.toUInt, buffer)

inline def free(): Unit = stdlib.free(buffer)
}

val structureSize: CSize = helpers.uv_scala_buf_struct_size()
val structureSize: CSize = helpers.scala_uv_buf_struct_size()

inline def unsafeFromPtr(ptr: Ptr[Byte]): Buffer = ptr

Expand All @@ -62,7 +62,7 @@ object Buffer {
size: CUnsignedInt
): Buffer = {
val uvBuf = stackalloc[Byte](structureSize)
helpers.uv_scala_buf_init(ptr, size, uvBuf)
helpers.scala_uv_buf_init(ptr, size, uvBuf)
uvBuf
}

Expand All @@ -71,7 +71,7 @@ object Buffer {
index: Int = 0
): Buffer = {
val uvBuf = stackalloc[Byte](structureSize)
helpers.uv_scala_buf_init(
helpers.scala_uv_buf_init(
array.at(index),
(array.length - index).toUInt,
uvBuf
Expand All @@ -83,7 +83,7 @@ object Buffer {
Zone
): Buffer = {
val uvBuf = alloc[Byte](structureSize)
helpers.uv_scala_buf_init(
helpers.scala_uv_buf_init(
array.at(index),
(array.length - index).toUInt,
uvBuf
Expand All @@ -93,13 +93,13 @@ object Buffer {

def malloc(base: Ptr[Byte], size: CSize): Buffer = {
val uvBuf = stdlib.malloc(structureSize.toULong)
helpers.uv_scala_buf_init(base, size.toUInt, uvBuf)
helpers.scala_uv_buf_init(base, size.toUInt, uvBuf)
uvBuf
}

def malloc(array: Array[Byte], index: Int = 0): Buffer = {
val uvBuf = stdlib.malloc(structureSize.toULong)
helpers.uv_scala_buf_init(
helpers.scala_uv_buf_init(
array.at(index),
(array.length - index).toUInt,
uvBuf
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scalauv/Req.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object ConnectReq {
extension (r: ConnectReq) {

inline def connectReqStreamHandle: StreamHandle =
helpers.uv_scala_connect_stream_handle(r)
helpers.scala_uv_connect_stream_handle(r)

}

Expand Down
18 changes: 9 additions & 9 deletions src/main/scala/scalauv/UvConstants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ object RunMode {
}

object FileOpenFlags {
val O_RDONLY = 0
val O_WRONLY = 1
val O_RDWR = 2
val O_RDONLY = helpers.scala_uv_value_o_rdonly()
val O_WRONLY = helpers.scala_uv_value_o_wronly()
val O_RDWR = helpers.scala_uv_value_o_rdwr()

val O_CREAT = 0x200
val O_EXCL = 0x800
val O_TRUNC = 0x400
val O_CREAT = helpers.scala_uv_value_o_creat()
val O_EXCL = helpers.scala_uv_value_o_excl()
val O_TRUNC = helpers.scala_uv_value_o_trunc()

val O_APPEND = 0x08
val O_DSYNC = 0x400000
val O_SYNC = 0x80
val O_APPEND = helpers.scala_uv_value_o_append()
val O_DSYNC = helpers.scala_uv_value_o_dsync()
val O_SYNC = helpers.scala_uv_value_o_sync()
}

object CreateMode {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/scalauv/UvUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ opaque type SocketAddressIp4 <: SocketAddress = Ptr[Byte]

object SocketAddressIp4 {

val size: CSize = helpers.uv_scala_sizeof_sockaddr_in()
val size: CSize = helpers.scala_uv_sizeof_sockaddr_in()

inline def apply(address: Ip4Address, port: Port): SocketAddressIp4 = {
val sockaddr = stackalloc[Byte](size).asInstanceOf[SocketAddressIp4]
helpers.uv_scala_init_sockaddr_in(address, port, sockaddr)
helpers.scala_uv_init_sockaddr_in(address, port, sockaddr)
sockaddr
}

Expand Down
43 changes: 30 additions & 13 deletions src/main/scala/scalauv/helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ import scala.scalanative.unsafe.*
@extern
private[scalauv] object helpers {

def uv_scala_buf_init(
def scala_uv_buf_init(
base: Ptr[CChar],
len: CUnsignedInt,
buffer: Ptr[Byte]
): Unit = extern

def uv_scala_buf_base(buffer: Ptr[Byte]): Ptr[Byte] = extern
def scala_uv_buf_base(buffer: Ptr[Byte]): Ptr[Byte] = extern

def uv_scala_buf_len(buffer: Ptr[Byte]): CSize = extern
def scala_uv_buf_len(buffer: Ptr[Byte]): CSize = extern

def uv_scala_buf_struct_size(): CSize = extern
def scala_uv_buf_struct_size(): CSize = extern

def uv_scala_mutex_t_size(): CSize = extern
def scala_uv_mutex_t_size(): CSize = extern

def uv_scala_connect_stream_handle(req: ConnectReq): StreamHandle = extern
def scala_uv_connect_stream_handle(req: ConnectReq): StreamHandle = extern

def uv_scala_shutdown_stream_handle(req: Req): StreamHandle =
def scala_uv_shutdown_stream_handle(req: Req): StreamHandle =
extern

def uv_scala_write_stream_handle(req: Req): StreamHandle = extern
def scala_uv_write_stream_handle(req: Req): StreamHandle = extern

def uv_scala_send_stream_handle(req: Req): StreamHandle = extern
def scala_uv_send_stream_handle(req: Req): StreamHandle = extern

def uv_scala_sizeof_sockaddr_in(): CSize = extern
def scala_uv_sizeof_sockaddr_in(): CSize = extern

def uv_scala_init_sockaddr_in(
def scala_uv_init_sockaddr_in(
address: CInt,
port: CInt,
socketAddress: SocketAddressIp4
): Unit =
extern

def uv_scala_sizeof_sockaddr_in6(): CSize = extern
def scala_uv_sizeof_sockaddr_in6(): CSize = extern

def uv_scala_init_sockaddr_in6(
def scala_uv_init_sockaddr_in6(
address: Ptr[Byte],
port: CInt,
flowInfo: CUnsignedInt,
Expand All @@ -48,4 +48,21 @@ private[scalauv] object helpers {
): Unit =
extern

def scala_uv_value_o_rdonly(): CInt = extern

def scala_uv_value_o_wronly(): CInt = extern

def scala_uv_value_o_rdwr(): CInt = extern

def scala_uv_value_o_creat(): CInt = extern

def scala_uv_value_o_excl(): CInt = extern

def scala_uv_value_o_trunc(): CInt = extern

def scala_uv_value_o_append(): CInt = extern

def scala_uv_value_o_dsync(): CInt = extern

def scala_uv_value_o_sync(): CInt = extern
}

0 comments on commit 4bf5213

Please sign in to comment.