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

db_byteorder.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_byteorder.c,v 12.1 2005/06/16 20:20:52 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 #endif
00015 
00016 #include "db_int.h"
00017 
00018 /*
00019  * __db_isbigendian --
00020  *      Return 1 if big-endian (Motorola and Sparc), not little-endian
00021  *      (Intel and Vax).  We do this work at run-time, rather than at
00022  *      configuration time so cross-compilation and general embedded
00023  *      system support is simpler.
00024  *
00025  * PUBLIC: int __db_isbigendian __P((void));
00026  */
00027 int
00028 __db_isbigendian()
00029 {
00030         union {                                 /* From Harbison & Steele.  */
00031                 long l;
00032                 char c[sizeof(long)];
00033         } u;
00034 
00035         u.l = 1;
00036         return (u.c[sizeof(long) - 1] == 1);
00037 }
00038 
00039 /*
00040  * __db_byteorder --
00041  *      Return if we need to do byte swapping, checking for illegal
00042  *      values.
00043  *
00044  * PUBLIC: int __db_byteorder __P((DB_ENV *, int));
00045  */
00046 int
00047 __db_byteorder(dbenv, lorder)
00048         DB_ENV *dbenv;
00049         int lorder;
00050 {
00051         int is_bigendian;
00052 
00053         is_bigendian = __db_isbigendian();
00054 
00055         switch (lorder) {
00056         case 0:
00057                 break;
00058         case 1234:
00059                 if (is_bigendian)
00060                         return (DB_SWAPBYTES);
00061                 break;
00062         case 4321:
00063                 if (!is_bigendian)
00064                         return (DB_SWAPBYTES);
00065                 break;
00066         default:
00067                 __db_err(dbenv,
00068             "unsupported byte order, only big and little-endian supported");
00069                 return (EINVAL);
00070         }
00071         return (0);
00072 }

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