net/bufpool: Call init automatically on alloc

Note: Initialize function of protocols (tcp, udp, pkt, etc.) are kept empty.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2025-01-02 12:10:09 +08:00 committed by Xiang Xiao
parent f702f1705f
commit 50b3ab7671
27 changed files with 23 additions and 272 deletions

View file

@ -143,20 +143,6 @@ struct sockaddr; /* Forward reference */
void bluetooth_initialize(void); void bluetooth_initialize(void);
/****************************************************************************
* Name: bluetooth_conn_initialize
*
* Description:
* Initialize the Bluetooth connection structure allocator. Called
* once and only from bluetooth_initialize().
*
* Assumptions:
* Called early in the initialization sequence
*
****************************************************************************/
void bluetooth_conn_initialize(void);
/**************************************************************************** /****************************************************************************
* Name: bluetooth_conn_alloc() * Name: bluetooth_conn_alloc()
* *

View file

@ -81,23 +81,6 @@ static const bt_addr_t g_any_addr =
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: bluetooth_conn_initialize
*
* Description:
* Initialize the Bluetooth connection structure allocator. Called
* once and only from bluetooth_initialize().
*
* Assumptions:
* Called early in the initialization sequence
*
****************************************************************************/
void bluetooth_conn_initialize(void)
{
NET_BUFPOOL_INIT(g_bluetooth_connections);
}
/**************************************************************************** /****************************************************************************
* Name: bluetooth_conn_alloc() * Name: bluetooth_conn_alloc()
* *

View file

@ -48,10 +48,6 @@
void bluetooth_initialize(void) void bluetooth_initialize(void)
{ {
/* Initialize connection structions */
bluetooth_conn_initialize();
/* Initialize the container allocator */ /* Initialize the container allocator */
bluetooth_container_initialize(); bluetooth_container_initialize();

View file

@ -83,7 +83,6 @@ static dq_queue_t g_active_can_connections;
void can_initialize(void) void can_initialize(void)
{ {
NET_BUFPOOL_INIT(g_can_connections);
} }
/**************************************************************************** /****************************************************************************

View file

@ -310,19 +310,6 @@ extern "C"
void devif_initialize(void); void devif_initialize(void);
/****************************************************************************
* Name: devif_callback_init
*
* Description:
* Configure the pre-allocated callback structures into a free list.
*
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/
void devif_callback_init(void);
/**************************************************************************** /****************************************************************************
* Name: devif_callback_alloc * Name: devif_callback_alloc
* *

View file

@ -237,23 +237,6 @@ static bool devif_event_trigger(uint16_t events, uint16_t triggers)
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: devif_callback_init
*
* Description:
* Configure the pre-allocated callback structures into a free list.
*
* Assumptions:
* Called early in the initialization sequence so that no special
* protection is required.
*
****************************************************************************/
void devif_callback_init(void)
{
NET_BUFPOOL_INIT(g_cbprealloc);
}
/**************************************************************************** /****************************************************************************
* Name: devif_callback_alloc * Name: devif_callback_alloc
* *

View file

@ -88,8 +88,5 @@ struct net_stats_s g_netstats;
void devif_initialize(void) void devif_initialize(void)
{ {
/* Initialize callback support */
devif_callback_init();
} }
#endif /* CONFIG_NET */ #endif /* CONFIG_NET */

View file

