1 /* gdbmdefs.h - The include file for dbm. Defines structure and constants. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1990, 1991, 1993, 2007, 2011 Free Software Foundation,
7 GDBM is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GDBM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbmconst.h"
23 #define DEFAULT_TEXT_DOMAIN PACKAGE
26 #define _(s) gettext (s)
29 /* The type definitions are next. */
31 /* The available file space is stored in an "avail" table. The one with
32 most activity is contained in the file header. (See below.) When that
33 one filles up, it is split in half and half is pushed on an "avail
34 stack." When the active avail table is empty and the "avail stack" is
35 not empty, the top of the stack is popped into the active avail table. */
37 /* The following structure is the element of the avaliable table. */
39 int av_size; /* The size of the available block. */
40 off_t av_adr; /* The file address of the available block. */
43 /* This is the actual table. The in-memory images of the avail blocks are
44 allocated by malloc using a calculated size. */
46 int size; /* The number of avail elements in the table.*/
47 int count; /* The number of entries in the table. */
48 off_t next_block; /* The file address of the next avail block. */
49 avail_elem av_table[1]; /* The table. Make it look like an array. */
52 /* The dbm file header keeps track of the current location of the hash
53 directory and the free space in the file. */
56 int header_magic; /* Version of file. */
57 int block_size; /* The optimal i/o blocksize from stat. */
58 off_t dir; /* File address of hash directory table. */
59 int dir_size; /* Size in bytes of the table. */
60 int dir_bits; /* The number of address bits used in the table.*/
61 int bucket_size; /* Size in bytes of a hash bucket struct. */
62 int bucket_elems; /* Number of elements in a hash bucket. */
63 off_t next_block; /* The next unallocated block address. */
64 avail_block avail; /* This must be last because of the psuedo
65 array in avail. This avail grows to fill
70 /* The dbm hash bucket element contains the full 31 bit hash value, the
71 "pointer" to the key and data (stored together) with their sizes. It also
72 has a small part of the actual key value. It is used to verify the first
73 part of the key has the correct value without having to read the actual
77 int hash_value; /* The complete 31 bit value. */
78 char key_start[SMALL]; /* Up to the first SMALL bytes of the key. */
79 off_t data_pointer; /* The file address of the key record. The
80 data record directly follows the key. */
81 int key_size; /* Size of key data in the file. */
82 int data_size; /* Size of associated data in the file. */
86 /* A bucket is a small hash table. This one consists of a number of
87 bucket elements plus some bookkeeping fields. The number of elements
88 depends on the optimum blocksize for the storage device and on a
89 parameter given at file creation time. This bucket takes one block.
90 When one of these tables gets full, it is split into two hash buckets.
91 The contents are split between them by the use of the first few bits
92 of the 31 bit hash function. The location in a bucket is the hash
93 value modulo the size of the bucket. The in-memory images of the
94 buckets are allocated by malloc using a calculated size depending of
95 the file system buffer size. To speed up write, each bucket will have
96 BUCKET_AVAIL avail elements with the bucket. */
99 int av_count; /* The number of bucket_avail entries. */
100 avail_elem bucket_avail[BUCKET_AVAIL]; /* Distributed avail. */
101 int bucket_bits; /* The number of bits used to get here. */
102 int count; /* The number of element buckets full. */
103 bucket_element h_table[1]; /* The table. Make it look like an array.*/
106 /* We want to keep from reading buckets as much as possible. The following is
107 to implement a bucket cache. When full, buckets will be dropped in a
108 least recently read from disk order. */
110 /* To speed up fetching and "sequential" access, we need to implement a
111 data cache for key/data pairs read from the file. To find a key, we
112 must exactly match the key from the file. To reduce overhead, the
113 data will be read at the same time. Both key and data will be stored
114 in a data cache. Each bucket cached will have a one element data
126 hash_bucket * ca_bucket;
128 char ca_changed; /* Data in the bucket changed. */
129 data_cache_elem ca_data;
132 /* This final structure contains all main memory based information for
133 a gdbm file. This allows multiple gdbm files to be opened at the same
134 time by one program. */
136 struct gdbm_file_info {
137 /* Global variables and pointers to dynamic variables used by gdbm. */
142 /* The reader/writer status. */
143 unsigned read_write :2;
145 /* Fast_write is set to 1 if no fsyncs are to be done. */
146 unsigned fast_write :1;
148 /* Central_free is set if all free blocks are kept in the header. */
149 unsigned central_free :1;
151 /* Coalesce_blocks is set if we should try to merge free blocks. */
152 unsigned coalesce_blocks :1;
154 /* Whether or not we should do file locking ourselves. */
155 unsigned file_locking :1;
157 /* Whether or not we're allowing mmap() use. */
158 unsigned memory_mapping :1;
160 /* Whether the database was open with GDBM_CLOEXEC flag */
163 /* Type of file locking in use. */
164 enum { LOCKING_NONE = 0, LOCKING_FLOCK, LOCKING_LOCKF,
165 LOCKING_FCNTL } lock_type;
167 /* The fatal error handling routine. */
168 void (*fatal_err) (const char *);
170 /* The gdbm file descriptor which is set in gdbm_open. */
173 /* The file header holds information about the database. */
174 gdbm_file_header *header;
176 /* The hash table directory from extendible hashing. See Fagin et al,
177 ACM Trans on Database Systems, Vol 4, No 3. Sept 1979, 315-344 */
180 /* The bucket cache. */
181 cache_elem *bucket_cache;
185 /* Points to the current hash bucket in the cache. */
188 /* The directory entry used to get the current hash bucket. */
191 /* Pointer to the current bucket's cache entry. */
192 cache_elem *cache_entry;
194 /* Bookkeeping of things that need to be written back at the
196 unsigned header_changed :1;
197 unsigned directory_changed :1;
198 unsigned bucket_changed :1;
199 unsigned second_changed :1;
202 size_t mapped_size_max;/* Max. allowed value for mapped_size */
203 void *mapped_region; /* Mapped region */
204 size_t mapped_size; /* Size of the region */
205 off_t mapped_pos; /* Current offset in the region */
206 off_t mapped_off; /* Position in the file where the region
210 /* Execute CODE without clobbering errno */
211 #define SAVE_ERRNO(code) \
220 /* Now define all the routines in use. */