Skip to content

Commit c8ac307

Browse files
SeppoTakalocarlescufi
authored andcommitted
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 <[email protected]>
1 parent 1f3cb08 commit c8ac307

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

subsys/net/lib/sockets/Kconfig

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,38 @@ config NET_SOCKETS_CAN_RECEIVERS
246246
config NET_SOCKETPAIR
247247
bool "Support for socketpair"
248248
select PIPES
249-
depends on HEAP_MEM_POOL_SIZE != 0
250249
help
251250
Communicate over a pair of connected, unnamed UNIX domain sockets.
252251

252+
if NET_SOCKETPAIR
253+
253254
config NET_SOCKETPAIR_BUFFER_SIZE
254255
int "Size of the intermediate buffer, in bytes"
255256
default 64
256257
range 1 4096
257-
depends on NET_SOCKETPAIR
258258
help
259259
Buffer size for socketpair(2)
260260

261+
choice
262+
prompt "Memory management for socketpair"
263+
default NET_SOCKETPAIR_HEAP if HEAP_MEM_POOL_SIZE != 0
264+
265+
config NET_SOCKETPAIR_STATIC
266+
bool "Pre-allocate memory statically"
267+
268+
config NET_SOCKETPAIR_HEAP
269+
bool "Use heap for allocating socketpairs"
270+
depends on HEAP_MEM_POOL_SIZE != 0
271+
272+
endchoice
273+
274+
if NET_SOCKETPAIR_STATIC
275+
config NET_SOCKETPAIR_MAX
276+
int "How many socketpairs to pre-allocate"
277+
default 1
278+
endif
279+
endif
280+
261281
config NET_SOCKETS_NET_MGMT
262282
bool "Network management socket support [EXPERIMENTAL]"
263283
depends on NET_MGMT_EVENT

subsys/net/lib/sockets/socketpair.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ __net_socket struct spair {
5656
uint8_t buf[CONFIG_NET_SOCKETPAIR_BUFFER_SIZE];
5757
};
5858

59+
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
60+
K_MEM_SLAB_DEFINE_STATIC(spair_slab, sizeof(struct spair), CONFIG_NET_SOCKETPAIR_MAX * 2,
61+
__alignof__(struct spair));
62+
#endif /* CONFIG_NET_SOCKETPAIR_STATIC */
63+
5964
/* forward declaration */
6065
static const struct socket_op_vtable spair_fd_op_vtable;
6166

@@ -188,7 +193,9 @@ static void spair_delete(struct spair *spair)
188193

189194
/* ensure no private information is released to the memory pool */
190195
memset(spair, 0, sizeof(*spair));
191-
#ifdef CONFIG_USERSPACE
196+
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
197+
k_mem_slab_free(&spair_slab, (void **) &spair);
198+
#elif CONFIG_USERSPACE
192199
k_object_free(spair);
193200
#else
194201
k_free(spair);
@@ -213,7 +220,14 @@ static struct spair *spair_new(void)
213220
struct spair *spair;
214221
int res;
215222

216-
#ifdef CONFIG_USERSPACE
223+
#ifdef CONFIG_NET_SOCKETPAIR_STATIC
224+
225+
res = k_mem_slab_alloc(&spair_slab, (void **) &spair, K_NO_WAIT);
226+
if (res != 0) {
227+
spair = NULL;
228+
}
229+
230+
#elif CONFIG_USERSPACE
217231
struct z_object *zo = z_dynamic_object_create(sizeof(*spair));
218232

219233
if (zo == NULL) {

0 commit comments

Comments
 (0)