diff options
-rw-r--r-- | ChangeLog | 18 | ||||
-rw-r--r-- | doc/gdbm.texinfo | 79 | ||||
-rw-r--r-- | src/gdbm.h.in | 30 | ||||
-rw-r--r-- | src/gdbmconst.h | 23 | ||||
-rw-r--r-- | src/gdbmdefs.h | 3 | ||||
-rw-r--r-- | src/gdbmopen.c | 11 | ||||
-rw-r--r-- | src/gdbmreorg.c | 2 | ||||
-rw-r--r-- | src/gdbmsetopt.c | 114 | ||||
-rw-r--r-- | src/mmap.c | 10 |
9 files changed, 228 insertions, 62 deletions
@@ -2,2 +2,20 @@ + Improve gdbm_setopt interface. + + * src/gdbm.h.in (GDBM_OPENMASK): New constant. + * src/gdbmconst.h: Remove constants duplicated in + gdbm.h.in. + * src/gdbmdefs.h (gdbm_file_info) <allow_mmap> + <mmap_inited>: Replace with a single member: memory_mapping. + All callers updated. + (GDBM_SETCACHESIZE,GDBM_SETSYNCMODE) + (GDBM_SETCENTFREE,GDBM_SETCOALESCEBLKS): New option names. + (GDBM_SETMMAP,GDBM_GETMMAP,GDBM_GETFLAGS) + (GDBM_GETCACHESIZE,GDBM_GETSYNCMODE,GDBM_GETCENTFREE) + (GDBM_GETCOALESCEBLKS,GDBM_GETMAXMAPSIZE): New option codes. + * src/gdbmsetopt.c: Implement new options. + * doc/gdbm.texinfo: Document new options. + +2011-08-11 Sergey Poznyakoff <gray@gnu.org.ua> + Improve memory mapping support. diff --git a/doc/gdbm.texinfo b/doc/gdbm.texinfo index 5dc8a97..04efeea 100644 --- a/doc/gdbm.texinfo +++ b/doc/gdbm.texinfo @@ -741,3 +741,3 @@ open database. void *@var{value}, int @var{size}) -Sets an option on the database. +Sets an option on the database or returns the value of an option. @@ -749,5 +749,6 @@ The pointer returned by @code{gdbm_open}. @item option -The option to be set. +The option to be set or retreived. @item value -A pointer to the value to which @var{option} will be set. +A pointer to the value to which @var{option} will be set or where to +place the option value (depending on the option). @item size @@ -761,3 +762,5 @@ The valid options are: @kwindex GDBM_CACHESIZE -@item GDBM_CACHESIZE +@kwindex GDBM_SETCACHESIZE +@item GDBM_SETCACHESIZE +@itemx GDBM_CACHESIZE Set the size of the internal bucket cache. This option may only be @@ -765,3 +768,19 @@ set once on each GDBM_FILE descriptor, and is set automatically to 100 upon the first access to the database. The @var{value} should point -to an integer holding the desired cache size. +to a @code{size_t} holding the desired cache size. + +The @samp{GDBM_CACHESIZE} option is provided for compatibility with +earlier versions. + +@kwindex GDBM_GETCACHESIZE +@item GDBM_GETCACHESIZE +Return the size of the internal bucket cache. The @var{value} should +point to a @code{size_t} variable, where the size will be stored. + +@kwindex GDBM_GETFLAGS +@item GDBM_GETFLAGS +Return the flags describing the state of the database. The @var{value} should +point to a @code{int} variable where to store the flags. The return +is the same as the flags used when opening the database (@pxref{Open, +gdbm_open}), except that it reflects the current state (which may have +been altered by another calls to @code{gdbm_setopt}. @@ -775,7 +794,9 @@ disable it. This option is retained for compatibility with previous versions of -@code{gdbm}. Its effect is the reverse of @code{GDBM_SYNCMODE} +@code{gdbm}. Its effect is the reverse of @code{GDBM_SETSYNCMODE} (see below). +@kwindex GDBM_SETSYNCMODE @kwindex GDBM_SYNCMODE -@item GDBM_SYNCMODE +@item GDBM_SETSYNCMODE +@itemx GDBM_SYNCMODE Turn on or off file system synchronization operations. This @@ -786,7 +807,17 @@ turn it off. Note, that this option is a reverse of @code{GDBM_FASTMODE}, -i.e. calling @code{GDBM_SYNCMODE} with @samp{TRUE} has the same effect +i.e. calling @code{GDBM_SETSYNCMODE} with @samp{TRUE} has the same effect as calling @code{GDBM_FASTMODE} with @samp{FALSE}. +The @samp{GDBM_SYNCMODE} option is provided for compatibility with +earlier versions. + +@kwindex GDBM_GETSYNCMODE +@item GDBM_GETSYNCMODE +Return the current synchronization status. The @var{value} should +point to an @code{int} where the status will be stored. + +@kwindex GDBM_SETCENTFREE @kwindex GDBM_CENTFREE -@item GDBM_CENTFREE +@item GDBM_SETCENTFREE +@itemx GDBM_CENTFREE @emph{NOTICE: This feature is still under study.} @@ -800,4 +831,9 @@ turn central block pool on, and @samp{FALSE} to turn it off. +The @samp{GDBM_CENTFREE} option is provided for compatibility with +earlier versions. + +@kwindex GDBM_SETCOALESCEBLKS @kwindex GDBM_COALESCEBLKS -@item GDBM_COALESCEBLKS +@item GDBM_SETCOALESCEBLKS +@itemx GDBM_COALESCEBLKS @emph{NOTICE: This feature is still under study.} @@ -812,5 +848,10 @@ turn it off. +@kwindex GDBM_GETCOALESCEBLKS +@item GDBM_GETCOALESCEBLKS +Return the current status of free block merging. The @var{value} should +point to an @code{int} where the status will be stored. + @kwindex GDBM_SETMAXMAPSIZE @item GDBM_SETMAXMAPSIZE -Sets maximum size of the memory mapped region. The @var{value} should +Sets maximum size of a memory mapped region. The @var{value} should point to a value of type @code{size_t}, @code{unsigned long} or @@ -819,2 +860,18 @@ boundary (the page size is obtained from @code{sysconf(_SC_PAGESIZE)}). + +@kwindex GDBM_GETMAXMAPSIZE +@item GDBM_GETMAXMAPSIZE +Return the maximum size of a memory mapped region. The @var{value} should +point to a value of type @code{size_t} where to return the data. + +@kwindex GDBM_SETMMAP +@item GDBM_SETMMAP +Enable or disable memory mapping mode. The @var{value} should point +to an integer: @samp{TRUE} to enable memory mapping or @samp{FALSE} to +disable it. + +@kwindex GDBM_GETMMAP +@item GDBM_GETMMAP +Check whether memory mapping is enabled. The @var{value} should point +to an integer where to return the status. @end table diff --git a/src/gdbm.h.in b/src/gdbm.h.in index 8eaee79..da88777 100644 --- a/src/gdbm.h.in +++ b/src/gdbm.h.in @@ -37,2 +37,4 @@ #define GDBM_NEWDB 3 /* A writer. Always create a new db. */ +#define GDBM_OPENMASK 7 /* Mask for the above. */ + #define GDBM_FAST 0x10 /* Write fast! => No fsyncs. OBSOLETE. */ @@ -48,8 +50,24 @@ /* Parameters to gdbm_setopt, specifing the type of operation to perform. */ -#define GDBM_CACHESIZE 1 /* Set the cache size. */ -#define GDBM_FASTMODE 2 /* Toggle fast mode. OBSOLETE. */ -#define GDBM_SYNCMODE 3 /* Turn on or off sync operations. */ -#define GDBM_CENTFREE 4 /* Keep all free blocks in the header. */ -#define GDBM_COALESCEBLKS 5 /* Attempt to coalesce free blocks. */ -#define GDBM_SETMAXMAPSIZE 6 /* Set maximum mapped memory size */ +#define GDBM_SETCACHESIZE 1 /* Set the cache size. */ +#define GDBM_FASTMODE 2 /* Toggle fast mode. OBSOLETE. */ +#define GDBM_SETSYNCMODE 3 /* Turn on or off sync operations. */ +#define GDBM_SETCENTFREE 4 /* Keep all free blocks in the header. */ +#define GDBM_SETCOALESCEBLKS 5 /* Attempt to coalesce free blocks. */ +#define GDBM_SETMAXMAPSIZE 6 /* Set maximum mapped memory size */ +#define GDBM_SETMMAP 7 + +/* Compatibility defines: */ +#define GDBM_CACHESIZE GDBM_SETCACHESIZE +#define GDBM_SYNCMODE GDBM_SETSYNCMODE +#define GDBM_CENTFREE GDBM_SETCENTFREE +#define GDBM_COALESCEBLKS GDBM_SETCENTFREE + +#define GDBM_GETFLAGS 8 +#define GDBM_GETMMAP 9 +#define GDBM_GETCACHESIZE 10 +#define GDBM_GETSYNCMODE 11 +#define GDBM_GETCENTFREE 12 +#define GDBM_GETCOALESCEBLKS 13 +#define GDBM_GETMAXMAPSIZE 14 + diff --git a/src/gdbmconst.h b/src/gdbmconst.h index 7a3f5b9..24eac5f 100644 --- a/src/gdbmconst.h +++ b/src/gdbmconst.h @@ -37,25 +37,2 @@ -/* Parameters to gdbm_open. */ -#define GDBM_READER 0 /* READERS only. */ -#define GDBM_WRITER 1 /* READERS and WRITERS. Can not create. */ -#define GDBM_WRCREAT 2 /* If not found, create the db. */ -#define GDBM_NEWDB 3 /* ALWAYS create a new db. (WRITER) */ -#define GDBM_OPENMASK 7 /* Mask for the above. */ -#define GDBM_FAST 0x10 /* Write fast! => No fsyncs. OBSOLETE. */ -#define GDBM_SYNC 0x20 /* Sync operations to the disk. */ -#define GDBM_NOLOCK 0x40 /* Don't do file locking operations. */ -#define GDBM_NOMMAP 0x80 /* Don't use mmap(). */ - -/* Parameters to gdbm_store for simple insertion or replacement in the - case a key to store is already in the database. */ -#define GDBM_INSERT 0 /* Do not overwrite data in the database. */ -#define GDBM_REPLACE 1 /* Replace the old value with the new value. */ - -/* Parameters to gdbm_setopt, specifing the type of operation to perform. */ -#define GDBM_CACHESIZE 1 /* Set the cache size. */ -#define GDBM_FASTMODE 2 /* Turn on or off fast mode. OBSOLETE. */ -#define GDBM_SYNCMODE 3 /* Turn on or off sync operations. */ -#define GDBM_CENTFREE 4 /* Keep all free blocks in the header. */ -#define GDBM_COALESCEBLKS 5 /* Attempt to coalesce free blocks. */ - /* In freeing blocks, we will ignore any blocks smaller (and equal) to diff --git a/src/gdbmdefs.h b/src/gdbmdefs.h index 2221aa5..e45abf8 100644 --- a/src/gdbmdefs.h +++ b/src/gdbmdefs.h @@ -152,4 +152,3 @@ struct gdbm_file_info { /* Whether or not we're allowing mmap() use. */ - unsigned allow_mmap :1; - unsigned mmap_inited :1; + unsigned memory_mapping :1; diff --git a/src/gdbmopen.c b/src/gdbmopen.c index a110d64..b1b38b4 100644 --- a/src/gdbmopen.c +++ b/src/gdbmopen.c @@ -86,3 +86,3 @@ gdbm_open (const char *file, int block_size, int flags, int mode, - dbf->mmap_inited = FALSE; + dbf->memory_mapping = FALSE; dbf->mapped_size_max = SIZE_T_MAX; @@ -111,3 +111,2 @@ gdbm_open (const char *file, int block_size, int flags, int mode, dbf->coalesce_blocks = FALSE; /* Default to not coalescing blocks. */ - dbf->allow_mmap = TRUE; /* Default to using mmap(). */ @@ -123,6 +122,2 @@ gdbm_open (const char *file, int block_size, int flags, int mode, } - if (flags & GDBM_NOMMAP) - { - dbf->allow_mmap = FALSE; - } @@ -397,6 +392,6 @@ gdbm_open (const char *file, int block_size, int flags, int mode, #if HAVE_MMAP - if (dbf->allow_mmap) + if (!(flags & GDBM_NOMMAP)) { if (_gdbm_mapped_init (dbf) == 0) - dbf->mmap_inited = TRUE; + dbf->memory_mapping = TRUE; else diff --git a/src/gdbmreorg.c b/src/gdbmreorg.c index aed372e..6bde222 100644 --- a/src/gdbmreorg.c +++ b/src/gdbmreorg.c @@ -178,3 +178,3 @@ gdbm_reorganize (GDBM_FILE dbf) /* Re-initialize mapping if required */ - if (dbf->allow_mmap) + if (dbf->memory_mapping) _gdbm_mapped_init (dbf); diff --git a/src/gdbmsetopt.c b/src/gdbmsetopt.c index 54a972f..f9494d0 100644 --- a/src/gdbmsetopt.c +++ b/src/gdbmsetopt.c @@ -69,3 +69,5 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) { - case GDBM_CACHESIZE: + /* Cache size: */ + + case GDBM_SETCACHESIZE: /* Optval will point to the new size of the cache. */ @@ -81,4 +83,13 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) + case GDBM_GETCACHESIZE: + if (!optval || optlen != sizeof (size_t)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(size_t*) optval = dbf->cache_size; + break; + + /* Obsolete form of GDBM_SETSYNCMODE. */ case GDBM_FASTMODE: - /* Obsolete form of SYNCMODE. */ if ((n = getbool (optval, optlen)) == -1) @@ -87,4 +98,6 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) break; - - case GDBM_SYNCMODE: + + /* SYNC mode: */ + + case GDBM_SETSYNCMODE: /* Optval will point to either true or false. */ @@ -95,3 +108,13 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) - case GDBM_CENTFREE: + case GDBM_GETSYNCMODE: + if (!optval || optlen != sizeof (int)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(int*) optval = !dbf->fast_write; + break; + + /* CENTFREE - set or get the stat of the central block repository */ + case GDBM_SETCENTFREE: /* Optval will point to either true or false. */ @@ -102,3 +125,13 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) - case GDBM_COALESCEBLKS: + case GDBM_GETCENTFREE: + if (!optval || optlen != sizeof (int)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(int*) optval = !dbf->central_free; + break; + + /* Coalesce state: */ + case GDBM_SETCOALESCEBLKS: /* Optval will point to either true or false. */ @@ -109,2 +142,42 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) + case GDBM_GETCOALESCEBLKS: + if (!optval || optlen != sizeof (int)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(int*) optval = dbf->coalesce_blocks; + break; + + /* Mmap mode */ + case GDBM_SETMMAP: + if ((n = getbool (optval, optlen)) == -1) + return -1; + __fsync (dbf); + if (n == dbf->memory_mapping) + return 0; + if (n) + { + if (_gdbm_mapped_init (dbf) == 0) + dbf->memory_mapping = TRUE; + else + return -1; + } + else + { + _gdbm_mapped_unmap (dbf); + dbf->memory_mapping = FALSE; + } + break; + + case GDBM_GETMMAP: + if (!optval || optlen != sizeof (int)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(int*) optval = dbf->memory_mapping; + break; + + /* Maximum size of a memory mapped region */ case GDBM_SETMAXMAPSIZE: @@ -121,2 +194,31 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen) + case GDBM_GETMAXMAPSIZE: + if (!optval || optlen != sizeof (size_t)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + *(size_t*) optval = dbf->mapped_size_max; + break; + + /* Flags */ + case GDBM_GETFLAGS: + if (!optval || optlen != sizeof (int)) + { + gdbm_errno = GDBM_OPT_ILLEGAL; + return -1; + } + else + { + int flags = dbf->read_write; + if (!dbf->fast_write) + flags |= GDBM_SYNC; + if (!dbf->file_locking) + flags |= GDBM_NOLOCK; + if (!dbf->memory_mapping) + flags |= GDBM_NOMMAP; + *(int*) optval = flags; + } + break; + default: @@ -210,3 +210,3 @@ _gdbm_mapped_read (GDBM_FILE dbf, void *buffer, size_t len) { - if (dbf->mmap_inited) + if (dbf->memory_mapping) { @@ -227,3 +227,3 @@ _gdbm_mapped_read (GDBM_FILE dbf, void *buffer, size_t len) - dbf->mmap_inited = FALSE; + dbf->memory_mapping = FALSE; if (lseek (dbf->desc, pos, SEEK_SET) != pos) @@ -260,3 +260,3 @@ _gdbm_mapped_write (GDBM_FILE dbf, void *buffer, size_t len) { - if (dbf->mmap_inited) + if (dbf->memory_mapping) { @@ -277,3 +277,3 @@ _gdbm_mapped_write (GDBM_FILE dbf, void *buffer, size_t len) - dbf->mmap_inited = FALSE; + dbf->memory_mapping = FALSE; if (lseek (dbf->desc, pos, SEEK_SET) != pos) @@ -314,3 +314,3 @@ _gdbm_mapped_lseek (GDBM_FILE dbf, off_t offset, int whence) { - if (dbf->mmap_inited) + if (dbf->memory_mapping) { |