eCosPro® Reference Manual
Prev |  | Next

# netbuf_ref()

## Name

netbuf_ref() -- Associate a data pointer with a netbuf

## Synopsis

``void 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.

## Example

Example 4-1. 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 */
        netbuf_ref(buf, string, sizeof(string));

        /* do something with the netbuf */
        /* [...] */

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

Prev | Home | Next
netbuf_free() | Up | netbuf_len()