Skip to content

Commit

Permalink
net: sockets: socketpair: Allow statically allocated socketpairs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
SeppoTakalo committed Jul 31, 2023
1 parent 3d37cc3 commit 1639d07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
24 changes: 22 additions & 2 deletions subsys/net/lib/sockets/Kconfig
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions subsys/net/lib/sockets/socketpair.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 1639d07

Please sign in to comment.