mpool: Refactor to call file operation via vtable.

Don't assume there's a POSIX system with read()/write(), etc., instead
accept function pointers to such routines via FILEVTABLE structure.

Other changes, like avoiding fstat() usage, using bundled version of BSD
sys/queue.h, etc.
This commit is contained in:
Paul Sokolovsky 2016-07-26 02:17:26 +03:00
parent 7b31cae3c0
commit e487a1b132
5 changed files with 75 additions and 28 deletions

1
PORT/include/bsd-queue.h Symbolic link
View file

@ -0,0 +1 @@
queue.h

1
PORT/include/filevtable.h Symbolic link
View file

@ -0,0 +1 @@
../../include/filevtable.h

54
include/filevtable.h Normal file
View file

@ -0,0 +1,54 @@
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _BDB_FILEVTABLE_H_
#define _BDB_FILEVTABLE_H_
#ifdef VIRT_FD_T_HEADER
#include VIRT_FD_T_HEADER
#endif
#include <sys/types.h>
#include <unistd.h>
#ifndef virt_fd_t
#define virt_fd_t int
#endif
typedef struct FILEVTABLE {
ssize_t (*read) __P((virt_fd_t, void *, size_t));
ssize_t (*write) __P((virt_fd_t, const void *, size_t));
off_t (*lseek) __P((virt_fd_t, off_t, int));
int (*fsync) __P((virt_fd_t));
} FILEVTABLE;
#endif /* _BDB_FILEVTABLE_H_ */

View file

@ -33,7 +33,8 @@
* @(#)mpool.h 8.2 (Berkeley) 7/14/94
*/
#include <sys/queue.h>
#include <bsd-queue.h>
#include <filevtable.h>
/*
* The memory pool scheme is a simple one. Each in-memory page is referenced
@ -65,8 +66,9 @@ typedef struct MPOOL {
pgno_t maxcache; /* max number of cached pages */
pgno_t npages; /* number of pages in the file */
u_long pagesize; /* file page size */
int fd; /* file descriptor */
virt_fd_t fd; /* virtual file descriptor */
/* page in conversion routine */
const FILEVTABLE *fvtable;
void (*pgin) __P((void *, pgno_t, void *));
/* page out conversion routine */
void (*pgout) __P((void *, pgno_t, void *));
@ -85,7 +87,7 @@ typedef struct MPOOL {
} MPOOL;
__BEGIN_DECLS
MPOOL *mpool_open __P((void *, int, pgno_t, pgno_t));
MPOOL *mpool_open __P((void *, virt_fd_t, const FILEVTABLE *, pgno_t, pgno_t));
void mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
void (*)(void *, pgno_t, void *), void *));
void *mpool_new __P((MPOOL *, pgno_t *));

View file

@ -36,14 +36,12 @@ static char sccsid[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <bsd-queue.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <db.h>
@ -59,28 +57,15 @@ static int mpool_write __P((MPOOL *, BKT *));
* Initialize a memory pool.
*/
MPOOL *
mpool_open(key, fd, pagesize, maxcache)
mpool_open(key, fd, fvtable, pagesize, maxcache)
void *key;
int fd;
virt_fd_t fd;
const FILEVTABLE * fvtable;
pgno_t pagesize, maxcache;
{
struct stat sb;
MPOOL *mp;
int entry;
/*
* Get information about the file.
*
* XXX
* We don't currently handle pipes, although we should.
*/
if (fstat(fd, &sb))
return (NULL);
if (!S_ISREG(sb.st_mode)) {
errno = ESPIPE;
return (NULL);
}
/* Allocate and initialize the MPOOL cookie. */
if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
return (NULL);
@ -88,7 +73,11 @@ mpool_open(key, fd, pagesize, maxcache)
for (entry = 0; entry < HASHSIZE; ++entry)
CIRCLEQ_INIT(&mp->hqh[entry]);
mp->maxcache = maxcache;
mp->npages = sb.st_size / pagesize;
mp->fvtable = fvtable;
off_t file_size = mp->fvtable->lseek(fd, 0, SEEK_END);
if (file_size == (off_t)-1)
return (NULL);
mp->npages = file_size / pagesize;
mp->pagesize = pagesize;
mp->fd = fd;
return (mp);
@ -203,9 +192,9 @@ mpool_get(mp, pgno, flags)
++mp->pageread;
#endif
off = mp->pagesize * pgno;
if (lseek(mp->fd, off, SEEK_SET) != off)
if (mp->fvtable->lseek(mp->fd, off, SEEK_SET) != off)
return (NULL);
if ((nr = read(mp->fd, bp->page, mp->pagesize)) != mp->pagesize) {
if ((nr = mp->fvtable->read(mp->fd, bp->page, mp->pagesize)) != mp->pagesize) {
if (nr >= 0)
errno = EFTYPE;
return (NULL);
@ -297,7 +286,7 @@ mpool_sync(mp)
return (RET_ERROR);
/* Sync the file descriptor. */
return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
return (mp->fvtable->fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
}
/*
@ -378,9 +367,9 @@ mpool_write(mp, bp)
(mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
off = mp->pagesize * bp->pgno;
if (lseek(mp->fd, off, SEEK_SET) != off)
if (mp->fvtable->lseek(mp->fd, off, SEEK_SET) != off)
return (RET_ERROR);
if (write(mp->fd, bp->page, mp->pagesize) != mp->pagesize)
if (mp->fvtable->write(mp->fd, bp->page, mp->pagesize) != mp->pagesize)
return (RET_ERROR);
bp->flags &= ~MPOOL_DIRTY;