1 /*- 2 * Copyright (c) 1991, 1993, 2007 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)compat.h 8.13 (Berkeley) 2/21/94 34 */ 35 36 #ifndef _DB_INT_H_ 37 #define _DB_INT_H_ 38 39 #include "config.h" 40 #include "db.h" 41 42 /* deal with autoconf-based stuff */ 43 44 #define DB_LITTLE_ENDIAN 1234 45 #define DB_BIG_ENDIAN 4321 46 47 #include <stdlib.h> 48 #ifdef HAVE_ENDIAN_H 49 # include <endian.h> 50 #endif 51 #ifdef HAVE_MACHINE_ENDIAN_H 52 # include <machine/endian.h> 53 #endif 54 #ifdef HAVE_SYS_PARAM_H 55 # include <sys/param.h> 56 #endif 57 /* Handle both BIG and LITTLE defined and BYTE_ORDER matches one, or 58 just one defined; both with and without leading underscores. 59 60 Ignore "PDP endian" machines, this code doesn't support them 61 anyways. */ 62 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 63 # ifdef __LITTLE_ENDIAN__ 64 # define LITTLE_ENDIAN __LITTLE_ENDIAN__ 65 # endif 66 # ifdef __BIG_ENDIAN__ 67 # define BIG_ENDIAN __BIG_ENDIAN__ 68 # endif 69 #endif 70 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 71 # ifdef _LITTLE_ENDIAN 72 # define LITTLE_ENDIAN _LITTLE_ENDIAN 73 # endif 74 # ifdef _BIG_ENDIAN 75 # define BIG_ENDIAN _BIG_ENDIAN 76 # endif 77 # ifdef _BYTE_ORDER 78 # define BYTE_ORDER _BYTE_ORDER 79 # endif 80 #endif 81 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 82 # ifdef __LITTLE_ENDIAN 83 # define LITTLE_ENDIAN __LITTLE_ENDIAN 84 # endif 85 # ifdef __BIG_ENDIAN 86 # define BIG_ENDIAN __BIG_ENDIAN 87 # endif 88 # ifdef __BYTE_ORDER 89 # define BYTE_ORDER __BYTE_ORDER 90 # endif 91 #endif 92 93 #if defined(_MIPSEL) && !defined(LITTLE_ENDIAN) 94 # define LITTLE_ENDIAN 95 #endif 96 #if defined(_MIPSEB) && !defined(BIG_ENDIAN) 97 # define BIG_ENDIAN 98 #endif 99 100 #if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(BYTE_ORDER) 101 # if LITTLE_ENDIAN == BYTE_ORDER 102 # define DB_BYTE_ORDER DB_LITTLE_ENDIAN 103 # elif BIG_ENDIAN == BYTE_ORDER 104 # define DB_BYTE_ORDER DB_BIG_ENDIAN 105 # else 106 # error "LITTLE_ENDIAN and BIG_ENDIAN defined, but can't determine byte order" 107 # endif 108 #elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) 109 # define DB_BYTE_ORDER DB_LITTLE_ENDIAN 110 #elif defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) 111 # define DB_BYTE_ORDER DB_BIG_ENDIAN 112 #else 113 # error "can't determine byte order from included system headers" 114 #endif 115 116 #if 0 117 #ifdef WORDS_BIGENDIAN 118 #define DB_BYTE_ORDER DB_BIG_ENDIAN 119 #else 120 #define DB_BYTE_ORDER DB_LITTLE_ENDIAN 121 #endif 122 #endif 123 124 /* end autoconf-based stuff */ 125 126 /* include necessary system header files */ 127 128 #ifdef HAVE_UNISTD_H 129 #include <unistd.h> 130 #endif 131 #include <limits.h> 132 #include <fcntl.h> 133 #include <stdio.h> 134 #include <errno.h> 135 #include <stdint.h> 136 #include <sys/types.h> 137 #include <sys/stat.h> 138 #include <sys/param.h> 139 140 /* types and constants used for database structure */ 141 142 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */ 143 typedef u_int32_t db_pgno_t; 144 #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */ 145 typedef u_int16_t indx_t; 146 #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */ 147 typedef u_int32_t recno_t; 148 149 /* 150 * Little endian <==> big endian 32-bit swap macros. 151 * M_32_SWAP swap a memory location 152 * P_32_SWAP swap a referenced memory location 153 * P_32_COPY swap from one location to another 154 */ 155 #define M_32_SWAP(a) { \ 156 u_int32_t _tmp = a; \ 157 ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 158 ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 159 ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 160 ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 161 } 162 #define P_32_SWAP(a) { \ 163 char _tmp[4]; \ 164 _tmp[0] = ((char *)a)[0]; \ 165 _tmp[1] = ((char *)a)[1]; \ 166 _tmp[2] = ((char *)a)[2]; \ 167 _tmp[3] = ((char *)a)[3]; \ 168 ((char *)a)[0] = _tmp[3]; \ 169 ((char *)a)[1] = _tmp[2]; \ 170 ((char *)a)[2] = _tmp[1]; \ 171 ((char *)a)[3] = _tmp[0]; \ 172 } 173 #define P_32_COPY(a, b) { \ 174 ((char *)&(b))[0] = ((char *)&(a))[3]; \ 175 ((char *)&(b))[1] = ((char *)&(a))[2]; \ 176 ((char *)&(b))[2] = ((char *)&(a))[1]; \ 177 ((char *)&(b))[3] = ((char *)&(a))[0]; \ 178 } 179 180 /* 181 * Little endian <==> big endian 16-bit swap macros. 182 * M_16_SWAP swap a memory location 183 * P_16_SWAP swap a referenced memory location 184 * P_16_COPY swap from one location to another 185 */ 186 #define M_16_SWAP(a) { \ 187 u_int16_t _tmp = a; \ 188 ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 189 ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 190 } 191 #define P_16_SWAP(a) { \ 192 char _tmp[2]; \ 193 _tmp[0] = ((char *)a)[0]; \ 194 _tmp[1] = ((char *)a)[1]; \ 195 ((char *)a)[0] = _tmp[1]; \ 196 ((char *)a)[1] = _tmp[0]; \ 197 } 198 #define P_16_COPY(a, b) { \ 199 ((char *)&(b))[0] = ((char *)&(a))[1]; \ 200 ((char *)&(b))[1] = ((char *)&(a))[0]; \ 201 } 202 203 /* open functions for each database type, used in dbopen() */ 204 205 #define __bt_open __kdb2_bt_open 206 #define __hash_open __kdb2_hash_open 207 #define __rec_open __kdb2_rec_open 208 #define __dbpanic __kdb2_dbpanic 209 210 DB *__bt_open __P((const char *, int, int, const BTREEINFO *, int)); 211 DB *__hash_open __P((const char *, int, int, const HASHINFO *, int)); 212 DB *__rec_open __P((const char *, int, int, const RECNOINFO *, int)); 213 void __dbpanic __P((DB *dbp)); 214 215 /* 216 * There is no portable way to figure out the maximum value of a file 217 * offset, so we put it here. 218 */ 219 #ifdef OFF_T_MAX 220 #define DB_OFF_T_MAX OFF_T_MAX 221 #else 222 #define DB_OFF_T_MAX LONG_MAX 223 #endif 224 225 #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */ 226 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 227 #endif 228 229 /* 230 * If you can't provide lock values in the open(2) call. Note, this 231 * allows races to happen. 232 */ 233 #ifndef O_EXLOCK /* 4.4BSD extension. */ 234 #define O_EXLOCK 0 235 #endif 236 237 #ifndef O_SHLOCK /* 4.4BSD extension. */ 238 #define O_SHLOCK 0 239 #endif 240 241 #ifndef EFTYPE 242 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */ 243 #endif 244 245 #ifndef STDERR_FILENO 246 #define STDIN_FILENO 0 /* ANSI C #defines */ 247 #define STDOUT_FILENO 1 248 #define STDERR_FILENO 2 249 #endif 250 251 #ifndef SEEK_END 252 #define SEEK_SET 0 /* POSIX 1003.1 seek values */ 253 #define SEEK_CUR 1 254 #define SEEK_END 2 255 #endif 256 257 #ifndef NULL /* ANSI C #defines NULL everywhere. */ 258 #define NULL 0 259 #endif 260 261 #ifndef MAX /* Usually found in <sys/param.h>. */ 262 #define MAX(_a,_b) ((_a)<(_b)?(_b):(_a)) 263 #endif 264 #ifndef MIN /* Usually found in <sys/param.h>. */ 265 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) 266 #endif 267 268 #ifndef S_ISDIR /* POSIX 1003.1 file type tests. */ 269 #define S_ISDIR(m) ((m & 0170000) == 0040000) /* directory */ 270 #define S_ISCHR(m) ((m & 0170000) == 0020000) /* char special */ 271 #define S_ISBLK(m) ((m & 0170000) == 0060000) /* block special */ 272 #define S_ISREG(m) ((m & 0170000) == 0100000) /* regular file */ 273 #define S_ISFIFO(m) ((m & 0170000) == 0010000) /* fifo */ 274 #endif 275 #ifndef S_ISLNK /* BSD POSIX 1003.1 extensions */ 276 #define S_ISLNK(m) ((m & 0170000) == 0120000) /* symbolic link */ 277 #define S_ISSOCK(m) ((m & 0170000) == 0140000) /* socket */ 278 #endif 279 280 #ifndef O_BINARY 281 #define O_BINARY 0 /* Needed for Win32 compiles */ 282 #endif 283 #endif /* _DB_INT_H_ */ 284