Wed Aug 7 17:15:46 2019

Asterisk developer's documentation


unaligned.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2009, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Handle unaligned data access
00021  */
00022 
00023 #ifndef _ASTERISK_UNALIGNED_H
00024 #define _ASTERISK_UNALIGNED_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 #ifdef __GNUC__
00031 /* If we just tell GCC what's going on, we can trust it to behave optimally */
00032 static inline uint64_t get_unaligned_uint64(const void *p)
00033 {
00034    const struct { uint64_t d; } __attribute__((packed)) *pp = p;
00035    return pp->d;
00036 }
00037 
00038 static inline unsigned int get_unaligned_uint32(const void *p)
00039 {
00040    const struct { unsigned int d; } __attribute__((packed)) *pp = p;
00041 
00042    return pp->d;
00043 }
00044 static inline unsigned short get_unaligned_uint16(const void *p)
00045 {
00046    const struct { unsigned short d; } __attribute__((packed)) *pp = p;
00047 
00048    return pp->d;
00049 }
00050 
00051 static inline void put_unaligned_uint64(void *p, uint64_t datum)
00052 {
00053    struct { uint64_t d; } __attribute__((packed,may_alias)) *pp = p;
00054 
00055    pp->d = datum;
00056 }
00057 
00058 static inline void put_unaligned_uint32(void *p, unsigned int datum)
00059 {
00060    struct { unsigned int d; } __attribute__((packed)) *pp = p;
00061 
00062    pp->d = datum;
00063 }
00064 
00065 static inline void put_unaligned_uint16(void *p, unsigned short datum)
00066 {
00067    struct { unsigned short d; } __attribute__((packed)) *pp = p;
00068 
00069    pp->d = datum;
00070 }
00071 #elif defined(SOLARIS) && defined(__sparc__)
00072 static inline uint64_t get_unaligned_uint64(const void *p)
00073 {
00074    const unsigned char *cp = p;
00075 
00076    return
00077       (((uint64_t) cp[0]) << 56) |
00078       (((uint64_t) cp[1]) << 48) |
00079       (((uint64_t) cp[2]) << 40) |
00080       (((uint64_t) cp[3]) << 32) |
00081       (((uint64_t) cp[4]) << 24) |
00082       (((uint64_t) cp[5]) << 16) |
00083       (((uint64_t) cp[6]) <<  8) |
00084       (((uint64_t) cp[7]) <<  0);
00085 }
00086 
00087 static inline unsigned int get_unaligned_uint32(const void *p)
00088 {
00089    const unsigned char *cp = p;
00090 
00091    return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
00092 }
00093 
00094 static inline unsigned short get_unaligned_uint16(const void *p)
00095 {
00096    const unsigned char *cp = p;
00097 
00098    return (cp[0] << 8) | cp[1] ;
00099 }
00100 
00101 static inline void put_unaligned_uint64(void *p, uint64_t datum)
00102 {
00103    unsigned char *cp = p;
00104 
00105    cp[0] = (datum >> 56) & 0xff;
00106    cp[1] = (datum >> 48) & 0xff;
00107    cp[2] = (datum >> 40) & 0xff;
00108    cp[3] = (datum >> 32) & 0xff;
00109    cp[4] = (datum >> 24) & 0xff;
00110    cp[5] = (datum >> 16) & 0xff;
00111    cp[6] = (datum >>  8) & 0xff;
00112    cp[7] = (datum >>  0) & 0xff;
00113 }
00114 
00115 static inline void put_unaligned_uint32(void *p, unsigned int datum)
00116 {
00117    unsigned char *cp = p;
00118 
00119    cp[0] = datum >> 24;
00120    cp[1] = datum >> 16;
00121    cp[2] = datum >> 8;
00122    cp[3] = datum;
00123 }
00124 
00125 static inline void put_unaligned_uint16(void *p, unsigned int datum)
00126 {
00127    unsigned char *cp = p;
00128 
00129    cp[0] = datum >> 8;
00130    cp[1] = datum;
00131 }
00132 #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
00133 #define get_unaligned_uint64(p) (*((uint64_t *)(p)))
00134 #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
00135 #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
00136 #define put_unaligned_uint64(p,d) do { uint64_t *__P = (p); *__P = d; } while(0)
00137 #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
00138 #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
00139 #endif
00140 
00141 #if defined(__cplusplus) || defined(c_plusplus)
00142 }
00143 #endif
00144 
00145 
00146 #endif /* _ASTERISK_UNALIGNED_H */

Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1