commit e0bc74ff013a3628b17911be624d17067259c07b
parent f602d80a10b4d62a8090af8c8b6b8fe17c9ea8a8
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Tue, 29 Jun 2021 12:23:00 +0000
cdb always mmaps
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
5 files changed, 31 insertions(+), 46 deletions(-)
diff --git a/package/deps.mak b/package/deps.mak
@@ -54,7 +54,6 @@ src/include/skalibs/strerr2.h: src/include/skalibs/strerr.h
src/include/skalibs/tai.h: src/include/skalibs/gccattributes.h src/include/skalibs/uint64.h
src/include/skalibs/textclient.h: src/include/skalibs/allreadwrite.h src/include/skalibs/tai.h src/include/skalibs/textmessage.h
src/include/skalibs/textmessage.h: src/include/skalibs/allreadwrite.h src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/gccattributes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h
-src/include/skalibs/uint32.h: src/include/skalibs/uint64.h
src/include/skalibs/unix-timed.h: src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h
src/include/skalibs/unix-transactional.h: src/include/skalibs/stralloc.h src/include/skalibs/uint64.h
src/include/skalibs/unixconnection.h: src/include/skalibs/unixmessage.h
@@ -321,11 +320,11 @@ src/libstddjb/cbuffer_wseek.o src/libstddjb/cbuffer_wseek.lo: src/libstddjb/cbuf
src/libstddjb/cdb_findnext.o src/libstddjb/cdb_findnext.lo: src/libstddjb/cdb_findnext.c src/include/skalibs/cdb.h src/include/skalibs/uint32.h
src/libstddjb/cdb_free.o src/libstddjb/cdb_free.lo: src/libstddjb/cdb_free.c src/include/skalibs/cdb.h
src/libstddjb/cdb_hash.o src/libstddjb/cdb_hash.lo: src/libstddjb/cdb_hash.c src/include/skalibs/cdb.h
-src/libstddjb/cdb_init_map.o src/libstddjb/cdb_init_map.lo: src/libstddjb/cdb_init_map.c src/include/skalibs/bsdsnowflake.h src/include/skalibs/cdb.h
+src/libstddjb/cdb_init.o src/libstddjb/cdb_init.lo: src/libstddjb/cdb_init.c src/include/skalibs/bsdsnowflake.h src/include/skalibs/cdb.h
src/libstddjb/cdb_make.o src/libstddjb/cdb_make.lo: src/libstddjb/cdb_make.c src/include/skalibs/buffer.h src/include/skalibs/cdb.h src/include/skalibs/cdb_make.h src/include/skalibs/diuint32.h src/include/skalibs/genalloc.h src/include/skalibs/uint32.h
src/libstddjb/cdb_mapfile.o src/libstddjb/cdb_mapfile.lo: src/libstddjb/cdb_mapfile.c src/include/skalibs/cdb.h src/include/skalibs/djbunix.h
src/libstddjb/cdb_nextkey.o src/libstddjb/cdb_nextkey.lo: src/libstddjb/cdb_nextkey.c src/include/skalibs/cdb.h src/include/skalibs/uint32.h
-src/libstddjb/cdb_read.o src/libstddjb/cdb_read.lo: src/libstddjb/cdb_read.c src/include/skalibs/allreadwrite.h src/include/skalibs/cdb.h src/include/skalibs/posixishard.h
+src/libstddjb/cdb_read.o src/libstddjb/cdb_read.lo: src/libstddjb/cdb_read.c src/include/skalibs/cdb.h src/include/skalibs/posixishard.h
src/libstddjb/cdb_successor.o src/libstddjb/cdb_successor.lo: src/libstddjb/cdb_successor.c src/include/skalibs/cdb.h
src/libstddjb/cdb_zero.o src/libstddjb/cdb_zero.lo: src/libstddjb/cdb_zero.c src/include/skalibs/cdb.h
src/libstddjb/child_spawn.o src/libstddjb/child_spawn.lo: src/libstddjb/child_spawn.c src/include/skalibs/allreadwrite.h src/include/skalibs/config.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/exec.h src/include/skalibs/sig.h src/include/skalibs/strerr2.h src/include/skalibs/sysdeps.h src/include/skalibs/types.h
diff --git a/src/include/skalibs/cdb.h b/src/include/skalibs/cdb.h
@@ -13,8 +13,7 @@ extern uint32_t cdb_hash (char const *, unsigned int) gccattr_pure ;
typedef struct cdb cdb_t, *cdb_t_ref ;
struct cdb
{
- char *map ; /* 0 if no map */
- int fd ; /* -1 if uninitted, negative if mapped, nonnegative if nomapped */
+ char *map ;
uint32_t size ; /* initialized if map is nonzero */
uint32_t loop ; /* number of hash slots searched under this key */
uint32_t khash ; /* initialized if loop is nonzero */
@@ -25,13 +24,13 @@ struct cdb
uint32_t dlen ; /* initialized if cdb_findnext() returns 1 */
} ;
-#define CDB_ZERO { .map = 0, .fd = -1, .size = 0, .loop = 0, .khash = 0, .kpos = 0, .hpos = 0, .hslots = 0, .dpos = 0, .dlen = 0 }
+#define CDB_ZERO { .map = 0, .size = 0, .loop = 0, .khash = 0, .kpos = 0, .hpos = 0, .hslots = 0, .dpos = 0, .dlen = 0 }
extern struct cdb const cdb_zero ;
extern void cdb_free (struct cdb *) ;
-#define cdb_init(c, fd) (cdb_init_map(c, (fd), 1) ? 0 : -1)
-extern int cdb_init_map (struct cdb *, int fd, int) ;
+#define cdb_init_map(c, fd, domap) (!cdb_init(c, fd))
+extern int cdb_init (struct cdb *, int fd) ;
extern int cdb_mapfile (struct cdb *, char const *) ;
extern int cdb_read (struct cdb *, char *, unsigned int, uint32_t) ;
#define cdb_findstart(c) ((c)->loop = 0)
diff --git a/src/libstddjb/cdb_init.c b/src/libstddjb/cdb_init.c
@@ -0,0 +1,23 @@
+/* ISC license. */
+
+#include <skalibs/bsdsnowflake.h>
+
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include <skalibs/cdb.h>
+
+int cdb_init (struct cdb *c, int fd)
+{
+ struct stat st ;
+ char *map ;
+ if (fstat(fd, &st) < 0) return -1 ;
+ if (st.st_size > UINT32_MAX) return (errno = EOVERFLOW, -1) ;
+ map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, -1) ;
+ if (map == MAP_FAILED) return -1 ;
+ c->map = map ;
+ c->size = st.st_size ;
+ return 0 ;
+}
diff --git a/src/libstddjb/cdb_init_map.c b/src/libstddjb/cdb_init_map.c
@@ -1,26 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/bsdsnowflake.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <stdint.h>
-#include <errno.h>
-#include <skalibs/cdb.h>
-
-int cdb_init_map (struct cdb *c, int fd, int domap)
-{
- if (domap)
- {
- struct stat st ;
- char *map ;
- if (fstat(fd, &st) < 0) return 0 ;
- if (st.st_size > UINT32_MAX) return (errno = EOVERFLOW, 0) ;
- map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
- if (map == MAP_FAILED) return 0 ;
- c->fd = -fd-2 ;
- c->map = map ;
- c->size = st.st_size ;
- }
- else c->fd = fd ;
- return 1 ;
-}
diff --git a/src/libstddjb/cdb_read.c b/src/libstddjb/cdb_read.c
@@ -1,24 +1,14 @@
/* ISC license. */
-#include <unistd.h>
#include <string.h>
#include <errno.h>
-#include <skalibs/allreadwrite.h>
#include <skalibs/cdb.h>
#include <skalibs/posixishard.h>
int cdb_read (struct cdb *c, char *buf, unsigned int len, uint32_t pos)
{
- if (c->map)
- {
- if ((pos > c->size) || (c->size - pos < len)) return (errno = EPROTO, -1) ;
- memcpy(buf, c->map + pos, len) ;
- }
- else
- {
- if (lseek(c->fd, pos, SEEK_SET) < 0) return -1 ;
- if (allread(c->fd, buf, len) < len) return -1 ;
- }
+ if ((pos > c->size) || (c->size - pos < len)) return (errno = EPROTO, -1) ;
+ memcpy(buf, c->map + pos, len) ;
return 0 ;
}