Reputation: 6738
I am sending UDP packets from an embedded system using lwip, using the netconn API. I create a netbuf like this:
struct netbuf* buf = netbuf_new();
netbuf_ref(buf, &signal_packet, sizeof(signal_packet_t));
and then I send the UDP packet like this:
err_t err = netconn_send(conn, buf);
At what point now can I re-use the memory pointed to by &signal_packet? Does the netconn API make a copy before returning, or does it continue to reference this memory from other threads?
Upvotes: 0
Views: 230
Reputation: 1
Your first line,
struct netbuf* buf = netbuf_new();
allocates a new netbuf structure but not a data buffer.
(the 'new' keyword being used in similar manner to OOP languages, indicating there should be a netbuf_delete()
function coming later for de-allocation.
netbuf_ref(...);
will simply update a given netbuf structure's data pointer to whatever buffer you've chosen, but this does not mean it manages it.
It is your responsibility to allocate (and de-allocate) your referenced memory.
In conclusion - netconn will not copy your buffer. In fact, as stated in the docs:
Based on Packet buffers (PBUF) internally to avoid copying data around.
If you wish to re-use the buffer across multiple threads after sending, you may also want to know that according to the same docs, this library is not thread-safe (except for netbuf_new()
and netbuf_delete()
).
Buffers must not be shared accross multiple threads, all functions except netbuf_new() and netbuf_delete() are not thread-safe.
Either implement your own thread safety or use a separate buffer for outgoing data :)
Author of the question did not reference this, but also a worthy mention:
netbuf_alloc(struct netbuf *buf, u16_t size)
will allocate a buffer for you, in a given netbuf, and will clear it on deletion.
Upvotes: 0