@ -162,19 +162,6 @@ EXTERN const struct sock_intf_s g_icmp_sockif;
void icmp_input(FAR struct net_driver_s *dev); void icmp_input(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: icmp_sock_initialize
*
* Description:
* Initialize the IPPROTO_ICMP socket connection structures. Called once
* and only from the network initialization layer.
*
****************************************************************************/
#ifdef CONFIG_NET_ICMP_SOCKET
void icmp_sock_initialize(void);
#endif
/**************************************************************************** /****************************************************************************
* Name: icmp_alloc * Name: icmp_alloc
* *

View file

@ -72,20 +72,6 @@ static dq_queue_t g_active_icmp_connections;
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: icmp_sock_initialize
*
* Description:
* Initialize the IPPROTO_ICMP socket connection structures. Called once
* and only from the network initialization layer.
*
****************************************************************************/
void icmp_sock_initialize(void)
{
NET_BUFPOOL_INIT(g_icmp_connections);
}
/**************************************************************************** /****************************************************************************
* Name: icmp_alloc * Name: icmp_alloc
* *

View file

@ -563,19 +563,6 @@ void icmpv6_rnotify(FAR struct net_driver_s *dev, int result);
# define icmpv6_rnotify(d,r) (0) # define icmpv6_rnotify(d,r) (0)
#endif #endif
/****************************************************************************
* Name: icmpv6_sock_initialize
*
* Description:
* Initialize the IPPROTO_ICMP socket connection structures. Called once
* and only from the network initialization layer.
*
****************************************************************************/
#ifdef CONFIG_NET_ICMPv6_SOCKET
void icmpv6_sock_initialize(void);
#endif
/**************************************************************************** /****************************************************************************
* Name: icmpv6_alloc * Name: icmpv6_alloc
* *

View file

@ -73,20 +73,6 @@ static dq_queue_t g_active_icmpv6_connections;
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: icmpv6_sock_initialize
*
* Description:
* Initialize the IPPROTO_ICMP socket connection structures. Called once
* and only from the network initialization layer.
*
****************************************************************************/
void icmpv6_sock_initialize(void)
{
NET_BUFPOOL_INIT(g_icmpv6_connections);
}
/**************************************************************************** /****************************************************************************
* Name: icmpv6_alloc * Name: icmpv6_alloc
* *

View file

@ -157,20 +157,6 @@ struct sockaddr; /* Forward reference */
void ieee802154_initialize(void); void ieee802154_initialize(void);
/****************************************************************************
* Name: ieee802154_conn_initialize
*
* Description:
* Initialize the IEEE 802.15.4 connection structure allocator. Called
* once and only from ieee802154_initialize().
*
* Assumptions:
* Called early in the initialization sequence
*
****************************************************************************/
void ieee802154_conn_initialize(void);
/**************************************************************************** /****************************************************************************
* Name: ieee802154_conn_alloc() * Name: ieee802154_conn_alloc()
* *

View file

@ -76,23 +76,6 @@ static dq_queue_t g_active_ieee802154_connections;
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: ieee802154_conn_initialize
*
* Description:
* Initialize the IEEE 802.15.4 connection structure allocator. Called
* once and only from ieee802154_initialize().
*
* Assumptions:
* Called early in the initialization sequence
*
****************************************************************************/
void ieee802154_conn_initialize(void)
{
NET_BUFPOOL_INIT(g_ieee802154_connections);
}
/**************************************************************************** /****************************************************************************
* Name: ieee802154_conn_alloc() * Name: ieee802154_conn_alloc()
* *

View file

@ -48,10 +48,6 @@
void ieee802154_initialize(void) void ieee802154_initialize(void)
{ {
/* Initialize connection structions */
ieee802154_conn_initialize();
/* Initialize the container allocator */ /* Initialize the container allocator */
ieee802154_container_initialize(); ieee802154_container_initialize();

View file

@ -96,8 +96,6 @@ void ipfwd_initialize(void)
*/ */
DEBUGASSERT(MAX_HDRLEN <= CONFIG_IOB_BUFSIZE); DEBUGASSERT(MAX_HDRLEN <= CONFIG_IOB_BUFSIZE);
NET_BUFPOOL_INIT(g_fwdpool);
} }
/**************************************************************************** /****************************************************************************

View file

@ -131,38 +131,16 @@ void net_initialize(void)
ipfwd_initialize(); ipfwd_initialize();
#endif #endif
#ifdef CONFIG_NET_ICMP_SOCKET
/* Initialize IPPPROTO_ICMP socket support */
icmp_sock_initialize();
#endif
#ifdef CONFIG_NET_ICMPv6_SOCKET
/* Initialize IPPPROTO_ICMP6 socket support */
icmpv6_sock_initialize();
#endif
#ifdef NET_TCP_HAVE_STACK #ifdef NET_TCP_HAVE_STACK
/* Initialize the TCP/IP connection structures */ /* Initialize the TCP/IP connection structures */
tcp_initialize(); tcp_initialize();
/* Initialize the TCP/IP write buffering */
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
tcp_wrbuffer_initialize();
#endif
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */
#ifdef NET_UDP_HAVE_STACK #ifdef NET_UDP_HAVE_STACK
/* Initialize the UDP connection structures */ /* Initialize the UDP connection structures */
udp_initialize(); udp_initialize();
#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
udp_wrbuffer_initialize();
#endif
#endif #endif
#ifdef CONFIG_NET_USRSOCK #ifdef CONFIG_NET_USRSOCK

