xiaoxiang781216 commented on code in PR #7525: URL: https://github.com/apache/nuttx/pull/7525#discussion_r1038784224
########## net/tcp/tcp_conn.c: ########## @@ -586,16 +586,32 @@ FAR struct tcp_conn_s *tcp_alloc_conn(void) void tcp_initialize(void) { -#ifndef CONFIG_NET_ALLOC_CONNS int i; +#ifndef CONFIG_NET_ALLOC_CONNS for (i = 0; i < CONFIG_NET_TCP_CONNS; i++) { /* Mark the connection closed and move it to the free list */ g_tcp_connections[i].tcpstateflags = TCP_CLOSED; dq_addlast(&g_tcp_connections[i].sconn.node, &g_free_tcp_connections); } +#else + FAR struct tcp_conn_s *conn; + + for (i = 0; i < CONFIG_NET_TCP_PREALLOC_CONNS; i++) + { + conn = kmm_zalloc(sizeof(struct tcp_conn_s)); Review Comment: > The reason that I do not want to use `g_tcp_connections` is because this way we will have connections allocated both in heap and BSS, and make memory management much more complicated (linker scripts etc). Especially in arches with multiple memory regions. > > It is much better to have everything in one place. Custom memory mappings and adjustment of the settings are becoming much easier. > If so, you can add a new option(e.g. NET_TCP_PREALLOC_CONNS to indicate the preallocated global array, but keep NET_TCP_ALLOC_CONNS as the dynamical allocation: 1. Set NET_TCP_PREALLOC_CONNS to zero to disable the global allocation 2. Set NET_TCP_ALLOC_CONNS to 1 to free conn instead put into a free list > > You are right about the heap overhead. I will change this to allocate all connections in a single `kmm_zalloc()` call. > The optimization is very hard to implement if you want to return the memory to the system heap instead in a free list since if you allocate the conn in a batch, you have to free the conn in batch too, which mean you have to track all conn in a bath is freed from user. That's why I suggest you free conn to the system heap only when NET_TCP_ALLOC_CONNS equals 1. > > But not, now we have two code path to initialize conn. > > This is a very simple change, all contained within the same function. It is not really complicating things. > > > Even worse, we have two option to CONFIG_NET_TCP_PREALLOC_CONNS and NET_TCP_CONNS for the similar config. > > I did this for clarity. But it is no problem to always use `NET_TCP_CONNS`. I will do this change soon. > But it's also good to share the same config and code as much as possible, instead introducing the unnecessary different code path. The functionality you want can be achieved with the simple change as I suggest before and keep the config and concept consistent. > > If you really want to allocate and free conn dynamically, it could be achieved simply by: > > Set CONFIG_NET_CONNS to 1 and kmm_free conn in case CONFIG_NET_CONNS equals 1 instead put it into free list. > > As I explained before, this is a different thing. With this approach we completely lose the benefit of having preallocated connections. Two solution I can come up: 1. Add NET_TCP_PREALLOC_CONNS to indicate the preallocated conn from .bss section 2. Remove the preallocation to satisfy your need(without allocation from .bss section) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org