diff --git a/net/bluetooth/bluetooth_conn.c b/net/bluetooth/bluetooth_conn.c index 58283b8973c..5b94fd6409e 100644 --- a/net/bluetooth/bluetooth_conn.c +++ b/net/bluetooth/bluetooth_conn.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -51,8 +52,10 @@ * network lock. */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct bluetooth_conn_s g_bluetooth_connections[CONFIG_NET_BLUETOOTH_NCONNS]; +#endif /* A list of all free packet socket connections */ @@ -85,28 +88,24 @@ static const bt_addr_t g_any_addr = void bluetooth_conn_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ dq_init(&g_free_bluetooth_connections); dq_init(&g_active_bluetooth_connections); - /* Mark connections as uninitialized */ - - memset(g_bluetooth_connections, 0, sizeof(g_bluetooth_connections)); - +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_BLUETOOTH_NCONNS; i++) { - /* Indicate a connection unbound with BTPROTO_NONE */ - - g_bluetooth_connections[i].bc_proto = BTPROTO_NONE; - /* Link each pre-allocated connection structure into the free list. */ dq_addlast(&g_bluetooth_connections[i].bc_node, &g_free_bluetooth_connections); } +#endif } /**************************************************************************** @@ -121,18 +120,38 @@ void bluetooth_conn_initialize(void) FAR struct bluetooth_conn_s *bluetooth_conn_alloc(void) { FAR struct bluetooth_conn_s *conn; +#ifdef CONFIG_NET_ALLOC_CONNS + int i; +#endif /* The free list is protected by the network lock */ net_lock(); - conn = (FAR struct bluetooth_conn_s *) - dq_remfirst(&g_free_bluetooth_connections); +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_active_bluetooth_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_BLUETOOTH_NCONNS); + if (conn != NULL) + { + for (i = 0; i < CONFIG_NET_BLUETOOTH_NCONNS; i++) + { + dq_addlast(&conn[i].bc_node, + &g_active_bluetooth_connections); + } + } + } +#endif + conn = (FAR struct bluetooth_conn_s *) + dq_remfirst(&g_free_bluetooth_connections); if (conn) { + /* Mark as unbound */ + + conn->bc_proto = BTPROTO_NONE; + /* Enqueue the connection into the active list */ - memset(conn, 0, sizeof(struct bluetooth_conn_s)); dq_addlast(&conn->bc_node, &g_active_bluetooth_connections); } @@ -184,14 +203,14 @@ void bluetooth_conn_free(FAR struct bluetooth_conn_s *conn) bluetooth_container_free(container); } + /* Reset structure */ + + memset(conn, 0, sizeof(*conn)); + /* Free the connection */ dq_addlast(&conn->bc_node, &g_free_bluetooth_connections); - /* Mark as unbound */ - - conn->bc_proto = BTPROTO_NONE; - net_unlock(); } diff --git a/net/can/can_conn.c b/net/can/can_conn.c index 574796ce282..dddba6b4df1 100644 --- a/net/can/can_conn.c +++ b/net/can/can_conn.c @@ -49,7 +49,9 @@ /* The array containing all NetLink connections. */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct can_conn_s g_can_connections[CONFIG_CAN_CONNS]; +#endif /* A list of all free NetLink connections */ @@ -97,7 +99,9 @@ static void _can_semgive(FAR sem_t *sem) void can_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ @@ -105,15 +109,14 @@ void can_initialize(void) dq_init(&g_active_can_connections); nxsem_init(&g_free_sem, 0, 1); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_CAN_CONNS; i++) { - FAR struct can_conn_s *conn = &g_can_connections[i]; - /* Mark the connection closed and move it to the free list */ - memset(conn, 0, sizeof(*conn)); - dq_addlast(&conn->node, &g_free_can_connections); + dq_addlast(&g_can_connections[i].node, &g_free_can_connections); } +#endif } /**************************************************************************** @@ -128,17 +131,30 @@ void can_initialize(void) FAR struct can_conn_s *can_alloc(void) { FAR struct can_conn_s *conn; +#ifdef CONFIG_NET_ALLOC_CONNS + int i; +#endif /* The free list is protected by a semaphore (that behaves like a mutex). */ _can_semtake(&g_free_sem); +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_free_can_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_CAN_CONNS); + if (conn != NULL) + { + for (i = 0; i < CONFIG_CAN_CONNS; i++) + { + dq_addlast(&conn[i].node, &g_free_can_connections); + } + } + } +#endif + conn = (FAR struct can_conn_s *)dq_remfirst(&g_free_can_connections); if (conn != NULL) { - /* Make sure that the connection is marked as uninitialized */ - - memset(conn, 0, sizeof(*conn)); - /* FIXME SocketCAN default behavior enables loopback */ #ifdef CONFIG_NET_CANPROTO_OPTIONS diff --git a/net/ieee802154/ieee802154_conn.c b/net/ieee802154/ieee802154_conn.c index 5b1ffbb2417..9f1c40ef4da 100644 --- a/net/ieee802154/ieee802154_conn.c +++ b/net/ieee802154/ieee802154_conn.c @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -50,8 +51,10 @@ * network lock. */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct ieee802154_conn_s g_ieee802154_connections[CONFIG_NET_IEEE802154_NCONNS]; +#endif /* A list of all free packet socket connections */ @@ -79,13 +82,16 @@ static dq_queue_t g_active_ieee802154_connections; void ieee802154_conn_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ dq_init(&g_free_ieee802154_connections); dq_init(&g_active_ieee802154_connections); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_IEEE802154_NCONNS; i++) { /* Link each pre-allocated connection structure into the free list. */ @@ -93,6 +99,7 @@ void ieee802154_conn_initialize(void) dq_addlast(&g_ieee802154_connections[i].node, &g_free_ieee802154_connections); } +#endif } /**************************************************************************** @@ -107,18 +114,31 @@ void ieee802154_conn_initialize(void) FAR struct ieee802154_conn_s *ieee802154_conn_alloc(void) { FAR struct ieee802154_conn_s *conn; +#ifdef CONFIG_NET_ALLOC_CONNS + int i; +#endif /* The free list is protected by the network lock. */ net_lock(); - conn = (FAR struct ieee802154_conn_s *) - dq_remfirst(&g_free_ieee802154_connections); +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_free_ieee802154_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_IEEE802154_NCONNS); + if (conn != NULL) + { + for (i = 0; i < CONFIG_NET_IEEE802154_NCONNS; i++) + { + dq_addlast(&conn[i].node, &g_free_ieee802154_connections); + } + } + } +#endif + conn = (FAR struct ieee802154_conn_s *) + dq_remfirst(&g_free_ieee802154_connections); if (conn) { - /* Enqueue the connection into the active list */ - - memset(conn, 0, sizeof(struct ieee802154_conn_s)); dq_addlast(&conn->node, &g_active_ieee802154_connections); } @@ -170,6 +190,10 @@ void ieee802154_conn_free(FAR struct ieee802154_conn_s *conn) ieee802154_container_free(container); } + /* Enqueue the connection into the active list */ + + memset(conn, 0, sizeof(*conn)); + /* Free the connection */ dq_addlast(&conn->node, &g_free_ieee802154_connections); diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c index fa2a302283c..50d64f44c86 100644 --- a/net/netlink/netlink_conn.c +++ b/net/netlink/netlink_conn.c @@ -50,7 +50,9 @@ /* The array containing all NetLink connections. */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct netlink_conn_s g_netlink_connections[CONFIG_NETLINK_CONNS]; +#endif /* A list of all free NetLink connections */ @@ -121,7 +123,9 @@ static void netlink_response_available(FAR void *arg) void netlink_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ @@ -129,15 +133,15 @@ void netlink_initialize(void) dq_init(&g_active_netlink_connections); nxsem_init(&g_free_sem, 0, 1); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NETLINK_CONNS; i++) { - FAR struct netlink_conn_s *conn = &g_netlink_connections[i]; - /* Mark the connection closed and move it to the free list */ - memset(conn, 0, sizeof(*conn)); - dq_addlast(&conn->node, &g_free_netlink_connections); + dq_addlast(&g_netlink_connections[i].node, + &g_free_netlink_connections); } +#endif } /**************************************************************************** @@ -152,18 +156,31 @@ void netlink_initialize(void) FAR struct netlink_conn_s *netlink_alloc(void) { FAR struct netlink_conn_s *conn; +#ifdef CONFIG_NET_ALLOC_CONNS + int i; +#endif /* The free list is protected by a semaphore (that behaves like a mutex). */ _netlink_semtake(&g_free_sem); +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_free_netlink_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NETLINK_CONNS); + if (conn != NULL) + { + for (i = 0; i < CONFIG_NETLINK_CONNS; i++) + { + dq_addlast(&conn[i].node, &g_free_netlink_connections); + } + } + } +#endif + conn = (FAR struct netlink_conn_s *) dq_remfirst(&g_free_netlink_connections); if (conn != NULL) { - /* Make sure that the connection is marked as uninitialized */ - - memset(conn, 0, sizeof(*conn)); - /* Enqueue the connection into the active list */ dq_addlast(&conn->node, &g_active_netlink_connections); diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c index af532049b08..fbc216d7f9b 100644 --- a/net/pkt/pkt_conn.c +++ b/net/pkt/pkt_conn.c @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -55,7 +56,9 @@ /* The array containing all packet socket connections */ +#ifndef CONFIG_NET_ALLOC_CONNS static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_CONNS]; +#endif /* A list of all free packet socket connections */ @@ -100,7 +103,9 @@ static inline void _pkt_semtake(FAR sem_t *sem) void pkt_initialize(void) { +#ifndef CONFIG_NET_ALLOC_CONNS int i; +#endif /* Initialize the queues */ @@ -108,13 +113,12 @@ void pkt_initialize(void) dq_init(&g_active_pkt_connections); nxsem_init(&g_free_sem, 0, 1); +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_PKT_CONNS; i++) { - /* Mark the connection closed and move it to the free list */ - - g_pkt_connections[i].ifindex = 0; dq_addlast(&g_pkt_connections[i].node, &g_free_pkt_connections); } +#endif } /**************************************************************************** @@ -129,17 +133,30 @@ void pkt_initialize(void) FAR struct pkt_conn_s *pkt_alloc(void) { FAR struct pkt_conn_s *conn; +#ifdef CONFIG_NET_ALLOC_CONNS + int i; +#endif /* The free list is protected by a semaphore (that behaves like a mutex). */ _pkt_semtake(&g_free_sem); +#ifdef CONFIG_NET_ALLOC_CONNS + if (dq_peek(&g_free_pkt_connections) == NULL) + { + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_PKT_CONNS); + if (conn != NULL) + { + for (i = 0; i < CONFIG_NET_PKT_CONNS; i++) + { + dq_addlast(&conn[i].node, &g_free_pkt_connections); + } + } + } +#endif + conn = (FAR struct pkt_conn_s *)dq_remfirst(&g_free_pkt_connections); if (conn) { - /* Make sure that the connection is marked as uninitialized */ - - conn->ifindex = 0; - /* Enqueue the connection into the active list */ dq_addlast(&conn->node, &g_active_pkt_connections); @@ -170,6 +187,10 @@ void pkt_free(FAR struct pkt_conn_s *conn) dq_rem(&conn->node, &g_active_pkt_connections); + /* Make sure that the connection is marked as uninitialized */ + + memset(conn, 0, sizeof(*conn)); + /* Free the connection */ dq_addlast(&conn->node, &g_free_pkt_connections);