2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Handle unaligned data access
23 #ifndef _ASTERISK_UNALIGNED_H
24 #define _ASTERISK_UNALIGNED_H
26 #if defined(__cplusplus) || defined(c_plusplus)
31 /* If we just tell GCC what's going on, we can trust it to behave optimally */
32 static inline unsigned int get_unaligned_uint32(void *p
)
34 struct { unsigned int d
; } __attribute__((packed
)) *pp
= (void *)p
;
38 static inline unsigned short get_unaligned_uint16(void *p
)
40 struct { unsigned short d
; } __attribute__((packed
)) *pp
= (void *)p
;
45 static inline void put_unaligned_uint32(void *p
, unsigned int datum
)
47 struct { unsigned int d
; } __attribute__((packed
)) *pp
= (void *)p
;
52 static inline void put_unaligned_uint16(void *p
, unsigned short datum
)
54 struct { unsigned short d
; } __attribute__((packed
)) *pp
= (void *)p
;
58 #elif defined(SOLARIS) && defined(__sparc__)
59 static inline unsigned int get_unaligned_uint32(void *p
)
61 unsigned char *cp
= p
;
63 return (cp
[0] << 24) | (cp
[1] << 16) | (cp
[2] << 8) | cp
[3];
66 static inline unsigned short get_unaligned_uint16(void *p
)
68 unsigned char *cp
= p
;
70 return (cp
[0] << 8) | cp
[1] ;
73 static inline void put_unaligned_uint32(void *p
, unsigned int datum
)
75 unsigned char *cp
= p
;
83 static inline void put_unaligned_uint16(void *p
, unsigned int datum
)
85 unsigned char *cp
= p
;
90 #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
91 #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
92 #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
93 #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
94 #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
97 #if defined(__cplusplus) || defined(c_plusplus)
102 #endif /* _ASTERISK_UNALIGNED_H */