From c8f814519573b226f0dbee3389a3a9fc70cce27d Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 23 Feb 2021 21:28:32 -0800 Subject: [PATCH] bsd: allow user-specified tmem backing buffer --- nx/include/switch/services/bsd.h | 3 +++ nx/source/services/bsd.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nx/include/switch/services/bsd.h b/nx/include/switch/services/bsd.h index 08b1ae43..80958a99 100644 --- a/nx/include/switch/services/bsd.h +++ b/nx/include/switch/services/bsd.h @@ -18,6 +18,9 @@ typedef struct { u32 version; ///< Observed 1 on [2.0.0+] LibAppletWeb, 2 on [3.0.0+]. + void *tmem_buffer; ///< User-provided buffer to use as backing for transfer memory. If NULL, a buffer will be allocated automatically. Must be large enough and page-aligned. + size_t tmem_buffer_size; ///< Size of the user-provided transfer memory backing buffer. Must be large enough and page-aligned. + u32 tcp_tx_buf_size; ///< Size of the TCP transfer (send) buffer (initial or fixed). u32 tcp_rx_buf_size; ///< Size of the TCP receive buffer (initial or fixed). u32 tcp_tx_buf_max_size; ///< Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value. diff --git a/nx/source/services/bsd.c b/nx/source/services/bsd.c index 8c0d68c5..0e3002be 100644 --- a/nx/source/services/bsd.c +++ b/nx/source/services/bsd.c @@ -37,6 +37,9 @@ static TransferMemory g_bsdTmem; static const BsdInitConfig g_defaultBsdInitConfig = { .version = 1, + .tmem_buffer = NULL, + .tmem_buffer_size = 0, + .tcp_tx_buf_size = 0x8000, .tcp_rx_buf_size = 0x10000, .tcp_tx_buf_max_size = 0x40000, @@ -213,8 +216,14 @@ Result _bsdInitialize(const BsdInitConfig *config, u32 num_sessions, u32 service if (R_SUCCEEDED(rc)) rc = smGetServiceWrapper(&g_bsdMonitor, bsd_srv); - if (R_SUCCEEDED(rc)) - rc = tmemCreate(&g_bsdTmem, _bsdGetTransferMemSizeForConfig(config), 0); + if (R_SUCCEEDED(rc)) { + const size_t min_tmem_size = _bsdGetTransferMemSizeForConfig(config); + + if (config->tmem_buffer != NULL && config->tmem_buffer_size >= min_tmem_size) + rc = tmemCreateFromMemory(&g_bsdTmem, config->tmem_buffer, config->tmem_buffer_size, 0); + else + rc = tmemCreate(&g_bsdTmem, min_tmem_size, 0); + } if (R_SUCCEEDED(rc)) rc = _bsdRegisterClient(&g_bsdTmem, config, &g_bsdClientPid);