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