Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.0 KiB

  1. /* $NetBSD: db.h,v 1.13 1994/10/26 00:55:48 cgd Exp $ */
  2. /*-
  3. * Copyright (c) 1990, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)db.h 8.7 (Berkeley) 6/16/94
  35. */
  36. #ifndef _DB_H_
  37. #define _DB_H_
  38. #include <sys/types.h>
  39. #include <sys/cdefs.h>
  40. #include <limits.h>
  41. #define RET_ERROR -1 /* Return values. */
  42. #define RET_SUCCESS 0
  43. #define RET_SPECIAL 1
  44. #ifndef __BIT_TYPES_DEFINED__
  45. #define __BIT_TYPES_DEFINED__
  46. typedef __signed char int8_t;
  47. typedef unsigned char u_int8_t;
  48. typedef short int16_t;
  49. typedef unsigned short u_int16_t;
  50. typedef int int32_t;
  51. typedef unsigned int u_int32_t;
  52. #ifdef WE_DONT_NEED_QUADS
  53. typedef long long int64_t;
  54. typedef unsigned long long u_int64_t;
  55. #endif
  56. #endif
  57. #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
  58. typedef u_int32_t pgno_t;
  59. #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
  60. typedef u_int16_t indx_t;
  61. #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
  62. typedef u_int32_t recno_t;
  63. /* Key/data structure -- a Data-Base Thang. */
  64. typedef struct {
  65. void *data; /* data */
  66. size_t size; /* data length */
  67. } DBT;
  68. /* Routine flags. */
  69. #define R_CURSOR 1 /* del, put, seq */
  70. #define __R_UNUSED 2 /* UNUSED */
  71. #define R_FIRST 3 /* seq */
  72. #define R_IAFTER 4 /* put (RECNO) */
  73. #define R_IBEFORE 5 /* put (RECNO) */
  74. #define R_LAST 6 /* seq (BTREE, RECNO) */
  75. #define R_NEXT 7 /* seq */
  76. #define R_NOOVERWRITE 8 /* put */
  77. #define R_PREV 9 /* seq (BTREE, RECNO) */
  78. #define R_SETCURSOR 10 /* put (RECNO) */
  79. #define R_RECNOSYNC 11 /* sync (RECNO) */
  80. typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
  81. /*
  82. * !!!
  83. * The following flags are included in the dbopen(3) call as part of the
  84. * open(2) flags. In order to avoid conflicts with the open flags, start
  85. * at the top of the 16 or 32-bit number space and work our way down. If
  86. * the open flags were significantly expanded in the future, it could be
  87. * a problem. Wish I'd left another flags word in the dbopen call.
  88. *
  89. * !!!
  90. * None of this stuff is implemented yet. The only reason that it's here
  91. * is so that the access methods can skip copying the key/data pair when
  92. * the DB_LOCK flag isn't set.
  93. */
  94. #if UINT_MAX > 65535
  95. #define DB_LOCK 0x20000000 /* Do locking. */
  96. #define DB_SHMEM 0x40000000 /* Use shared memory. */
  97. #define DB_TXN 0x80000000 /* Do transactions. */
  98. #else
  99. #define DB_LOCK 0x2000 /* Do locking. */
  100. #define DB_SHMEM 0x4000 /* Use shared memory. */
  101. #define DB_TXN 0x8000 /* Do transactions. */
  102. #endif
  103. /* Access method description structure. */
  104. typedef struct __db {
  105. DBTYPE type; /* Underlying db type. */
  106. int (*close) __P((struct __db *));
  107. int (*del) __P((const struct __db *, const DBT *, u_int));
  108. int (*get) __P((const struct __db *, const DBT *, DBT *, u_int));
  109. int (*put) __P((const struct __db *, DBT *, const DBT *, u_int));
  110. int (*seq) __P((const struct __db *, DBT *, DBT *, u_int));
  111. int (*sync) __P((const struct __db *, u_int));
  112. void *internal; /* Access method private. */
  113. int (*fd) __P((const struct __db *));
  114. } DB;
  115. #define BTREEMAGIC 0x053162
  116. #define BTREEVERSION 3
  117. /* Structure used to pass parameters to the btree routines. */
  118. typedef struct {
  119. #define R_DUP 0x01 /* duplicate keys */
  120. u_long flags;
  121. u_int cachesize; /* bytes to cache */
  122. int maxkeypage; /* maximum keys per page */
  123. int minkeypage; /* minimum keys per page */
  124. u_int psize; /* page size */
  125. int (*compare) /* comparison function */
  126. __P((const DBT *, const DBT *));
  127. size_t (*prefix) /* prefix function */
  128. __P((const DBT *, const DBT *));
  129. int lorder; /* byte order */
  130. } BTREEINFO;
  131. #define HASHMAGIC 0x061561
  132. #define HASHVERSION 2
  133. /* Structure used to pass parameters to the hashing routines. */
  134. typedef struct {
  135. u_int bsize; /* bucket size */
  136. u_int ffactor; /* fill factor */
  137. u_int nelem; /* number of elements */
  138. u_int cachesize; /* bytes to cache */
  139. u_int32_t /* hash function */
  140. (*hash) __P((const void *, size_t));
  141. int lorder; /* byte order */
  142. } HASHINFO;
  143. /* Structure used to pass parameters to the record routines. */
  144. typedef struct {
  145. #define R_FIXEDLEN 0x01 /* fixed-length records */
  146. #define R_NOKEY 0x02 /* key not required */
  147. #define R_SNAPSHOT 0x04 /* snapshot the input */
  148. u_long flags;
  149. u_int cachesize; /* bytes to cache */
  150. u_int psize; /* page size */
  151. int lorder; /* byte order */
  152. size_t reclen; /* record length (fixed-length records) */
  153. u_char bval; /* delimiting byte (variable-length records */
  154. char *bfname; /* btree file name */
  155. } RECNOINFO;
  156. #ifdef __DBINTERFACE_PRIVATE
  157. /*
  158. * Little endian <==> big endian 32-bit swap macros.
  159. * M_32_SWAP swap a memory location
  160. * P_32_SWAP swap a referenced memory location
  161. * P_32_COPY swap from one location to another
  162. */
  163. #define M_32_SWAP(a) { \
  164. u_int32_t _tmp = a; \
  165. ((char *)&a)[0] = ((char *)&_tmp)[3]; \
  166. ((char *)&a)[1] = ((char *)&_tmp)[2]; \
  167. ((char *)&a)[2] = ((char *)&_tmp)[1]; \
  168. ((char *)&a)[3] = ((char *)&_tmp)[0]; \
  169. }
  170. #define P_32_SWAP(a) { \
  171. u_int32_t _tmp = *(u_int32_t *)a; \
  172. ((char *)a)[0] = ((char *)&_tmp)[3]; \
  173. ((char *)a)[1] = ((char *)&_tmp)[2]; \
  174. ((char *)a)[2] = ((char *)&_tmp)[1]; \
  175. ((char *)a)[3] = ((char *)&_tmp)[0]; \
  176. }
  177. #define P_32_COPY(a, b) { \
  178. ((char *)&(b))[0] = ((char *)&(a))[3]; \
  179. ((char *)&(b))[1] = ((char *)&(a))[2]; \
  180. ((char *)&(b))[2] = ((char *)&(a))[1]; \
  181. ((char *)&(b))[3] = ((char *)&(a))[0]; \
  182. }
  183. /*
  184. * Little endian <==> big endian 16-bit swap macros.
  185. * M_16_SWAP swap a memory location
  186. * P_16_SWAP swap a referenced memory location
  187. * P_16_COPY swap from one location to another
  188. */
  189. #define M_16_SWAP(a) { \
  190. u_int16_t _tmp = a; \
  191. ((char *)&a)[0] = ((char *)&_tmp)[1]; \
  192. ((char *)&a)[1] = ((char *)&_tmp)[0]; \
  193. }
  194. #define P_16_SWAP(a) { \
  195. u_int16_t _tmp = *(u_int16_t *)a; \
  196. ((char *)a)[0] = ((char *)&_tmp)[1]; \
  197. ((char *)a)[1] = ((char *)&_tmp)[0]; \
  198. }
  199. #define P_16_COPY(a, b) { \
  200. ((char *)&(b))[0] = ((char *)&(a))[1]; \
  201. ((char *)&(b))[1] = ((char *)&(a))[0]; \
  202. }
  203. #endif
  204. __BEGIN_DECLS
  205. DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
  206. #ifdef __DBINTERFACE_PRIVATE
  207. DB *__bt_open __P((const char *, int, int, const BTREEINFO *, int));
  208. DB *__hash_open __P((const char *, int, int, const HASHINFO *, int));
  209. DB *__rec_open __P((const char *, int, int, const RECNOINFO *, int));
  210. void __dbpanic __P((DB *dbp));
  211. #endif
  212. __END_DECLS
  213. #endif /* !_DB_H_ */