Question

What does struct uart_omap_port *up = (struct uart_omap_port *)port means?.

Is it similar to container_of macro?

Was it helpful?

Solution

struct uart_omap_port *up = (struct uart_omap_port *)port;

It means that you "port" pointer variable should be typecast to of type "struct uart_omap_port". This is not required in C as it would implicitly do the typecasting(if your port is of void*). This is not macro it just way(typecasting) the pointer with the different type.

EDIT

Code Snippet From Linux/drivers/tty/serial/omap-serial.c

static void serial_omap_enable_ms(struct uart_port *port) {
    .....
    struct uart_omap_port *up = to_uart_omap_port(port);
}


#define to_uart_omap_port(p)    ((container_of((p), struct uart_omap_port, port)))

#define container_of(ptr, type, member) ({                    \
     const typeof(((type *)0)->member)*__mptr = (ptr);    \
     (type *)((char *)__mptr - offsetof(type, member)); })

This is the code which you have reffered. Yes,'to_uart_omap_port' is the MACRO which internally uses 'container_of' and 'offsetof' MACRO.

This has been written to get the pointer 'up' which is of type 'struct uart_omap_port' from the pointer 'port' of type 'struct uart_port'. This is bit complicated and you need to check how 'struct uart_port' and 'struct uart_omap_port' been declared.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top