Skip to content
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: buf: Make a net_buf_move(&buf) API #72798

Open
jori-nordic opened this issue May 15, 2024 · 4 comments
Open

net: buf: Make a net_buf_move(&buf) API #72798

jori-nordic opened this issue May 15, 2024 · 4 comments
Assignees
Labels
area: Networking Buffers net_buf/net_buf_simple API & implementation Feature Request A request for a new feature

Comments

@jori-nordic
Copy link
Contributor

We sometimes do an implicit move of netbufs.
In that case, the best we do is leave a comment, and maybe NULL out the original variable, like this

	/* Move `buf` into `temp` */
	temp = buf;
	buf = NULL;

It would be nice to have a proper API to do this in net/buf.h, something like

	temp = net_buf_move(&buf);
	__ASSERT_NO_MSG(buf == NULL);

It could be defined like so

struct net_buf *net_buf_move(struct net_buf **original)
{
	struct net_buf *new = *original;

	__ASSERT_NO_MSG(new->ref == 1);
	*original = NULL;

	return new;
}
@jori-nordic jori-nordic added Feature Request A request for a new feature area: Networking Buffers net_buf/net_buf_simple API & implementation labels May 15, 2024
@jhedberg
Copy link
Member

@jori-nordic thanks for creating this issue. Just a few quick thoughts:

  • I don't think we want to constrain this API to ref == 1. Moving a reference doesn't need to imply that this is the only reference there is - that's a separate use-case specific constraint IMO.
  • Since we have atomic_ptr_clear() I'd just use it. Most places may not need it right now, but as we move towards supporting preemptible threads then being able to use this API without locking will be nice.

@jukkar
Copy link
Member

jukkar commented May 17, 2024

To me this new function gives impression of moving the data from/to the net_buf, like what memmove() does. Not sure what it could be called, but some examples could be net_buf_replace(), net_buf_swap() or similar.

@jhedberg
Copy link
Member

jhedberg commented May 17, 2024

To me this new function gives impression of moving the data from/to the net_buf, like what memmove() does. Not sure what it could be called, but some examples could be net_buf_replace(), net_buf_swap() or similar.

@jukkar point taken. That said, we do tend to use net_buf_*_mem() in the net_buf API for memory operations. Also, "move" is verb that's used for analogous things in other languages, like Rust. Would it help to append _ref() to the API, i.e. net_buf_move_ref()? (not sure I prefer that more, but that could just be due to my dislike for unnecessarily long symbol names :)

@jukkar
Copy link
Member

jukkar commented May 17, 2024

Yes, net_buf_move_ref() would indeed be better here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking Buffers net_buf/net_buf_simple API & implementation Feature Request A request for a new feature
Projects
None yet
Development

No branches or pull requests

3 participants