Unaligned Access Helpers
[Utility Library]

Collaboration diagram for Unaligned Access Helpers:

Reading and Writing Unaligned Native Endian Values



static __always_inline uint16_t read_unaligned_16 (const void *ptr)
 Read 16-bit value from a possibly unaligned address.
static __always_inline uint32_t read_unaligned_32 (const void *ptr)
 Read 32-bit value from a possibly unaligned address.
static __always_inline void write_unaligned_16 (void *ptr, uint16_t value)
 Write 16-bit value to a possibly unaligned address.
static __always_inline void write_unaligned_32 (void *ptr, uint32_t value)
 Write 32-bit value to a possibly unaligned address.

Reading and Writing Unaligned Big Endian Values



#define read_unaligned_be16(ptr)   be16_to_cpu(read_unaligned_16(ptr))
 Read 16-bit Big Endian value from a possibly unaligned address.
#define read_unaligned_be32(ptr)   be32_to_cpu(read_unaligned_32(ptr))
 Read 32-bit Big Endian value from a possibly unaligned address.
#define write_unaligned_be16(ptr, value)   write_unaligned_16(ptr, cpu_to_be16(value))
 Write 16-bit Big Endian value to a possibly unaligned address.
#define write_unaligned_be32(ptr, value)   write_unaligned_32(ptr, cpu_to_be32(value))
 Write 32-bit Big Endian value to a possibly unaligned address.

Reading and Writing Unaligned Little Endian Values



#define read_unaligned_le16(ptr)   le16_to_cpu(read_unaligned_16(ptr))
 Read 16-bit Little Endian value from a possibly unaligned address.
#define read_unaligned_le32(ptr)   le32_to_cpu(read_unaligned_32(ptr))
 Read 32-bit Little Endian value from a possibly unaligned address.
#define write_unaligned_le16(ptr, value)   write_unaligned_16(ptr, cpu_to_le16(value))
 Write 16-bit Little Endian value to a possibly unaligned address.
#define write_unaligned_le32(ptr, value)   write_unaligned_32(ptr, cpu_to_le32(value))
 Write 32-bit Little Endian value to a possibly unaligned address.

Detailed Description

Different CPUs may have different addressing limitations on memory accesses. In particular, many CPUs cannot load a 16-bit value from an address which isn't a multiple of two and similarly for larger values. The helper functions and macros defined here will ensure that the optimum method of accessing unaligned variables is selected based on the target platform.

Since unaligned fields are commonly found in protocol-mandated structures, such fields may often need to be accessed using non-native endian as well, so endian-aware versions of the accessors are also included.

Example
Consider a file system which stores the inode number and file size at some arbitrary offset within a block of data. Both numbers are stored in little endian byte order regardless of the CPU, so that disks, memory cards, etc. created on one system may be easily read on another.
The code below will access those numbers correctly regardless of the CPU's byte order and addressing limitations.
uint16_t read_inode_number(const uint8_t *data, unsigned int offset)
{
        return read_unaligned_le16(&data[offset]);
}

void write_file_size(uint8_t *data, unsigned int offset, uint32_t size)
{
        write_unaligned_le16(&data[offset], size);
} 
See also:
Byte Order Conversion

Define Documentation

#define read_unaligned_be16 ( ptr   )     be16_to_cpu(read_unaligned_16(ptr))

Read 16-bit Big Endian value from a possibly unaligned address.

Definition at line 72 of file unaligned-direct.h.

#define read_unaligned_be32 ( ptr   )     be32_to_cpu(read_unaligned_32(ptr))

Read 32-bit Big Endian value from a possibly unaligned address.

Definition at line 73 of file unaligned-direct.h.

#define read_unaligned_le16 ( ptr   )     le16_to_cpu(read_unaligned_16(ptr))

Read 16-bit Little Endian value from a possibly unaligned address.

Definition at line 79 of file unaligned-direct.h.

#define read_unaligned_le32 ( ptr   )     le32_to_cpu(read_unaligned_32(ptr))

Read 32-bit Little Endian value from a possibly unaligned address.

Definition at line 80 of file unaligned-direct.h.

#define write_unaligned_be16 ( ptr,
value   )     write_unaligned_16(ptr, cpu_to_be16(value))

Write 16-bit Big Endian value to a possibly unaligned address.

Definition at line 74 of file unaligned-direct.h.

#define write_unaligned_be32 ( ptr,
value   )     write_unaligned_32(ptr, cpu_to_be32(value))

Write 32-bit Big Endian value to a possibly unaligned address.

Definition at line 76 of file unaligned-direct.h.

#define write_unaligned_le16 ( ptr,
value   )     write_unaligned_16(ptr, cpu_to_le16(value))

Write 16-bit Little Endian value to a possibly unaligned address.

Definition at line 81 of file unaligned-direct.h.

#define write_unaligned_le32 ( ptr,
value   )     write_unaligned_32(ptr, cpu_to_le32(value))

Write 32-bit Little Endian value to a possibly unaligned address.

Definition at line 83 of file unaligned-direct.h.


Function Documentation

uint16_t read_unaligned_16 ( const void *  ptr  )  [static]

Read 16-bit value from a possibly unaligned address.

Definition at line 52 of file unaligned-direct.h.

uint32_t read_unaligned_32 ( const void *  ptr  )  [static]

Read 32-bit value from a possibly unaligned address.

Definition at line 57 of file unaligned-direct.h.

void write_unaligned_16 ( void *  ptr,
uint16_t  value 
) [static]

Write 16-bit value to a possibly unaligned address.

Definition at line 62 of file unaligned-direct.h.

void write_unaligned_32 ( void *  ptr,
uint32_t  value 
) [static]

Write 32-bit value to a possibly unaligned address.

Definition at line 67 of file unaligned-direct.h.

Generated on Thu Apr 29 14:09:43 2010 for uart-loopback by  doxygen 1.6.3