mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-10-31 00:17:44 +00:00
064106a91b
Create a linux/unaligned directory similar in spirit to the linux/byteorder folder to hold generic implementations collected from various arches. Currently there are five implementations: 1) packed_struct.h: C-struct based, from asm-generic/unaligned.h 2) le_byteshift.h: Open coded byte-swapping, heavily based on asm-arm 3) be_byteshift.h: Open coded byte-swapping, heavily based on asm-arm 4) memmove.h: taken from multiple implementations in tree 5) access_ok.h: taken from x86 and others, unaligned access is ok. All of the new implementations checks for sizes not equal to 1,2,4,8 and will fail to link. API additions: get_unaligned_{le16|le32|le64|be16|be32|be64}(p) which is meant to replace code of the form: le16_to_cpu(get_unaligned((__le16 *)p)); put_unaligned_{le16|le32|le64|be16|be32|be64}(val, pointer) which is meant to replace code of the form: put_unaligned(cpu_to_le16(val), (__le16 *)p); The headers that arches should include from their asm/unaligned.h: access_ok.h : Wrappers of the byteswapping functions in asm/byteorder Choose a particular implementation for little-endian access: le_byteshift.h le_memmove.h (arch must be LE) le_struct.h (arch must be LE) Choose a particular implementation for big-endian access: be_byteshift.h be_memmove.h (arch must be BE) be_struct.h (arch must be BE) After including as needed from the above, include unaligned/generic.h and define your arch's get/put_unaligned as (for LE): Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com> Cc: <linux-arch@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
70 lines
1.4 KiB
C
70 lines
1.4 KiB
C
#ifndef _LINUX_UNALIGNED_LE_BYTESHIFT_H
|
|
#define _LINUX_UNALIGNED_LE_BYTESHIFT_H
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
static inline u16 __get_unaligned_le16(const u8 *p)
|
|
{
|
|
return p[0] | p[1] << 8;
|
|
}
|
|
|
|
static inline u32 __get_unaligned_le32(const u8 *p)
|
|
{
|
|
return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
|
|
}
|
|
|
|
static inline u64 __get_unaligned_le64(const u8 *p)
|
|
{
|
|
return (u64)__get_unaligned_le32(p + 4) << 32 |
|
|
__get_unaligned_le32(p);
|
|
}
|
|
|
|
static inline void __put_unaligned_le16(u16 val, u8 *p)
|
|
{
|
|
*p++ = val;
|
|
*p++ = val >> 8;
|
|
}
|
|
|
|
static inline void __put_unaligned_le32(u32 val, u8 *p)
|
|
{
|
|
__put_unaligned_le16(val >> 16, p + 2);
|
|
__put_unaligned_le16(val, p);
|
|
}
|
|
|
|
static inline void __put_unaligned_le64(u64 val, u8 *p)
|
|
{
|
|
__put_unaligned_le32(val >> 32, p + 4);
|
|
__put_unaligned_le32(val, p);
|
|
}
|
|
|
|
static inline u16 get_unaligned_le16(const void *p)
|
|
{
|
|
return __get_unaligned_le16((const u8 *)p);
|
|
}
|
|
|
|
static inline u32 get_unaligned_le32(const void *p)
|
|
{
|
|
return __get_unaligned_le32((const u8 *)p);
|
|
}
|
|
|
|
static inline u64 get_unaligned_le64(const void *p)
|
|
{
|
|
return __get_unaligned_le64((const u8 *)p);
|
|
}
|
|
|
|
static inline void put_unaligned_le16(u16 val, void *p)
|
|
{
|
|
__put_unaligned_le16(val, p);
|
|
}
|
|
|
|
static inline void put_unaligned_le32(u32 val, void *p)
|
|
{
|
|
__put_unaligned_le32(val, p);
|
|
}
|
|
|
|
static inline void put_unaligned_le64(u64 val, void *p)
|
|
{
|
|
__put_unaligned_le64(val, p);
|
|
}
|
|
|
|
#endif /* _LINUX_UNALIGNED_LE_BYTESHIFT_H */
|