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.

218 lines
7.5 KiB

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