New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: sockets: socketpair: Allow statically allocated socketpairs #60914
Conversation
If this is acceptable, I would use the socketpairs on LwM2M engine #60887 Not all boards define heap and I don't want to push that requirement to all LwM2M users, so I would propose this instead. |
subsys/net/lib/sockets/socketpair.c
Outdated
@@ -56,6 +56,11 @@ __net_socket struct spair { | |||
uint8_t buf[CONFIG_NET_SOCKETPAIR_BUFFER_SIZE]; | |||
}; | |||
|
|||
#ifdef CONFIG_NET_SOCKETPAIR_STATIC | |||
static struct spair storage[CONFIG_NET_SOCKETPAIR_MAX * 2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use k_mem_slab
to hide the allocation details? It could simplify the allocation/deallocation of static objects a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sys_bitarray is also useful for this sort of thing and it works with an arbitrary number of entries
subsys/net/lib/sockets/Kconfig
Outdated
config NET_SOCKETPAIR_MAX | ||
int "How many socketpairs to pre-allocate" | ||
default 1 | ||
range 1 16 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we map this to 32bit var, then minimum is to check that if this value is changed, there will be a build error, so please add BUILD_ASSERT somewhere to enforce this.
Also we could allow arbitrary long bitmasks with ATOMIC_DEFINE and not rely on int size. Something like this could be implemented later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that an int can be used without a range, and then just use a build assert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int res; | ||
|
||
#ifdef CONFIG_USERSPACE | ||
#ifdef CONFIG_NET_SOCKETPAIR_STATIC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This search is O(n)
but search with sys_bitarray is O(n/64)
.. which is also O(n)
..
I've never done a performance comparison though 🤷🏼♂️
It's close. |
Thank you for both of the suggestions. |
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>
When the target board does not have heap by default, allows statically reserving the space for required socketpairs.
Maximum number of statically allocated pairs is 16, so we can fit the usage flags into 32-bit atomic variable.