Back to index

lightning-sunbird  0.9+nobinonly
mcom_db.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Netscape Public License
00006  * Version 1.1 (the "License"); you may not use this file except in
00007  * compliance with the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/NPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is 
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the NPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the NPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*- 
00040  * Copyright (c) 1990, 1993, 1994
00041  *     The Regents of the University of California.  All rights reserved.
00042  *
00043  * Redistribution and use in source and binary forms, with or without
00044  * modification, are permitted provided that the following conditions
00045  * are met:
00046  * 1. Redistributions of source code must retain the above copyright
00047  *    notice, this list of conditions and the following disclaimer.
00048  * 2. Redistributions in binary form must reproduce the above copyright
00049  *    notice, this list of conditions and the following disclaimer in the
00050  *    documentation and/or other materials provided with the distribution.
00051  * 3. All advertising materials mentioning features or use of this software
00052  *    must display the following acknowledgement:
00053  *     This product includes software developed by the University of
00054  *     California, Berkeley and its contributors.
00055  * 4. Neither the name of the University nor the names of its contributors
00056  *    may be used to endorse or promote products derived from this software
00057  *    without specific prior written permission.
00058  *
00059  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00060  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00061  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00062  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00063  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00064  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00065  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00066  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00067  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00068  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00069  * SUCH DAMAGE.
00070  *
00071  *     @(#)db.h      8.7 (Berkeley) 6/16/94
00072  */
00073 
00074 #ifndef _DB_H_
00075 #define       _DB_H_
00076 
00077 
00078 #ifdef WINCE
00079 #define off_t long
00080 #endif
00081 
00082 #ifndef macintosh
00083 #include <sys/types.h>
00084 #endif
00085 #include "prtypes.h"
00086 
00087 #include <limits.h>
00088 
00089 #ifdef __DBINTERFACE_PRIVATE
00090 
00091 #ifdef HAVE_SYS_CDEFS_H
00092 #include <sys/cdefs.h>
00093 #else
00094 #include "cdefs.h"
00095 #endif
00096 
00097 #ifdef HAVE_SYS_BYTEORDER_H
00098 #include <sys/byteorder.h>
00099 #endif
00100 
00101 #if defined(__linux) || defined(__BEOS__)
00102 #include <endian.h>
00103 #ifndef BYTE_ORDER
00104 #define BYTE_ORDER    __BYTE_ORDER
00105 #define BIG_ENDIAN    __BIG_ENDIAN
00106 #define LITTLE_ENDIAN __LITTLE_ENDIAN
00107 #endif
00108 #endif /* __linux */
00109 
00110 #ifdef __sgi
00111 #define BYTE_ORDER BIG_ENDIAN
00112 #define BIG_ENDIAN      4321
00113 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
00114 #endif
00115 
00116 #ifdef __sun
00117 #define BIG_ENDIAN      4321
00118 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
00119 
00120 #ifndef __SVR4
00121 /* compat.h is only in 4.1.3 machines. - dp */
00122 #include <compat.h>
00123 #endif
00124 
00125 /* XXX - dp
00126  * Need to find a general way of defining endian-ness in SunOS 5.3
00127  * SunOS 5.4 defines _BIG_ENDIAN and _LITTLE_ENDIAN
00128  * SunOS 5.3 does nothing like this.
00129  */
00130 
00131 #ifndef BYTE_ORDER
00132 
00133 #if defined(_BIG_ENDIAN)
00134 #define BYTE_ORDER BIG_ENDIAN
00135 #elif defined(_LITTLE_ENDIAN)
00136 #define BYTE_ORDER LITTLE_ENDIAN
00137 #elif !defined(__SVR4)
00138 /* 4.1.3 is always BIG_ENDIAN as it was released only on sparc platforms. */
00139 #define BYTE_ORDER BIG_ENDIAN
00140 #elif !defined(vax) && !defined(ntohl) && !defined(lint) && !defined(i386)
00141 /* 5.3 big endian. Copied this above line from sys/byteorder.h */
00142 /* Now we are in a 5.3 SunOS rather non 5.4 or above SunOS  */
00143 #define BYTE_ORDER BIG_ENDIAN
00144 #else
00145 #define BYTE_ORDER LITTLE_ENDIAN
00146 #endif
00147 
00148 #endif /* !BYTE_ORDER */
00149 #endif /* __sun */
00150 
00151 #if defined(__hpux) || defined(__hppa)
00152 #define BYTE_ORDER BIG_ENDIAN
00153 #define BIG_ENDIAN      4321
00154 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
00155 #endif
00156 
00157 #if defined(AIXV3) || defined(AIX)
00158 /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
00159 #include <sys/machine.h>
00160 #endif
00161 
00162 /* Digital Unix */
00163 #ifdef __osf__
00164 #include <machine/endian.h>
00165 #endif
00166 
00167 #ifdef __alpha
00168 #ifndef WIN32
00169 #else
00170 /* Alpha NT */
00171 #define BYTE_ORDER LITTLE_ENDIAN
00172 #define BIG_ENDIAN      4321
00173 #define LITTLE_ENDIAN   1234 
00174 #endif
00175 #endif
00176 
00177 #ifdef NCR
00178 #include <sys/endian.h>
00179 #endif
00180 
00181 #ifdef __QNX__
00182 #ifdef __QNXNTO__
00183 #include <sys/param.h>
00184 #else
00185 #define LITTLE_ENDIAN       1234
00186 #define BIG_ENDIAN   4321
00187 #define BYTE_ORDER   LITTLE_ENDIAN
00188 #endif
00189 #endif
00190 
00191 #ifdef SNI
00192 /* #include <sys/hetero.h> */
00193 #define BYTE_ORDER BIG_ENDIAN
00194 #define BIG_ENDIAN      4321
00195 #define LITTLE_ENDIAN   1234
00196 #endif
00197 
00198 #if defined(_WINDOWS) || defined(XP_OS2_VACPP)
00199 #ifdef BYTE_ORDER
00200 #undef BYTE_ORDER
00201 #endif
00202 
00203 #define BYTE_ORDER LITTLE_ENDIAN
00204 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
00205 #define BIG_ENDIAN      4321
00206 #endif
00207 
00208 #ifdef macintosh
00209 #define BIG_ENDIAN 4321
00210 #define LITTLE_ENDIAN 1234
00211 #define BYTE_ORDER BIG_ENDIAN
00212 #endif
00213 
00214 #endif  /* __DBINTERFACE_PRIVATE */
00215 
00216 #ifdef SCO
00217 #define MAXPATHLEN   1024              
00218 #endif
00219 
00220 #include <fcntl.h>
00221 
00222 #if defined(_WINDOWS) || defined(XP_OS2)
00223 #include <stdio.h>
00224 #include <io.h>
00225 
00226 #ifndef XP_OS2 
00227 #define MAXPATHLEN   1024               
00228 #endif
00229 
00230 #ifdef XP_OS2_VACPP
00231 #include <os2.h>
00232 #define       MAXPATHLEN    CCHMAXPATH
00233 #define       EPERM         EINVAL
00234 #define       ENOTDIR              EBADPOS
00235 #define       S_ISDIR(s)    ((s) & S_IFDIR)
00236 #endif
00237 
00238 #define       EFTYPE        EINVAL        /* POSIX 1003.1 format errno. */
00239 
00240 #ifndef       STDERR_FILENO
00241 #define       STDIN_FILENO  0             /* ANSI C #defines */
00242 #define       STDOUT_FILENO 1
00243 #define       STDERR_FILENO 2
00244 #endif
00245 
00246 #ifndef O_ACCMODE                  /* POSIX 1003.1 access mode mask. */
00247 #define       O_ACCMODE     (O_RDONLY|O_WRONLY|O_RDWR)
00248 #endif
00249 #endif
00250 
00251 #ifdef macintosh
00252 #include <stdio.h>
00253 #include "xp_mcom.h"
00254 #define O_ACCMODE       3       /* Mask for file access modes */
00255 #define EFTYPE 2000
00256 PR_BEGIN_EXTERN_C
00257 int mkstemp(const char *path);
00258 PR_END_EXTERN_C
00259 #endif /* MACINTOSH */
00260 
00261 #if !defined(_WINDOWS) && !defined(macintosh)
00262 #include <sys/stat.h>
00263 #include <errno.h>
00264 #endif
00265 
00266 /* define EFTYPE since most don't */
00267 #ifndef EFTYPE
00268 #define EFTYPE      EINVAL      /* POSIX 1003.1 format errno. */
00269 #endif
00270 
00271 #define       RET_ERROR     -1            /* Return values. */
00272 #define       RET_SUCCESS    0
00273 #define       RET_SPECIAL    1
00274 
00275 #define       MAX_PAGE_NUMBER      0xffffffff    /* >= # of pages in a file */
00276 
00277 #ifndef __sgi
00278 typedef uint32       pgno_t;
00279 #endif
00280 
00281 #define       MAX_PAGE_OFFSET      65535         /* >= # of bytes in a page */
00282 typedef uint16       indx_t;
00283 #define       MAX_REC_NUMBER       0xffffffff    /* >= # of records in a tree */
00284 typedef uint32       recno_t;
00285 
00286 /* Key/data structure -- a Data-Base Thang. */
00287 typedef struct {
00288        void   *data;               /* data */
00289        size_t  size;               /* data length */
00290 } DBT;
00291 
00292 /* Routine flags. */
00293 #define       R_CURSOR      1             /* del, put, seq */
00294 #define       __R_UNUSED    2             /* UNUSED */
00295 #define       R_FIRST              3             /* seq */
00296 #define       R_IAFTER      4             /* put (RECNO) */
00297 #define       R_IBEFORE     5             /* put (RECNO) */
00298 #define       R_LAST        6             /* seq (BTREE, RECNO) */
00299 #define       R_NEXT        7             /* seq */
00300 #define       R_NOOVERWRITE 8             /* put */
00301 #define       R_PREV        9             /* seq (BTREE, RECNO) */
00302 #define       R_SETCURSOR   10            /* put (RECNO) */
00303 #define       R_RECNOSYNC   11            /* sync (RECNO) */
00304 
00305 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
00306 
00307 typedef enum { LockOutDatabase, UnlockDatabase } DBLockFlagEnum;
00308 
00309 /*
00310  * !!!
00311  * The following flags are included in the dbopen(3) call as part of the
00312  * open(2) flags.  In order to avoid conflicts with the open flags, start
00313  * at the top of the 16 or 32-bit number space and work our way down.  If
00314  * the open flags were significantly expanded in the future, it could be
00315  * a problem.  Wish I'd left another flags word in the dbopen call.
00316  *
00317  * !!!
00318  * None of this stuff is implemented yet.  The only reason that it's here
00319  * is so that the access methods can skip copying the key/data pair when
00320  * the DB_LOCK flag isn't set.
00321  */
00322 #if UINT_MAX > 65535
00323 #define       DB_LOCK              0x20000000    /* Do locking. */
00324 #define       DB_SHMEM      0x40000000    /* Use shared memory. */
00325 #define       DB_TXN        0x80000000    /* Do transactions. */
00326 #else
00327 #define       DB_LOCK                  0x2000    /* Do locking. */
00328 #define       DB_SHMEM          0x4000    /* Use shared memory. */
00329 #define       DB_TXN            0x8000    /* Do transactions. */
00330 #endif
00331 
00332 /* Access method description structure. */
00333 typedef struct __db {
00334        DBTYPE type;                /* Underlying db type. */
00335        int (*close)  (struct __db *);
00336        int (*del)    (const struct __db *, const DBT *, uint);
00337        int (*get)    (const struct __db *, const DBT *, DBT *, uint);
00338        int (*put)    (const struct __db *, DBT *, const DBT *, uint);
00339        int (*seq)    (const struct __db *, DBT *, DBT *, uint);
00340        int (*sync)   (const struct __db *, uint);
00341        void *internal;                    /* Access method private. */
00342        int (*fd)     (const struct __db *);
00343 } DB;
00344 
00345 #define       BTREEMAGIC    0x053162
00346 #define       BTREEVERSION  3
00347 
00348 /* Structure used to pass parameters to the btree routines. */
00349 typedef struct {
00350 #define       R_DUP         0x01   /* duplicate keys */
00351        uint32 flags;
00352        uint   cachesize;    /* bytes to cache */
00353        int    maxkeypage;   /* maximum keys per page */
00354        int    minkeypage;   /* minimum keys per page */
00355        uint   psize;        /* page size */
00356        int    (*compare)    /* comparison function */
00357            (const DBT *, const DBT *);
00358        size_t (*prefix)     /* prefix function */
00359            (const DBT *, const DBT *);
00360        int    lorder;              /* byte order */
00361 } BTREEINFO;
00362 
00363 #define       HASHMAGIC     0x061561
00364 #define       HASHVERSION   2
00365 
00366 /* Structure used to pass parameters to the hashing routines. */
00367 typedef struct {
00368        uint   bsize;        /* bucket size */
00369        uint   ffactor;      /* fill factor */
00370        uint   nelem;        /* number of elements */
00371        uint   cachesize;    /* bytes to cache */
00372        uint32        /* hash function */
00373               (*hash) (const void *, size_t);
00374        int    lorder;              /* byte order */
00375 } HASHINFO;
00376 
00377 /* Structure used to pass parameters to the record routines. */
00378 typedef struct {
00379 #define       R_FIXEDLEN    0x01   /* fixed-length records */
00380 #define       R_NOKEY              0x02   /* key not required */
00381 #define       R_SNAPSHOT    0x04   /* snapshot the input */
00382        uint32 flags;
00383        uint   cachesize;    /* bytes to cache */
00384        uint   psize;        /* page size */
00385        int    lorder;              /* byte order */
00386        size_t reclen;              /* record length (fixed-length records) */
00387        uint8  bval;         /* delimiting byte (variable-length records */
00388        char   *bfname;      /* btree file name */ 
00389 } RECNOINFO;
00390 
00391 #ifdef __DBINTERFACE_PRIVATE
00392 /*
00393  * Little endian <==> big endian 32-bit swap macros.
00394  *     M_32_SWAP     swap a memory location
00395  *     P_32_SWAP     swap a referenced memory location
00396  *     P_32_COPY     swap from one location to another
00397  */
00398 #define       M_32_SWAP(a) {                                                 \
00399        uint32 _tmp = a;                                        \
00400        ((char *)&a)[0] = ((char *)&_tmp)[3];                          \
00401        ((char *)&a)[1] = ((char *)&_tmp)[2];                          \
00402        ((char *)&a)[2] = ((char *)&_tmp)[1];                          \
00403        ((char *)&a)[3] = ((char *)&_tmp)[0];                          \
00404 }
00405 #define       P_32_SWAP(a) {                                                 \
00406        uint32 _tmp = *(uint32 *)a;                      \
00407        ((char *)a)[0] = ((char *)&_tmp)[3];                           \
00408        ((char *)a)[1] = ((char *)&_tmp)[2];                           \
00409        ((char *)a)[2] = ((char *)&_tmp)[1];                           \
00410        ((char *)a)[3] = ((char *)&_tmp)[0];                           \
00411 }
00412 #define       P_32_COPY(a, b) {                                       \
00413        ((char *)&(b))[0] = ((char *)&(a))[3];                         \
00414        ((char *)&(b))[1] = ((char *)&(a))[2];                         \
00415        ((char *)&(b))[2] = ((char *)&(a))[1];                         \
00416        ((char *)&(b))[3] = ((char *)&(a))[0];                         \
00417 }
00418 
00419 /*
00420  * Little endian <==> big endian 16-bit swap macros.
00421  *     M_16_SWAP     swap a memory location
00422  *     P_16_SWAP     swap a referenced memory location
00423  *     P_16_COPY     swap from one location to another
00424  */
00425 #define       M_16_SWAP(a) {                                                 \
00426        uint16 _tmp = a;                                        \
00427        ((char *)&a)[0] = ((char *)&_tmp)[1];                          \
00428        ((char *)&a)[1] = ((char *)&_tmp)[0];                          \
00429 }
00430 #define       P_16_SWAP(a) {                                                 \
00431        uint16 _tmp = *(uint16 *)a;                      \
00432        ((char *)a)[0] = ((char *)&_tmp)[1];                           \
00433        ((char *)a)[1] = ((char *)&_tmp)[0];                           \
00434 }
00435 #define       P_16_COPY(a, b) {                                       \
00436        ((char *)&(b))[0] = ((char *)&(a))[1];                         \
00437        ((char *)&(b))[1] = ((char *)&(a))[0];                         \
00438 }
00439 #endif
00440 
00441 PR_BEGIN_EXTERN_C
00442 #if defined(__WATCOMC__) || defined(__WATCOM_CPLUSPLUS__)
00443 extern DB *
00444 #else
00445 PR_EXTERN(DB *)
00446 #endif
00447 dbopen (const char *, int, int, DBTYPE, const void *);
00448 
00449 /* set or unset a global lock flag to disable the
00450  * opening of any DBM file
00451  */
00452 void dbSetOrClearDBLock(DBLockFlagEnum type);
00453 
00454 #ifdef __DBINTERFACE_PRIVATE
00455 DB     *__bt_open (const char *, int, int, const BTREEINFO *, int);
00456 DB     *__hash_open (const char *, int, int, const HASHINFO *, int);
00457 DB     *__rec_open (const char *, int, int, const RECNOINFO *, int);
00458 void    __dbpanic (DB *dbp);
00459 #endif
00460 
00461 PR_END_EXTERN_C
00462 
00463 #endif /* !_DB_H_ */