View file

@ -150,7 +150,6 @@ netlink_get_terminator(FAR const struct nlmsghdr *req)
void netlink_initialize(void) void netlink_initialize(void)
{ {
NET_BUFPOOL_INIT(g_netlink_connections);
} }
/**************************************************************************** /****************************************************************************

View file

@ -87,7 +87,6 @@ static dq_queue_t g_active_pkt_connections;
void pkt_initialize(void) void pkt_initialize(void)
{ {
NET_BUFPOOL_INIT(g_pkt_connections);
} }
/**************************************************************************** /****************************************************************************

View file

@ -1768,21 +1768,6 @@ bool tcp_should_send_recvwindow(FAR struct tcp_conn_s *conn);
int psock_tcp_cansend(FAR struct tcp_conn_s *conn); int psock_tcp_cansend(FAR struct tcp_conn_s *conn);
/****************************************************************************
* Name: tcp_wrbuffer_initialize
*
* Description:
* Initialize the list of free write buffers
*
* Assumptions:
* Called once early initialization.
*
****************************************************************************/
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
void tcp_wrbuffer_initialize(void);
#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
struct tcp_wrbuffer_s; struct tcp_wrbuffer_s;

View file

@ -606,7 +606,6 @@ int tcp_selectport(uint8_t domain,
void tcp_initialize(void) void tcp_initialize(void)
{ {
NET_BUFPOOL_INIT(g_tcp_connections);
} }
/**************************************************************************** /****************************************************************************

View file

@ -62,22 +62,6 @@ NET_BUFPOOL_DECLARE(g_wrbuffer, sizeof(struct tcp_wrbuffer_s),
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: tcp_wrbuffer_initialize
*
* Description:
* Initialize the list of free write buffers
*
* Assumptions:
* Called once early initialization.
*
****************************************************************************/
void tcp_wrbuffer_initialize(void)
{
NET_BUFPOOL_INIT(g_wrbuffer);
}
/**************************************************************************** /****************************************************************************
* Name: tcp_wrbuffer_timedalloc * Name: tcp_wrbuffer_timedalloc
* *

View file

@ -480,21 +480,6 @@ int udp_setsockopt(FAR struct socket *psock, int option,
FAR const void *value, socklen_t value_len); FAR const void *value, socklen_t value_len);
#endif #endif
/****************************************************************************
* Name: udp_wrbuffer_initialize
*
* Description:
* Initialize the list of free write buffers
*
* Assumptions:
* Called once early initialization.
*
****************************************************************************/
#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
void udp_wrbuffer_initialize(void);
#endif /* CONFIG_NET_UDP_WRITE_BUFFERS */
/**************************************************************************** /****************************************************************************
* Name: udp_wrbuffer_alloc * Name: udp_wrbuffer_alloc
* *

View file

@ -549,7 +549,6 @@ errout:
void udp_initialize(void) void udp_initialize(void)
{ {
NET_BUFPOOL_INIT(g_udp_connections);
} }
/**************************************************************************** /****************************************************************************

View file

@ -61,22 +61,6 @@ NET_BUFPOOL_DECLARE(g_wrbuffer, sizeof(struct udp_wrbuffer_s),
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: udp_wrbuffer_initialize
*
* Description:
* Initialize the list of free write buffers
*
* Assumptions:
* Called once early initialization.
*
****************************************************************************/
void udp_wrbuffer_initialize(void)
{
NET_BUFPOOL_INIT(g_wrbuffer);
}
/**************************************************************************** /****************************************************************************
* Name: udp_wrbuffer_alloc * Name: udp_wrbuffer_alloc
* *

View file

@ -300,8 +300,6 @@ void usrsock_setup_datain(FAR struct usrsock_conn_s *conn,
void usrsock_initialize(void) void usrsock_initialize(void)
{ {
NET_BUFPOOL_INIT(g_usrsock_connections);
/* Register /dev/usrsock character device. */ /* Register /dev/usrsock character device. */
usrsock_register(); usrsock_register();

View file

@ -44,7 +44,7 @@ struct net_bufnode_s
}; };
/**************************************************************************** /****************************************************************************
* Public Functions * Private Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
@ -58,11 +58,13 @@ struct net_bufnode_s
* *
****************************************************************************/ ****************************************************************************/
void net_bufpool_init(FAR struct net_bufpool_s *pool) static void net_bufpool_init(FAR struct net_bufpool_s *pool)
{ {
int i; int i;
sq_init(&pool->freebuffers); DEBUGASSERT(pool->nodesize < 0);
pool->nodesize = -pool->nodesize;
for (i = 0; i < pool->prealloc; i++) for (i = 0; i < pool->prealloc; i++)
{ {
FAR struct net_bufnode_s *node = (FAR struct net_bufnode_s *) FAR struct net_bufnode_s *node = (FAR struct net_bufnode_s *)
@ -71,6 +73,10 @@ void net_bufpool_init(FAR struct net_bufpool_s *pool)
} }
} }
/****************************************************************************
* Public Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: net_bufpool_timedalloc * Name: net_bufpool_timedalloc
* *
@ -95,6 +101,12 @@ FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
int ret; int ret;
int i; int i;
if (pool->nodesize < 0)
{
net_bufpool_init(pool);
DEBUGASSERT(pool->nodesize > 0);
}
ret = net_sem_timedwait_uninterruptible(&pool->sem, timeout); ret = net_sem_timedwait_uninterruptible(&pool->sem, timeout);
if (ret != OK) if (ret != OK)
{ {
@ -138,6 +150,8 @@ FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node) void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node)
{ {
DEBUGASSERT(pool->nodesize > 0);
if (pool->dynalloc == 1 && if (pool->dynalloc == 1 &&
((FAR char *)node < pool->pool || ((FAR char *)node < pool->pool ||
(FAR char *)node >= pool->pool + pool->prealloc * pool->nodesize)) (FAR char *)node >= pool->pool + pool->prealloc * pool->nodesize))

View file

@ -94,11 +94,11 @@
pool##_buffer[0], \ pool##_buffer[0], \
prealloc, \ prealloc, \
dynalloc, \ dynalloc, \
nodesize, \ -(int)(nodesize), \
SEM_INITIALIZER(NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc)) \ SEM_INITIALIZER(NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc)), \
{ NULL, NULL } \
}; };
#define NET_BUFPOOL_INIT(p) net_bufpool_init(&p)
#define NET_BUFPOOL_TIMEDALLOC(p,t) net_bufpool_timedalloc(&p, t) #define NET_BUFPOOL_TIMEDALLOC(p,t) net_bufpool_timedalloc(&p, t)
#define NET_BUFPOOL_TRYALLOC(p) net_bufpool_timedalloc(&p, 0) #define NET_BUFPOOL_TRYALLOC(p) net_bufpool_timedalloc(&p, 0)
#define NET_BUFPOOL_ALLOC(p) net_bufpool_timedalloc(&p, UINT_MAX) #define NET_BUFPOOL_ALLOC(p) net_bufpool_timedalloc(&p, UINT_MAX)
@ -125,9 +125,9 @@ struct net_bufpool_s
/* Allocation configuration */ /* Allocation configuration */
FAR char *pool; /* The beginning of the pre-allocated buffer pool */ FAR char *pool; /* The beginning of the pre-allocated buffer pool */
const int prealloc; /* The number of pre-allocated buffers */ int prealloc; /* The number of pre-allocated buffers */
const int dynalloc; /* The number per dynamic allocations */ int dynalloc; /* The number per dynamic allocations */
const int nodesize; /* The size of each node in the pool */ int nodesize; /* The size of each node in the pool */
sem_t sem; /* The semaphore for waiting for free buffers */ sem_t sem; /* The semaphore for waiting for free buffers */
@ -367,19 +367,6 @@ FAR void *net_ipv6_payload(FAR struct ipv6_hdr_s *ipv6, FAR uint8_t *proto);
uint16_t net_iob_concat(FAR struct iob_s **iob1, FAR struct iob_s **iob2); uint16_t net_iob_concat(FAR struct iob_s **iob1, FAR struct iob_s **iob2);
#endif #endif
/****************************************************************************
* Name: net_bufpool_init
*
* Description:
* Initialize a network buffer pool.
*
* Input Parameters:
* pool - The pool to be initialized
*
****************************************************************************/
void net_bufpool_init(FAR struct net_bufpool_s *pool);
/**************************************************************************** /****************************************************************************
* Name: net_bufpool_timedalloc * Name: net_bufpool_timedalloc
* *