From efc1bd47ff5afa9d54e5b7bd6d55419e20feab34 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 7 Mar 2021 13:36:30 +0100 Subject: [PATCH] wutsocket: only link in networking layer if actually used --- libraries/wutcrt/wut_crt.c | 20 +++---- libraries/wutsocket/socket.c | 4 +- libraries/wutsocket/wut_socket.c | 80 ------------------------- libraries/wutsocket/wut_socket_common.c | 63 ++++++++++++++++++- 4 files changed, 70 insertions(+), 97 deletions(-) delete mode 100644 libraries/wutsocket/wut_socket.c diff --git a/libraries/wutcrt/wut_crt.c b/libraries/wutcrt/wut_crt.c index 3da4244..e60b9b8 100644 --- a/libraries/wutcrt/wut_crt.c +++ b/libraries/wutcrt/wut_crt.c @@ -1,35 +1,31 @@ void __init_wut_malloc(); void __init_wut_newlib(); -extern void __init_wut_stdcpp(); +void __init_wut_stdcpp(); void __init_wut_devoptab(); -void __init_wut_socket(); +void __attribute__((weak)) __init_wut_socket(); void __fini_wut_malloc(); void __fini_wut_newlib(); -extern void __fini_wut_stdcpp(); +void __fini_wut_stdcpp(); void __fini_wut_devoptab(); -void __fini_wut_socket(); +void __attribute__((weak)) __fini_wut_socket(); void __attribute__((weak)) __init_wut() { __init_wut_malloc(); __init_wut_newlib(); - //if (__init_wut_stdcpp) { - __init_wut_stdcpp(); - //} + __init_wut_stdcpp(); __init_wut_devoptab(); - __init_wut_socket(); + if (&__init_wut_socket) __init_wut_socket(); } void __attribute__((weak)) __fini_wut() { - __fini_wut_socket(); + if (&__fini_wut_socket) __fini_wut_socket(); __fini_wut_devoptab(); - //if (__fini_wut_stdcpp) { - __fini_wut_stdcpp(); - //} + __fini_wut_stdcpp(); __fini_wut_newlib(); __fini_wut_malloc(); } diff --git a/libraries/wutsocket/socket.c b/libraries/wutsocket/socket.c index 8f16e39..c06e42d 100644 --- a/libraries/wutsocket/socket.c +++ b/libraries/wutsocket/socket.c @@ -7,7 +7,7 @@ socket(int domain, { int rc, fd, dev; - dev = FindDevice("sock:"); + dev = FindDevice("soc:"); if (dev == -1) { return -1; } @@ -22,7 +22,7 @@ socket(int domain, __release_handle(fd); return __wut_get_nsysnet_result(NULL, rc); } - + *(int *)__get_handle(fd)->fileStruct = rc; return fd; } diff --git a/libraries/wutsocket/wut_socket.c b/libraries/wutsocket/wut_socket.c deleted file mode 100644 index 336e0e5..0000000 --- a/libraries/wutsocket/wut_socket.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "wut_socket.h" - -static devoptab_t -__wut_socket_devoptab = -{ - .name = "sock", - .structSize = sizeof(int), - .open_r = __wut_socket_open, - .close_r = __wut_socket_close, - .write_r = __wut_socket_write, - .read_r = __wut_socket_read, - .seek_r = NULL, - .fstat_r = NULL, - .stat_r = NULL, - .link_r = NULL, - .unlink_r = NULL, - .chdir_r = NULL, - .rename_r = NULL, - .mkdir_r = NULL, - .dirStateSize = 0, - .diropen_r = NULL, - .dirreset_r = NULL, - .dirnext_r = NULL, - .dirclose_r = NULL, - .statvfs_r = NULL, - .ftruncate_r = NULL, - .fsync_r = NULL, - .deviceData = 0, - .chmod_r = NULL, - .fchmod_r = NULL, - .rmdir_r = NULL, -}; - -static BOOL -__wut_socket_initialised = FALSE; - -void -__init_wut_socket() -{ - BOOL connected = FALSE; - int dev; - - if (__wut_socket_initialised) { - return; - } - - ACInitialize(); - ACConnect(); - - ACIsApplicationConnected(&connected); - if (!connected) { - ACFinalize(); - return; - } - - RPLWRAP(socket_lib_init)(); - - dev = AddDevice(&__wut_socket_devoptab); - if (dev == -1) { - RPLWRAP(socket_lib_finish)(); - ACFinalize(); - return; - } - - __wut_socket_initialised = TRUE; -} - -void -__fini_wut_socket() -{ - if (!__wut_socket_initialised) { - return; - } - - RPLWRAP(socket_lib_finish)(); - ACFinalize(); - - __wut_socket_initialised = FALSE; -} - diff --git a/libraries/wutsocket/wut_socket_common.c b/libraries/wutsocket/wut_socket_common.c index 366f7ab..efc2352 100644 --- a/libraries/wutsocket/wut_socket_common.c +++ b/libraries/wutsocket/wut_socket_common.c @@ -2,6 +2,20 @@ #define NSYSNET_UNKNOWN_ERROR_OFFSET 10000 +static BOOL +__wut_socket_initialised = FALSE; + +static devoptab_t +__wut_socket_devoptab = +{ + .name = "soc", + .structSize = sizeof(int), + .open_r = __wut_socket_open, + .close_r = __wut_socket_close, + .write_r = __wut_socket_write, + .read_r = __wut_socket_read, +}; + static unsigned char __wut_nsysnet_error_code_map[] = { @@ -59,6 +73,50 @@ __wut_nsysnet_error_code_map[] = EMFILE, }; +void +__init_wut_socket() +{ + BOOL connected = FALSE; + int dev; + + if (__wut_socket_initialised) { + return; + } + + ACInitialize(); + ACConnect(); + + ACIsApplicationConnected(&connected); + if (!connected) { + ACFinalize(); + return; + } + + RPLWRAP(socket_lib_init)(); + + dev = AddDevice(&__wut_socket_devoptab); + if (dev == -1) { + RPLWRAP(socket_lib_finish)(); + ACFinalize(); + return; + } + + __wut_socket_initialised = TRUE; +} + +void +__fini_wut_socket() +{ + if (!__wut_socket_initialised) { + return; + } + + RPLWRAP(socket_lib_finish)(); + ACFinalize(); + + __wut_socket_initialised = FALSE; +} + int __wut_get_nsysnet_fd(int fd) { @@ -67,7 +125,7 @@ __wut_get_nsysnet_fd(int fd) errno = EBADF; return -1; } - if (strcmp(devoptab_list[handle->device]->name, "sock") != 0) { + if (strcmp(devoptab_list[handle->device]->name, "soc") != 0) { errno = ENOTSOCK; return -1; } @@ -93,7 +151,7 @@ __wut_get_nsysnet_result(struct _reent *r, if (sockerror < sizeof(__wut_nsysnet_error_code_map)) { error = __wut_nsysnet_error_code_map[sockerror]; } else { - error = NSYSNET_UNKNOWN_ERROR_OFFSET + sockerror; + error = NSYSNET_UNKNOWN_ERROR_OFFSET + sockerror; } if (r) { @@ -104,4 +162,3 @@ __wut_get_nsysnet_result(struct _reent *r, return -1; } -