Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:56 2009

## 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 - 2005, 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 unsigned int get_unaligned_uint32(void *p)
    00033 {
    00034    struct { unsigned int d; } __attribute__((packed)) *pp = (void *)p;
    00035
    00036    return pp->d;
    00037 }
    00038 static inline unsigned short get_unaligned_uint16(void *p)
    00039 {
    00040    struct { unsigned short d; } __attribute__((packed)) *pp = (void *)p;
    00041
    00042    return pp->d;
    00043 }
    00044
    00045 static inline void put_unaligned_uint32(void *p, unsigned int datum)
    00046 {
    00047    struct { unsigned int d; } __attribute__((packed)) *pp = (void *)p;
    00048
    00049    pp->d = datum;
    00050 }
    00051
    00052 static inline void put_unaligned_uint16(void *p, unsigned short datum)
    00053 {
    00054    struct { unsigned short d; } __attribute__((packed)) *pp = (void *)p;
    00055
    00056    pp->d = datum;
    00057 }
    00058 #elif defined(SOLARIS) && defined(__sparc__)
    00059 static inline unsigned int get_unaligned_uint32(void *p)
    00060 {
    00061    unsigned char *cp = p;
    00062
    00063    return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
    00064 }
    00065
    00066 static inline unsigned short get_unaligned_uint16(void *p)
    00067 {
    00068    unsigned char *cp = p;
    00069
    00070    return (cp[0] << 8) | cp[1] ;
    00071 }
    00072
    00073 static inline void put_unaligned_uint32(void *p, unsigned int datum)
    00074 {
    00075    unsigned char *cp = p;
    00076
    00077    cp[0] = datum >> 24;
    00078    cp[1] = datum >> 16;
    00079    cp[2] = datum >> 8;
    00080    cp[3] = datum;
    00081 }
    00082
    00083 static inline void put_unaligned_uint16(void *p, unsigned int datum)
    00084 {
    00085    unsigned char *cp = p;
    00086
    00087    cp[0] = datum >> 8;
    00088    cp[1] = datum;
    00089 }
    00090 #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
    00091 #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
    00092 #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
    00093 #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
    00094 #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
    00095 #endif
    00096
    00097 #if defined(__cplusplus) || defined(c_plusplus)
    00098 }
    00099 #endif
    00100
    00101
    00102 #endif /* _ASTERISK_UNALIGNED_H */