net: Add buffer pool to replace connection allocation

Our net socket connection allocations are powerful but redundant
because they're implemented once in each protocol.  This is not good for
further optimizing and extending to other allocations, so maybe we can
add a common implementation for the usage.

Impact:
1. We add a `struct net_bufpool_s` as pool descriptor, which may use a
   little bit more memory than previous implementation (~28Bytes).
2. We share same functions between pools, so code size may shrink under
   some scenarios.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng
2024-12-17 18:29:14 +08:00
committed by Alan C. Assis
parent badb5c5ac6
commit 1fe07d0838
15 changed files with 437 additions and 724 deletions
+17 -65
View File
@@ -43,6 +43,15 @@
#include <nuttx/net/usrsock.h>
#include "usrsock/usrsock.h"
#include "utils/utils.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NET_USRSOCK_MAX_CONNS
# define CONFIG_NET_USRSOCK_MAX_CONNS 0
#endif
/****************************************************************************
* Private Data
@@ -50,14 +59,10 @@
/* The array containing all usrsock connections. */
#if CONFIG_NET_USRSOCK_PREALLOC_CONNS > 0
static struct usrsock_conn_s
g_usrsock_connections[CONFIG_NET_USRSOCK_PREALLOC_CONNS];
#endif
/* A list of all free usrsock connections */
static dq_queue_t g_free_usrsock_connections;
NET_BUFPOOL_DECLARE(g_usrsock_connections, sizeof(struct usrsock_conn_s),
CONFIG_NET_USRSOCK_PREALLOC_CONNS,
CONFIG_NET_USRSOCK_ALLOC_CONNS,
CONFIG_NET_USRSOCK_MAX_CONNS);
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
/* A list of all allocated usrsock connections */
@@ -80,38 +85,12 @@ static dq_queue_t g_active_usrsock_connections;
FAR struct usrsock_conn_s *usrsock_alloc(void)
{
FAR struct usrsock_conn_s *conn;
#if CONFIG_NET_USRSOCK_ALLOC_CONNS > 0
int i;
#endif
/* The free list is protected by a a mutex. */
nxmutex_lock(&g_free_lock);
#if CONFIG_NET_USRSOCK_ALLOC_CONNS > 0
if (dq_peek(&g_free_usrsock_connections) == NULL)
{
#if CONFIG_NET_USRSOCK_MAX_CONNS > 0
if (dq_count(&g_active_usrsock_connections) +
CONFIG_NET_USRSOCK_ALLOC_CONNS > CONFIG_NET_USRSOCK_MAX_CONNS)
{
nxmutex_unlock(&g_free_lock);
return NULL;
}
#endif
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_USRSOCK_ALLOC_CONNS);
if (conn != NULL)
{
for (i = 0; i < CONFIG_NET_USRSOCK_ALLOC_CONNS; i++)
{
dq_addlast(&conn[i].sconn.node, &g_free_usrsock_connections);
}
}
}
#endif
conn = (FAR struct usrsock_conn_s *)
dq_remfirst(&g_free_usrsock_connections);
conn = NET_BUFPOOL_TRYALLOC(g_usrsock_connections);
if (conn)
{
/* Make sure that the connection is marked as uninitialized */
@@ -154,22 +133,9 @@ void usrsock_free(FAR struct usrsock_conn_s *conn)
nxsem_destroy(&conn->resp.sem);
/* If this is a preallocated or a batch allocated connection store it in
* the free connections list. Else free it.
*/
/* Free the connection. */
#if CONFIG_NET_USRSOCK_ALLOC_CONNS == 1
if (conn < g_usrsock_connections || conn >= (g_usrsock_connections +
CONFIG_NET_USRSOCK_PREALLOC_CONNS))
{
kmm_free(conn);
}
else
#endif
{
memset(conn, 0, sizeof(*conn));
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
}
NET_BUFPOOL_FREE(g_usrsock_connections, conn);
nxmutex_unlock(&g_free_lock);
}
@@ -334,21 +300,7 @@ void usrsock_setup_datain(FAR struct usrsock_conn_s *conn,
void usrsock_initialize(void)
{
#if CONFIG_NET_USRSOCK_PREALLOC_CONNS > 0
FAR struct usrsock_conn_s *conn;
int i;
for (i = 0; i < CONFIG_NET_USRSOCK_PREALLOC_CONNS; i++)
{
conn = &g_usrsock_connections[i];
/* Mark the connection closed and move it to the free list */
conn->usockid = USRSOCK_USOCKID_INVALID;
conn->state = USRSOCK_CONN_STATE_UNINITIALIZED;
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
}
#endif
NET_BUFPOOL_INIT(g_usrsock_connections);
/* Register /dev/usrsock character device. */