xref: /openbsd/gnu/usr.bin/perl/ext/SDBM_File/sdbm.h (revision eac174f2)
1 /*
2  * sdbm - ndbm work-alike hashed database library
3  * based on Per-Ake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4  * author: oz@nexus.yorku.ca
5  * status: public domain.
6  */
7 #ifndef PERL_SDBM_FILE_SDBM_H_
8 #define PERL_SDBM_FILE_SDBM_H_
9 
10 #define DBLKSIZ 4096
11 #define PBLKSIZ 1024
12 #define PAIRMAX 1008			/* arbitrary on PBLKSIZ-N */
13 #define SPLTMAX	10			/* maximum allowed splits */
14                                         /* for a single insertion */
15 #ifdef VMS
16 #define DIRFEXT	".sdbm_dir"
17 #else
18 #define DIRFEXT	".dir"
19 #endif
20 #define PAGFEXT	".pag"
21 
22 typedef struct {
23         int dirf;		       /* directory file descriptor */
24         int pagf;		       /* page file descriptor */
25         int flags;		       /* status/error flags, see below */
26         long maxbno;		       /* size of dirfile in bits */
27         long curbit;		       /* current bit number */
28         long hmask;		       /* current hash mask */
29         long blkptr;		       /* current block for nextkey */
30         int keyptr;		       /* current key for nextkey */
31         long blkno;		       /* current page to read/write */
32         long pagbno;		       /* current page in pagbuf */
33         char pagbuf[PBLKSIZ];	       /* page file block buffer */
34         long dirbno;		       /* current block in dirbuf */
35         char dirbuf[DBLKSIZ];	       /* directory file block buffer */
36 } DBM;
37 
38 #define DBM_RDONLY	0x1	       /* data base open read-only */
39 #define DBM_IOERR	0x2	       /* data base I/O error */
40 
41 /*
42  * utility macros
43  */
44 #define sdbm_rdonly(db)		((db)->flags & DBM_RDONLY)
45 #define sdbm_error(db)		((db)->flags & DBM_IOERR)
46 
47 #define sdbm_clearerr(db)	((db)->flags &= ~DBM_IOERR)  /* ouch */
48 
49 #define sdbm_dirfno(db)	((db)->dirf)
50 #define sdbm_pagfno(db)	((db)->pagf)
51 
52 typedef struct {
53         const char *dptr;
54         int dsize;
55 } datum;
56 
57 extern const datum nullitem;
58 
59 /*
60  * flags to sdbm_store
61  */
62 #define DBM_INSERT	0
63 #define DBM_REPLACE	1
64 
65 /*
66  * ndbm interface
67  */
68 extern DBM *sdbm_open(char *, int, int);
69 extern void sdbm_close(DBM *);
70 extern datum sdbm_fetch(DBM *, datum);
71 extern int sdbm_delete(DBM *, datum);
72 extern int sdbm_store(DBM *, datum, datum, int);
73 extern datum sdbm_firstkey(DBM *);
74 extern datum sdbm_nextkey(DBM *);
75 extern int sdbm_exists(DBM *, datum);
76 
77 /*
78  * other
79  */
80 extern DBM *sdbm_prep(char *, char *, int, int);
81 extern long sdbm_hash(const char *, int);
82 
83 #ifndef SDBM_ONLY
84 #define dbm_open sdbm_open
85 #define dbm_close sdbm_close
86 #define dbm_fetch sdbm_fetch
87 #define dbm_store sdbm_store
88 #define dbm_delete sdbm_delete
89 #define dbm_firstkey sdbm_firstkey
90 #define dbm_nextkey sdbm_nextkey
91 #define dbm_error sdbm_error
92 #define dbm_clearerr sdbm_clearerr
93 #endif
94 
95 /* Most of the following is stolen from perl.h.  We don't include
96    perl.h here because we just want the portability parts of perl.h,
97    not everything else.
98 */
99 #ifndef H_PERL  /* Include guard */
100 #include "embed.h"  /* Follow all the global renamings. */
101 
102 /*
103  * The following contortions are brought to you on behalf of all the
104  * standards, semi-standards, de facto standards, not-so-de-facto standards
105  * of the world, as well as all the other botches anyone ever thought of.
106  * The basic theory is that if we work hard enough here, the rest of the
107  * code can be a lot prettier.  Well, so much for theory.  Sorry, Henry...
108  */
109 
110 #include <errno.h>
111 #ifdef HAS_SOCKET
112 #   ifdef I_NET_ERRNO
113 #     include <net/errno.h>
114 #   endif
115 #endif
116 
117 #include <stdio.h>
118 #include <ctype.h>
119 #include <setjmp.h>
120 
121 #if defined(I_UNISTD)
122 #include <unistd.h>
123 #endif
124 
125 #ifdef VMS
126 #  include <file.h>
127 #  include <unixio.h>
128 #endif
129 
130 #ifdef I_SYS_PARAM
131 #   if !defined(WIN32) && !defined(VMS)
132 #       ifdef PARAM_NEEDS_TYPES
133 #	    include <sys/types.h>
134 #       endif
135 #       include <sys/param.h>
136 #   endif
137 #endif
138 
139 #ifndef _TYPES_		/* If types.h defines this it's easy. */
140 #   ifndef major		/* Does everyone's types.h define this? */
141 #	include <sys/types.h>
142 #   endif
143 #endif
144 
145 #include <sys/stat.h>
146 
147 #ifndef SEEK_SET
148 # ifdef L_SET
149 #  define SEEK_SET	L_SET
150 # else
151 #  define SEEK_SET	0  /* Wild guess. */
152 # endif
153 #endif
154 
155 /* Use all the "standard" definitions */
156 #include <stdlib.h>
157 
158 #define MEM_SIZE Size_t
159 
160 /* This comes after <stdlib.h> so we don't try to change the standard
161  * library prototypes; we'll use our own instead. */
162 
163 #if defined(MYMALLOC) && !defined(PERL_POLLUTE_MALLOC)
164 #  define malloc  Perl_malloc
165 #  define calloc  Perl_calloc
166 #  define realloc Perl_realloc
167 #  define free    Perl_mfree
168 
169 #ifdef __cplusplus
170 extern "C" {
171 #endif
172 
173 Malloc_t Perl_malloc(MEM_SIZE nbytes);
174 Malloc_t Perl_calloc(MEM_SIZE elements, MEM_SIZE size);
175 Malloc_t Perl_realloc(Malloc_t where, MEM_SIZE nbytes);
176 Free_t   Perl_mfree(Malloc_t where);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif /* MYMALLOC */
183 
184 #include <string.h>
185 
186 #define memzero(d,l) memset(d,0,l)
187 
188 #ifdef BUGGY_MSC
189 #  pragma function(memcmp)
190 #endif
191 
192 #define memNE(s1,s2,l) (memcmp(s1,s2,l))
193 #define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
194 
195 #ifdef I_NETINET_IN
196 #  ifdef VMS
197 #    include <in.h>
198 #  else
199 #    include <netinet/in.h>
200 #  endif
201 #endif
202 
203 #endif /* Include guard */
204 
205 #endif /* PERL_SDBM_FILE_SDBM_H_ */
206