Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_swap.h

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1990, 1993, 1994
00009  *      The Regents of the University of California.  All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the University nor the names of its contributors
00020  *    may be used to endorse or promote products derived from this software
00021  *    without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  *
00035  * $Id: db_swap.h,v 12.3 2005/06/16 20:21:47 bostic Exp $
00036  */
00037 
00038 #ifndef _DB_SWAP_H_
00039 #define _DB_SWAP_H_
00040 
00041 /*
00042  * Little endian <==> big endian 64-bit swap macros.
00043  *      M_64_SWAP       swap a memory location
00044  *      P_64_COPY       copy potentially unaligned 4 byte quantities
00045  *      P_64_SWAP       swap a referenced memory location
00046  */
00047 #undef  M_64_SWAP
00048 #define M_64_SWAP(a) {                                                  \
00049         u_int64_t _tmp;                                                 \
00050         _tmp = (u_int64_t)a;                                            \
00051         ((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[7];                   \
00052         ((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[6];                   \
00053         ((u_int8_t *)&a)[2] = ((u_int8_t *)&_tmp)[5];                   \
00054         ((u_int8_t *)&a)[3] = ((u_int8_t *)&_tmp)[4];                   \
00055         ((u_int8_t *)&a)[4] = ((u_int8_t *)&_tmp)[3];                   \
00056         ((u_int8_t *)&a)[5] = ((u_int8_t *)&_tmp)[2];                   \
00057         ((u_int8_t *)&a)[6] = ((u_int8_t *)&_tmp)[1];                   \
00058         ((u_int8_t *)&a)[7] = ((u_int8_t *)&_tmp)[0];                   \
00059 }
00060 #undef  P_64_COPY
00061 #define P_64_COPY(a, b) {                                               \
00062         ((u_int8_t *)b)[0] = ((u_int8_t *)a)[0];                        \
00063         ((u_int8_t *)b)[1] = ((u_int8_t *)a)[1];                        \
00064         ((u_int8_t *)b)[2] = ((u_int8_t *)a)[2];                        \
00065         ((u_int8_t *)b)[3] = ((u_int8_t *)a)[3];                        \
00066         ((u_int8_t *)b)[4] = ((u_int8_t *)a)[4];                        \
00067         ((u_int8_t *)b)[5] = ((u_int8_t *)a)[5];                        \
00068         ((u_int8_t *)b)[6] = ((u_int8_t *)a)[6];                        \
00069         ((u_int8_t *)b)[7] = ((u_int8_t *)a)[7];                        \
00070 }
00071 #undef  P_64_SWAP
00072 #define P_64_SWAP(a) {                                                  \
00073         u_int64_t _tmp;                                                 \
00074         P_64_COPY(a, &_tmp);                                            \
00075         ((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[7];                    \
00076         ((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[6];                    \
00077         ((u_int8_t *)a)[2] = ((u_int8_t *)&_tmp)[5];                    \
00078         ((u_int8_t *)a)[3] = ((u_int8_t *)&_tmp)[4];                    \
00079         ((u_int8_t *)a)[4] = ((u_int8_t *)&_tmp)[3];                    \
00080         ((u_int8_t *)a)[5] = ((u_int8_t *)&_tmp)[2];                    \
00081         ((u_int8_t *)a)[6] = ((u_int8_t *)&_tmp)[1];                    \
00082         ((u_int8_t *)a)[7] = ((u_int8_t *)&_tmp)[0];                    \
00083 }
00084 
00085 /*
00086  * Little endian <==> big endian 32-bit swap macros.
00087  *      M_32_SWAP       swap a memory location
00088  *      P_32_COPY       copy potentially unaligned 4 byte quantities
00089  *      P_32_SWAP       swap a referenced memory location
00090  */
00091 #undef  M_32_SWAP
00092 #define M_32_SWAP(a) {                                                  \
00093         u_int32_t _tmp;                                                 \
00094         _tmp = (u_int32_t)a;                                            \
00095         ((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[3];                   \
00096         ((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[2];                   \
00097         ((u_int8_t *)&a)[2] = ((u_int8_t *)&_tmp)[1];                   \
00098         ((u_int8_t *)&a)[3] = ((u_int8_t *)&_tmp)[0];                   \
00099 }
00100 #undef  P_32_COPY
00101 #define P_32_COPY(a, b) {                                               \
00102         ((u_int8_t *)b)[0] = ((u_int8_t *)a)[0];                        \
00103         ((u_int8_t *)b)[1] = ((u_int8_t *)a)[1];                        \
00104         ((u_int8_t *)b)[2] = ((u_int8_t *)a)[2];                        \
00105         ((u_int8_t *)b)[3] = ((u_int8_t *)a)[3];                        \
00106 }
00107 #undef  P_32_SWAP
00108 #define P_32_SWAP(a) {                                                  \
00109         u_int32_t _tmp;                                                 \
00110         P_32_COPY(a, &_tmp);                                            \
00111         ((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[3];                    \
00112         ((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[2];                    \
00113         ((u_int8_t *)a)[2] = ((u_int8_t *)&_tmp)[1];                    \
00114         ((u_int8_t *)a)[3] = ((u_int8_t *)&_tmp)[0];                    \
00115 }
00116 
00117 /*
00118  * Little endian <==> big endian 16-bit swap macros.
00119  *      M_16_SWAP       swap a memory location
00120  *      P_16_COPY       copy potentially unaligned 2 byte quantities
00121  *      P_16_SWAP       swap a referenced memory location
00122  */
00123 #undef  M_16_SWAP
00124 #define M_16_SWAP(a) {                                                  \
00125         u_int16_t _tmp;                                                 \
00126         _tmp = (u_int16_t)a;                                            \
00127         ((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[1];                   \
00128         ((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[0];                   \
00129 }
00130 #undef  P_16_COPY
00131 #define P_16_COPY(a, b) {                                               \
00132         ((u_int8_t *)b)[0] = ((u_int8_t *)a)[0];                        \
00133         ((u_int8_t *)b)[1] = ((u_int8_t *)a)[1];                        \
00134 }
00135 #undef  P_16_SWAP
00136 #define P_16_SWAP(a) {                                                  \
00137         u_int16_t _tmp;                                                 \
00138         P_16_COPY(a, &_tmp);                                            \
00139         ((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[1];                    \
00140         ((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[0];                    \
00141 }
00142 
00143 #undef  SWAP32
00144 #define SWAP32(p) {                                                     \
00145         P_32_SWAP(p);                                                   \
00146         (p) += sizeof(u_int32_t);                                       \
00147 }
00148 #undef  SWAP16
00149 #define SWAP16(p) {                                                     \
00150         P_16_SWAP(p);                                                   \
00151         (p) += sizeof(u_int16_t);                                       \
00152 }
00153 
00154 /*
00155  * Berkeley DB has local versions of htonl() and ntohl() that operate on
00156  * pointers to the right size memory locations; the portability magic for
00157  * finding the real system functions isn't worth the effort.
00158  */
00159 #undef  DB_HTONL
00160 #define DB_HTONL(p) do {                                                \
00161         if (!__db_isbigendian())                                        \
00162                 P_32_SWAP(p);                                           \
00163 } while (0)
00164 #undef  DB_NTOHL
00165 #define DB_NTOHL(p) do {                                                \
00166         if (!__db_isbigendian())                                        \
00167                 P_32_SWAP(p);                                           \
00168 } while (0)
00169 
00170 #endif /* !_DB_SWAP_H_ */

Generated on Sun Dec 25 12:14:22 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2