net: sockets: socketpair: Allow statically allocated socketpairs

When the target board does not have heap by default, allows
statically reserving the space for required socketpairs.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2023-07-28 13:05:58 +03:00 committed by Carles Cufí
commit c8ac3070cc
2 changed files with 38 additions and 4 deletions

View file

@ -246,18 +246,38 @@ config NET_SOCKETS_CAN_RECEIVERS
config NET_SOCKETPAIR
bool "Support for socketpair"
select PIPES
depends on HEAP_MEM_POOL_SIZE != 0
help
Communicate over a pair of connected, unnamed UNIX domain sockets.
if NET_SOCKETPAIR
config NET_SOCKETPAIR_BUFFER_SIZE
int "Size of the intermediate buffer, in bytes"
default 64
range 1 4096
depends on NET_SOCKETPAIR
help
Buffer size for socketpair(2)
choice
prompt "Memory management for socketpair"
default NET_SOCKETPAIR_HEAP if HEAP_MEM_POOL_SIZE != 0
config NET_SOCKETPAIR_STATIC
bool "Pre-allocate memory statically"
config NET_SOCKETPAIR_HEAP
bool "Use heap for allocating socketpairs"
depends on HEAP_MEM_POOL_SIZE != 0
endchoice
if NET_SOCKETPAIR_STATIC
config NET_SOCKETPAIR_MAX
int "How many socketpairs to pre-allocate"
default 1
endif
endif
config NET_SOCKETS_NET_MGMT
bool "Network management socket support [EXPERIMENTAL]"
depends on NET_MGMT_EVENT

View file

@ -56,6 +56,11 @@ __net_socket struct spair {
uint8_t buf[CONFIG_NET_SOCKETPAIR_BUFFER_SIZE];
};
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
K_MEM_SLAB_DEFINE_STATIC(spair_slab, sizeof(struct spair), CONFIG_NET_SOCKETPAIR_MAX * 2,
__alignof__(struct spair));
#endif /* CONFIG_NET_SOCKETPAIR_STATIC */
/* forward declaration */
static const struct socket_op_vtable spair_fd_op_vtable;
@ -188,7 +193,9 @@ static void spair_delete(struct spair *spair)
/* ensure no private information is released to the memory pool */
memset(spair, 0, sizeof(*spair));
#ifdef CONFIG_USERSPACE
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
k_mem_slab_free(&spair_slab, (void **) &spair);
#elif CONFIG_USERSPACE
k_object_free(spair);
#else
k_free(spair);
@ -213,7 +220,14 @@ static struct spair *spair_new(void)
struct spair *spair;
int res;
#ifdef CONFIG_USERSPACE
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
res = k_mem_slab_alloc(&spair_slab, (void **) &spair, K_NO_WAIT);
if (res != 0) {
spair = NULL;
}
#elif CONFIG_USERSPACE
struct z_object *zo = z_dynamic_object_create(sizeof(*spair));
if (zo == NULL) {