Name

netbuf_ref() — Associate a data pointer with a netbuf

Synopsis

err_t netbuf_ref (struct netbuf *buf , void *data , u16_t size );

Description

Associates the external memory pointed to by the data pointer with the netbuf buf. The size of the external memory is given by size. Any memory previously allocated to the netbuf is deallocated. The difference between allocating memory for the netbuf with netbuf_alloc() and allocating memory using, e.g., malloc() and referencing it with netbuf_ref() is that in the former case, space for protocol headers is allocated as well which makes processing and sending the buffer faster.

The result returned will be ERR_OK if the data is referenced successfully, or the error ERR_MEM if the data could not be referenced due to lack of memory.

Example

Example 164.2. This example shows a simple use of the netbuf_ref()

int
main()
{
    struct netbuf *buf;
    char string[] = "A string";

    /* create a new netbuf */
    buf = netbuf_new();

    /* reference the string */
    if (netbuf_ref(buf, string, sizeof(string)) == ERR_OK) {
        /* do something with the netbuf */
        /* […] */
    }

    /* deallocate netbuf */
    netbuf_delete(buf);
}